VEX/ROBOTC Session 1

125
VEX/ROBOTC Session 1

description

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. - PowerPoint PPT Presentation

Transcript of VEX/ROBOTC Session 1

Page 1: VEX/ROBOTC Session 1

VEX/ROBOTC Session 1

Page 2: 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

Page 3: VEX/ROBOTC Session 1

Getting Started• There are three main sections to the ROBOTC

Integrated Development Environment (IDE):1. The Editor2. The Code Templates / Function Library3. The Main Menu / Toolbar

Page 4: VEX/ROBOTC Session 1

The Editor

• This is the part of the interface where the user can write code.

Page 5: VEX/ROBOTC Session 1

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.

Page 6: VEX/ROBOTC Session 1

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.

Page 7: VEX/ROBOTC Session 1

THE POE TEST BEDParts are more than just parts

Page 8: VEX/ROBOTC Session 1

The POE Test Bed

Page 9: VEX/ROBOTC Session 1

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

Page 10: VEX/ROBOTC Session 1

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.

Page 11: VEX/ROBOTC Session 1

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

Page 12: VEX/ROBOTC Session 1

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.

Page 13: VEX/ROBOTC Session 1

The Potentiometer

• With the Potentiometer you can determine both position and direction of rotation.

• Measure Angular Position.• 250-Degrees of

Adjustment.• Adjustable mounting

angle.•

Page 14: VEX/ROBOTC Session 1

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.

Page 15: VEX/ROBOTC Session 1

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.

Page 16: VEX/ROBOTC Session 1

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.

Page 17: VEX/ROBOTC Session 1

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.

Page 18: VEX/ROBOTC Session 1

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

Page 19: VEX/ROBOTC Session 1

Other Parts

Motor Controller Flashlight

Green LED

Battery

Page 20: VEX/ROBOTC Session 1

OPENING THE PLTW TEMPLATEGetting Started…

Page 21: VEX/ROBOTC Session 1

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

Page 22: VEX/ROBOTC Session 1

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

Page 23: VEX/ROBOTC Session 1

Setting the Platform and Communications method:

• Select View• Preferences• Detailed

Preference…

Page 24: VEX/ROBOTC Session 1

ROBOTC Preferences

• Select the Platform Type so that it shows Natural Language (VEX Cortex)

• Leave the Communication as Automatic SelectionNOTE – Get your students in the habit of checking this every time they start a new project

Page 25: VEX/ROBOTC Session 1

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

Page 26: VEX/ROBOTC Session 1

Setting up the Motors and Sensors• For this Project ROBOTC has pre-programmed

some Standard Model Configurations

Page 27: VEX/ROBOTC Session 1

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

Page 28: VEX/ROBOTC Session 1

Motor and Sensors Setup

• VEX 2.0 Analog Sensors• Naming the sensor– Use descriptive names

• Like before, have the students check the wiring

Page 29: VEX/ROBOTC Session 1

Motor and Sensors Setup

• VEX 2.0 Digital Sensors

• Naming the sensor– Use descriptive

names• Like before, have

the students check the wiring

Page 30: VEX/ROBOTC Session 1

Time to Compile and Download…

• Select Robot– Compile and Download

Program

Page 31: VEX/ROBOTC Session 1

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

Page 32: VEX/ROBOTC Session 1

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

Page 33: VEX/ROBOTC Session 1

Debugger Windows

• From the Menu– Robot– Debugger Windows– Motors and Sensors

Page 34: VEX/ROBOTC Session 1

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

Page 35: VEX/ROBOTC Session 1

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?

Page 36: VEX/ROBOTC Session 1

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:

Page 37: VEX/ROBOTC Session 1

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

Page 38: VEX/ROBOTC Session 1

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 .

Page 39: VEX/ROBOTC Session 1

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

Page 40: VEX/ROBOTC Session 1

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

Page 41: VEX/ROBOTC Session 1

The Actual Codetask main()

{

turnLEDOff(green);wait (1);turnLEDOn(green);wait(1);turnLEDOff (green);wait (1);turnLEDOn (green);wait (1);turnLEDOff (green);

}

