Interesting use of the experimental method, trial and errorâŚ
Here is a long response full of details, donât let it give you the wrong idea about simpler questions.
Here is how to add to variables:
Use assignment statement pulldown: insert var, insert operator +=, insert value to add.
or type directly in the assignment statement block window like these examples:
var = var + 27; // increment by 27
var += var2; // increment by var2, same as var = var + var2;
var++; // increment by 1 after using var
++var; // increment by 1 before using var
var= var++; // illegal operation, multiple results for var both before and after.
Yes, use encoder feedback to change the speed of the motors so they spin at the same speeds.
Here is some calculus/physics:
position = x; // this is what your encoder returns.
velocity = prev_x - this_x ; // rate of change is delta since you last checked
speed = abs( velocity ); // velocity knows direction, speed doesnât
Doing a ztr turn is really nearly exactly the same as driving straight for some distance, except one of the motor speeds is inverted.
It might be easier to start with drive-straight routine.
untested pseudo code outline for driving straight or turning.
// previous step in autonomous routine
while(1) { // do this step in autonomos routine
wait(20); // motors dont update faster than this anyway
prev_enc1 = enc1; // save previous values
prev_enc2 = enc2;
enc1 = GetEncoder(1); // get current values
enc2 = GenEncoder(2);
speed1 = enc1-prev_enc1; // calculate current speeds
speed2 = enc2-prev_enc2;
if (speed1 > speed2 ) { motor1 -= 10; motor2 += 10; }
if (speed2 > speed1 ) { motor2 -= 10; motor1 += 10; }
stop_me = 0; // clear
if (enc1 > enc1_limit) { set_motor(1,0); ++stop_me} //stop it
else {set_motor(1,motor1); } // else set speed
if (enc2 > enc2_limit) { set_motor(2,0); ++stop_me} //stop it
else {set_motor(2,motor2); } // else set speed
if (stop_me >= 2) { break; } // get out of while() loop, go to next step.
} //wend
// next step in autonomous routine
Details for you to work out, if you cant find a previously posted drive straight code to start with;
- create and initialize all the variables, fix all the syntax;
- Keep the motor variable from going outside +/- 127.
- If motor was set to 100, and you want to speed it up, add more.
If motor was set to -100, and you want to speed it up, subtract more.
figure out how to do which one.
- Depending on how your encoders are mounted and plugged in, they may be counting in opposite directions, and which is increasing depends on turning left or right. How will you figure that out to calculate speeds correctly?
- This is simple feedback; to do the P part of PID, change from +/- 10 to +/- N, where N is proportional to the difference in the speed.
Eventually, youâll want to make this a subroutine, so you can make a stack of subroutine calls to replaced timed driving in your autonomous. If you think about what parameters you need to call the subroutines in advance of writing them, it will help you write them. This is called âtop down designâ.
A example of simple set of subroutine calls might be:
DriveStraight( distance );
TurnLeft ( distance );
TurnRight( distance );
These could be coded independently, or eventually be combined into one;
Drive( direction, speed, distance );
I wonder if long answers will be more effective. The short answer is âYesâ