08 - Using the Physical Robot

Lesson Overview

  1. Building the Physical Robot

  2. Connecting the Robot to the Handheld VEXnet Controller/Joystick via USB

  3. Connecting the Robot to the Computer via USB to Compile and Download your First Program

  4. Settings up Your Ports / Using the Debugger Windows

  5. Programming with the Handheld VEXnet Controller/Joystick

  6. Classwork Activities

  7. Common Mistakes

Building the Physical Robot

  1. Link #1 - Here is how to build the robot with no sensors/encoders. You do this first.

  2. Link #2 - Here is how to add the sensors and encoders (yes you have to disassemble parts of the robot)

  3. Link #3 - Here are useful tips from other students that have gone through this process.

  4. Important tips from the teacher below

    1. Wiring Diagram below

Important tips from the teacher

  1. There are no pop rivets, use nuts and bolts

  2. When attaching the wheels (very first steps in the very first building instructions), move them forward one notch. Instead of the three black 3 holed plastic pieces being centered on the metal rim they will be moved forward one notch. Note that the two sides are mirror images of one another, so on one side you move them left one notch and on the other you move them right one notch. Their is an example robot in the front of the room. The reason for this is that we want to put the wheel encoders in a different spot than the directions state.

  3. We will not be using all of the sensors - we do not have gyro sensors. Do not attach the potentiometers. These can easily be broken if you are not careful in your programming - so let's leave these off.

Wiring Diagram

Motors

port1 (port1) - leftMotor

port6 (port6) - clawMotor

port7 (port7) - armMotor

port10 (port10) - rightMotor

Analog Sensors

port 1 (in1) - leftLineFollower

port 2 (in2) - centerLineFollower

port 3 (in3) - rightLineFollower

Digital Sensors

port 1 (dgtl1) - rightEncoder

port 2 - The 2nd plug from the right encoder

port 3 (dgtl3) - leftEncoder

port 4 - The 2nd wire from the left encoder

port 6 (dgtl6) - touchSensor

port 8 (dgtl8) - sonarSensor (cm)

port 9 - The 2nd wire from the sonarSensor

Note that when connecting wires, the black side of the connector goes towards the outside and the white/orange/red go towards the inside.

Note - On our actual physical robots, we do not have a gyro sensor or an arm encoder so these are not listed above - even though these are part of the "standard" setup we have been using with our Virtual Worlds robot.

