Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming in RobotC

Similar presentations


Presentation on theme: "Introduction to Programming in RobotC"— Presentation transcript:

1 Introduction to Programming in RobotC
Vex IQ

2 Build a simple structure to support each of the brain, two motors and each sensor as listed to the right. Connect them as listed Port Connection 1 Motor – LEFT 2 Motor – RIGHT 3 Bumper Switch 4 Touch LED 5 Color Sensor 6 Distance Sensor 7 Gyro Set-up

3 Programming Each student will work alone
When a student thinks they have a program written to complete one of the tasks, one of the instructors will come around with a robot practice station for you to download your program and allow the instructor to check that your program works as expected. Programming

4 Programming Everyone should complete task #3 first.
Display your name on the first line of the display and put a fun quote on the second line of the display Programming

5 Programming Basics Open RobotC – NOT the graphical program
Set up motors and sensors These are listed on page 3 of your packet Give each sensor or motor a unique name Make sure to “apply” the name given Programming

6 Programming Programming Basics
Almost all lines of code must end with a semi colon Most commands you need can be dragged onto the program from the “text functions” panel on the left All parenthesis must open and close Conditional commands use fancy brackets Inside the fancy brackets, many individual commands can be included Programming

7 Programming Task #3, simple
Put the following inside the fancy brackets for task main displayTextLine(1,”your name here”); displayTextLine(2,”Hello World”); Everyone test that this works Programming

8 The basics of a complete program
Programming

9 It is nice to have words in your code that tells yourself and others what the code is doing.
At the end of a line add // then everything on that line after the // will not be read as code, but is a comment Longer comments are contained between the following /* comment goes here */ Comments

10 Programming Lets change our text to display a button being pushed
FIRST – Save the program with a new name Programming

11 Programming To get the results of a button use the following
vexRT[BtnEUp] Replace BtnEUp with other buttons based on the drop down list that appears Programming

12 Buttons to the BRAIN Test this code
So, is the mathematical value of the buttons? Buttons to the BRAIN

13 Buttons What results do the brain see when a button is changed?
Buttons send a zero when not pushed and a one when pushed Buttons

14 Buttons to the BRAIN Try the same thing with the joysticks
So, are the mathematical values of the joysticks? Buttons to the BRAIN

15 Conditionals Conditionals only do something when the condition is met.
Conditionals include If While Conditionals must be followed by fancy brackets Conditionals

16 Button turn on light The if statement is followed by brackets
Exactly what do you expect to happen Button turn on light

17 How could you change the code so the light goes out when the button is not pushed?
Button turn on light

18 Try this Button turn on light

19 We can change things with variables
Just like math class, we can assign a letter (or a whole word) to represent a value Variables

20 Programming To do more, we need to add some math Variables
Variables allow us to remember a state, however the brain needs information on what kind of variable you may use Programming

21 What kind of information might you want to store in a variable?
String = for storing text Int = for storing integer numbers Float = for storing numbers with decimals Bool = for storing a state, true or false Char = stores a single character Programming

22 Programming

23 Try this Counting

24 Try this Variables

25 Sensors What about sensors? What values do they send?
The bumper switch is very much like the buttons on the controller Sensors

26 Bumper Switch

27 Programming How can I make the motors move? motor[Left]=vexRT[ChA];
“Left” must match the name applied in the pragma Add to your program so both motors spin with joysticks. Programming

28 Let’s work toward programming something to make a robot move
Can you display the results of the joysticks on the screen of your brain? Programming

29 Tank Drive Controls

30 Arcade Drive Tank drive is easiest to write.
Most drivers prefer arcade drive. What does it take to turn and drive forward? Arcade Drive

31 Arcade 2 Controls

32 Your turn. Make both motors move forward or backwards with the “a” joystick Then Make the “b” joystick turn the robot Programming

33 Arcade 1 Controls

34 Distance sensor Save value to a variable int sonarDist; Get variable sonarDist = getDistanceValue(SonarSensor); Programming

35 task main() { int sonarDist; // Initialize a variable for the sonar while(true) sonarDist = getDistanceValue(SonarSensor); displayTextLine(1,"%d",sonarDist); // Display the sonar value } // end while true } // end task main Sonar Sensor

36 Sonar Sensor

37 How could this be used to affect the drive motors on the robot?
Sonar

38 Sonar How could this be used to affect the drive motors on the robot?
Drive slower when closer to an object Drive faster when farther away Sonar

39 Because the sonar values are generally large, I divided to make them smaller

40 task main() { int sonarDist; // Initialize a variable for the sonar while(true) sonarDist = getDistanceValue(SonarSensor); sonarDist = sonarDist / 5; // scale the speed to move slower if (sonarDist > 100) sonarDist = 100; // if speed is over 100, reset motor[Left] = sonarDist; // set motor value displayTextLine(1,"%d",sonarDist); // Display the sonar value } // end while true } // end task main Sonar

41 Sonar

42 More interesting things happen when you can record and save a “state” for your robot.
The following with two variables creates a “latch” that keeps the robot in the same state until the same button is pushed again. Programming

43 task main() { bool buttonToggleState = false;// possible states are true or false bool buttonPressed = false; while(true) // check for a button press only if we are not already pressed. if( vexRT[ BtnFUp ] == 1 ) if( ! buttonPressed ) buttonToggleState = !buttonToggleState;// change the toggle state buttonPressed = true;// Note the button is pressed } else buttonPressed = false;// the button is not pressed // Now do something with our toggle flag if( buttonToggleState ) displayTextLine(4,"State is On"); // Display the state setTouchLEDColor(Touch, colorRed);// Turn LED on and Red displayTextLine(4,"State is Off"); // Display the state setTouchLEDColor(Touch, colorNone); // Turn LED light off }// while-true loop }// task main Latch

44 Latch

45 Programming Challenge
If the bumper switch is mounted on the bumper, when the robot hits a wall, can you program it to switch to reverse? Bump it again, and switch to forward Programming Challenge

46 Bumper Latch task main() {
bool buttonToggleState = false;// possible states are true or false bool buttonPressed = false; while(true) // check for a button press only if we are not already pressed. if(getBumperValue(Bump) == 1 ) if( ! buttonPressed ) buttonToggleState = !buttonToggleState;// change the toggle state buttonPressed = true;// Note the button is pressed } }else{ buttonPressed = false;// the button is not pressed // Now do something with our toggle flag if( buttonToggleState ) motor[Left] = 70; // Drive Forward motor[Right] = 70; // Drive Forward motor[Left] = -70; // Drive Backwards motor[Right] = -70; // Drive Backwards }// while-true loop }// task main Bumper Latch

47 Bumper Latch

48 Your turn Combine your earlier program of a “Latch” to turn the right motor on with a button Then turn it off again with the same button Programming

49 Color Sensor So you have a color sensor connected to port 5.
What values does this provide to your brain? Can you write a program to figure it out? Color Sensor

50 Can we display this color number as a color?
Color Sensor

51 Try This!!


Download ppt "Introduction to Programming in RobotC"

Similar presentations


Ads by Google