Program begins within the curly braces must have one at

the start and end.

All commands end with a semi-colon

The Program starts with task main()

Special command to turn on or off the LED

Once finished typing in the code – Compile and Download the program

Page 42: VEX/ROBOTC Session 1

green vs. dgtl12task 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

Page 43: VEX/ROBOTC Session 1

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?

Page 44: VEX/ROBOTC Session 1

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

Page 45: VEX/ROBOTC Session 1

A3_1_2_PART2

You can drag and drop these commands in

between the curly braces

Page 46: VEX/ROBOTC Session 1

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

Page 47: VEX/ROBOTC Session 1

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.

Page 48: VEX/ROBOTC Session 1

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

Page 49: VEX/ROBOTC Session 1

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.

Page 50: VEX/ROBOTC Session 1

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.

Page 51: VEX/ROBOTC Session 1

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.

Page 52: VEX/ROBOTC Session 1

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

Page 53: VEX/ROBOTC Session 1

Modify A3_1_2_Part4

Program for the Servo so that the following happens:

1. Go to position -127 for 2 seconds2. Go to position -63 for 3 seconds3. Go to position 0 for 2 seconds4. Go position 63 for 3 seconds5. Go to position 127 for 2 seconds.

Page 54: VEX/ROBOTC Session 1

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?

Page 55: VEX/ROBOTC Session 1

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:

Page 56: VEX/ROBOTC Session 1

Motor and Sensor Set-Up

• Select the POE + CIM Testbed for this activity

Page 57: VEX/ROBOTC Session 1

Enter the following….

• untilBump(bumpSwitch);• startMotor(rightMotor, 67);• wait(5);• stopMotor(rightMotor);

1. Compile and Download the program.2. What happened?

Page 58: VEX/ROBOTC Session 1

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.

Page 59: VEX/ROBOTC Session 1

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

Page 60: VEX/ROBOTC Session 1

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.

Page 61: VEX/ROBOTC Session 1

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.

Page 62: VEX/ROBOTC Session 1

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?

Page 63: VEX/ROBOTC Session 1

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.

Page 64: VEX/ROBOTC Session 1

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);

Page 65: VEX/ROBOTC Session 1

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

Page 66: VEX/ROBOTC Session 1

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

Page 67: VEX/ROBOTC Session 1

A3_1_3_Part4

• Between the curly braces enter the following:

1. startMotor(leftMotor, 63);2. startMotor(rightMotor, 63);3. untilEncoderCounts(480,quad);4. stopMotor(leftMotor);5. stopMotor(rightMotor);

Page 68: VEX/ROBOTC Session 1

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

Page 69: VEX/ROBOTC Session 1

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

Page 70: VEX/ROBOTC Session 1

A3_1_3_Part5

• Before we begin to program for the Line Follower, we must figure out its “Threshold”

1. Place a white object about a 1/4” away from the Line Follower

2. From the Program Debug window click on Snapshot under the Refresh option – Record this value

3. Do the same thing, this time with a dark object.

4. Add the two values and divide by 2

Page 71: VEX/ROBOTC Session 1

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.

Page 72: VEX/ROBOTC Session 1

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

Page 73: VEX/ROBOTC Session 1

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?

Page 74: VEX/ROBOTC Session 1

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.

Page 75: VEX/ROBOTC Session 1

Check for Understanding

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

Page 76: VEX/ROBOTC Session 1

Check for Understanding

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

Page 77: VEX/ROBOTC Session 1

Check for Understanding

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

Page 78: VEX/ROBOTC Session 1

Check for Understanding

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

Page 79: VEX/ROBOTC Session 1

Check for Understanding

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

Page 80: VEX/ROBOTC Session 1

Break for Lunch

Page 81: VEX/ROBOTC Session 1

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 loop1. The Word While2. The Condition3. Body - Commands To Be Repeated

Page 82: VEX/ROBOTC Session 1

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

Page 83: VEX/ROBOTC Session 1

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.

Page 84: VEX/ROBOTC Session 1

Lets Give This A Try

