VEX Cortex RobotC help

Hello everyone,

I am working on a the code for a VEX robot, using RobotC for Cortex. I have never used C, C++ let alone robotc (Java is my primary language). So far, I am having a tough time understanding how to write the code for our robot.

I’ll give you a description of what it is and what I’d like it to do. It is a 4 wheeled robot with an arm controllable by 1 servo (We only had 5 motors and the arm was the only part that only had to move a certain degree). There is an intake that has 1 motor to control it.

So, the tl;dr version is 4 motors for driving, 1 servo to move the arm and 1 motor to do intake.

What I want to do is do tank drive on the 4 motors, have the two triggers (Equivalent to R1 and L1 on PS3) move the arm up and down, and have the “A” button turn on the intake.

I havn’t been able to get much past the template. I have the 4 motor tank drive working, but that’s it.

#pragma config(Motor,  port2,           frontRightMotor, tmotorServoContinuousRotation, openLoop, reversed)
#pragma config(Motor,  port3,           backRightMotor, tmotorServoContinuousRotation, openLoop, reversed)
#pragma config(Motor,  port4,           frontLeftMotor, tmotorServoContinuousRotation, openLoop)
#pragma config(Motor,  port5,           backLeftMotor, tmotorServoContinuousRotation, openLoop)
#pragma config(Motor,  port6,           armServo, tmotorServoStandard, openLoop)

task main()
  while(1 == 1)
    //Right side of the robot is controlled by the right joystick, Y-axis
    motor[frontRightMotor] = vexRT[Ch2];
    motor[backRightMotor]  = vexRT[Ch2];
    //Left side of the robot is controlled by the left joystick, Y-axis
    motor[frontLeftMotor] = vexRT[Ch3];
    motor[backLeftMotor]  = vexRT[Ch3];

I can answer any questions that might help you understand what I’m trying to do. Need this to be done this week (5 days) and I don’t have enough time in the day to learn a lot of the syntax. All help is appreciated.



There are three different pieces of code you want:

-The drivetrain
-The arm
-The roller

They will all run in a while(1) loop.

The drivetrain is fairly simple. Your code is already correct, although it is likely you will need to invert some of the motors (via trial and error).

The arm has two possible control modes with a servo:
-Bumping the position with the two buttons to make it act like a motor that holds position
-Setting specific positions with buttons.
It is also possible that a servo will not have enough torque to move an arm of significant size. Depending on how much motion you need, you can gear it down (you have 180degrees of range on the servo, if you only need 60 you can gear it 12:36).

In the first case, you would do something like this:

//Outside of task main:
int servopos = 0;//Starting position

//Inside of while(1):
//Add or subtract a constant if the button is pressed
if(vexRT[Btnx]) servopos += 10;
if(vexRT[Btny]) servopos -= 10;
//Limit servopos to +-127 range
if(servopos > 127) servopos = 127;
if(servopos < -127) servopos = -127;
//Set motor to servopos
motor[armServo] = servopos;

In the second case, you would do something like this:

//Outside of task main:
int servopos = 0;//Starting position

//Inside of while(1):
if(vexRT[Btnx]) servopos = 20;//This is one possible position
if(vexRT[Btny]) servopos = -50;//This is another position
if(vexRT[Btnz]) servopos = 100;//This is yet another position
//You can have more if you want.
//Set motor to servopos
motor[armServo] = servopos;

You should definitely see if you have enough torque to move your arm with a servo, and possibly gear it down if you don’t need the full range. You can do this empirically (by trying it and seeing if it works) or mathematically (by calculating the torque you will need based on the design and seeing if it matches up to the servo specs on If you can, I highly recommend using the latex tubing or rubber bands to counterforce the load on the arm, reducing the servo load.

The roller is easy:

//Inside of while(1):
int roller = 0;
if(vexRT[Btnx]) roller = 127;
motor[rollerMotor] = roller;

Please note that you will need to find the correct button name to put inside vexRT].

Thanks for the great response! Could have taken me a while. (Simple solution, wrong mindset)

I will try it out when I get home tonight.