2008 sbpli/first programming workshop tom boehm patchogue medford high school, team 329 motorola...

15
2008 SBPLI/FIRST 2008 SBPLI/FIRST Programming Workshop Programming Workshop Tom Boehm Patchogue Medford High Tom Boehm Patchogue Medford High School, Team 329 Motorola Inc. School, Team 329 Motorola Inc. Mark McLeod Hauppauge High School Mark McLeod Hauppauge High School Team 358 Northrop Grumman Corp. Team 358 Northrop Grumman Corp.

Upload: osborne-lee

Post on 04-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2008 SBPLI/FIRST Programming Workshop Tom Boehm Patchogue Medford High School, Team 329 Motorola Inc. Mark McLeod Hauppauge High School Team 358 Northrop

2008 SBPLI/FIRST 2008 SBPLI/FIRST Programming Programming WorkshopWorkshop

Tom Boehm Patchogue Medford High Tom Boehm Patchogue Medford High School, Team 329 Motorola Inc.School, Team 329 Motorola Inc.

Mark McLeod Hauppauge High School Mark McLeod Hauppauge High School Team 358 Northrop Grumman Corp.Team 358 Northrop Grumman Corp.

Page 2: 2008 SBPLI/FIRST Programming Workshop Tom Boehm Patchogue Medford High School, Team 329 Motorola Inc. Mark McLeod Hauppauge High School Team 358 Northrop

GoalsGoals

Well, duh, we want to actually make these Well, duh, we want to actually make these robots DO something.robots DO something.

Learn about some basic programming Learn about some basic programming language elements.language elements.

Learn about the structure of a ‘C’ program.Learn about the structure of a ‘C’ program. Learn about programming the robot Learn about programming the robot

controller.controller. Learn about how this will all be different from Learn about how this will all be different from

what we actually see in January.what we actually see in January. Find out how to get more information.Find out how to get more information.

Page 3: 2008 SBPLI/FIRST Programming Workshop Tom Boehm Patchogue Medford High School, Team 329 Motorola Inc. Mark McLeod Hauppauge High School Team 358 Northrop

But First, a Little Vocabulary But First, a Little Vocabulary Lesson.Lesson.

You gotta talk the talk…You gotta talk the talk…

See the handout. There are lots of See the handout. There are lots of them to become familiar with, and lots them to become familiar with, and lots more that I haven’t put down. more that I haven’t put down. Memorize all of them, there will be a Memorize all of them, there will be a test on them next week…test on them next week…

Page 4: 2008 SBPLI/FIRST Programming Workshop Tom Boehm Patchogue Medford High School, Team 329 Motorola Inc. Mark McLeod Hauppauge High School Team 358 Northrop

Simple Programming Simple Programming StructureStructure A single line of code that does A single line of code that does

SOMETHING, is usually called a SOMETHING, is usually called a statement.statement.

A group a statements collected A group a statements collected together to perform a specific set of together to perform a specific set of actions is typically called a function.actions is typically called a function.

A group of functions that are A group of functions that are collected together are called a collected together are called a program.program.

Page 5: 2008 SBPLI/FIRST Programming Workshop Tom Boehm Patchogue Medford High School, Team 329 Motorola Inc. Mark McLeod Hauppauge High School Team 358 Northrop

StatementsStatements

Try to make them very simple:Try to make them very simple:int i, j, k;int i, j, k;j = -50;j = -50;i = 15 + j;i = 15 + j;k = 22;k = 22;

But they can get very complex:But they can get very complex:k = (i * 23) + (j * k * -98.34) / (j >5) ? 23 : 12;k = (i * 23) + (j * k * -98.34) / (j >5) ? 23 : 12;/* This is a comment. It does not execute. k = /* This is a comment. It does not execute. k =

10509. */10509. */

Page 6: 2008 SBPLI/FIRST Programming Workshop Tom Boehm Patchogue Medford High School, Team 329 Motorola Inc. Mark McLeod Hauppauge High School Team 358 Northrop

OK, so now what?OK, so now what?

That’s all well and good, but how do we That’s all well and good, but how do we make decisions?make decisions?

We use conditional expressions.We use conditional expressions.if (j > 5) then do somethingif (j > 5) then do somethingelse do some other thing.else do some other thing.

if (j > 5) if (j > 5) k = j + 1; k = j + 1;elseelse k = 0; k = 0;

There are many comparison operations, There are many comparison operations, >, <, >=, <=, ==, !=, etc.>, <, >=, <=, ==, !=, etc.

Page 7: 2008 SBPLI/FIRST Programming Workshop Tom Boehm Patchogue Medford High School, Team 329 Motorola Inc. Mark McLeod Hauppauge High School Team 358 Northrop

What if we need to do What if we need to do something more than something more than once?once? Loops! They usually have the form:Loops! They usually have the form:

Initialize some loop condition.Initialize some loop condition.While (this condition is true)While (this condition is true)

Do this set of stuff.Do this set of stuff.Modify the loop condition.Modify the loop condition.

For example:For example:j = 0;j = 0;while ( j < 5 )while ( j < 5 ){{

k = k + j;k = k + j;j = j + 1;j = j + 1;

}}

