testbed: exercises

43
Testbed: Exercises

Upload: salene

Post on 22-Feb-2016

48 views

Category:

Documents


2 download

DESCRIPTION

Testbed: Exercises. Be Mindful / Not R andom! (be a thinker not a tinker). There are a lot of variables when it comes to programming [ variables hinder troubleshooting ] - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Testbed: Exercises

Testbed:Exercises

Page 2: Testbed: Exercises

Be Mindful / Not Random! (be a thinker not a tinker)

• There are a lot of variables when it comes to programming [ variables hinder troubleshooting ]

• If you are not mindful of what you do or don’t do, I cannot systematically help you eliminate variables. [ IDK, I think so = frustration for you ]

• If we can not limit the variables, I cannot help you solve your problem in a timely manner

• If I cannot solve your problem in a timely manner, I will need to move on to the next group

Page 3: Testbed: Exercises

Safety• Keep long hair clear of your testbed, especially your gears!

• No plugging or unplugging of anything with the power on! – Turn-off the Cortex 1st!!

• You could get shocked and have to pay for anything you damage (intentional or not)

Page 4: Testbed: Exercises

Check your Cortex - Properly inserted plugs?

Parallel to top and bottom edgesWhite wire towards the middle

Page 5: Testbed: Exercises

Are you ready to program your testbed?If you followed your packet, your software should be set-up, now check your hardware:Hardware Checklist: properly inserted plugs into the cortex (white

wire to the middle as shown on the last slide)two motors with motor controllers plugged into

motor ports 2 & 3 (red to red & black to black as shown on the next slide)

limit switch & bump switch in digital ports 1 & 2

Page 6: Testbed: Exercises

Connecting the Motors• Two-wire motors

can be plugged directly into MOTOR ports 1 & 10 on the Cortex, and 2-9 using the Motor Controller 29 – red to red, black to black

Page 7: Testbed: Exercises

FYI - VEX Motors

• 2 Types (labeled on motor):– 2-wire motor 269– Newer 2-wire motor 393

Review• All motors have the same values between

127 (full forward) and -127 (full reverse)

jdonnan
mention old VEX motors... but don't include on slide3 wire motor picture can be removed
Page 8: Testbed: Exercises

Practice Exercise• Copy the code on the next slide into your

RobotC file

Page 9: Testbed: Exercises

Practice - Motor for 5 Seconds

Page 10: Testbed: Exercises

Running a Program• Choose Robot, compile and download,

and start

Page 11: Testbed: Exercises

How it works• The next series of slides explain what is

happening

Page 12: Testbed: Exercises

Motor for 5 Seconds

Displays configuration changes from the Motors and Sensors Setup

Defines the “main task” of the robot

All commands belonging to task main must be in-between these curly braces

PRAGMA – generates from motors & sensors setup

Page 13: Testbed: Exercises

Motor for 5 Seconds

Turns the port2 rightMotor on at half power forward

Page 14: Testbed: Exercises

Motor for 5 Seconds

Causes the robot to wait here in the program for 5.0 seconds

Page 15: Testbed: Exercises

Motor for 5 Seconds

Stops the port2 rightMotor.

End Result: rightMotor spins for 5.0 seconds.

Page 16: Testbed: Exercises

• Modify the code from the practice activity so it works for the 1st programing exercise on the next slide.

Page 17: Testbed: Exercises

Motor Exercises [1 of 6]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.

Teacher initial ____________

Page 18: Testbed: Exercises

Basic Programming:Until Commands

Page 19: Testbed: Exercises

Touch Sensors• Touch Sensor Check

– Plugged into Digital 1 & 2• How they work

– Digital sensor - Pressed or Released – 1 = pressed– 0 = released

• Two Types– Limit Switches – Bumper Switches

• A very brief wait can be inserted after touch sensor related commands to reduce the bouncing effect:

( bumpSwitch ) ;

Page 20: Testbed: Exercises

• Use a combination of the practice code from earlier and the bumpswitch code on the last slide to complete the programing exercise on the next slide

Page 21: Testbed: Exercises

Bump Switch Exercise[2 of 6]

• Exercise: Program the rightMotor to turn on at half power, until the bump switch is pressed. The motor should then stop.

Page 22: Testbed: Exercises

• Use your limit switch to complete the exercise on the next slide

• Instead of bumping the bumpSwitch, your are bumping the limitSwitch

Page 23: Testbed: Exercises

Limit Switch Exercise[3 of 6]

