robotc for beginners tyler lutz and keaton bonds drss enterprise

20
RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise

Upload: juniper-thompson

Post on 23-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise

RobotC For BeginnersTyler Lutz and Keaton Bonds

DRSS Enterprise

Page 2: 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 “*/”

Page 3: RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise

Hardware

• NXT brick

• Motor

• Servo

• Sensors

• Motor/Servo controllers

Page 4: RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise

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)

Page 5: RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise

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.

Page 6: RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise

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);

}

Page 7: RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise

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);

Page 8: RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise

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);

}

Page 9: RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise

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.

Page 10: RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise

Example

• This will repeatedly send the signal for motors at full power, forever.

• task main(){while(true){

motor[motorLeft] = 100;motor[motorRight] = 100;

}}

Page 11: RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise

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);

}}

Page 12: RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise

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;

Page 13: RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise

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;}

}

Page 14: RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise

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.

Page 15: RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise

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.

Page 16: RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise

Bluetooth / USB Connections

• Please follow along as we connect our robot; we will answer any of your questions.

Page 17: RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise

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.

Page 18: RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise

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.

Page 19: RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise

Touch Sensortask 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 A is run at a -75 power level

motor[motorB] = -75; //motor B is run at a -75 power level

wait1Msec(1000); //the program waits 1000 milliseconds before running further code

}

Page 20: RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise

Ultrasonic Sensortask 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

}