Introduction to RobotC and Tetrix with Lego Mindstorms|
These lessons will introduce autonomous robot programs in RobotC using the Lego NXT Mindstorms Robotics Kit. Topics will cover:
This tutorial uses the "Basic Tri-Bot Version II"
1 = NXT Touch Sensor
2 = NXT Sound Sensor
3 = NXT Light Sensor
4 = NXT UltraSonic Sensor
A = "Arm"
B = Right Drive Motor
C = Left Drive Motor
When you start programming in RobotC, you will need to define all the motor and sensor connections. This is like defining the "objects" in a program before you start creating instructions and methods. RobotC has two menu driven tools to accomplish this configuration.
1. Start RobotC and Select File-New-New File to start a new program.
2. Select "Robot->Platform Type" from the menu bar and select "LEGO Mindstorms NXT".
3. Select "Robot->Motors and Sensors Setup"
4. Click the "Motors" tab. Name the motors as shown below. Note that I used the name "Arm" for "motorA." Note that you can give the Motors names. Choose names that make sense for the robot's purpose. For this lesson Motor C will be labeled "Left" and Motor B will be Labeled "Right." This will make your Code easier to understand later.
5. Click "Apply."
6. Click the "Sensors" tab. Set Sensors S1, S2, S3, and S4 as shown below. Make sure to select "Touch", "Reflected Light", and "Sonar." in "Type."
7. Click "Apply."
8. Click "OK."
9. Note that the first 7 lines of your program now contain auto generated code that configures the Hubs, Sensors, Motors, and Servos. (The "pragma config") These define the robot objects in the program.
10. Save your program! Name it "IntroRobotC"
Lesson 2: Beginning to Drive: The "Forward - Stop"
The most basic element of robot programming follows the format of
"Start the Task -> Wait for a condition -> Stop the Task"
In this program, we will:
1. Start the motors
2. Wait for 2 seconds
3. Stop the motors
In your program - type the following code below the Configuration Code:
To run your program:
1. Plug the Robot USB Cable into the Robot and Computer. Make sure the robot is on.
2. Select "Robot -> Compile and Download Program." Then close the "Debugger" and select and Run the program on the NXT Brick.
To Run a Program on the NXT Brick:
Select "Software Files" with Orange Button
Select "My Files"
Select your program "IntroRobotC"
All RobotC programs start with the "task main." Inside the curly brackets are the instructions:
Here is a breakdown:
Now try the following program "Out and Back"
Lesson 3: Making methods
One of the main goals of programming is to create re-usable pieces of directions that make sense to the human reading your Code, and save time and space (not having to re-type long series of commands over and over again.) Thus we are going to start making a series of "Methods" that do common tasks.
A "Method" is a block of instructions that you can call in the Main Task over and over again. We will make two methods: "driveStraight" and "stopMotors."
Type the following above your "task main()":
1. We use the term "void" before the Method because the method does not return a value. (It only directions action.)
2. The "int power" is a parameter for the driveStraight method. This allows the programmer to define how much power will go to the motors. The "int" means "integer."
Type the following into the task main() to run these methods:
Note that the main task now uses three lines of code to do what took us 5 lines earlier.
Exercise: Make the Robot Drive Forward and Backward using the driveStraight and stopMotors methods. Hint: Negative numbers will make the motors turn backwards.
Lesson 4: Methods using Sensors and While Loops
Up to now we have been creating linear sequential programs that only use motor output. The Sensors on the robot (Touch, Light, and Sonar) provide input and allow us to use some event driven programming.
We will use Sensors such as Touch, Light, and Sonar to help guide the robot through the world. These series of methods follow the same basic robot pattern:
"Start the motors -> Wait for a Sensor Condition -> Stop the motors"
Add the following method and edit the main task in your program for "TouchStop." Compile and test the program:
1. We use the parameter "int power" again to let the programmer define how fast the robot should go.
2. The "while loop" tells the program to run the "driveStraight" method until the touch sensor is touched.
3. "SensorValue[Touch] != 1" means: "Touch Sensor does not equal 1" or "Touch Sensor Not Pressed."
4. Translation of method: "While the Touch Sensor is not pressed, Drive Straight. When the Touch sensor is pressed, stop the motors."
Try these methods for UltraSonic Stop and Light Stop:
1. In the "nearStop" method, note the two parameters: "distance" and "power." This allows the user to define the distance in centimeters for the stop and the speed of the robot. The Default distance for the Sonar is measured in centimeters.
2. In the "darkStop" program the parameters "brightness" and "power" allow the user to specify the darkness the robot is looking for and the speed of the motors. The default range of values for the Light sensors ranges from 0 to 100. 0 being perfectly dark and 100 being absolutely bright.
We can also reduce the basic Forward for Time type program with this method. Also included is a Method for Point Turns:
Lesson 5: More Advanced Programs - using variables and methods we have already created.
Example 1: The Nearest Object program. This example spins the robot around. While the robot is spinning, it is seeking out the nearest object using the UltraSonic Sensor. When it is finished spinning, it points back to the nearest object and travels up to the object and stops.
Extras: Make the back up and then seek the next nearest object
Note: Compare the Nearest Object program to this Java Example:
Note: Compare to this example in Python:
Example 2: Proportional Line Following
Variables for Line Following
1. Kp is the Constant of Proportion. Multiply the "error" to increase the "sensitivity" of the Line follower.
2. offset is the threshold value for dark. Where the robot will travel straight.
3. Tp is the Constant for Power. This is the baseline power for the Left and Right wheels.
Method for Line Following:
Main Task for Line Following: