Azlan's Website

Homepage for Azlan's websites

View my GitHub

Vex Programmming Notes

Code Examples Vex Doccumentation (External link)

Contents

  1. Initiating Motors
  2. Initiating Controllers
  3. Spinning Motors
  4. Stopping Motors
  5. Wait
  6. Printing
    1. Brain
    2. Controller
  7. Set Cursor
    1. Brain
    2. Controller
  8. New Line
    1. Brain
    2. Controller
  9. Clear Line
    1. Brain
    2. Controller
  10. New Line
    1. Brain
    2. Controller
  11. Controller Input
    1. Axis
    2. Button
  12. Controller Vibration
  13. Motor Settings
    1. Stopping
    2. Velocity
    3. Rotation
    4. Reset Rotation
    5. Timeout
    6. Max Torque
  14. Motor Data
    1. Spinning
    2. Direction
    3. Rotation
    4. Velocity
    5. Current
    6. Power
    7. Torque
    8. Efficiency
    9. Temperature
  15. Digital Out
    1. Setting
    2. Getting Data

Initiating Motors

Motors are initiated in the following format:

motor motorname = motor(PORT1, gearSetting::ratio18_1,false);

In the motor() function there are 3 arguments.

  1. port number
  2. The gear setting
Gear Code Description
6:1
gearSetting::ratio6_1
(600 RPM) turbo motor
18:1
gearSetting::ratio18_1
(200 RPM) speed motor
36:1
gearSetting::ratio36_1
(100 RPM) strength motor
  1. Wheather to reverse the motors
    • true (will reverse)
    • false (will not reverse)

Initiating Controllers

Controllers are initiated in the following format:

controller controllername1 = controller(primary);
controller controllername2 = controller(partner);

In the controller() function there is 1 argument.

  1. The controller types
    • primary (main controller or otherwise known as Horizontal controller)
    • partner (secondary controller or otherwise known as Vertical controller)

Spinning Motors

You can spin motors in the following format:

motorname.spin(directionType::fwd,100,velocityUnits::pct);

In the motor.spin() function there are 3 arguments.

  1. The direction
    • directionType::fwd(Forward)
    • directionType::rev(Revese/Backward)
  2. Speed
    • Any value between -100 to 100 (negative numbers go backwards)
  3. The mesurement used in speed
    • velocityUnits::pct(Percentage)
    • velocityUnits::rpm(Rotations Per Minute)
    • velocityUnits::dps(Degrees Per Second)

Note:

  1. In the second arguement, Speed, if number exceeds the maximum capability of the motor (E.g. 110%), it will only go at the maximum speed (E.g, 100%).
  2. Add controlername1.axis1.value() in the second arguement, Speed, if you want the motor to spin according to the movement of the joystick on the controller. axis1 being the axis you want to control with the motor and controlername1 being the name of the controller you initialised it with.

Stopping Motors

If your motor spins indefinitely due to functions like motor.spin(directionType::fwd), you will eventually need the motor to stop. You can do so like this:

motor.stop();

Alternatively, you can also specify the way you want it to stop:

motor1.stop(brakeType::coast);
motor2.stop(brakeType::brake);
motor3.stop(brakeType::hold);

motor.stop() takes 1 optional argument:

  1. brakeType (How you want the motor to stop. There are 3 types of brakeType:
brakeType Description
brakeType::coast
Motor stop accelerating, slowly coming to a stop. Motor is still movable by external forces.
brakeType::brake
Stops motor completely. Does not return to original position (when braking was called) if moved.
brakeType::hold
Stops motor completely and returns to original position (when braking was called) if moved.

Wait

You can add a wait block using the following format:

task::sleep(Time);

In the sleep() function there is 1 argument.

  1. Time in miliseconds
    • Any number

Printing

Printing things on the controllers and brains help make it easier to test things out.

Both functions below take in 1 argument:

  1. Message
    • As a ‘Const Char’ (a.k.a String) format

Brain

Brain.Screen.print("Autonomous code started");
Brain.Screen.print("Turnning left");
//Turn left... Go Straight... Turn right...

Controllers

H.Screen.print("H Controller");
V.Screen.print("V Controller");

Set Cursor

Setting cursor position for printing.

Both functions for Brain and Controller below take in 2 arguments:

  1. Row
    • As a ‘int32_t ‘ (a.k.a integer) format.
  2. Column
    • As a ‘int32_t ‘ (a.k.a integer) format.

Brain

Brain.Screen.setCursor(1, 1);
Brain.Screen.print("Autonomous code started");
Brain.Screen.setCursor(2, 2);
Brain.Screen.print("Turnning left");
Brain.Screen.setCursor(3, 2);
Brain.Screen.print("Going Straight");
//Turn left... Go Straight... Turn right...

Below is an image from codev5.vex.com to explain the columns and rows in a Brain’s screen. Image

Controllers

Brain.Screen.setCursor(1, 1);
H.Screen.print("H Controller");
Brain.Screen.setCursor(2, 1);
H.Screen.print("Controller Ready");

Below is an image from codev5.vex.com to explain the columns and rows in a controller screen. Image

New Line

Sets cursor to the next line.

Both functions below don’t take arguments.

Brain

Brain.Screen.print("Autonomous code started");
Brain.Screen.newLine();
Brain.Screen.print("Turnning left");
//Turn left... Go Straight... Turn right...

Controllers

H.Screen.print("H Controller");
Brain.Screen.newLine();
H.Screen.print("Controller Ready");

Clear Line

Clears the line/row the cursor is currently on.

Both functions below don’t take in arguments.

Brain

Brain.Screen.print("Autonomous code started");
Brain.Screen.cleatLine();
Brain.Screen.print("Turnning left");
Brain.Screen.cleatLine();
Brain.Screen.print("Going Straight");
Brain.Screen.cleatLine();
//Turn left... Go Straight... Turn right...

Controllers

H.Screen.print("H Controller");
Brain.Screen.cleatLine();
H.Screen.print("Controller Ready");

Clear Screen

Clears the screen of all printing.

Both functions below don’t take in arguments.

Brain

Brain.Screen.print("Autonomous code started");
Brain.Screen.print("Turnning left");
//Turns left... Go Straight... Turns right...
Brain.Screen.print("Autonomous complete!");
Brain.Screen.clearScreen();

Controllers

H.Screen.print("H Controller");
H.Screen.clearScreen();
H.Screen.print("Controller Ready");

Controller Input

Controller input is the most important as it allows you to control your robot based on controller inputs. Below showcases a controller and all it’s input methods.
!Image Image source:Holonomic drive train code - VEX Robotics Competition Discussion / VRC > Change Up (20/21) - VEX Forum
There are 2 main types of inputs:

  1. Axis (joystick)
  2. Button

Axis

In each controller there are 4 axis (2 joysticks). The axis range from -128 to 128. to get this value, call one the following functions based on the axis you want:

controller.Axis1.value();
controller.Axis2.value();
controller.Axis3.value();
controller.Axis4.value();

Button

In each controller there are 12 buttons (not including power button and hidden reset button):

  1. ButtonR1
  2. ButtonR2
  3. ButtonL1
  4. ButtonL2
  5. ButtonUp
  6. ButtonDown
  7. ButtonLeft
  8. ButtonRight
  9. ButtonB
  10. ButtonA
  11. ButtonX
  12. ButtonY

Based on the button that you want, you can call the following function which will return a bool (true or false):

controller.ButtonR1.pressing();
controller.ButtonR2.pressing();
controller.ButtonL1.pressing();
controller.ButtonL2.pressing();
controller.ButtonUp.pressing();
controller.ButtonDown.pressing();
controller.ButtonLeft.pressing();
controller.ButtonRight.pressing();
controller.ButtonA.pressing();
controller.ButtonB.pressing();
controller.ButtonX.pressing();
controller.ButtonY.pressing();

Controller Vibration

Controller vibration allows you to get the attention of the controller if something happened or went wrong such as motor port disconnection. You can do so using the following function:

controller.rumble('-.-');

This function takes in 1 of the following arguments:

  1. const char *str (A string consisting of dots and dashes to represent a rumble pattern) The table below explains the dots and dashes:
Type Use
-
Long Rumble
.
Short Rumble
  1. predefined rumble options:
    • rumbleLong (Same as ----)
    • rumbleShort (Same as ....)
    • rumblePulse (Same as -.-.)

Motor Settings

When you move motors but don’t specify the settings, it relys on the default setting or the one it was set. For example if motor.stop() was called without the optional argument breakType, it will rely on the setting set by motor.setStopping().
There are many settings you can set: Below is a table of most common settings and uses:

Setting Use Description Arguements
Stopping
motor.setStopping();
Sets the default

breakType

  1. breakType
    • breakType::coast
    • breakType::brake
    • brakeType::hold
Velocity
motor.setVelocity();
Sets the default

velocityUnits

  1. Velocity (

    double

    )
  2. velocityUnits
    • velocityUnits::pct
      (Percentage)
    • velocityUnits::rpm
      (Rotations per minute)
    • velocityUnits::dps
      (Degrees per second)
Rotation
motor.setRotation();
Sets the value of the motor's built-in encoder.
  1. Rotation (

    double

    )
  2. rotationUnits
    • rotationUnits::deg
      (Degrees)
    • rotationUnits::rev
      (Revolutions)
    • rotationUnits::raw
      (Raw data format)
Reset Rotation
motor.resetRotation();
Resets the motor's built-in encoder value to 0.

void

Timeout
motor.setTimeout();
Sets the timeout for the motor if it does not reach its commanded position prior to the completion of the timeout. The motor will then stop.
  1. Time (

    int32_t

    )
  2. timeUnits
    • timeUnits::sec
      (Seconds)
    • timeUnits::msec
      (milliseconds)
Max Torque
motor.setMaxTorque();
Sets the max torque the motor is allowed to handle.
  1. Torque (

    double

    )
  2. percentUnits
    • percentUnits::pct
      (Percentage)

Motor Data

You can get a motor’s data to calciulate turns or detect if a motor has been disconnected. Below shows a table of the data you can get and the functions you can use to get that data.

Data Use Arguments Return Type
Spinning
motor.isSpinning();
void
bool
Direction
motor.direction();
void
[object Object]
(

directionType

)
  • directionType::fwd
  • directionType::rev
  • Rotation
    motor.rotation();
    1. rotationUnits
      • rotationUnits::deg
        (Degrees)
      • rotationUnits::rev
        (Revolutions)
      • rotationUnits::raw
        (Raw data format)
    double
    Velocity
    motor.velocity();
    1. velocityUnits
      • velocityUnits::pct
        (Percentage)
      • velocityUnits::rpm
        (Rotations per minute)
      • velocityUnits::dps
        (Degrees per second)
    double
    Current
    motor.current();
    1. currentUnits
      • currentUnits::amp
        (Ampere)
    double
    Power
    motor.power();
    1. powerUnits
      • powerUnits::watt
        (Watts)
    double
    Torque
    motor.torque();
    1. torqueUnits
      • torqueUnits::Nm
        (Newton Meters)
      • torqueUnits::InLb
        (Inch Pounds)
    double
    Efficiency
    motor.efficiency();
    1. percentUnits
      • percentUnits::pct
        (Percentage)
    double
    Temperature
    motor.temperature();
    1. percentUnits
      • percentUnits::pct
        (Percentage)
    double

    Digital Out

    Digital out is used for pistons in 3 wire ports.

    Setting

    The state of a Digital Out component can be set through digital_out.set(bool value) It takes in 1 argument:

    1. bool (State true or false)

    Getting Data

    Getting the state of a Digital Out component can be obtain through digital_out.value() It returns a bool (State true or false)