VEX/ROBOTC Session 1 Agenda • Introductions to VEX and ROBOTC. • Basic Programming– Inputs and Outputs – Basic Outputs Programming – Basic Input Programming • Basic Programming – While and If statements – Variable and Functions • Basic Programming – Open and Closed Loop Systems Getting Started • There are three main sections to the ROBOTC Integrated Development Environment (IDE): 1. The Editor 2. The Code Templates / Function Library 3. The Main Menu / Toolbar The Editor • This is the part of the interface where the user can write code. The Code Templates / Function Library • This portion of the interface allows user to see all the functions available in ROBOTC at their user level. • Portions of code from the "Function Library" can be dragged into the Editor. The Main Menu / Toolbar • This area allows you to perform basic tasks (saving, copy & pasting, undo, etc.), switch between multiple open programs using the tabbed interface, and access all of ROBOTC's additional functionality through the various menus. Parts are more than just parts THE POE TEST BED The POE Test Bed VEX Components • The Cortex – STMicroelectronics ARM CortexM3 user processor – Wireless with built-in VEXnet technology – (8) Standard 3-wire Motor ports – (2) 2-wire Motor ports – (1) I2C "smart sensor" port – (2) UART Serial Ports – (8) Hi-res (12-bit) Analog Inputs – (12) Fast digital I/O ports which can be used as interrupts The Bump Switch • The Bumper switch works as a trigger that provides feedback to the Microcontroller as events "happen". • Rugged bumpers allows triggering by large impacts. • Useful as a trigger in a variety of applications. • Functionality is preprogrammed in the VEX Microcontroller. The Limit Switch • These switches are great for signaling when a robot arm has reached the top or bottom of its motion. • Bendable sheet metal switch "arm" is very versatile • Useful as a trigger in a variety of applications • Functionality is preprogrammed in the VEX Microcontroller The Line Tracker • Program your robot to follow a black line over a white surface. • Navigate down a marked path. • Use for light sensing. • More autonomous function. The Potentiometer • With the Potentiometer you can determine both position and direction of rotation. • Measure Angular Position. • 250-Degrees of Adjustment. • Adjustable mounting angle. • The Optical Shaft Encoder • With the Quadrature Encoder's two output channels you can measure both the position and direction of rotation of a VEX shaft. • Calculate Shaft Speed. • Calculate Distance Traveled. • Increase Navigational Control. Ultrasonic Range Finder • Avoid obstacles and measure distances with the ultrasonic range finder. Device emits a high-frequency sound wave that alerts the robot to things in its path. The Light Sensor • The light sensor uses a photocell that allows your robot to detect and react to light. • Analog input of light levels. • Usable range of 0 to 6 feet. • Find dark or bright areas. The Two Wire Motor • The motor connects directly to 2-Wire Motor Ports (Ports 1 & 10 on the Cortex Microcontroller). • For 3-Wire Motor Ports (Ports 2-9 on the Cortex), a Motor Controller must be used in between the Microcontroller and the motor. Three Wire Servo Motor • Servo motors are a type of motor that can be directed to turn to face a speciļ¬c direction, rather than just spin forward or backward. • Expand functionality • Add pan and tilt to a camera • 100 degrees of rotation • Do exact positioning Other Parts Green LED Flashlight Motor Controller Battery Getting Started… OPENING THE PLTW TEMPLATE Open the PLTW Template Notice the File Path – If you are not in the VEX2 Folder take this time to find it. Select the PLTWtemplate The PLTW Template • Once the Template is Opened – Have the students SAVE AS the name of the assignment that they are working on. • For this example save as A3_1_1 Setting the Platform and Communications method: • Select View • Preferences • Detailed Preference… ROBOTC Preferences • Select the Platform Type so that it shows Natural Language (VEX Cortex) • Leave the Communication as Automatic Selection NOTE – Get your students in the habit of checking this every time they start a new project Put your Name on your Assignments • • • • Project Title: INPUTS AND OUTPUTS 3.1.1 Team Members: ROY ROSNIK Date: SEPT 1 2012 Section: 1 Setting up the Motors and Sensors • For this Project ROBOTC has pre-programmed some Standard Model Configurations Motor and Sensors Setup • Select the Motors Tab • Notice that each item has a name • When naming the motor – Use a descriptive name • Good time to check to see if the motor is actually in the correct port on the VEX Cortex Motor and Sensors Setup • VEX 2.0 Analog Sensors • Naming the sensor – Use descriptive names • Like before, have the students check the wiring Motor and Sensors Setup • VEX 2.0 Digital Sensors • Naming the sensor – Use descriptive names • Like before, have the students check the wiring Time to Compile and Download… • Select Robot – Compile and Download Program Three most commons Messages The VEX Cortex is not plugged into the computer Power is turned off or the battery needs to be replaced The wrong Compiler Target is selected Program Debug • This is used to check to see if all of the Inputs, Outputs and motors are working correctly. • Make sure that the Refresh is set to Continuous Debugger Windows • From the Menu – Robot – Debugger Windows – Motors and Sensors The Debugger Window • • • • Bottom of your screen Tabs for each item Errors – gives you an idea what is wrong with the code Motors – lets you test each motor – Power can be manually changed from -127 to 127 – Try different values for each item • Sensors – – – – Both Analog and Digital are displayed Items in red will automatically change their value Items in Black will change to red when input is given Try each Sensor Check your Understanding • How are motors and sensors used. • Describe a scenario where a motor or sensor might not respond as you would predict. • What is the purpose of the ROBOTC Editor Window? • What is the purpose of the ROBOTC Debug window? • What it the purpose of the FUNCTION LIBRARY? • What is the function of the Menu Bar? Basic Outputs Programming – VEX • Close all previously opened Programs • Open a new copy of the PLTW Template – SAVE AS A3_1_2_PART1 • Edit – – – – – – Project Title: Team Members: Date: Section: Task Description: Set up the Motors and Sensors Set the Motors so that only Ports 2,3 and 9 are used. Set Digital Sensors so that only digl 12 is being used The Color of the Code • Normal Text is displayed as black text. – Normal text is used for variable and function names that are userdefined and not recognized by the compiler as reserved words. • Comments are displayed as green text . – Comments are text in a program prefaced with a// or surrounded by a /* * / . – This "commented text" is not considered code and is ignored when the compiler generates byte-code to send to the robot controller. • Reserved Words/Pre-Defined Functions (int, motor) are displayed as blue text . – These are words that are 'reserved' by ROBOTC and have special meaning; because of this, they cannot be used as names for variables, functions, etc. • Constants and Parameters (50, motor1) are displayed as dark red text . • Operators (+, -, *, {, <, [, etc.) are displayed as light red text . Comments •Project title, Team Members, Date and Section •Task Description – Overview of the program •Pseudocode – Detailed step-by-step account of the program. •All Comments are in GREEN #pragma – not a dirty word • When you set up the motors and sensors, you gave each one a port and name. • RobotC gave it more information • When entering commands, the programmer will indicate what is to operate by port or the name you gave it. The Actual Code task main() { turnLEDOff(green); wait (1); turnLEDOn(green); wait(1); turnLEDOff (green); wait (1); turnLEDOn (green); wait (1); turnLEDOff (green); } The Program starts with task main() Program begins within the curly braces must have one at the start and end. All commands end with a semi-colon Special command to turn on or off the LED Once finished typing in the code – Compile and Download the program green vs. dgtl12 task main() { turnLEDOff(dgtl12); wait (1); turnLEDOn(dgtl12); wait(1); turnLEDOff (dgtl12); wait (1); turnLEDOn (dgtl12); wait (1); turnLEDOff (dgtl12); } •Notice that the word green has been replaced by dglt12 •Either word will work •Give it try – replace green with dgtl12 •Compile and download Check for Understanding • In the ROBOTC Editor Window the Comments are displayed as what color? • In the ROBOTC Editor Window the Reserved Words are displayed as what color? • In the ROBOTC Editor Window the Constants and Parameters are displayed as what color? • The Motor and Sensor Setup is one of the central points of ROBOTC. What is it used to do? Try it on your own • Save A3_1_2_PART1 • SAVE AS A3_1_2_PART2 – This will keep all the Motor and Sensor Setting – Delete the Task Description, Pseudocode and the code between the curly braces. – Save A3_1_2_PART2 You can drag and drop these commands in between the curly braces Drag and Drop • You can drag and drop the commands from the Function Library in between the curly braces. • Notice the text in black • These are user defined variables or functions Before changing the black text After changing the black text – noticed it turned dark red Edit your program • Turn on leftMotor at the same time that rightMotor is turned on. • Test the program and troubleshoot if needed until the expected behavior has occurred. •Spelling does matter. •In this case, line 31 leftmotor should have been spelled leftMotor •Do not let your students get away with this. Modify your Program even more • Reverse both motors using two different programming methods. One option is change the setting Second option is to change the value to negative A3_1_2_Part3 • Save A3_1_2_Part2 • Save As A3_1_2_Part3 – Delete the Task Description, Pseudocode and code between the curly braces. A3_1_2_Part3 • Write a program that performs the following simple behaviors. • Add comments at the end of each command line to explain the purpose of each step. 1. Turn the rightMotor on forward at half speed for 5 seconds, then stop. 2. Turn the leftMotor on in reverse at three-fourths speed for 2.5 seconds, then stop. 3. Turn both motors on at full power, and spinning in the same direction, for 7.25 seconds, then stop. A3_1_2_Part4 • Save A3_1_2_Part3 • Save As A3_1_2_Part4 – Delete the Task Description, Pseudocode and code between the curly braces. A3_1_2_Part4 • Between the curly braces type the following: 1. setServo(servoMotor, -127); 2. wait(2); 3. setServo(servoMotor, 0); 4. wait(2); 5. setServo(servoMotor, 127); 6. wait(2); Compile and Download Program Modify A3_1_2_Part4 Program for the Servo so that the following happens: 1. Go to position -127 for 2 seconds 2. Go to position -63 for 3 seconds 3. Go to position 0 for 2 seconds 4. Go position 63 for 3 seconds 5. Go to position 127 for 2 seconds. Check for Understanding 1. Explain any challenges that you encountered while developing the program. 2. Explain how these outputs might be used in an application. 3. Writing Code is the primary focus of ROBOTC. What environment is the code written in? 4. What portion of the interface allows user to see all the functions available in ROBOTC at their user level? Basic Inputs Programming – VEX • Close all previously opened Programs • Open a new copy of the PLTW Template – SAVE AS A3_1_3_Part1 • Edit – – – – – – Project Title: Team Members: Date: Section: Task Description: Motor and Sensor Set-Up • Select the POE + CIM Testbed for this activity Enter the following…. • • • • untilBump(bumpSwitch); startMotor(rightMotor, 67); wait(5); stopMotor(rightMotor); 1. Compile and Download the program. 2. What happened? untilBump(sensorPort, delayTimeMS); • untilBump (bumpSwitch); • In this example we did not give any delay time. • The nothing happens until the touch sensor is pressed in and then released out. • If we gave it a delay time, nothing would happen until the touch sensor is pressed in and then released out and the amount of delay time is measured. A3_1_3_Part2 • Save A3_1_3_Part1 • Save As A3_1_3_Part2 – Delete the Task Description, Pseudocode and code between the curly braces. • We will look at the Bump Switch A3_1_3_Part2 • Write a program that performs the following simple behaviors. • Add comments at the end of each command line to explain the purpose of each step. 1. Wait for the bumper switch to be bumped. Note that bump means that a switch is pressed and released and not simply pressed and held. 2. Both motors turn on at half power until the sensor is bumped again. 3. Both motors should then move in reverse at half power for 3.5 seconds. 4. Both motors will stop. Variable and Function Names • It does not matter if you use the name of the item or the port number. • BUT DO NOT MIX IT UP • It is just a bad practice. Good or Bad? Look at these two samples of Code – They both work the same, but one is Good and one is Bad – Can you tell? Why? A3_1_3_Part3 • Save A3_1_3_Part2 • Save As A3_1_3_Part3 – Delete the Task Description, Pseudocode and code between the curly braces. • We will look at the Potentiometer. A3_1_3_Part3 • Between the curly braces enter the following: 1. turnLEDOn(green); 2. untilPotentiometerGreaterThan(2048, potentiometer); 3. turnLEDOff(green); 4. startMotor(leftMotor, 63); 5. wait(3.5); 6. stopMotor(leftMotor); Before you Start • • • • Compile and Download the program Before you start this program – Select the Sensors tab Turn the potentiometer so that the value reads 0 A3_1_3_Part4 • Save A3_1_3_Part3 • Save As A3_1_3_Part4 – Delete the Task Description, Pseudocode and code between the curly braces. • We will look at the Optical Encoder A3_1_3_Part4 • Between the curly braces enter the following: 1. 2. 3. 4. 5. startMotor(leftMotor, 63); startMotor(rightMotor, 63); untilEncoderCounts(480,quad); stopMotor(leftMotor); stopMotor(rightMotor); Before you Start • • • • Compile and Download the program Before you start this program – Select the Sensors tab Make sure that the Optical Encoder (quad) has a value other than 480 A3_1_3_Part5 • Save A3_1_3_Part4 • Save As A3_1_3_Part5 – Delete the Task Description, Pseudocode and code between the curly braces. • We will look at the Line Follower A3_1_3_Part5 • Before we begin to program for the Line Follower, we must figure out its “Threshold” 1. 2. 3. 4. Place a white object about a 1/4” away from the Line Follower From the Program Debug window click on Snapshot under the Refresh option – Record this value Do the same thing, this time with a dark object. Add the two values and divide by 2 A3_1_3_Part5 • Between the curly braces enter the following: • setServo(servoMotor, 127); • untilLight(793, lineFollower); • setServo(servoMotor, -127); NOTE – the value of 793 is the Threshold Value for my Line Follower – Yours may be different. A3_1_3_Part6 • Save A3_1_3_Part5 • Save As A3_1_3_Part6 – Delete the Task Description, Pseudocode and code between the curly braces. • We will look at the Ultrasonic Range Finder A3_1_3_Part6 • Between the curly braces enter the following: • • • • • • • • startMotor(leftMotor, 63); startMotor(rightMotor, 63); untilSonarLessThan(20, sonar); stopMotor(leftMotor); stopMotor(rightMotor); turnLEDOn(green); wait(6.25); turnLEDOff(green); Compile and Download the Program. What happens when you run it? A3_1_3_Part6 • Modify your program to perform the following: – Wait until an object is detected within 20 cm to turn both motors on. – Wait for the object to move more than 25 cm away before turning the motors off. Check for Understanding The picture above shows a bump switch along with a snippet of code. Explain what will happen. Check for Understanding The picture above shows a potentiometer along with a snippet of code. Explain what will happen. Check for Understanding The picture above shows an Optical Encoder along with a snippet of code. Explain what will happen. Check for Understanding The picture above shows a Line Follower along with a snippet of code. Explain what will happen. Check for Understanding The picture above shows an Ultra Sonic sensor along with a snippet of code. Explain what will happen. Break for Lunch While and If-Else Loop Systems – VEX • While loop is a structure within ROBOTC • Allows a section of code to be repeated as long as a certain condition remains true • Three main parts to every while loop 1. The Word While 2. The Condition 3. Body - Commands To Be Repeated The Condition • Condition controls how long or how many times a while loop repeats – When condition is true, the while loop repeats – When condition is false, the while loop ends and the remainder of the program executes The Condition can be expressed by a Boolean Logic or by a value of a sensor The Condition •The sample of code has the condition of 1==1. •The Flashlight would turn on and off depending on the value of the light sensor. •Loops forever •This sample of code has a condition set for the Ultrasonic sensor •The motors run until the sonar has a value of 20. •Since the condition of the while loop has been met – the program goes on to the next step. Lets Give This A Try • Close all programs • Open a new PLTW Template • Save As A3_1_4_Part1 A3_1_4_Part1 • From the Function Library, Drag and drop the while (condition\{\body\}/ between the curly braces. A3_1_4_Part1 Notice when you dragged the command between the curly braces, it added two more curly braces – one before and after the body The While Command with Boolean Logic • Edit the condition to the following: • while(1 == 1) • Edit the Body to the following: • { • turnFlashlightOn(flashlight, 127); • untilDark(450, lightSensor); • turnFlashlightOff(flashlight); • untilLight(450, lightSensor); • } Compile and Download Program A3_1_4_Part2 Boolean Logic Table The While Command with a timed condition • Between the curly braces, input the following: • • • • • • • • ClearTimer(T1); while (time1[T1] < 20000) { turnLEDOn(green); wait(2); turnLEDOff(green); wait(2); } Compile and Download Program A3_1_4_Part3 If Statements • If statement in the program is evaluated by condition contained in parenthesis – If condition is true, commands between braces are run – If condition is false, those commands are ignored • Very similar to how a while loop works, but does not repeat the code The If Statement cont… • Once the If statement has been satisfied – the program continues • In this example the LED will turn on and off depending If the bump switch is pressed or not. Lets give it a try.. • Between the curly braces, input the following: 1. while (1 ==1) //Loop program indefinitely 2. { 3. if (SensorValue(bumpSwitch) == 1) 4. { 5. turnLEDOn(green); 6. } 7. if (SensorValue(bumpSwitch) == 0) 8. { 9. turnLEDOff(green); BE SURE TO 10. } 11. } INCLUDE ALL THE Compile and Download Program Save AS A3_1_4_Part4 CURLY BRACES If Else Statements • If-else statement is an expansion of if statement – If checks condition and runs appropriate commands when it evaluates to true – Else allows code to run when condition is false – Either if or else branch is always run once Lets Modify A3_1_4_Part4 • • • • • • • • if (condition) { body; } else { body; } Same Results •The two samples of code above result in the same behavior. •When the bump switch is pressed, the LED turns on and off. Check for Understanding • What are the three main parts to every while loop? • In ROBOTC, statements that can be only true or false are called? • Describe any challenges that you encountered while developing the program. • Describe one application each for While and IfElse loops. Variables and Functions – VEX • A variable is a space in your robot’s memory where you can store data, such as whole numbers, decimal numbers, and words. • Functions group together several lines of code, which can then be referenced many times in task main, or even other functions. • The use of variables and functions allows for complex control of a system. Different Variables Data Type Description Example Code Integer Positive and negative whole numbers, as well as zero -35, -1, 0, 33, 100 int Floating Point Number Numeric values with decimal points (even -.123, 0.56, if the decimal part is zero) 3.0, 1000.07 float Boolean True or false – Useful for expressing the outcomes of comparisons true, false bool Character Individual characters, placed in single quotes ‘L’, ‘f’, ‘8’ char String Strings of characters, such as words and sentences placed in double quotes “Hello World!”, “asdf” string Variables and Functions – VEX • Set up the Motors and Sensors to the following: • Save As A3_1_5_Part1 Creating a Variable To create a variable, you must give it a: 1. Type: Data type it will hold 2. Name: How variable can be referenced Variables can be set to different values throughout program Giving a variable an initial value is called “initializing the variable” Lets Give this a Try • Between the curly braces input the following: • int motorcount; • motorcount = 0; • while (motorcount < 3) • { • startMotor(rightMotor, 95); • wait(2); • stopMotor(rightMotor); • wait(2); • motorcount = motorcount + 1; • } A3_1_5_Part2 • Save A3_1_5_Part1 • Save As A3_1_5_Part2 – Delete the Task Description, Pseudocode and code between the curly braces. • Create a count-based while loop that turns on the LED for 3 seconds, then off for 3 seconds, 5 times A3_1_5_Part2 • In this example the int was named LED • This is what we wanted to count – On – Off • You might have named it differently Global or Local Variables • Variables can be considered either “global” or “local”. – Global variable • Can be read or changed from any task or function in your code. • Its value can be seen/read globally. – Local variable • belongs to the task or function in which it was created • Value can only be read or changed from within that task or function • Value can only be seen/read locally • Generally the type of variable you’ll want to use Local Variables (preferred for beginers) • To create a local variable, declare it within the curly braces of task main or one of your functions. • You will only be able to change the value of this variable within its task or function. Global Function • To create a global variable, declare it after your #pragma statements, but before task main or any function declarations. • This will allow your variable to be changed by any task or function in your program. Functions • Functions – Group together several lines of code – Referenced many times in task main or in other functions • Creating Functions Example: LED on if bumper is pressed, off if released 1. Function header (name of function) 2. Function definition (code in the function) 3. Function call (where function code will run) Function Declarations • Function declarations declare that a function exists and indicates its name • Function declarations between #pragma statements and task main • Function declaration optional if function definition is above task main The Function Defintion • Function definitions define the code that belongs to the function Calling the Function • Function calls – Call and run code from function – Placed in task main or other functions • • • • • • • • • • • void LEDControl() { if(SensorValue[bumpSwitch] == 1) { turnLEDOn(green); } Else { turnLEDOff(green); } } • • • • • • • task main() { while (1==1) { LEDControl(); } } Lets Give it a Try… •Save A3_1_5_Part3 •Save As A3_1_5_Part3 •Delete the Task Description, Pseudocode and code between the curly braces. •Create a Function called LEDControl. •Define the function so that the LED turns on and off with an If Else statement. •In the task main, create a while loop that will loop forever using the Function LEDControl Check for Understanding • Where can a user see all of the values of the sensors, timers, and global variables? Examine the sample program to the left. What is the ‘LED’ In lines 24, 25, 26, and 32? Check for Understanding • Examine the code to the right. How many times will the LED turn on and off? • How do you know this? Open and Closed Loop Systems – VEX • If you set the clothes dryer to run for 45 minutes, your clothes might be dry or they might not be dry. • A clothes dryer is an open loop system because the process provides no feedback to the device. • Newer clothes dryers possess moisture sensors. – The moisture sensors inform the machine when the clothes are dry, at which point the dryer can stop running. • The feedback provided by the sensor makes this a closed loop system. Reconfigure the Testbed 1. Take the wheel off the Left Motor . 2. Take the Gear off the Servo Motor 3. Attach the Gear to the Left Motor. 4. Reposition the Limit Switch A3_1_6_Part1 • Open a new copy of the PLTW Template • Save As A3_1_6_Part1 • Write a program that will oscillate (go back and forth) the motor 0.5 second each way 10 times. • Once complete, Compile and Download the program • Observe and Record what happens when your start the program. Helpful Hints • Create a function that will allow the program to run 20 times • Use the Bump Switch Code (do not confuse with physical switch) • Use the While Command OPEN LOOP Sample Code A3_1_6_Part2 • Open a new copy of the PLTW Template • Save As A3_1_6_Part2 • Develop a program that will oscillate the motor each way 20 times. – One direction will use the limit switch sensor input. – The opposite direction will use a 0.5 s time limit. • When finished, Compile and Download the Program • Before you start the program, make sure that the Limit Switch is resting on the “Standoff” • Run the program and observe the final resting place of the indicator standoff CLOSED LOOP • Where did the indicator standoff stop? Is this an open loop or closed loop system? CLOSED LOOP Sample Program A Helpful Site • http://www.education.rec.ri .cmu.edu/products/teachin g_robotc_cortex/index.html Log onto the Virtual Academy Please complete the Post Assessment Once finished with the Post Assessment – Please take a minute more and complete the PLTW Teacher Survey Thank you