xtreme robot olympiad programming basics dr. peter laz associate professor department of engineering...

21
Xtreme Robot Olympiad Programming Basics Dr. Peter Laz Associate Professor Department of Engineering University of Denver

Upload: diane-banks

Post on 04-Jan-2016

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Xtreme Robot Olympiad Programming Basics Dr. Peter Laz Associate Professor Department of Engineering University of Denver

Xtreme Robot OlympiadProgramming Basics

Dr. Peter Laz Associate Professor

Department of EngineeringUniversity of Denver

Page 2: Xtreme Robot Olympiad Programming Basics Dr. Peter Laz Associate Professor Department of Engineering University of Denver

Outline

• Basic commands• Variables• Loops• Conditional statements

• Motor and servo operation• PWM signals

• Programming with EasyC• How to program the microcontroller?

Page 3: Xtreme Robot Olympiad Programming Basics Dr. Peter Laz Associate Professor Department of Engineering University of Denver

Variables

• Different types of variables• int = integer (whole number)• float = decimal • char = character

Page 4: Xtreme Robot Olympiad Programming Basics Dr. Peter Laz Associate Professor Department of Engineering University of Denver

Common Commands

• Operations• Sets variables equal to specific values• Standard operators: + - * /

• Loops – cause actions to be repeated• for• while

• Conditional statements – actions executed depend on results of comparisons• if … then• if … then … else• Comparators: >, <, >=, <=, ==

Page 5: Xtreme Robot Olympiad Programming Basics Dr. Peter Laz Associate Professor Department of Engineering University of Denver

Program Tracking

Variable ValuePrinted to the screen

Other Actions

Page 6: Xtreme Robot Olympiad Programming Basics Dr. Peter Laz Associate Professor Department of Engineering University of Denver

Basic Commands

• Example: if else

int x

If (x>10){ x=x+5}else{

x=x-1}

What value does x have if

x = 5?

x = 18?

x = 10?

Page 7: Xtreme Robot Olympiad Programming Basics Dr. Peter Laz Associate Professor Department of Engineering University of Denver

Basic Commands

• Example: for loop

int i

for (i=0;i<7;i=i+1)

{

print(“tiger\n”)

}How many times is tiger printed to the screen?

Page 8: Xtreme Robot Olympiad Programming Basics Dr. Peter Laz Associate Professor Department of Engineering University of Denver

Basic Commands

• Example: for loop

int i

for (i=0;i<7;i=i+2)

{

print(“tiger\n”)

}How many times is tiger printed to the screen?

Page 9: Xtreme Robot Olympiad Programming Basics Dr. Peter Laz Associate Professor Department of Engineering University of Denver

Basic Commands

• Example: while loop

int iint flagi=0; flag = 0while (flag != 1) {i=i+1print(i)If (i>=9)

{flag = 1}

}

What is printed to the screen?

Page 10: Xtreme Robot Olympiad Programming Basics Dr. Peter Laz Associate Professor Department of Engineering University of Denver

Exercise

• Using basic commands, develop a flow chart/program to print the first 10 numbers of the Fibonacci sequence• 1,1,2,3,5,8,13,21,34,55

Page 11: Xtreme Robot Olympiad Programming Basics Dr. Peter Laz Associate Professor Department of Engineering University of Denver

Motor and Servo Components

• Motors – operate continuously• Servos – have a range of 120 degrees

• Both use pulse width modulated signals

• PWM values range from 0 to 255• Corresponds to the duty cycle or amount of time the

pulse is on• 0 = full counterclockwise• 127 = stop• 255 = full clockwise

Page 12: Xtreme Robot Olympiad Programming Basics Dr. Peter Laz Associate Professor Department of Engineering University of Denver

PWM Signals

• Repeating pulse signal• Pulse width determines the amount of voltage to

the motor • 127 or 50% duty cycle corresponds to 1.5ms

http://en.wikipedia.org

Page 13: Xtreme Robot Olympiad Programming Basics Dr. Peter Laz Associate Professor Department of Engineering University of Denver

Motor Control

• Note: to make the robot go forward• Left motor CW• Right motor

CCW

Page 14: Xtreme Robot Olympiad Programming Basics Dr. Peter Laz Associate Professor Department of Engineering University of Denver

Basic Commands

• Define variables • Type of and initial value of variable

• Assignment• Assign a value to a variable

• Wait(time in ms)• wait(500)• 1000 ms = 1 sec• This applies a delay to allow the vehicle to move

forward or turn• Sensors

• Read in data into a variable• Commands vary depending on the sensor

Page 15: Xtreme Robot Olympiad Programming Basics Dr. Peter Laz Associate Professor Department of Engineering University of Denver

Programming Lab with EasyC

Page 16: Xtreme Robot Olympiad Programming Basics Dr. Peter Laz Associate Professor Department of Engineering University of Denver

Programming

1. Determine the appropriate com port• Right click on my computer, properties,

hardware, device manager• Look at which com port has “Prolific USB to

Serial Comm port”

2. Open easyC

3. Under Build&Download, choose loader setup

• Pick the correct com port

Page 17: Xtreme Robot Olympiad Programming Basics Dr. Peter Laz Associate Professor Department of Engineering University of Denver

Programming

4. With the vehicle off, plug the programming module into the microcontroller.

5. Turn the vehicle on, be aware the robot will begin to run the previously loaded program

6. Hit the orange button on the programming cable to connect the vehicle and the computer

7. Initiate programming through the build and download menu

Page 18: Xtreme Robot Olympiad Programming Basics Dr. Peter Laz Associate Professor Department of Engineering University of Denver

EasyC

Page 19: Xtreme Robot Olympiad Programming Basics Dr. Peter Laz Associate Professor Department of Engineering University of Denver

Exercises

• Download Testprogram.ecp

• Download Online Window

Page 20: Xtreme Robot Olympiad Programming Basics Dr. Peter Laz Associate Professor Department of Engineering University of Denver

Exercises

• Write a program to drive in a 5 foot square• Challenge: use a for loop in your code

1. Draw the flowchart

2. Write the program

3. Test the wait() values

Page 21: Xtreme Robot Olympiad Programming Basics Dr. Peter Laz Associate Professor Department of Engineering University of Denver

This workforce solution was funded by a grant awarded under the Workforce Innovation in Regional Development (WIRED) as implemented by the U.S. Department of Labor’s Employment and Training Administration working in partnership with the Colorado Department of Labor and Employment, the Metro Denver Economic Development Corporation, and the City and County of Denver's Office of Economic Development.  The solution was created by the grantee and does not necessarily reflect the official position of the U.S. Department of Labor.  The Department of Labor makes no guarantees, warranties, or assurances of any kind, express or implied, with respect to such information, including any information on linked sites and including, but not limited to, accuracy of the information or its completeness, timeliness, usefulness, adequacy, continued availability, or ownership.  This solution is copyrighted by the institution that created it. Internal use by an organization and/or personal use by an individual for non-commercial purposes is permissible.  All other uses require the prior authorization of the copyright owner.