Auton Programming Help

Hi, I was working on our teams autonomous code and was wanting to make a function for our holonomic drive to refer to either driving forward backward or strafe. An example to referring to this is " drive(f);" This would make it drive forward and similar for backwards. I was just wondering how you would determine that “f” in your function means forward.

I am basically asking in your function how to show that “drive(f)” means go forward, “drive(b)” goes backwards, “drive(l)” strafes left, and how “drive®” strafes right.

My drive cortex ports are
Front Left: 2
Front Right: 3
Back Left: 4
Back Right; 5

Just for reference my function is currently set up similar to:

void drive(){
//requested help would go in here
}

It’s a bit harder to do that in the holonomic driving because you need to tell the motors both the drive and rotation at the same time.

Normally you have “motor[one_of_them] = +X + Y + Rotation” kind of notation. Hard to go back and put the rotate in later. Not impossible, just not very clean.

Taken from Cody’s tutorial

driveX() and driveY() and rotate() were ones I have seen in autonomous where it returned the X Y and rotate values back to the drive controlling function. Those functions each had their own PID for the dimension of movement. But take care as rotate with drive gets tricky.

Note that I got ALL the signs wrong in that picture because I guessed which way the motor spins wrong.

You may find some of this code interesting.

So i’m not exactly sure what you’re saying. what I was going to do was something similar to this sudo code…

(f) {
motor(FL) = 127;
motor(FR) = -127;
motor(BL) = -127;
motor(BR) = 127;
}
Here I’m trying to make a “sub-function” inside my drive function that would make the program go forward, although I don’t know the syntax on how to assign this series of commands to a variable.
Then to reference it I was wanting to be able to do something similar to this…

task main(){
while(true)
{
drive(f);
}
}

I would do this inside the drive function for all my directions Ex: Forward, Backward, and Strafe.

I just don’t know how to make it reference the “sub-function” or whatever you might call it.

Oh ok I get it, that methodology will work but you’ll have drifting issues.

You want something like so…

// Pseudocode
forward(int time) {

	motor(FL) = 127;
	motor(FR) = -127;
	motor(BL) = -127;
	motor(BR) = 127;

	wait(time);

	stopAllMotors();

}

task autonomous(){

	forward(100);
	turn();
	forward(250);
	score();

	# Loop endlessly here so the autonomous task doesn't stop prematurely
	while(true) {
		wait(1)
	}
}

It’s WAYYYY better to use encoder values instead of time as the battery voltage and other factors will make the robot drive different amounts forward each time.

That is more correct, although I wanted to keep all my drive “functions” in one function. so for example…

void drive(){
forward{ //drive forward full speed
motor(FL) = 127;
motor(FR) = -127;
motor(BL) = -127;
motor(BR) = 127;
}
backward{ //drive backward full speed
motor(FL) = -127;
motor(FR) = 127;
motor(BL) = 127;
motor(BR) = -127;
}
strafe_left{ //strafe left full speed
motor(FL) = -127;
motor(FR) = -127;
motor(BL) = -127;
motor(BR) = -127;
}
strafe_right{ //strafe right full speed
motor(FL) = 127;
motor(FR) = 127;
motor(BL) = 127;
motor(BR) = 127;
}
}
void stop(){
motor(port1-10) = 0; //NOTE: This is just to reference that all motors are stopped, in actuality
} // it would have to be written out 10 times, one for each motor.

Then I could reference it in my auton as shown below…

task autonomous(){
drive(f); //drive forwards for .5 sec
wait1msec(500);
stop(); //stop all motors
drive(sl); //strafe left for .5 sec
wait1msec(500);
stop(); //stop all motors
drive(b); //drive backwards for .5 sec
wait1msec(500);
stop(); //stop all motors
drive(sr); //strafe right for .5 sec
wait1msec(500);
stop(); //stop all motors
}

I want my end result to be something like this so that I only have to use a parameter within my drive function to specify whether or not I’m going forward, backward, strafe left, or strafe right.

P.S. I was planning on using encoders after I got this tidbit of code to work.

I would not recommend using ‘f’, b’, etc. as parameters for an autonomous function call. However, if you really want to, you can do this:


void drive(char dir) {
  switch dir {
    case 'f': //If argument is 'f', drive Forward
      motor[flDrive] = 
        motor[frDrive] =
        motor[blDrive] =
        motor[brDrive] =
       127;
    break;
    case 'b': //If argument is 'b', drive Backward
      motor[flDrive] =
        motor[frDrive] =
        motor[blDrive] =
        motor[brDrive] =
        -127;
    break;
    case 'l': //If argument is 'l', strafe Left
      motor[flDrive] =
        motor[brDrive] =
        -127;
      motor[frDrive] =
        motor[blDrive] =
        127;
    break;
    case 'r': //If argument is 'r', strafe Right
      motor[flDrive] =
        motor[brDrive] =
        127;
      motor[frDrive] =
        motor[blDrive] =
        -127;
    break;
    case default: //If the char passed in is not f, l, r, or b, stop motors
      motor[flDrive] = 0;
      motor[frDrive] = 0;
      motor[blDrive] = 0;
      motor[brDrive] = 0;
    break;
  }
}

Also, you need to include single quotation marks (’ ') around the argument when you call the function. This tells the program that it’s a char rather than a variable name. That looks like this:


drive('f');

Thank you soooo much!!! This is exactly what i was looking for in how to write my auton code. I will let you know how it works after I implement it into my code.

Thanks!