Presentation is loading. Please wait.

Presentation is loading. Please wait.

VEX/ROBOTC Session 1.

Similar presentations


Presentation on theme: "VEX/ROBOTC Session 1."— Presentation transcript:

1 VEX/ROBOTC Session 1

2 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 Open and Closed Loop Systems

3 Getting Started There are three main sections to the ROBOTC Integrated Development Environment (IDE): The Editor The Code Templates / Function Library The Main Menu / Toolbar

4 The Editor This is the part of the interface where the user can write code. C Standard Should do comments as much as possible and label and describe the code Below comments, task main, don’t mess with task main just fill in curly brackets, one curly bracket must have a second one to close out

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

6 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. Open debugger window to change motor speeds find on main tool bar

7 Parts are more than just parts
The poe test bed

8 The POE Test Bed

9 VEX Components The Cortex
STMicroelectronics ARM Cortex-M3 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

10 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 pre-programmed in the VEX Microcontroller. On or off, digital (LED – digital)

11 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 pre-programmed in the VEX Microcontroller

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

13 The Potentiometer With the Potentiometer you can determine both position and direction of rotation. Measure Angular Position. 250-Degrees of Adjustment. Adjustable mounting angle.

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

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

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

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

18 Three Wire Servo Motor Servo motors are a type of motor that can be directed to turn to face a specific direction, rather than just spin forward or backward. Expand functionality Add pan and tilt to a camera 100 degrees of rotation Do exact positioning Servo’s have a lot of power, good for knocking something over, more powerful

19 Other Parts Green LED Flashlight Motor Controller Battery

20 Opening the pltw template
Getting Started… Opening the pltw template

21 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

22 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

23 Setting the Platform and Communications method:
Select View Preferences Detailed Preference…

24 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

25 Put your Name on your Assignments
Project Title: INPUTS AND OUTPUTS 3.1.1 Team Members: ROY ROSNIK Date: SEPT Section: 1

26 Setting up the Motors and Sensors
For this Project ROBOTC has pre-programmed some Standard Model Configurations

27 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

28 Motor and Sensors Setup
VEX 2.0 Analog Sensors Naming the sensor Use descriptive names Like before, have the students check the wiring

29 Motor and Sensors Setup
VEX 2.0 Digital Sensors Naming the sensor Use descriptive names Like before, have the students check the wiring

30 Time to Compile and Download…
Select Robot Compile and Download Program

31 Three most commons Messages
The VEX Cortex is not plugged into the computer The wrong Compiler Target is selected Power is turned off or the battery needs to be replaced

32 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

33 Debugger Windows From the Menu Robot Debugger Windows
Motors and Sensors

34 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

35 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?

36 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:

37 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

38 The Color of the Code Normal Text is displayed as black text.
Normal text is used for variable and function names that are user-defined 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 .

39 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

40 #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.

41 The Actual Code { } The Program starts with task main()
turnLEDOff(green); wait (1); turnLEDOn(green); wait(1); turnLEDOff (green); turnLEDOn (green); } 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

