03 - Movement / Motors

How to Check Your Motor's Ports and/or Names

Each robot setup will have different motors or sensors plugged into different ports (this goes for digital robot setups as well). We need to make sure that we are using the correct setup and then see what the actual ports and/or names of our motors and sensors are. This way we can accurately refer to them in our code.

To look at your robot setup, go to the menu at the top and select "Robot ---> Motors and Sensors Setup". An image of this is shown below.

You will be looking at a screen with four tabs like the one shown below. Make sure that on the first tab, you have the correct robot selected. In this class, we will be using the "RVW CLAWBOT"

You can explore the settings in the next three tabs, though this is unnecessary at the moment. By selecting the "RVW CLAWBOT" in the first tab, the RobotC program will automatically set up everything for you. Each tab will show you which port and name are assigned to which elements.

    • The "Motors" tab describes the setup of the four motors:

      • leftMotor - powers the left wheels to move forward or backwards

      • rightMotor - powers the right wheels to move forward or backwards

      • armMotor - powers the arm to raise or lower

      • clawMotor - powers the claw to open or close

    • The "VEX 2.0 Analog Sensors 1-8" tab describes the setup of the four analog sensors:

      • leftLineFollower - light sensor pointed downwards at the ground below the robot that detects whether it is passing over a dark or light surface. This one is on the left side of the robot.

      • centerLineFollower - same sensor as above but in the center robot (middle of the three sensors).

      • rightLineFollower - same sensor as above but on the right side of the robot (rightmost of the three sensors).

      • gyro - a directional sensor similar to a compass, but in degrees (or actually tenths of degrees). It senses in which direction on the horizontal ground that your robot is pointing (e.g. north, west, etc)

    • The "VEX 2.0 Digital Sensors 1-12" tab describes the setup of the five digital sensors.

    • touchSensor - a physical sensor on the front of the robot that detects whether or not the robot (or more specifically this sensor) has physically bumped into/touched another object.

      • sonarSensor - the infrared range finder detects how far an object is from the front of the robot (in centimeters).

      • Three encoders - these record how far something has rotated in degrees. These can be useful for traveling accurate distances, in straight lines, or for knowing at what angle the arm of the robot is pointing.

        • leftEncoder - measures (in degrees) how much the left wheels have rotated.

        • rightEncoder - measures (in degrees) how much the right wheels have rotated.

        • armEncoder - measures (in degrees) how much the arm joint has rotated to raise or lower the arm.

How to Move your Robot (The Official RobotC Tutorial)

Visit the official RobotC tutorial at this link: Link. This tutorial is very clear and even asks you to run through a few quick problems and will tell you whether your code is correct or not.

Basic C Instructions to Move Your Robot

There are two basic methods we will use in moving the robot. They are:

  1. motor[ ] = ;

  2. wait1Msec( );

Let's separately look at each of these functions.

motor[name-or-port] = number;

This function above sets the power level of the given motor to a given number. In the square brackets you either enter the port of the motor (port1, port2, etc) or the name that you have assigned to the motor. In the standard CLAWBOT setup that we will be using, the ports are already named (as mentioned above). After the equals sign, you put in a number between -127 and +127. Negative means the wheel will turn in the reverse direction at this power level. This power level does not directly translate to speed, but the more power the motor gets, the faster it will go. A few examples of this are below with notes about them.

//Note - In the standard CLAWBOT setup the left and right motors are logically named "leftMotor" and "rightMotor"

//Turns on the left motor (leftMotor) to full power (127) moving forward (positive number)

motor[leftMotor] = 127;

//Turns off the left motor (leftMotor) by setting the power level of the motor to zero

motor[leftMotor] = 0;

//Turns on the right motor (rightMotor) to around half power (64) moving backwards (negative number)

motor[rightMotor] = -64;

//Turns off the right motor (leftMotor) by setting the power level of the motor to zero

motor[leftMotor] = 0;

