Introduction to Programming in Robot C Vex IQ
Introduction to Programming in Robot. C Vex IQ
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 1 2 3 4 5 6 7 Set-up Connection Motor – LEFT Motor – RIGHT Bumper Switch Touch LED Color Sensor Distance Sensor Gyro
• 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
• 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
• Basics 1. Open Robot. C – NOT the graphical program 2. Set up motors and sensors 1. These are listed on page 3 of your packet 3. Give each sensor or motor a unique name 4. Make sure to “apply” the name given 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
• Task #3, simple • Put the following inside the fancy brackets for task main display. Text. Line(1, ”your name here”); display. Text. Line(2, ”Hello World”); Everyone test that this works Programming
The basics of a complete program Programming
• 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
• Lets change our text to display a button being pushed • FIRST – Save the program with a new name Programming
• To get the results of a button use the following vex. RT[Btn. EUp] Replace Btn. EUp with other buttons based on the drop down list that appears Programming
Test this code So, is the mathematical value of the buttons? Buttons to the BRAIN
• What results do the brain see when a button is changed? • Buttons send a zero when not pushed and a one when pushed Buttons
Try the same thing with the joysticks So, are the mathematical values of the joysticks? Buttons to the BRAIN
• Conditionals only do something when the condition is met. • Conditionals include If While • Conditionals must be followed by fancy brackets Conditionals
The if statement is followed by brackets Exactly what do you expect to happen Button turn on light
How could you change the code so the light goes out when the button is not pushed? Button turn on light
Try this Button turn on light
We can change things with variables Just like math class, we can assign a letter (or a whole word) to represent a value Variables
• To do more, we need to add some math • Variables allow us to remember a state, however the brain needs information on what kind of variable you may use Programming
• 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
Programming
Try this Counting
Try this Variables
• What about sensors? What values do they send? • The bumper switch is very much like the buttons on the controller Sensors
Bumper Switch
• How can I make the motors move? • motor[Left]=vex. RT[Ch. A]; “Left” must match the name applied in the pragma Add to your program so both motors spin with joysticks. Programming
• 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
Tank Drive Controls
• Tank drive is easiest to write. • Most drivers prefer arcade drive. • What does it take to turn and drive forward? Arcade Drive
Arcade 2 Controls
• Your turn. • Make both motors move forward or backwards with the “a” joystick Then • Make the “b” joystick turn the robot Programming
Arcade 1 Controls
Distance sensor Save value to a variable int sonar. Dist; Get variable sonar. Dist = get. Distance. Value(Sonar. Sensor); Programming
task main() { int sonar. Dist; // Initialize a variable for the sonar while(true) { sonar. Dist = get. Distance. Value(Sonar. Sensor); display. Text. Line(1, "%d", sonar. Dist); // Display the sonar value } // end while true } // end task main Sonar Sensor
Sonar Sensor
• How could this be used to affect the drive motors on the robot? 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
• Because the sonar values are generally large, I divided to make them smaller Sonar
task main() { int sonar. Dist; // Initialize a variable for the sonar while(true) { sonar. Dist = get. Distance. Value(Sonar. Sensor); sonar. Dist = sonar. Dist / 5; // scale the speed to move slower if (sonar. Dist > 100) sonar. Dist = 100; // if speed is over 100, reset motor[Left] = sonar. Dist; // set motor value display. Text. Line(1, "%d", sonar. Dist); // Display the sonar value } // end while true } // end task main Sonar
Sonar
• 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
• • • • • • • • • task main() { bool button. Toggle. State = false; // possible states are true or false bool button. Pressed = false; while(true) { // check for a button press only if we are not already pressed. if( vex. RT[ Btn. FUp ] == 1 ) { if( ! button. Pressed ) { button. Toggle. State = !button. Toggle. State; // change the toggle state button. Pressed = true; // Note the button is pressed } } else { button. Pressed = false; // the button is not pressed } // Now do something with our toggle flag if( button. Toggle. State ) { display. Text. Line(4, "State is On"); // Display the state set. Touch. LEDColor(Touch, color. Red); // Turn LED on and Red } else { display. Text. Line(4, "State is Off"); // Display the state set. Touch. LEDColor(Touch, color. None); // Turn LED light off } }// while-true loop }// task main Latch
Latch
• 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
• • • • • • • task main() { bool button. Toggle. State = false; // possible states are true or false bool button. Pressed = false; while(true) { // check for a button press only if we are not already pressed. if(get. Bumper. Value(Bump) == 1 ) { if( ! button. Pressed ) { button. Toggle. State = !button. Toggle. State; // change the toggle state button. Pressed = true; // Note the button is pressed } }else{ button. Pressed = false; // the button is not pressed } // Now do something with our toggle flag if( button. Toggle. State ) { motor[Left] = 70; // Drive Forward motor[Right] = 70; // Drive Forward }else{ motor[Left] = -70; // Drive Backwards motor[Right] = -70; // Drive Backwards } }// while-true loop }// task main Bumper Latch
Bumper Latch
• 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
• 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
Can we display this color number as a color? Color Sensor
Try This!!
- Slides: 51