42 green vs. dgtl12 task main() { turnLEDOff(dgtl12); wait (1); turnLEDOn(dgtl12); wait(1); turnLEDOff (dgtl12); turnLEDOn (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

43 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?

44 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

45 You can drag and drop these commands in between the curly braces
A3_1_2_PART2 You can drag and drop these commands in between the curly braces

46 After changing the black text – noticed it turned dark red
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

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

48 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

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

50 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. Turn the rightMotor on forward at half speed for 5 seconds, then stop. Turn the leftMotor on in reverse at three-fourths speed for 2.5 seconds, then stop. Turn both motors on at full power, and spinning in the same direction, for 7.25 seconds, then stop.

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

52 A3_1_2_Part4 Between the curly braces type the following:
setServo(servoMotor, -127); wait(2); setServo(servoMotor, 0); setServo(servoMotor, 127); Compile and Download Program

53 Modify A3_1_2_Part4 Program for the Servo so that the following happens: Go to position -127 for 2 seconds Go to position -63 for 3 seconds Go to position 0 for 2 seconds Go position 63 for 3 seconds Go to position 127 for 2 seconds.

54 Check for Understanding
Explain any challenges that you encountered while developing the program. Explain how these outputs might be used in an application. Writing Code is the primary focus of ROBOTC. What environment is the code written in? What portion of the interface allows user to see all the functions available in ROBOTC at their user level?

55 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:

56 Motor and Sensor Set-Up
Select the POE + CIM Testbed for this activity

57 Enter the following…. untilBump(bumpSwitch);
startMotor(rightMotor, 67); wait(5); stopMotor(rightMotor); Compile and Download the program. What happened?

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

59 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

60 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. 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. Both motors turn on at half power until the sensor is bumped again. Both motors should then move in reverse at half power for 3.5 seconds. Both motors will stop.

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

62 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?

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

64 A3_1_3_Part3 Between the curly braces enter the following:
turnLEDOn(green); untilPotentiometerGreaterThan(2048, potentiometer); turnLEDOff(green); startMotor(leftMotor, 63); wait(3.5); stopMotor(leftMotor);

65 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 (Senors tab) Robot- Debugger windows- senors

66 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

67 A3_1_3_Part4 Between the curly braces enter the following:
startMotor(leftMotor, 63); startMotor(rightMotor, 63); untilEncoderCounts(480,quad); stopMotor(leftMotor); stopMotor(rightMotor);

68 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

69 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

70 A3_1_3_Part5 Before we begin to program for the Line Follower, we must figure out its “Threshold” 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

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

72 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

73 A3_1_3_Part6 Compile and Download the Program.
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?

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

75 Check for Understanding
The picture above shows a bump switch along with a snippet of code. Explain what will happen.

76 Check for Understanding
The picture above shows a potentiometer along with a snippet of code. Explain what will happen.

77 Check for Understanding
The picture above shows an Optical Encoder along with a snippet of code. Explain what will happen.

78 Check for Understanding
The picture above shows a Line Follower along with a snippet of code. Explain what will happen.

79 Check for Understanding
The picture above shows an Ultra Sonic sensor along with a snippet of code. Explain what will happen.

80 Break for Lunch

81 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 The Word While The Condition Body - Commands To Be Repeated

82 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

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

84 Lets Give This A Try Close all programs Open a new PLTW Template
Save As A3_1_4_Part1

85 A3_1_4_Part1 From the Function Library, Drag and drop the while (condition\{\body\}/ between the curly braces.

86 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

87 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

88 Boolean Logic Table

89 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); } Compile and Download Program A3_1_4_Part3

90 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

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

92 Lets give it a try.. BE SURE TO INCLUDE ALL THE CURLY BRACES
Between the curly braces, input the following: while (1 ==1)    //Loop program indefinitely {   if (SensorValue(bumpSwitch) == 1)   {     turnLEDOn(green);   }   if (SensorValue(bumpSwitch) == 0)     turnLEDOff(green); } BE SURE TO INCLUDE ALL THE CURLY BRACES Compile and Download Program Save AS A3_1_4_Part4

93 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

94 Lets Modify A3_1_4_Part4 if (condition) {   body; } else

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

96 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 If-Else loops.

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

98 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 if the decimal part is zero) -.123, 0.56, 3.0, 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

99 Variables and Functions – VEX
Set up the Motors and Sensors to the following: Save As A3_1_5_Part1

100 Creating a Variable To create a variable, you must give it a:
Type: Data type it will hold 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”

101 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);     motorcount = motorcount + 1;   }

102 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

103 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

104 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

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

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

107 Functions Functions Creating 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 Function header (name of function) Function definition (code in the function) Function call (where function code will run)

108 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

109 The Function Defintion
Function definitions define the code that belongs to the function

110 Calling the Function Function calls Call and run code from function
Placed in task main or other functions

111 Lets Give it a Try… Save A3_1_5_Part3 Save As A3_1_5_Part3
void LEDControl() { if(SensorValue[bumpSwitch] == 1) turnLEDOn(green); } Else turnLEDOff(green); task main() while (1==1) LEDControl(); 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

112

113 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?

114 Check for Understanding
Examine the code to the right. How many times will the LED turn on and off? How do you know this?

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

116 Reconfigure the Testbed
Take the wheel off the Left Motor . Take the Gear off the Servo Motor Attach the Gear to the Left Motor. Reposition the Limit Switch

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

118 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

119 OPEN LOOP Sample Code

120 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

121 CLOSED LOOP Where did the indicator standoff stop? Is this an open loop or closed loop system?

122 CLOSED LOOP Sample Program

123 A Helpful Site

124 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

125 Thank you


Download ppt "VEX/ROBOTC Session 1."

Similar presentations


Ads by Google