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

Post on 04-Jan-2016

215 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

Transcript

Xtreme Robot OlympiadProgramming Basics

Dr. Peter Laz Associate Professor

Department of EngineeringUniversity of Denver

Outline

• Basic commands• Variables• Loops• Conditional statements

• Motor and servo operation• PWM signals

• Programming with EasyC• How to program the microcontroller?

Variables

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

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: >, <, >=, <=, ==

Program Tracking

Variable ValuePrinted to the screen

Other Actions

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?

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?

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?

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?

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

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

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

Motor Control

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

CCW

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

Programming Lab with EasyC

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

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

EasyC

Exercises

• Download Testprogram.ecp

• Download Online Window

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

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. 

top related