Page 8: 2008 SBPLI/FIRST Programming Workshop Tom Boehm Patchogue Medford High School, Team 329 Motorola Inc. Mark McLeod Hauppauge High School Team 358 Northrop

Loops are CoolLoops are Cool

Loops are used to count, repeat Loops are used to count, repeat actions, or wait for something to actions, or wait for something to occur.occur. Counting loops are so Counting loops are so commonplace, in ‘C’ they are commonplace, in ‘C’ they are given their own keyword: ‘for’:given their own keyword: ‘for’:

for ( j = 0; j < 5 ; j = j + 1;)for ( j = 0; j < 5 ; j = j + 1;)

k = k + j;k = k + j;// This does the same thing as last page.// This does the same thing as last page.

Page 9: 2008 SBPLI/FIRST Programming Workshop Tom Boehm Patchogue Medford High School, Team 329 Motorola Inc. Mark McLeod Hauppauge High School Team 358 Northrop

FunctionsFunctions

A function is a set of statements that can be A function is a set of statements that can be executed the same way over and over again, executed the same way over and over again, without a loop, and without re-writing the without a loop, and without re-writing the same code over and over again.same code over and over again.

Benefits: Benefits: – Once you get it working, it will always work that Once you get it working, it will always work that

way no matter where you call it. way no matter where you call it. – You do not have to retype the same code over You do not have to retype the same code over

again, possibly mistyping it.again, possibly mistyping it.– It makes the whole program easier to understand.It makes the whole program easier to understand.– If you do it right, maybe you can use it again next If you do it right, maybe you can use it again next

year.year.

Page 10: 2008 SBPLI/FIRST Programming Workshop Tom Boehm Patchogue Medford High School, Team 329 Motorola Inc. Mark McLeod Hauppauge High School Team 358 Northrop

PeripheralsPeripherals

A peripheral is something that is attached to the A peripheral is something that is attached to the microcomputer that performs a specialized function.microcomputer that performs a specialized function.

Examples:Examples:– Digital Port Pins: Can be 0 or 1, True or False, High or Digital Port Pins: Can be 0 or 1, True or False, High or

Low, On or Off, etc. Can be either an input or an output. Low, On or Off, etc. Can be either an input or an output. Used for monitoring and control.Used for monitoring and control.

– Motor PWM: Used to provide variable power to a motor to Motor PWM: Used to provide variable power to a motor to control motor speed and direction.control motor speed and direction.

– Gear tooth sensor: Counts gear teeth as they whiz by. Gear tooth sensor: Counts gear teeth as they whiz by. Can be used to determine distance.Can be used to determine distance.

– Gyroscope: Can be used to measure a heading.Gyroscope: Can be used to measure a heading.– Accelerometer: Can be used to measure forces acting on Accelerometer: Can be used to measure forces acting on

the robot.the robot.– Digital cameras, Wireless network cards, USB ports, Digital cameras, Wireless network cards, USB ports,

Serial ports, Pneumatic actuators, and many, many Serial ports, Pneumatic actuators, and many, many more…more…

Page 11: 2008 SBPLI/FIRST Programming Workshop Tom Boehm Patchogue Medford High School, Team 329 Motorola Inc. Mark McLeod Hauppauge High School Team 358 Northrop

A Simple Program…A Simple Program…

Let’s just make this robot move Let’s just make this robot move forward, stop, turn around and forward, stop, turn around and move backward again…move backward again…

Page 12: 2008 SBPLI/FIRST Programming Workshop Tom Boehm Patchogue Medford High School, Team 329 Motorola Inc. Mark McLeod Hauppauge High School Team 358 Northrop

Now we’ll control it…Now we’ll control it…

Let’s see how we can control it Let’s see how we can control it with the remote control…with the remote control…

Page 13: 2008 SBPLI/FIRST Programming Workshop Tom Boehm Patchogue Medford High School, Team 329 Motorola Inc. Mark McLeod Hauppauge High School Team 358 Northrop

Now let’s combine Now let’s combine them…them… This simple program will have the This simple program will have the

robot be operator controlled. It robot be operator controlled. It will also use a bumper to detect will also use a bumper to detect an object (by hitting it) and an object (by hitting it) and automatically reverse away from automatically reverse away from it and then go back to operator it and then go back to operator control.control.

Page 14: 2008 SBPLI/FIRST Programming Workshop Tom Boehm Patchogue Medford High School, Team 329 Motorola Inc. Mark McLeod Hauppauge High School Team 358 Northrop

Now you try it…Now you try it…

Look at the sample program on Look at the sample program on your laptops…your laptops…– Operator control with object Operator control with object

avoidanceavoidance– Autonomous square by counter.Autonomous square by counter.– Autonomous square by timing.Autonomous square by timing.– Speed incrementing and Speed incrementing and

decrementing.decrementing.

Page 15: 2008 SBPLI/FIRST Programming Workshop Tom Boehm Patchogue Medford High School, Team 329 Motorola Inc. Mark McLeod Hauppauge High School Team 358 Northrop

Try This:Try This:

Modify the program given to Modify the program given to make the robot run in a circle.make the robot run in a circle.

Run an oval.Run an oval. Run a figure eight.Run a figure eight. User the bumper to select User the bumper to select

between the three.between the three.