Chain and Sprocket Programming Guide (ROBOTC®) TETRIX® Add ...

5
111 Chain and Sprocket Programming Guide (ROBOTC ® ) Introduction: In this guide, the Ranger Bot with Chain and Sprocket will be programmed to move forward until it detects a black line and will then turn and follow the line until it detects an object within 30 cm. After finding the object, the robot will turn 180° and follow the line in the other direction until it detects a second object. This guide is for use with the ROBOTC ® programming language. Getting Started: 1. To start the program, type the Main Task function followed by an opening brace. task main() { 2. Declare four integer variables as seen in the code below. Set them to the values shown. Beside each variable, there are two slashes (//) followed by what that variable is representing. These mark a comment. Notice that comments appear in green when code is typed into ROBOTC. Note: A comment is not part of the code that the NXT Brick will execute, but it helps the programmer by allowing the addition of notes to describe what has been done. int dValue = 15; // Forward drive motor power int tValue = 30; // Turning motor power int ultraThresh = 30; // Ultrasonic Sensor threshold value int lightThresh = 50; // Light Sensor threshold value 3. Add a While Loop to make the contained code repeatedly execute while the Light Sensor detects a value more than the light threshold. This loop means the robot will drive forward with a power of the dValue variable while the Light Sensor de- tects a white surface. When the Light Sensor detects the black line, its reading will be less than 50 and this While Loop will stop. while(SensorValue[LightSensor] > lightThres) // Stops when line is detected { motor[motorD] = dValue; motor[motorE] = dValue; } 4. Add another While Loop to make the contained code repeatedly execute while the Light Sensor senses a value below the light threshold. This short While Loop makes the robot turn sharply (one motor moving forward and one backward) until the Light Sensor detects a white surface. This sharp turn helps the robot properly follow the line in the next step. while(SensorValue[LightSensor] < lightThres) // The first turn of the robot, a failsafe if the robot approches the line at a sharp angle (90 degrees) { motor[motorD] = dValue; motor[motorE] = dValue * (-1); } TETRIX ® Add-On Extensions Chain and Sprocket

Transcript of Chain and Sprocket Programming Guide (ROBOTC®) TETRIX® Add ...

Page 1: Chain and Sprocket Programming Guide (ROBOTC®) TETRIX® Add ...

111

Chain and Sprocket Programming Guide (ROBOTC®)

Introduction:

In this guide, the Ranger Bot with Chain and Sprocket will be programmed to move forward until it detects a black line and will then turn and follow the line until it detects an object within 30 cm. After finding the object, the robot will turn 180° and follow the line in the other direction until it detects a second object. This guide is for use with the ROBOTC® programming language.

Getting Started:

1. To start the program, type the Main Task function followed by an opening brace.

task main()

{

2. Declare four integer variables as seen in the code below. Set them to the values shown. Beside each variable, there are two slashes (//) followed by what that variable is representing. These mark a comment. Notice that comments appear in green when code is typed into ROBOTC.

Note: A comment is not part of the code that the NXT Brick will execute, but it helps the programmer by allowing the addition of notes to describe what has been done.

int dValue = 15; // Forward drive motor power

int tValue = 30; // Turning motor power

int ultraThresh = 30; // Ultrasonic Sensor threshold value

int lightThresh = 50; // Light Sensor threshold value

3. Add a While Loop to make the contained code repeatedly execute while the Light Sensor detects a value more than the light threshold. This loop means the robot will drive forward with a power of the dValue variable while the Light Sensor de-tects a white surface. When the Light Sensor detects the black line, its reading will be less than 50 and this While Loop will stop.

while(SensorValue[LightSensor] > lightThres) // Stops when line is detected

{

motor[motorD] = dValue;

motor[motorE] = dValue;

}

4. Add another While Loop to make the contained code repeatedly execute while the Light Sensor senses a value below the light threshold. This short While Loop makes the robot turn sharply (one motor moving forward and one backward) until the Light Sensor detects a white surface. This sharp turn helps the robot properly follow the line in the next step.

while(SensorValue[LightSensor] < lightThres) // The first turn of the robot, a failsafe if the robot approches the line at a sharp angle (90 degrees)

{

motor[motorD] = dValue;

motor[motorE] = dValue * (-1);

}

TETRIX® Add-On ExtensionsChain and Sprocket

Page 2: Chain and Sprocket Programming Guide (ROBOTC®) TETRIX® Add ...

112

5. Add a third While Loop. This loop is the main line-following loop. This code will repeat while the Ultrasonic Sensor reading is greater than the threshold.

while(SensorValue[SonarSensor] > ultraThres) // Until robot is within 30cm of first object...

{

6. In the line-following loop, a conditional statement is needed so the robot knows when and where to turn. If the Light Sensor detects white, it is on the right side of the line, therefore it will need to turn left toward the line again. Similarly if the sensor detects black, it needs to turn left. These Light Sensor inputs will be the if and else conditions.

if(SensorValue[LightSensor] < lightThres)

// If senses black, turn left

{

motor[motorD] = tValue;

motor[motorE] = 0;

}

else

// If senses white, turn right

{

motor[motorD] = 0;

motor[motorE] = tValue;

}

}

7. When the robot senses on object 30 cm away, it should stop for 2,000 ms and then turn around in place for 2,500 ms.

motor[motorD] = 0; // Stops robot for two seconds

motor[motorE] = 0;

wait1Msec(2000);

motor[motorD] = dValue * (-1); // Turns robot 180 degrees

motor[motorE] = dValue;

wait1Msec(2500);

Chain and Sprocket Programming Guide (ROBOTC®)

Chain and SprocketTETRIX® Add-On Extensions

Page 3: Chain and Sprocket Programming Guide (ROBOTC®) TETRIX® Add ...

113

8. After the first object is detected and the robot has turned around, the robot looks for the second object in its path. This means that it must once again follow the line until the robot is within 30 cm of the second object.

Note: This loop contains the same code as the first line-following loop and can be copied from above.

while(SensorValue[SonarSensor] > ultraThres) // Until robot is within 30cm of second object...

{

if(SensorValue[LightSensor] < lightThres) // If senses black, turn left

{

motor[motorD] = tValue;

motor[motorE] = 0;

}

else // If senses white, turn right

{

motor[motorD] = 0;

motor[motorE] = tValue;

}

}

9. Stop the left and right motors

motor[motorD] = 0;

motor[motorE] = 0;

10. Add a closing brace to end the program.

}

Completed Code:

task main

{

int dValue = 15; // Forward drive motor power

int tValue = 30; // Turning motor power

int ultraThres = 30; // Ultrasonic Sensor threshold value

int lightThres = 50; // Light Sensor threshold value

while(SensorValue[LightSensor] > lightThres) // Stops when line is detected

{

motor[motorD] = dValue;

motor[motorE] = dValue;

}

Chain and Sprocket Programming Guide (ROBOTC®)

TETRIX® Add-On ExtensionsChain and Sprocket

Page 4: Chain and Sprocket Programming Guide (ROBOTC®) TETRIX® Add ...

114

Chain and Sprocket Programming Guide (ROBOTC®)

Completed Code (continued):

while(SensorValue[LightSensor] < lightThres) // The first turn of the robot, a failsafe if the robot approches the line at a sharp angle (90 degrees)

{

motor[motorD] = dValue;

motor[motorE] = dValue * (-1);

}

while(SensorValue[SonarSensor] > ultraThres) // Until robot is within 30cm of first object...

{

if(SensorValue[LightSensor] < lightThres) // If senses black, turn left

{

motor[motorD] = tValue;

motor[motorE] = 0;

}

else // If senses white, turn right

{

motor[motorD] = 0;

motor[motorE] = tValue;

}

}

motor[motorD] = 0; // Stops robot for two seconds

motor[motorE] = 0;

wait1Msec(2000);

motor[motorD] = dValue * (-1); // Turns robot around almost 180 degrees

motor[motorE] = dValue;

wait1Msec(2500);

while(SensorValue[SonarSensor] > ultraThres) // Until robot is within 30cm of second object...

{

if(SensorValue[LightSensor] < lightThres) // If senses black, turn left

{

motor[motorD] = tValue;

motor[motorE] = 0;

}

Chain and SprocketTETRIX® Add-On Extensions

Page 5: Chain and Sprocket Programming Guide (ROBOTC®) TETRIX® Add ...

115

Completed Code (continued):

else // If senses white, turn right

{

motor[motorD] = 0;

motor[motorE] = tValue;

}

}

motor[motorD] = 0; // End of program

motor[motorE] = 0;

}

Chain and Sprocket Programming Guide (ROBOTC®)

TETRIX® Add-On ExtensionsChain and Sprocket