Azlan's Website

Homepage for Azlan's websites

View my GitHub

Contents

  1. 4-motor drivetrain initialization
  2. Move Base Function
  3. 2 controller initialization
  4. Control Motor Via Controller Axsis
  5. Check Button Pressed

4-motor drivetrain initialization

The following example is how to initalise motors for a 4 motor drivetrain

vex::motor LF = vex::motor(PORT1, gearSetting::ratio18_1, true);
vex::motor LB = vex::motor(PORT2, gearSetting::ratio18_1, true);
vex::motor RF = vex::motor(PORT3, gearSetting::ratio18_1, true);
vex::motor RB = vex::motor(PORT4, gearSetting::ratio18_1, true);

The last argument in the functions may vary based on how the robot is built

2-controller-initialization

The following is the default method to initialise Horizontal and Vertical (Primary and Secondary) controllers.

vex::controller H = vex::controller(primary);
vex::controller V = vex::controller(partner);

Move Base Function

The following example is a function that will make the robot move based on a specified speed and duration.

void moveBase(int speed, int duration)
{
  LF.spin(directionType::fwd, speed, velocityUnits::pct);
  LB.spin(directionType::fwd, speed, velocityUnits::pct);
  RF.spin(directionType::fwd, speed, velocityUnits::pct);
  RB.spin(directionType::fwd, speed, velocityUnits::pct);
  task::sleep(duration);
  LF.stop();
  LB.stop();
  RF.stop();
  RB.stop();
}

To run the function, just call it in the main() as shown below

int main()
{
  moveBase(100, 500);
}

take note that task::sleep(duration) is in milliseconds so…
1 second = 1000 milliseconds

Control Motor Via Controller Axsis

The following code is usually put in the main() function during user-control period

LF.spin(directionType::fwd, H.Axis3.value(), velocityUnits::pct);
LB.spin(directionType::fwd, H.Axis3.value(), velocityUnits::pct);
RF.spin(directionType::fwd, H.Axis2.value(), velocityUnits::pct);
RB.spin(directionType::fwd, H.Axis2.value(), velocityUnits::pct);

The image below is for reference Image Image source:Holonomic drive train code - VEX Robotics Competition Discussion / VRC > Change Up (20/21) - VEX Forum

Check Button Pressed

The following code moves an arm motor when L1 or L2 is pressed.
It is usually placed in the main() function.

if (V.ButtonL1.pressing)
{
  arm.spin(directionType::fwd, 100, velocityUnits::pct);
}
else
{
  arm.stop();
  // alternatively you could do the following
  arm.spin(directionType::fwd, 0, velocityUnits::pct);
}