Well, I’ll show you some code so you can get a start on it. The other stuff should be pretty similar, or at least require a similar level of thinking – so once you understand what’s going on here, you should be well on your way.
First, let’s say you want a function ‘void Turn_Robot (int angle)’ to turn the robot by some specified angle ‘angle.’ How do you do this? Well, without encoders or gyros, the answer is pray! But you’ll have to settle for a simple counter. E.g.,
/*
Hopefully you can follow this. I'll try to comment liberally (something that's rather hard for me ;)
In your auto mode routine, you might say something like:
if (!FinishedTurn) Turn_Robot (radian measure);
else {/* put other code here */}
Note that you'll need a global variable FinishedTurn, initialized to false (0), and a handfull of defines.
*/
void Turn_Robot (int angle)
{
// static means that we only set it to 0 the first time, and when it is
// called again, it is persistant, meaning it won't lose its old value
static unsigned int counter = 0;
// we are going to count up from 0. On the left side of the equation, we
// have a unit of cycles, so we have to find out how to convert radians
// to cycles. This is done by an *emprically found constant*
// CYCLES_PER_RADIAN (dependent on voltage, so beware).
if (counter++ > abs(angle) * CYCLES_PER_RADIAN)
{ // Robot has finished turning, ergo ...
// we're done, so nicely set counter back to 0 for next time
// set left and right drive to neutral, and set flag FinishedTurn to true
counter = 0;
LEFT_DRIVE = 127;
RIGHT_DRIVE = 127;
FinishedTurn = 1;
}
else {
// We aren't done turning. We have to set up some convention for
// angle (turning PI/4 radians doens't mean anything without a
// coordinate system, afterall!) Ergo,
if (angle > 0)
{ // Positive angle means turn left
LEFT_DRIVE = LTURN_SPD_LDRIVE;
RIGHT_DRIVE = LTURN_SPD_RDRIVE;
}
else { // Negative angle means turn right
LEFT_DRIVE = RTURN_SPD_LDRIVE;
RIGHT_DRIVE = pwm04 = RTURN_SPD_RDRIVE;
}
FinishedTurn = 0; // NOTE: set this back to false; else, when called
// after successful move, it will still be true!
}
}
A similar Move_Robot function should be easy (the turn is slightly more complicated, so that’s why I included this one for you). Now, how to find out all those constants? Well, that’s the fun part (by the way, I put in their radians as the default, but you can set it up to any unit you feel like).
Okay, set the constant CYCLES_PER_RADIAN to some value, any value (I’d guess what you think, from judging how fast the robot can turn). Don’t do anything in your code, but call the function Turn_Robot (angle), with some angle you feel like turning. If the robot doesn’t turn enough, increase the constant by a variable amount, depending on how much is left to turn. If the robot turns too much, decrease the constant. (You might want to put tape on the floor marking off a 45 deg angle, or some such sort that is easily determined, so you can quickly tell what to do.)
Oh, and I haven’t tested this code, so beware (I’m frequently wrong ;)) As Knuth said, Beware, I’ve only proven the above code correct, I haven’t tested it.