Presentation is loading. Please wait.

Presentation is loading. Please wait.

RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise.

Similar presentations


Presentation on theme: "RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise."— Presentation transcript:

1 RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise

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

3 Hardware NXT brick Motor Servo Sensors Motor/Servo controllers

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

5 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.

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

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

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

9 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.

10 Example This will repeatedly send the signal for motors at full power, forever. task main(){ while(true){ motor[motorLeft] = 100; motor[motorRight] = 100; } }

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

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

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

14 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.

15 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.

16 Bluetooth / USB Connections Please follow along as we connect our robot; we will answer any of your questions.

17 Logic statements 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. A for loop runs only a specified amount. First, you create a variable (usually "i", nut can pretty much be anything you want), commonly an int. Then, inside the loop, you specify the amount the variable starts at (this is usually 0, but could, again, be different, since you might want to use the variable inside your loop); then, you specify up to what variable amount you want the loop to run (which would be the amount of loops -1, if your variable starts off at 0); finally, you tell the program how much the variable needs to be increased each loop (this is usually 1).

18 task main() { int i = 0; //the variable "i" is declared as an integer, and initialized to equal zero while(i < 20) //a while loop is delcared with the variable "i" being less than 20 as its true condition { 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(4000); //the program waits 4000 milliseconds before running further code 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(750); //the program waits 750 milliseconds before running further code i++; //the variable "i" is incremented (increased) by 1 }

19 task main() { int i; //the variable "i" is declared as an integer for(i = 0; i< 20; i++) //a for loop is declared that: intializes i equal to zero, continues to run under the condition i is less than 20, and increments i by one after each iteration { 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(4000); //the program waits 4000 milliseconds before running further code 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(750); //the program waits 750 milliseconds before running further code }

20 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.

21 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.

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

23 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 ppt "RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise."

Similar presentations


Ads by Google