|
Re: HELP Arcade style program for first Robot
Put the robot up on blocks, so the wheels can spin freely as you test your program.
In a loop, read the joystick fwd/reverse axis into an unsigned char variable called ucjoyfwd. Print it so you can see the value. Copy ucjoyfwd to PWM outputs for both wheels. If one wheel runs backward, reverse the wiring on the Victor so that both run the same way.
In your printout, you see that ucjoyfwd ranges from 0-254 as you move the joystick to change the wheel speeds. I would like you to also load this to an integer variable called ispeed like this:
ispeed = (int) ucjoyfwd - 127
If you print ispeed too, you will see that it ranges from -127 to +127, as ucjoyfwd ranges from 0 to 255. In this new range, you can think of 0 as "stopped", -127 as "full speed reverse", 127 as "full speed forward".
Now instead of copying ucjoyfwd to the PWM outputs, copy (ispeed + 127) to them instead ... which converts ispeed back to the 0-254 range for the purposes of controlling the PWM Victors. The wheels should still turn the same ways and same speeds as before.
ispeedleft = ispeed + 127
ispeedright = ispeed + 127
Now for rotation, we want the wheels to spin at different speeds. So we want to take ispeed and add speed to one wheel, and take it away from the other.
So read the joystick left/right axis value into a different variable ucleftright, and convert it to the +/- 127 range using the same method. Print ucleftright and irotate, to see what they are doing as you move the joystick.
irotate = (int) ucleftright - 127
Now we're coming to the good part.
ispeedleft = ispeed - irotate;
if (ispeedleft > 127) ispeedleft = 127
if (ispeedleft < -127) ispeedleft = -127
ispeedright = ispeed + irotate
if (ispeedright > 127) ispeedright = 127
if (ispeedright < -127) ispeedright = -127
Send ispeedleft + 127 to one PWM Victor, ispeedright + 127 to the other.
Now the fwd/rev motion of the joystick controls your base speed, and the left/right motion controls the difference in speed between left and right motors (ie: the robot turns).
|