Making the robot turn
|
Turning Concepts
For this type of vehicle, there are two kinds of turns. The first is a point turn, in which the vehicle does not change its position. The second is a swing turn, which is basically any turn that is not a point turn.
Point Turn Concepts
Point turns require the center of rotation to be centered between the drive wheels. For this to happen, one side must go forward while the other side goes backwards. Since the attempted directions of travel are opposite, they counteract each other and cause the vehicle to turn without changing position. This type of turn can be very useful, and is very simple to predict. However, not all vehicles can perform this type of turn. Thankfully, the Lego robot can perform them, and the point turn will be the primary turning method for our robots.
| LEGO + Arduino robot doing point turns |
Swing Turn Concepts
A swing turn is a turn where a vehicle "swings" around a pivot point. This occurs when one side of the vehicle is travelling faster than the other. It is often the case that one side is just stopped, causing the vehicle to just pivot around the 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. However, for these first few projects we only care about cases where one side of the vehicle is stopped.
| LEGO + Arduino robot doing swing turns |
Programing
To demonstrate the different turning styles, we will make two programs in this tutorial. 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 make things go a little bit faster, and to help you become a more independent programmer, from now on, these tutorials will no longer be providing step-by-step instructions on how to configure the motors and sensors.
So, for this program we have a DFRobot Motor Shield, the left continuous servo plugged into pin 6, and the right continuous servo plugged into pin 5, both of which need to be reversed.
Since a point turn works by having both sides of the vehicle trying to go opposite directions, we need to tell the robot to do that. We can do so by using the following commands:
motor[leftServo] = 100; motor[rightServo] = -100; |
The previous code would cause the robot to turn to the right. To make it turn to the left, we just have to reverse both servos’ direction and end up with the following.
motor[leftServo] = -100; motor[rightServo] = 100; |
We already know how to stop the servos, so if you put all the bits of code together to replace the psudocode, you should end up with something like this.
task main() { while(true) { //set servos for a left point turn motor[leftServo] = -100; motor[rightServo] = 100; //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] = 100; motor[rightServo] = -100; //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] = -100; motor[rightServo] = 100; //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. So to make this work, we would need to have code to 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 } } |
So, for this program we have a DFRobot Motor Shield, the left continuous servo plugged into pin 6 named "leftServo", and the right continuous servo plugged into pin 5 named "rightServo" both of which need to be reversed.
Since this kind of swing turn works by having one side stopped, we need to tell the robot to do that. We can do this by using the following commands.
motor[leftServo] = 100; motor[rightServo] = 0; |
The previous code would cause the robot to turn to the front right, or make a forward right swing turn. To make it turn to the left, we just have to change which servo is moving, and end up with the following.
motor[leftServo] = 0; motor[rightServo] = 100; |
To make a backwards turn all you have to do is reverse the direction of the side that is moving.
We already know how to stop the servos, so if you put all the bits of code together to replace the psudocode, you should end up with something like this.
task main() { while(true) { //set servos for a forward left swing turn motor[leftServo] = 0; motor[rightServo] = 100; //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] = -100; //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] = 100; 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] = -100; 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); } } |
