File - DRSS Robotics

advertisement
RobotC For Beginners
Tyler Lutz and Keaton Bonds
DRSS Enterprise
Intro to the RobotC UI
•
•
•
•
•
•
Text based
Similar to C# or C++.
Capitalization matters (eg. lower case “task”)
When the program runs out of statements, the program ends.
Automatically colors words it recognizes (eg. task will appear as task)
Organize your code with comments so that you can understand what you coded later.
•
•
“//” Makes the rest of the line a comment
“/*” Starts a comment, that continues until you use “*/”
Hardware
• NXT brick
• Motor
• Servo
• Sensors
• Motor/Servo controllers
Pragma Configuration
• Tells the robot what motor controllers in what port.
#pragma config(Hubs, S1, HTMotor, none, none, none)
#pragma config(Sensor, S1, ,
sensorI2CMuxController)
#pragma config(Motor, mtr_S1_C1_1, mL,
tmotorTetrix, openLoop)
#pragma config(Motor, mtr_S1_C1_2, mR,
tmotorTetrix, openLoop, reversed)
Starting out
• To declare the start of your program, write:
task main()
{
//Code goes here.
}
• Recall that “//” indicates a comment. This is just for indication.
Starting out
•
This code is telling the motor named ‘motorLeft’ to move at 100% power.
motor[motorLeft] = 100;
•
To go backwards, set the power to a This code is telling the motor named ‘motorLeft’ to move at
100% power.
motor[motorLeft] = 100;
•
Waits for a given amount of time
wait1Msec(1000);
•
Place the statements inside the task main() structure.
task main(){
motor[motorLeft] = 100;
motor[motorRight] = 100;
wait1Msec(1000);
}
Don’t forget:
• Semicolons end a statement in RobotC. “;”
• Brackets vs. Parenthesis.
•
•
Brackets are used for differentiation between motors, servos, and sensors declared in
pragma configuration, for example motor[motorLeft];
Parenthesis are used to indicate conditions for a function, such as wait1Msec(3000);
Practice
• Try to make a program drives the robot forwards for 1 second, then
backwards for 4, then turns in any direction for 2 seconds.
Hint: One motor forward, one motor backwards to turn.
• task main(){
}
motor[motorLeft] = 100;
motor[motorRight] = 100;
wait1Msec(1000);
motor[motorLeft] = -100;
motor[motorRight] = -100;
wait1Msec(4000);
motor[motorLeft] = 100;
motor[motorRight] = -100;
wait1Msec(2000);
The while loop
•
•
•
A while loop is a code structure that allows code inside of the statement to run over
and over again as long as certain conditions remain true.
task main(){
while(true){
//Stuff to be repeated
}
}
The word condition above will always evaluate to true, so the loop will continue
until the program terminates.
Example
• This will repeatedly send the signal for motors at full power, forever.
• task main(){
while(true){
motor[motorLeft] = 100;
motor[motorRight] = 100;
}
}
Practice
• Make a program that drives forward for 3 seconds, backwards for 1 second,
and repeats this infinitely.
• task main(){
while(true){
motor[motorLeft] = 100;
motor[motorRight] = 100;
wait1Msec(3000);
motor[motorLeft] = -100;
motor[motorRight] = -100;
wait1Msec(1000);
}
}
•
•
•
•
•
•
•
•
•
The Integer (int)
Abbreviated ‘int’
First, the variable must be created.
int motorPower;
Next, you must set the variable to have a value. This can be combined with the first
step, or completed at any other time;
int motorPower = 84;
motorPower = 12;
Finally, to use the variable, replace wherever you would put an integer with the
variable’s name.
motor[motorRight] = motorPower;
variable= otherVariable;
The Boolean
•
•
•
•
Abbreviated ‘bool’
Can be true or false
Similar to int in creation and usage.
bool flag =true;
while(flag){
if(condition2){
flag = false;
}
if(condition3){
flag = false;
}
}
Working with #include
•
•
Used to allow access to methods and variables from other files.
Example:
…
pragma config(Servo, srvo_S1_C3_6,
servo6,
tServoNone)
#include “JoystickDriver.c”
task main(){
…
•
•
Note:
No semi-colon for this, just like no semi-colon for pragma configuration.
Please go ahead and add this to your code.
Basic Teleop
• Use this expression to force the joysticks to update:
getJoystickSettings(joystick);
• To use the readings of the joystick, try these expressions:
motor[mL] = joystick.joy1_y1;
motor[mR] = joystick.joy1_y2;
• Make sure your code compiles at this point, before we go into
communications management.
Bluetooth / USB Connections
• Please follow along as we connect our robot; we will answer any of your
questions.
Touch and Ultrasonic Sensors
• A touch sensor allows you to detect when something is pressing on the
touch sensor or not
• An Ultrasonic Sensor allows you to detect when something is in front of the
sensor or not.
Touch Sensor
•
SensorValue[touchSensor] is statement in RobotC that measures the values that a specific
sensor brings in or records. In this case it is for a touch sensor.
•
Put this into a while loop by simply putting while in front of SensorValue[touchSensor]
while (SensorValue[touchSensor]) == 0) then insert something that you want to happen
such as motors being powered or motors turning off. Example:
while(SensorValue[touchSensor]) == 0)
{
motor[motorA] = 100;
motor[motorB] = 100;
}
•
Now lets look at implementation.
Touch Sensor
task main()
{
while(SensorValue[touchSensor]) == 0) //a while loop is declared with the touchsensor's
value being 0 as it true condition
{
motor[motorA] = 100;
//motor A is run at a 100 power level
motor[motorB] = 100;
//motor B is run at a 100 power level
}
motor[motorA] = -75;
motor[motorB] = -75;
wait1Msec(1000);
further code
}
//motor A is run at a -75 power level
//motor B is run at a -75 power level
//the program waits 1000 milliseconds before running
Ultrasonic Sensor
task main()
{
do
//do instructs the computer to run the code in its braces and
after 1 iteration check the condition at the while statment that follows
{
motor[motorA] = 75;
//motor A is run at a 75 power level
motor[motorB] = 75;
//motor B is run at a 75 power level
}
while(SensorValue(sonarSensor) > 20); //after each iteration of the loop is
conducted, the truth condition, if the sonar sensor value is greater than 20, is
checked
}
Download