/*****************************************************************************************************************************************************

* Note that this code runs through as fast as the machine can handle.

* This means that the robot will have two super quick spurts of motion that are nearly instantaneous then remain still.

* This is because the machine sets the left motor to full power, the in the next instant sets it to zero power (off).

* The program then turns the right motor to half power in reverse, then in the next instant sets it to zero power (off).

* We need a way to make the program wait. There are two ways to do this. One is the basic - wait a set amount of time (described below).

* The second way is the more precise which is using encoders to measure how many degrees the wheels have rotated, and

* if you know the size of the wheel you can calculate the distance traveled per degree.

**************************************************************************************************************************************************************************/

wait1Msec(time);

This function sets tells the robot to wait some number of milliseconds before running the next command. The settings remain the way they were before this function was called. In other words, if the left and right motors are set to full power (+127) and then the robot is told to wait 2,000 milliseconds, the motors will be on full power for those 2,000 milliseconds. An example of this is below with explanation notes.

/Note - In the standard CLAWBOT setup the left and right motors are logically named "leftMotor" and "rightMotor"

//Turns on the left and right motors to around half power (+64)

motor[leftMotor] = 64;

motor[rightMotor] = 64;

//Tells the robot to keep these settings and wait 2,000 milliseconds, or 2 seconds, before running the next line of code

wait1Msec(2000);

//Stops the forward motion of the robot by turning off the left and right motors (power levels of zero))

motor[leftMotor] = 0;

motor[rightMotor] = 0;

A Few Basic Movements

Moving Forward or Backwards - in the beginning we move forward by setting the two motors to equal power. Later on we realize that all motors are not the same, so equal power does not result in a straight line. We will use encoders for this in the future to be more certain that we are moving in a straight line. Example of the simple version is below.

// Moves forward for two seconds

// Turns on the left and right motors to equal power (around half power) for two seconds

motor[leftMotor] = 64;

motor[rightMotor] = 64;

wait1Msec(2000);

// Moves backwards for two seconds

// Turns on the left and right motors to equal power BUT in reverse (around half power)

motor[leftMotor] = -64;

motor[rightMotor] = -64;

wait1Msec(2000);

Turning Left or Right - in the beginning we turn by setting one wheel motor to forward and the other wheel motor to backwards for a set amount of time. Later on we can be more accurate in our turn by using the gyro sensor to know when to stop or readjust if we have turned too far. Example of a turn is below.

// Turns Left for one second

// Turns on the left motor backwards at half power (-64) and the right motor forwards at half power (+64)

// I have no idea how many degrees this will turn, as it is just a random 1-second time duration spent turning

motor[leftMotor] = -64;

motor[rightMotor] = 64;

wait1Msec(1000);

// Turns Right for one second

// Turns on the left motor forwards at half power (+64) and the right motor backwards at half power (-64)

// I have no idea how many degrees this will turn, as it is just a random 1-second time duration spent turning

motor[leftMotor] = 64;

motor[rightMotor] = -64;

wait1Msec(1000);

Stopping - we can stop the physical movement of the robot at any time by setting the power levels to zero. This is a helpful initialization state in between turns and movements. It improves accuracy as if you switch from forward motion to backwards without slowing or stopping there is some slipping on the actual physical ground and thus a loss in accuracy. Example of a stop is below.

// Stops the wheels from turning at all

motor[leftMotor] = 0;

motor[rightMotor] = 0;

Common Mistakes

    1. No wait time - The important thing to know is that when the program you write is finished running through, your robot will do nothing. If you do not have a loop or a wait time in your code, the entire code will finish running in a fraction of a second and then your robot will sit there, doing nothing. There are two examples below of this. The program on the left will run in a hundred thousandth of a second and be done and do nothing. The program on the right will run for at least one full second (1,000 milliseconds).

            1. The code above will finish running in about 0.0000001 seconds (depending on the speed of your computer/robot CPU). Then your robot will do nothing. For that split second, the motors were set to full power, but then all power was shut off.

            1. This program sets the left and right motors to full power, then it says to wait 1 full second (1,000 milliseconds) without changing anything else. This means the motors will be on full power for this second. Then your program ends and your robot stops doing anything and just sits there.

    1. More coming soon ...