Launch 2019

The schedule for the week for Launch 2019

The schedule for the week for Launch 2019

You’ll be making this robot to use for the first few days of class until you have designed your competition robot. Click on the image for full build instructions.

You’ll be making this robot to use for the first few days of class until you have designed your competition robot. Click on the image for full build instructions.

Robot kit list of parts

Vex V5 Competition Superkit list of parts with links to the descriptions of each part.

The competition rules are posted here. You should study the rules and analyze the game to determine how to score the most points in a match. Remember, the matches are played with 2 robots against 2 opponents.

Lectures presented during the week

Sample turning and driving code

Programs that we were playing with that would drive specified distances and making predictable turns.

#include "robot-config.h"
          
// drives straight for a specified number of inches
void goStraight(int inches) {
    LeftMotor.startRotateFor(27 * inches * 5, rotationUnits::deg,
                             100, velocityUnits::rpm);
    RightMotor.rotateFor(27 * inches * 5, rotationUnits::deg,
                         100, velocityUnits::rpm);    
}

// turns for a specified number of degrees. This always turns in the same direction.
void turn(int degrees) {
    LeftMotor.startRotateFor(27 * 11.0/90 * degrees * 5, rotationUnits::deg,
                            100, velocityUnits::rpm);
    RightMotor.rotateFor(- 27 * 11.0/90 * degrees * 5,rotationUnits::deg,
                         100, velocityUnits::rpm);
}

// moves the arm up to -400 degrees (remember that it is dependent on the starting positioon
void armUp() {
       ArmMotor.rotateTo(-400, rotationUnits::deg, 50, velocityUnits::rpm); 
}

// moves the arm up to -400 degrees (remember that it is dependent on the starting positioon
void armDown() {
        ArmMotor.rotateTo(-800, rotationUnits::deg, 50, velocityUnits::rpm);
}

int main() {
    armUp();    
    task::sleep(1000);
    armDown();
//    goStraight(11);
//    turn(45);
//    goStraight(5);
}

Competition Sample Program

The following code is a sample competition program for handling the autonomous and driver control periods of the competition

#include "robot-config.h"

//Creates a competition object that allows access to Competition methods.
vex::competition    Competition;

void pre_auton( void ) {
  // All activities that occur before the competition starts
  // Example: clearing encoders, setting servo positions, ...
}

void autonomous( void ) {
  // ..........................................................................
  // Insert autonomous user code here.
  // ..........................................................................
    LeftMotor.startRotateFor(5, rotationUnits::rev, 200, velocityUnits::rpm);
    RightMotor.rotateFor(5, rotationUnits::rev, 200, velocityUnits::rpm);
}


void usercontrol( void ) {
  // User control code here, inside the loop
  while (1){
    // This is the main execution loop for the user control program.
    // Each time through the loop your program should update motor + servo 
    // values based on feedback from the joysticks.

    // ........................................................................
    // Insert user code here. This is where you use the joystick values to 
    // update your motors, etc.
    // ........................................................................

      LeftMotor.spin(directionType::fwd, Controller1.Axis3.value(), velocityUnits::pct);
      RightMotor.spin(directionType::fwd, Controller1.Axis3.value(), velocityUnits::pct);
      if (Controller1.ButtonR1.pressing()) {
          ArmMotor.spin(directionType::fwd, 50, velocityUnits::rpm);
      } else if (Controller1.ButtonR2.pressing()) {
          ArmMotor.spin(directionType::rev, 50, velocityUnits::rpm);
      } else {
          ArmMotor.stop();
      }
      
    vex::task::sleep(20); //Sleep the task for a short amount of time to prevent wasted resources. 
  }
}

//
// Main will set up the competition functions and callbacks.
//
int main() {
    
    //Run the pre-autonomous function. 
    pre_auton();
    
    //Set up callbacks for autonomous and driver control periods.
    Competition.autonomous( autonomous );
    Competition.drivercontrol( usercontrol );

    //Prevent main from exiting with an infinite loop.                        
    while(1) {
      vex::task::sleep(100);//Sleep the task for a short amount of time to prevent wasted resources.
    }       
}

Selecting programs and options

Using jumpers to select red vs blue sides of field and one of many autonomous strategies. In this program a digital input port selects program options for being on the red vs blue side of the field. An additional digital input port selects which autonomous strategy to run by selecting one or another autonomous function.

#include "robot-config.h"

//Creates a competition object that allows access to Competition methods.
vex::competition    Competition;

vex::digital_in RedBlue(Brain.ThreeWirePort.C);
vex::digital_in AutoVersion(Brain.ThreeWirePort.D);

void pre_auton( void ) {
    task::sleep(1000);
    Brain.Screen.printAt(0, 30, "Port C: %d", RedBlue.value());
    Brain.Screen.printAt(0, 50, "Port D: %d", AutoVersion.value());
}

void driveForward(int inches) {
}

void turnRight() {    
}

void turnLeft() {
}

void killAllRobots() {
    driveForward(15);
    if (RedBlue.value() == 0) {
        turnLeft();
    } else {
        turnRight();
    }
    driveForward(12);
}

void theOtherAutoFunction() {
        LeftMotor.startRotateFor(5, rotationUnits::rev, 200, velocityUnits::rpm);
        RightMotor.rotateFor(5, rotationUnits::rev, 200, velocityUnits::rpm);
}

void autonomous( void ) {
    if (AutoVersion.value() == 0) {
        killAllRobots();
    } else {
        theOtherAutoFunction();
    }
}

Line Following example

This example shows how to use proportional control to follow a line with a single sensor. The sensor shines an LED at the floor and measures the reflected light intensity. Since the taped lines on the field are more reflective than the carpet the returned value shows that. Following a line with a single sensor is done by actually following the edge of the line. Imagine if the sensor is directly over the edge of the line, then the value returned will be half way between full brightness and minimum brightness. If that “center” value is considered the goal (because you want the robot to always exactly straddle itself over the line edge) then we can take subtract that from the reflected light value. If the result is zero then the robot should drive straight. If the value is positive the robot needs to correct in one direction and if negative it needs to correct in the other direction. And the magnitude of the value tells how fast the robot should correct - further off the center should use faster corrections. You can get this value and arithmetically add it to the two wheel speeds to make the corrections faster or slower.

const int Kp = 2;
const int center = 24;
vex::line leftLine( Brain.ThreeWirePort.A );

int main() {
    while (true) {
        int leftSensor = LeftLine.value(analogUnits::mV);
        Brain.Screen.printAt(0, 30, "LeftSensor: %d", leftSensor);
        int error = leftSensor - center;
        Brain.Screen.printAt(0, 50, "error: %d  ", error);
        LeftMotor.spin(directionType::fwd, 50 + error * Kp, velocityUnits::pct);
        RightMotor.spin(directionType::fwd, 50 - error * Kp, velocityUnits::pct);
        task::sleep(10);
    }
}