Making the robot turn

From ROBOTC API Guide
Jump to: navigation, search

Turning Concepts

This type of vehicle is called a Differential drive vehicle. Differential drive vehicles have two types of turns: Swing turns, and point turns. In a point turn, the vehicle 'turns on a dime' and does not change position. In a swing turn, the vehicle does change position when turning (e.g. an automobile turn).

Point Turn Concepts

Point turns require that the center of rotation is between the drive wheels. For this to happen one side must go forward while the other side goes backwards at the same power. Since the attempted directions of travel are opposite they counteract each other and cause the vehicle to turn. This type of turn can be very useful, and is very simple to predict. Keep in mind that only differential drive vehicles can perform this turn.

depiction of a point turn.

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. Often, the case is that one side is simply stopped, causing the vehicle to 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. For these first few projects we only care about cases where one side of the vehicle is stopped.

depiction of a swing turn


Programming

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.

ROBOTC Configuration

Notepad.gif 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.

First we need to set the ROBOTC configuration with the following settings.

  • motor 10 as a Continuous Rotation Servo named "leftServo"
  • motor 11 as a reversed Continuous Rotation Servo named "rightServo"

Note that the configuration is the same as for the previous section on driving forwards and backwards. As such, if you are having trouble setting the configuration, please go back to the previous section to review the procedure.


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. 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
 
    //stop the servos
    //pause for 1 second
 
    //set servos for a right point turn
    //pause for 2 seconds
 
    //stop the servos
    //pause for 1 second
 
    //set servos for a left point turn
    //pause for 1 second
 
    //stop the servos
    //pause for 1 second
  }
}


Since a point turn works by having both sides of the vehicle turning in opposite directions, we need to tell the robot to do that. We can do this by using the following commands.

motor[leftServo] = 50;
motor[rightServo] = -50;

The previous code would cause the robot to turn to the right. To make it turn to the left, we just have the servos change direction and end up with the following.

motor[leftServo] = -50;
motor[rightServo] = 50;

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] = -50;
    motor[rightServo] = 50;
    //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] = 50;
    motor[rightServo] = -50;
    //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] = -50;
    motor[rightServo] = 50;
    //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
  }
}

Since this kind of swing turn works by having one side stopped, we need to set one motor to power 0. We can do this by using the following commands:

motor[leftServo] = 50;
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 the servos change direction and end up with the following.

motor[leftServo] = 0;
motor[rightServo] = 50;

To make a backwards turn all we 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] = 50;
    //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] = -50;
    //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] = 50;
    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] = -50;
    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);
  }
}