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.
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
Syntax error on commas
void Drive (int dir2, int dir3, int dir8, int dir9, int time)
{
motor[port2] = (dir2127);
motor[port3] = (dir3127);
motor[port8] = (dir8127);
motor[port9] = (dir9127);
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.
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.)
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!
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