• Wait for the limit switch to be touched before the right motor turns on at half power for 5 seconds, then stops.

• Wait for the limit switch to be touched before 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.

Page 24: Testbed: Exercises

VEX LED• Plugged into DIGITAL

Port 12 (with an extension see next slide)

• Set as “VEX LED”• Red, Green, and Yellow

colors available

Page 25: Testbed: Exercises

VEX LED• Connect the LED to the PWM Extension

wire.– Turn-off the cortex! – no plugging orunplugging of anything with the power on!– The outer terminal on the LED should be

plugged in on the white wire.– The center terminal on the LED should be plugged in on the red wire.– The black wire should have nothing connected.

Page 26: Testbed: Exercises

Decision Making:While Loops and Boolean Logic

Page 27: Testbed: Exercises

While Loops• A while loop is a structure within ROBOTC which

allows a section of code to be repeated as long as a certain condition remains true.

• There are three main parts to every while loop.

Page 28: Testbed: Exercises

1. The word “while”• Every while loop begins with the keyword “while”.

Page 29: Testbed: Exercises

2. The Condition• The condition controls how long or how many times a

while loop repeats. While the condition is true, the while loop repeats; when the condition is false, the while loop ends and the robot moves on in the program.

• The condition is checked every time the loop repeats, before the commands between the curly braces are run.

Page 30: Testbed: Exercises

3. Commands to be Repeated• Commands placed between the curly braces will repeat

while the (condition) is true when the program checks at the beginning of each pass through the loop.

Page 31: Testbed: Exercises

The Truth About while() Loops

Page 32: Testbed: Exercises

1. while() Loops do NOT Constantly Check their Conditions• while() loops check their conditions before

running the body of code• After the body of code is run, the while()

loop checks the condition again• The condition is NOT checked while the

body of code is being run

Page 33: Testbed: Exercises

2. while() Loops do NOT Keep Programs Running Forever• Exception: Infinite while() loops

example “while (1 == 1)” or “while (true)”• Once the while() loop’s condition is

met/false, the robot moves past the while loop in the program and does not revisit it

• Students often assume that because there is a while() loop in the code, the program keeps on running

Page 34: Testbed: Exercises

3. while() Loops are a Programming Structure, not a Command• They do not get a semicolon (;) after the

condition• Adding a semicolon will cause the while() loop to

constantly check the condition, without running the body of code

Page 35: Testbed: Exercises

4. All “until” commands in the NL are actually while() loops • All “until” commands are just a while() loop

with a wait command, to hold the “Program Flow” at that spot in the code

Page 36: Testbed: Exercises

• Modify the code on the previous slides to complete the exercise on the next slide

Page 37: Testbed: Exercises

While Loop Exercise 1[4 of 6]

• Example: Program the greenLED to repeatedly turn on for 2 seconds, then off for 2 seconds, while the limit switch isn’t pressed.

• So pressing the limit switch will stop the led from flashing

Page 38: Testbed: Exercises

Timers• More loop control please?

– Question: Where would the wait statement go if we wanted the loop to repeat for a controlled amount of time?

– Answer: Nowhere! We need something else.

• Solution: Timers– Can be thought of as internal stopwatches (4 available)– Timers should be “cleared” anytime before they are used

• Watch where you clear them!

Page 39: Testbed: Exercises

TimersIn the program below, timer T1 is used as the condition for the while loop, which will run for 30 seconds:

[bumpSwitch] == 1)

Page 40: Testbed: Exercises

While Loop Exercise 2[5 of 6]

• Program the greenLED to repeatedly turn on for 0.5 seconds, then off for 0.5 seconds, while less than 5 seconds have elapsed.

Page 41: Testbed: Exercises

If Statements• When your robot reaches an if Statement in the program,

it evaluates the condition contained between the parenthesis. – If the condition is true, any commands between the braces are run. – If the condition is false, those same commands are ignored.

• Very similar to how a while loop works, but does not repeat the code!

Page 42: Testbed: Exercises

If-else statements• The if-else Statement is an expansion of the basic if

Statement. – The “if” section still checks the condition and runs the appropriate

commands when it evaluates to true– Using the “else” allows for specific code to be run only when the

condition is false. • Either the “if” or the “else” branch is always run; no more,

no less.

Page 43: Testbed: Exercises

If-else if Exercise 1[6 of 6]

• Program the greenLED to turn on if the bumpswitch is pressed, and off if it’s released. Loop Forever.

.