Introduction to Programming in RobotC 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 Connection 1 Motor – LEFT 2 Motor – RIGHT 3 Bumper Switch 4 Touch LED 5 Color Sensor 6 Distance Sensor 7 Gyro Set-up
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
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
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
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
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
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
Programming Lets change our text to display a button being pushed FIRST – Save the program with a new name Programming
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
Buttons to the BRAIN Test this code So, is the mathematical value of the buttons? Buttons to the BRAIN
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
Buttons to the BRAIN Try the same thing with the joysticks So, are the mathematical values of the joysticks? Buttons to the BRAIN
Conditionals Conditionals only do something when the condition is met. Conditionals include If While Conditionals must be followed by fancy brackets Conditionals
Button turn on light 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
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
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
Sensors What about sensors? What values do they send? The bumper switch is very much like the buttons on the controller Sensors
Bumper Switch
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
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
Arcade Drive 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 sonarDist; Get variable sonarDist = getDistanceValue(SonarSensor); Programming
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
Sonar Sensor
How could this be used to affect the drive motors on the robot? Sonar
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
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
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 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
Latch
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
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
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
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
Can we display this color number as a color? Color Sensor
Try This!!