Connecting the Robot to the Handheld VEXnet Controller/Joystick via USB

    1. Connect the large blue battery to your robot.

    2. Make sure everything is off (controller and robot).

    3. Connect the controller to the robot via the orange USB cable.

    4. Turn on the controller and the robot at the same time.

    5. Wait until two green lights appear on both the handheld controller and the mini-controller cortex (the robot's "brain") as shown below.

Updating the Firmware (optional)

If there are continual issues with the connections, we may need to download the firmware for RobotC onto the Cortex and/or Controller.

    1. Connect the robot cortex to your computer via the USB cable.

    2. In RobotC, from the top menu select "Robot --> Download Firmware --> Manually Update Firmware --> Master CPU --> Standard File (CORTEX_V4_25.BIN)"

    3. Connect the handheld Vexnet controller/joystick to your computer via the USB cable.

    4. In RobotC, from the top menu select "Robot --> Download Firmware --> Manually Update Firmware --> VEXnet Joystick Firmware --> Standard File (JOY_V4_25.BIN)"

    5. At any time (nothing needs to be connected), select from the top menu "Robot --> Download Firmware --> Manually Update Firmware --> RobotC ..."

Connecting the Robot to the Computer via USB to Compile and Download your First Program

    1. Connect the large blue battery to the robot.

    2. Turn the robot off.

    3. Connect the robot to the computer via the orange USB cable.

    1. Set the compiler target to the physical robot as shown below.

    1. Write a simple code such as copying and pasting the one below.

      1. #pragma config(StandardModel, "RVW CLAWBOT")

        1. task main()

        2. {

        3. motor[leftMotor] = 127; //Turns on only the Left Motor for one second

        4. wait1Msec(1000);

        5. motor[leftMotor]=0;

        6. motor[rightMotor]=127; //Turns on only the Right Motor for one second

        7. wait1Msec(1000);

        8. motor[rightMotor]=0;

        9. motor[armMotor]=127; //Turns on only the Arm Motor for one second

        10. wait1Msec(1000);

        11. motor[armMotor]=0;

        12. motor[clawMotor]=127; //Turns on only the Claw Motor for one second

        13. wait1Msec(1000);

        14. motor[clawMotor] = 0; //Turns off all of the motors

        15. }

    2. With your robot still connected to your computer via the USB cable, compile and download your program.

    3. On your computer screen you should see a screen like:

    1. Now you can run the program in three ways:

      • Connected to computer - Turn on your robot and with your robot still connected to your computer via the USB cable, click "Start" on your computer. Careful as this will start the program and your robot will drive off the table.

      • Connected to handheld VEXnet controller/joystick via USB cable - Unplug the USB cable from your computer and connect it to your handheld VEXnet controller/joystick. When ready, turn both on at the same time. If you do not have any controller/joystick buttons waiting to be pressed it will start immediately. Otherwise it will wait for you to press a button if you coded it that way.

      • Connected to the handheld VEXnet controller/joystick using USB WiFi dongles (wireless) - Unplug the USB cable from both your computer and the robot. Turn both your robot and the controller off. Insert the one of the WiFi dongles into the robot's VEX cortex and insert the other Wifi dongle into the handheld VEXnet controller/joystick. Turn both on at the same time and wait for the green lights as shown in the first image on this page (near the top of the page). Note - these WiFi dongles have been very glitchy so overall we usually just use the USB cable for a wired connection.

Setting Up Your Ports / Using the Debugger Windows

Overall, you need to check that your motors and sensors are connected to the correct ports. If they are not connected to the standard Clawbot ports, you have two options. You can either unplug the motors/sensors and plug them into the correct ports, or you can reconfigure the settings in RobotC to make the physical setup. As the engineering class builds our robots for us and they come built with the wires zip tied together, we will reconfigure RobotC rather than cutting the zip ties and rewiring the robots.

Link to the standard Clawbot setup: Clawbot Specs

Part 1 - Changing the Standard Setup to Match your Wiring

    1. One by one, follow the cables from each motor and sensor to find what ports they are plugged into. Three of the four sensors we use have two cables instead of one. The two cables should be plugged into adjacent ports. Below is the standard setup:

              1. Motors

              1. Analog Sensors

              1. Digital Sensors

            1. port1 (port1) - leftMotor

            2. port6 (port6) - clawMotor

            3. port7 (port7) - armMotor

            4. port10 (port10) - rightMotor

            1. port 1 (in1) - leftLineFollower

            2. port 2 (in2) - centerLineFollower

            3. port 3 (in3) - rightLineFollower

            1. port 1 (dgtl1) - rightEncoder

            2. port 2 - The 2nd plug from the right encoder

            3. port 3 (dgtl3) - leftEncoder

            4. port 4 - The 2nd wire from the left encoder

            5. port 6 (dgtl6) - touchSensor

            6. port 8 (dgtl8) - sonarSensor (cm)

            7. port 9 - The 2nd wire from the sonarSensor

    1. Note - On our actual physical robots, we do not have a gyro sensor or an arm encoder so these are not listed above - even though these are part of the "standard" setup we have been using with our Virtual Worlds robot.

    2. If your cables are all plugged into the standard ports, move on to Part 2 of this section below, titled "Part 2 - Testing the Motors..."

    3. If your cables are plugged into different ports than the ones listed above, we will change the standard setup to match the current wiring.

      1. In RobotC from the top menu go to "Robot --> Motors and Sensors Setup"

      2. In the first tab titled "Standard Models", click on the drop down menu and select "RVW CLAWBOT"

      3. In the second tab titled "Motors", you will reconfigure these to match your wiring. You will do this by copying the information from the old port to the new port of your current wiring. For an example, let us say that our wiring has the left-side wheel motor in port 3. We will take all of the information about the left motor in port 1 (standard setup) and move it to port 3 (our actual wiring setup at the moment).

              1. ... should become

      1. Notice that I copied all of the information exactly as it was from the 5 parameters - The name, type of motor, whether or not the motor is reversed, the encoder port, and the PID control.

      2. Notice also that I removed all of the information from port1 so there is no duplicate information confusing our configuration.

      3. Do this for each motor and sensor that is not currently in the "Standard RVW CLAWBOT" setup. Once you have these configured to match your physical wiring, move on to "Part 2 - Testing the Motors ..." below.

Part 2 - Testing the Motors (green and black plastic boxes on the robot)

    1. Connect your robot to your computer via the USB cable.

    2. Compile and download a simple test program to your robot such as the one below:

      1. #pragma config(StandardModel, "RVW CLAWBOT")

        1. task main()

        2. {

        3. motor[leftMotor] = 127; //Turns on only the Left Motor for one second

        4. wait1Msec(1000);

        5. motor[leftMotor]=0;

        6. motor[rightMotor]=127; //Turns on only the Right Motor for one second

        7. wait1Msec(1000);

        8. motor[rightMotor]=0;

        9. motor[armMotor]=127; //Turns on only the Arm Motor for one second

        10. wait1Msec(1000);

        11. motor[armMotor]=0;

        12. motor[clawMotor]=127; //Turns on only the Claw Motor for one second

        13. wait1Msec(1000);

        14. motor[clawMotor] = 0; //Turns off all of the motors

        15. }

    3. On your computer screen you should see a screen like:

    1. With your robot still connected to the computer via the USB cable, turn on your robot and click the "Start" button on your computer screen (as shown above). Make sure to pick your robot up off of the table, as the wheels, arm and claw will begin to spin and move when you hit start. Using the copied and pasted program above, it should turn on each of the motors to full positive power for one second in sequence.

    2. Which motors worked? Which did not? Some things to consider and some possible fixes:

      • Did one of the wheel motors spin backwards? If so, go to the main menu at the top and select "Robot --> Motor and Sensor Setup" and under the "Motors" tab, click the "Reversed" checkbox next to that motor so that positive makes it spin forwards instead of in reverse.

      • Did one of the motors not work? If so, look at the physical motor and trace the cable coming out of it. It should go into one of the 10 motor ports on the VEXcortex on the robot. If it is connected to a non-standard port, follow the instructions above in "Part 1 - Changing the Standard Setup to Match your Wiring" to reconfigure your RobotC for the actual wiring of your physical robot.

      • Errors downloading and compiling to the robot? This is usually an issue with the USB ports on our laptops. Our laptops have issues in that when we physically close and reopen the laptop, the USB ports sometimes do not come out of hibernation mode. Try these three things:

        • Plugging the USB cable into a different port on your computer.

        • Restarting your computer

        • Emailing the code to another group member and use their computer

        • Re-download the firmware for the VEXcortex processor on the robot (see the instructions at the bottom of the first section of these page titled "Connecting the Robot to the Handheld VEXnet...")

Part 3 - Testing the Sensors (red and black plastic boxes on the robot)

  1. Connect your robot to your computer via the USB cable.

  2. Compile and download a simple program where the wheels turn, such as the one below

    1. #pragma config(StandardModel, "RVW CLAWBOT")

      1. task main()

      2. {

      3. SensorValue[leftEncoder]=0; SensorValue[rightEncoder]=0;

      4. motor[rightMotor]=127; //Turns on only the Right Motor for three seconds

      5. wait1Msec(3000);

      6. motor[rightMotor]=0;

      7. motor[leftMotor]=127; //Turns on only the Left Motor for three seconds

      8. wait1Msec(3000);

      9. motor[leftMotor] = 0; //Turns off all of the motors

      10. }

  3. On your screen you should see a screen like this:

  1. Before you click "Start" or unplug your robot, from the top menu select "Robot --> Debugger Windows --> Sensors"

  2. With your robot still connected to the computer via the USB cable, turn on your robot and click the "Start" button on your computer screen (as shown above). Make sure to pick your robot up off of the table, as the wheels, arm and claw will begin to spin and move when you hit start. Using the copied and pasted program above, it should turn on only the right motor for three seconds then stop it. It will then turn on the left motor for three seconds and stop all the motors.

  3. While the code is running, check these:

      • For the first three seconds (while the right motor is turning), is your right encoder's value changing? It doesn't matter at the moment whether it is negative or positive.

      • For the next three seconds (while the left motor is turning), is your left encoder's value changing? It doesn't matter at the moment whether it is negative or positive

      • When you push in the bumper/touch sensor on the front of the robot does your "touchSensor" change from "0" to "1"?

      • When you wave your hand in front of the ultrasonic rangefinder in front of your robot, does the value for "sonarSensor" change from "-1" to a distance in cm of your hand in front of the robot?

      • When you put things of different lightness/darkness under the three line followers on the bottom of the robot, do the values or the three "...LineFollower"s change?

  4. If a sensor is not working, here are a few things you can try:

      • Check the wiring again - did you accidentally think it went into port 3 but now you look and port 3 is empty? Double check the cable.

      • Check the "Motors and Sensors Setup" again - did you accidentally copy the touchSensor info into port 5 instead of port 4? Try opening a new tab and in the new tab select the Standard Model "RVW CLAWBOT" to double check that your sensor's settings match the original but just in a new port. Maybe you accidentally selected the wrong thing from the drop-down menu.

      • Rewire/Swap ports - Theoretically it shouldn't matter which port the sensors are plugged into or the order of the two cables for the sensors with two cables. However, sometimes swapping the cables can be like hitting the reset button. Try either moving the cable(s) to a wholly new empty port and reconfiguring your RobotC to match the new port -OR- try switching the locations of the two cables for a sensor that has two cables (encoders and ultrasonic range finder).

      • Update the firmware - Sometimes reloading the robot's firmware can help fix things. Instructions for this are at the bottom of the first section at the top of this page. The section is titled "Connecting the Robot to the Handheld Controller/Joystick via USB" ... or an easier way to find it is just search this page for the word "firmware".

      • Try it out on a new computer - sometimes your RobotC can be glitchy, and starting fresh on someone else's laptop can help

Programming with the Handheld VEXnet Controller/Joystick

Main difference from using Windows and a regular USB game controller is that you do not include these two lines of code anymore. These were telling RobotC how to interact with Windows on your computer:

DO NOT include these when using the VEX Controller and the physical robot

#include "JoystickDriver.c"

getJoystickSettings(joystick);

Just as with the USB game controllers in RobotC, the big picture of the code is the same. To program a button, we set an if statement in a while loop to detect the button press. The button is detected in a while loop that runs indefinitely. In other words, the overall loop will look like this:

while (true) {

if ( button 1 is pressed) {

do something

}

else if ( button 2 is pressed) {

do something

}

else {

leave/turn everything off

}

}

Everything on the VEX Controller is either a button or an analog joystick. The values can be gotten like this:

Buttons

Joystick

Example: vexRT[Btn6U] == 1

Each set of buttons is labeled with a number, then each individual button has a direction following it, so there is a 7U (7-up) and 7D (7-down) and so on. Each one is clearly labeled as shown below

Example: vexRT[Ch3]

Each direction on each joystick is a channel number from 1-4. These are clearly labeled on the controller as shown below. These channels have values from -127 to +127.

The actual condition you will use for a button press looks like this:

if( vexRT[Btn6U] == 1 ) {...}

The vexRT[Btn6U] represents the 6U button shown below.

Every button on the controller is clearly labeled with a white number and a black (colorless) indented letter. The black letters represent the direction - U for up, D for down, L for left, and R for right. Look at the image below. Around the white number 7 in the top left are buttons 7U, 7D, 7L and 7R.

To learn the values of all the buttons on the handheld VEXnet controller/joystick, visit this link

Example Code using Two Different Buttons

This code moves the robot forward when the 6U button is pressed, and backwards when the 6D button is pressed. It stops when no button is being pressed.

//Make sure to setup the motors and sensors accordingly.

task main()

{

while(true)

{

if( vexRT[Btn6U] == 1 )

{

motor[leftMotor] = 60;

motor[rightMotor] = 60;

}

else if( vexRT[Btn6D] == 1 )

{

motor[leftMotor] = -60;

motor[rightMotor] = -60;

}

else

{

motor[leftMotor] = 0;

motor[rightMotor] = 0;

}

}

}

Analog Stick

The analog stick is implemented by sending values based upon how far you pushed the stick in a direction. The further the stick is pushed, the greater that value is sent. The x- and y- axis of the analog sticks are scaled to represent the numbers from -127 to +127 for easy implementation in your code.

There are 2 axis on each stick, the horizontal x-axis and the vertical y-axis. Which direction is which is clearly labeled by the joysticks. As shown below, the x-axis of the left joystick is Channel 4. The y-axis of the left joystick is Channel 3. The channels for the right joystick are Channel 1 (x-axis) and Channel 2 (y-axis).

To use the analog stick, you can set the motor speed to the channel (Axis) of the desired analog stick within an infinite loop. This way the loop keeps running, millions of times a second. Each time the loop runs, it sets the motor power to the location of the joystick. In real time it detects where the joystick is and speeds up or slows down your robot. If you are not touching the joystick, the motor power will be at zero - a.k.a. the point (0,0).

The following code used the analog stick to move the left and right wheel based upon the axis 3 and 2 of the analog stick.

//put this inside your main

while(true)

{

motor[leftMotor] = vexRT[Ch3];

motor[rightMotor] = vexRT[Ch2];

}

In other words, this will drive your robot like a classic tank in video games.

    • Both joysticks up will move the robot forwards.

    • Both joysticks down will move the robot backwards

    • Left joystick up and right joystick down will turn the robot right while staying in place

    • Left joystick down and right joystick up will turn the robot left while staying in place.

Classwork Activities

Activity 1

Implement code for your robot to move forward and backwards with buttons 8U and 8D.

Activity 2

Implement code for your robot to turn left and right in place with buttons 8L and 8R.

Activity 3

Implement code for your robot to raise and lower the arm with buttons of your choice.

Activity 4

Implement code for your robot to open and close the claw with buttons of your choice.

Activity 5

Implement code so your robot can move in all directions, and have use of the arm and claw using the analog stick.

Common Mistakes

  1. Code is not compiling - The easiest fix for most common problems (other than checking that things are physically connected, you have a battery connected that is not dead, and everything is turned on) is to manually reinstall the drivers. I will not re-explain how to do this here. Instead search the page for "Updating the Firmware" and follow the instructions.

  2. Controller Code has an Error about "This driver is not supported on this platform" - It also says that there is an undefined variable 'JoystickCopy'. 'short' assumed. This error is happening because you are using the two lines #include "JoystickDriver.c" and getJoystickSettings(joystick); which were specifically only used to communicate with the Windows operating system on your computer. The physical robot's main CPU (the little brain of it that you download programs to) has code built in to handle the controller. Delete the two lines mentioned and then use the default button names that are already on your robot, namely vexRT[Btn6U] and vexRT[Ch3]