Shortening Program


motor[port7] = 127;
wait1Msec(1350);   ///mogo out
motor[port7] = 0;

motor[port2] = 127;
motor[port3] = 127;
motor[port8] = 127;///drive forward
motor[port9] = 127;
wait1Msec(2400);

motor[port7] = -127;
wait1Msec(1350);  ///mogo in
motor[port7] = 0;

motor[port2] = -127;
motor[port3] = -127;
motor[port8] = 127;///turn around
motor[port9] = 127;
wait1Msec(1200);

motor[port2] = 127;
motor[port3] = 127;
motor[port8] = 127;///drive forward
motor[port9] = 127;
wait1Msec(1800);

motor[port7] = 127;
wait1Msec(1350);   ///mogo out
motor[port7] = 0;

motor[port2] = -127;
motor[port3] = -127;
motor[port8] = -127;///drive forward
motor[port9] = -127;
wait1Msec(500);

This is a new autonomous that I have programmed. This is my first year using RobotC, so i dont know much about it. I was wondering if there is a way to shorten this code, and make it more readable. Thank you so much.

I am no expert, but I think the answer is yes and no. You can make a task for each operation, and it would clean up this part of the code, but would result in the extra coding of the tasks.

So which way would be easier to read, if i wanted to present my program to a judge?
@SkinnyPanda Robotics

I’d reccomendation making a function for your program, maybe something where you input a positive or negative number, and then multiply it by the 127 you have there. You could also have the delay be a part of the function. Doing that would make your program much easier to read.

Depending on your drive (this is assuming it’s a tank drive) you could slave your motors.
If you have 2 motors on the left side you can slave the second left motor to the first left motor (basically slaving makes two motors do the same thing)
To explain that better here is a quick sample:


slaveMotor(port3,port2);

Then when you want to tell your drive to do something you can just say:


motor[port2] = -127;

Instead of:

motor[port2] = -127;
motor[port3] = -127;

This shortens the code slightly and makes it easier to read

Do you have an example, or test code I can look onto?

If it is an arcade drive, will it still work?

Try out the following
put this right on top


void Drive (int dir2, int dir3,int dir8, int dir9. int time)
{
motor[port2] = (dir2*127);
motor[port3] = (dir3*127);
motor[port8] = (dir8*127);
motor[port9] = (dir9*127);
wait1Msec(time);
}

the above does only full speed backward or forwards, but if you use double instead of int, you can enter the percent torque you want as a decimal.

With this code, you could replace


motor[port2] = 127;
motor[port3] = 127;
motor[port8] = 127;///drive forward
motor[port9] = 127;
wait1Msec(1800);

with


Drive(1,1,1,1,1800);

(put that where the code it is replacing is)

I’d also recomend taking a look at http://www.education.rec.ri.cmu.edu/products/teaching_robotc_vex/reference/hp_functions.pdf

What does the dir2,dir3,dir8,dir9 stand for?

Syntax error on commas
void Drive (int dir2, int dir3, int dir8, int dir9, int time)
{
motor[port2] = (dir2127);
motor[port3] = (dir3
127);
motor[port8] = (dir8127);
motor[port9] = (dir9
127);
wait1Msec(time);
}
They’re just variables. By setting them to 1 you set motor power to 127 since 1*127 is 127. By setting it to -1, motor power is -127.

You can also do some fun things with functions to include some degree of turning, if you wish:

void setDrive(int speed, int turning) {
    // Set left motors
    motor[port2] = speed + turning;
    motor[port3] = speed + turning;

    // Set right motors
    motor[port8] = speed - turning;
    motor[port9] = speed - turning;
}

void driveStraightForTime(int speed, int time) {
    setDrive(127, 0);
    wait1Msec(time);
    setDrive(0, 0);
}

void turnForTime(int time) {
    setDrive(0, 127);
    wait1Msec(time);
    setDrive(0, 0);
}

Arcade drive meaning you use one joystick for forward and backward and one joystick to turn?
If so than yes this would still work because both wheels on the same side are always turning in the same direction.
(It would not work if you had an x-drive/holonomic drive because when strafing the wheels on the same side turn opposite directions.)

1 Like

@RHarris @Grystrion Does this code look any better?


task autonomous()
{
MoGo(1,1350);//MoGo Out
Drive(1,1,1,1,2200);//Drive Forward
MoGo(-1,1350);//Mogo In
Drive(1,1,-1,-1,500);//Turn Around
Drive(1,1,1,1,1800);//Drive Forward
MoGo(1,1000);//Mogo Out
Drive(-1,-1,-1,-1,500);//Drive Back
}

looks quite a lot better. Using functions is a good idea pretty much whenever you can use them. Anyways, it looks a lot neater and is much easier to interpret. If you would like to try something more complicated, you could use the slaveMotor function @RHarris talked about above to reduce the parameters above. Also, if you desire to decrease the amount of parameters for the function, you can reuse the same parameter multiple times in the function body (the area within the function) instead, assuming that the motors with the same parameter are going to be doing the same thing every time the function is called. Anyways, good work!

@RHarris @Grystrion

When i do the slave motor, which port will i refer too?

For example:


slavemotor(port2,port3);

Would I use port 2 or port 3?

The first Motor, port 2, is set to do what port 3 is given, so if you put that statement somewhere at the top of your code, so all other reference to port 2 can be basically deleted of my understanding of slavemotor is correct

Yes when you reference a slaved motor you use the second name in the parenthesis at all other times.