Vex EasyC Custom Drivetrain Code

The easiest thing to do is to just use the preinstalled drivetrain function that is already in the EasyC library.  But as many people may have noted, this can make the robot feel jerky, unresponsive, and sometimes hard to control.  EasyC gives you many options and configurations for different types of drivetrain, but unfortunately they do not give you an option to adjust the joystick sensitivity.  This tutorial will give you some basic tips as well as sample code to get a more responsive feel from your drivetrain.

arcade drive

To write your own custom code you will need the function GetJoystickAnalog ().

EasyC Get Joystick Analog

You already know that the output to the motor ranges from -127 to 127, where 0 is off and 127 is full on.   The analog input from the joystick returns a value from -127 to 127, where 0 is the neutral position and 127 is when you push the stick all the way to the edge.  The default code in EasyC uses a linear mapping for the controls, meaning it takes the value of the joystick and send it directly to the motor.  This can make the controls feel too sensitive, especially if the robot is geared for speed.  If you push the joystick all the way to the edge, the robot will go at full speed, which is fine, but driving slow or steering can be difficult because the program is sending too much power to the motors.  Moving the stick just a little could send too high of a signal to the motors.

y = x

To solve this, we want a nonlinear curve for the controls.  In my case I chose an exponential curve.  What this does is that when the driver move the joystick a little, the program will send less power to the motors, making it easier to drive slow and make other fine adjustments.  When the driver pushes the joystick all the way to the edge the program will send full power to the motors allowing the robot to go at full speed.  Overall, this will have the effect of making the controls feel less sensitive and making the robot easier to drive.  The robot will be more maneuverable while maintaining full speed capabilities.

y = 127 * (x/127) ^2

y = 127 * (x/127) ^3

Turning

If you are doing a tank drive, then the left stick controls the left drivetrain and the right stick controls the right drivetrain.  However, if you are doing an arcade style control layout, then you need to add the driving input from the less stick with the steering input from the right stick.  When you do this you will need to cap the values so that they do not exceed 127.  See the example code below.

Vex EasyC Custom Drivetrain Code

 

CustomDrive 

#include "Main.h" 

void CustomDrive ( void )
{
     float driveRight;
     float driveLeft; 
     stickLeft = GetJoystickAnalog( 1 , 3 ) ; 
     stickRight = GetJoystickAnalog( 1 , 1 ) ; 
     driveLeft = ( stickLeft + stickRight ) / 127 ; 
     driveRight = ( stickLeft - stickRight ) / 127 ; 
     driveLeft = ( driveLeft * driveLeft ) * 127 * ( driveLeft / abs(driveLeft )) ; 
     driveRight = ( driveRight * driveRight ) * 127 * ( driveRight / abs(driveRight )) ; 

     // cap 
 
     if ( driveLeft < -127 ) 
     { 
          driveLeft = -127 ;
     } 
     else if ( driveLeft > 127 ) 
     { 
          driveLeft = 127 ; 
     } 

     if ( driveRight < -127 ) 
     { 
          driveRight = -127 ; 
     } 
     else if ( driveRight > 127 ) 
     { 
          driveRight = 127 ; 
     } 

     // control motors 

     driveOutputLeft = driveLeft ; 
     driveOutputRight = -driveRight ; 
     SetMotor ( 2 , driveOutputLeft ) ; 
     SetMotor ( 3 , driveOutputLeft ) ; 
     SetMotor ( 4 , driveOutputRight ) ; 
     SetMotor ( 5 , driveOutputRight ) ; 
}

Thank you for reading.  I hope this was helpful, and good luck!

Leave a Reply

Your email address will not be published. Required fields are marked *

CommentLuv badge