• Close all programs• Open a new PLTW Template• Save As A3_1_4_Part1

Page 85: VEX/ROBOTC Session 1

A3_1_4_Part1

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

Page 86: VEX/ROBOTC Session 1

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

Page 87: VEX/ROBOTC Session 1

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 ProgramA3_1_4_Part2

Page 88: VEX/ROBOTC Session 1

Boolean Logic Table

Page 89: VEX/ROBOTC Session 1

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 ProgramA3_1_4_Part3

Page 90: VEX/ROBOTC Session 1

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

Page 91: VEX/ROBOTC Session 1

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.

Page 92: VEX/ROBOTC Session 1

Lets give it a try..• Between the curly braces, input the following:

1. while (1 ==1) //Loop program indefinitely2. {3. if (SensorValue(bumpSwitch) == 1)4. {5. turnLEDOn(green);6. }7. if (SensorValue(bumpSwitch) == 0)8. {9. turnLEDOff(green);10. }11. }

BE SURE TO INCLUDE ALL THE CURLY BRACESCompile and Download Program

Save AS A3_1_4_Part4

Page 93: VEX/ROBOTC Session 1

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

Page 94: VEX/ROBOTC Session 1

Lets Modify A3_1_4_Part4

• if (condition)• {• body;• }• else• {• body;• }

Page 95: VEX/ROBOTC Session 1

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.

Page 96: VEX/ROBOTC Session 1

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.

Page 97: VEX/ROBOTC Session 1

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.

Page 98: VEX/ROBOTC Session 1

Different VariablesData 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, 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

Page 99: VEX/ROBOTC Session 1

Variables and Functions – VEX

• Set up the Motors and Sensors to the following:• Save As A3_1_5_Part1

Page 100: VEX/ROBOTC Session 1

Creating a VariableTo create a variable, you must give it a:

1. Type: Data type it will hold2. 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”

Page 101: VEX/ROBOTC Session 1

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;• }

Page 102: VEX/ROBOTC Session 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

Page 103: VEX/ROBOTC Session 1

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

Page 104: VEX/ROBOTC Session 1

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

Page 105: VEX/ROBOTC Session 1

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.

Page 106: VEX/ROBOTC Session 1

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.

Page 107: VEX/ROBOTC Session 1

Functions

• Functions– Group together several lines of code– Referenced many times in task main or in other functions

• Creating FunctionsExample: 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)

Page 108: VEX/ROBOTC Session 1

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

Page 109: VEX/ROBOTC Session 1

The Function Defintion

• Function definitions define the code that belongs to the function

Page 110: VEX/ROBOTC Session 1

Calling the Function

• Function calls– Call and run code from function– Placed in task main or other functions

Page 111: VEX/ROBOTC Session 1

Lets Give it a Try…• 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

Page 112: VEX/ROBOTC Session 1
Page 113: VEX/ROBOTC Session 1

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?

Page 114: VEX/ROBOTC Session 1

Check for Understanding

• Examine the code to the right. How many times will the LED turn on and off?

• How do you know this?

Page 115: VEX/ROBOTC Session 1

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.

Page 116: VEX/ROBOTC Session 1

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

Page 117: VEX/ROBOTC Session 1

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.

Page 118: VEX/ROBOTC Session 1

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

Page 119: VEX/ROBOTC Session 1

OPEN LOOP Sample Code

Page 120: VEX/ROBOTC Session 1

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

Page 121: VEX/ROBOTC Session 1

CLOSED LOOP

• Where did the indicator standoff stop? Is this an open loop or closed loop system?

Page 122: VEX/ROBOTC Session 1

CLOSED LOOP Sample Program

Page 123: VEX/ROBOTC Session 1

A Helpful Site

•http://www.education.rec.ri.cmu.edu/products/teaching_robotc_cortex/index.html

Page 124: VEX/ROBOTC Session 1

Log onto the Virtual AcademyPlease complete

the Post Assessment

Once finished with the Post Assessment – Please take a

minute more and complete the PLTW Teacher Survey

Page 125: VEX/ROBOTC Session 1

Thank you