Making the robot turn
|
Turning Concepts
For this type of vehicle, there are two types of turns: swing turns and point turns. A swing turn is when the two drive servos are set to different values and the robot's path makes a curve. A point turn is when the two drive servos set to equal but opposite (100 and -100, for example) power levels. A point turn causes the robot to spin in place.
Point Turn Concepts
Point turns require that the center of rotation is centered between the drive wheels. For this to happen one side must go forward while the other side goes backwards. Since the attempted direction of travel are opposite they counteract each other and cause the vehicle to spin in place. This type of turn can be very useful and is very simple to predict. However, not all vehicles can perform this type of turn.
| Parallax BoeBot + Arduino making point turns |
Swing Turn Concepts
A swing turn is where a vehicle "swings" around a pivot point. This occurs when one side of the vehicle is traveling faster than the other. It is often the case that one side is completely stopped, causing the vehicle to pivot around the stopped wheel. However, it is also possible to have the vehicle make a much more gradual turn by having both sides going forward but at different speeds.
| Parallax BoeBot + Arduino making swing turns |
Programing
To demonstrate the different turning styles we will make two programs. The first will be for point turns, while the second will be for swing turns.
Point Turn Program
In this program we want to make the robot make a point turn to the left for 1 second, pause for one second, make a point turn to the right for 2 seconds, pause for 1 second, make a point turn to the left for 1 second, pause for 1 second and repeat. So to make this work we would need to have code to do the following
task main() { while(true) { //set servos for a left point turn //pause for 1 second //set servos to stop //pause for 1 second //set servos for a right point turn //pause for 2 seconds //set servos to stop //pause for 1 second //set servos for a left point turn //pause for 1 second //set servos to stop //pause for 1 second } } |
NOTE: To streamline the programming, and to help you become a more independent programmer, these tutorials will assume the user has configured the Motors and Sensors Setup window and will no longer be providing step-by-step instructions on how to configure the motors and sensors.
For this program you will need a Parallax BoeBot (with a BOEShield), an Arduino UNO attached to it, the left continuous servo plugged into pin 10, and the right continuous servo plugged into pin 11.
Since a point turn works by having both sides of the vehicle moving in opposite directions, we need to program the two drive servos to move opposite one another:
motor[leftServo] = 20; motor[rightServo] = -20; |
This will cause the robot to turn to the right. To make it turn to the left, both servos need to change direction:
motor[leftServo] = -20; motor[rightServo] = 20; |
We already know how to stop the servos, so if you put all the segments of code together to replace the psuedocode, you will end up with something like this:
task main() { while(true) { //set servos for a left point turn motor[leftServo] = -20; motor[rightServo] = 20; //pause for 1 second wait1Msec(1000); //set servos to stop motor[leftServo] = 0; motor[rightServo] = 0; //pause for 1 second wait1Msec(1000); //set servos for a right point turn motor[leftServo] = 20; motor[rightServo] = -20; //pause for 2 seconds wait1Msec(2000); //set servos to stop motor[leftServo] = 0; motor[rightServo] = 0; //pause for 1 second wait1Msec(1000); //set servos for a left point turn motor[leftServo] = -20; motor[rightServo] = 20; //pause for 1 second wait1Msec(1000); //set servos to stop motor[leftServo] = 0; motor[rightServo] = 0; //pause for 1 second wait1Msec(1000); } } |
Swing Turn Program
In this program we want the robot to make a forward swing turn to the left, pause, make a backward swing turn to the left, pause, make a forward swing turn to the right, pause, make a backward swing turn to the right, pause then repeat.
To make this work we need to have the code do the following:
task main() { while(true) { //set servos for a forward left swing turn //pause for 1 second //set servos to stop //pause for 1 second //set servos for a backward left swing turn //pause for 1 seconds //set servos to stop //pause for 1 second //set servos for a forward right swing turn //pause for 1 second //set servos to stop //pause for 1 second //set servos for a backward right swing turn //pause for 1 seconds //set servos to stop //pause for 1 second } } |
Since this kind of swing turn works by having one side stopped while the other side is moving, we will need to move one drive servo forward while the other stays stationary:
motor[leftServo] = 20; motor[rightServo] = 0; |
This will cause the robot to turn to the front right, or make a forward right swing turn. To make it turn to the left, we will move the right servo forward and stop the left motor:
motor[leftServo] = 0; motor[rightServo] = 20; |
To make a backwards turn all you have to do is reverse the direction of the side that is moving:
motor[leftServo] = -20; motor[rightServo] = 0; |
We already know how to stop the servos, so if you put all the segments of code together to replace the psudocode, you will end up with something like this.
task main() { while(true) { //set servos for a forward left swing turn motor[leftServo] = 0; motor[rightServo] = 20; //pause for 1 second wait1Msec(1000); //set servos to stop motor[leftServo] = 0; motor[rightServo] = 0; //pause for 1 second wait1Msec(1000); //set servos for a backward left swing turn motor[leftServo] = 0; motor[rightServo] = -20; //pause for 1 second wait1Msec(1000); //set servos to stop motor[leftServo] = 0; motor[rightServo] = 0; //pause for 1 second wait1Msec(1000); //set servos for a forward right swing turn motor[leftServo] = 20; motor[rightServo] = 0; //pause for 1 second wait1Msec(1000); //set servos to stop motor[leftServo] = 0; motor[rightServo] = 0; //pause for 1 second wait1Msec(1000); //set servos for a backward right swing turn motor[leftServo] = -20; motor[rightServo] = 0; //pause for 1 second wait1Msec(1000); //set servos to stop motor[leftServo] = 0; motor[rightServo] = 0; //pause for 1 second wait1Msec(1000); } } |