robot building lab: introduction lab 1 the handy board and interactive-c (ic) ...

23
Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC) http://plan.mcs.drexel.edu /courses/robotlab/labs/lab 1.ps

Upload: silvia-owens

Post on 13-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

Lab 1

The Handy Board and Interactive-C (IC)

http://plan.mcs.drexel.edu/courses/robotlab/labs/lab1.ps

Page 2: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

Purpose

Introduce the Handy Board micro-controller

Introduce IC programming languageWrite simple control program in IC

Page 3: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

Handy BoardMotorola 6811 microprocessor, 2

MHz. 32K of static RAM. ability to drive 4 DC motors. 16 x 2 character LCD screen. 2 programmable buttons, one

knob, and piezo beeper.inputs for 7 analog sensors and

9 digital sensors.9.6v nicad battery with

recharging circuit.8-pin serial connectorPlus more …

(copyright Prentice Hall 2001)

Page 4: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

Attaching Motors and Sensors

(copyright Prentice Hall 2001)

Page 5: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

Connecting PC to HB

Page 6: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

Serial Line Circuit HB communicates with host

computer over RS-232 serial line

Serial Interface/Battery Charger board performs voltage conversion

Connecting Handy Board

(copyright Prentice Hall 2001)

Page 7: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

Interactive-CVery similar to C (subset)

void main(void) { printf("Press start!\n"); start_press(); while (!stop_button()) { if (analog(0) > analog(1)) { motor(1, 0); motor(0, 50); } else { motor(0, 0); motor(1, 50); } } ao();/* end main */}What does this program do?

Page 8: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

Brief Introduction to Programming in Interactive-C Interactively

– C> 1+2;– C> {start_press(); motor(0,50); stop_press();

ao();}

Downloading- write a function – “main” is the default- Download to Handy Board- Execute main()- Main() automatically executed on power up

- (unless you press “start” or “stop” (re-load pcode) during power up)

Page 9: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

Interactive C Features Interactive experimentation Compiled into P-code (pseudo-code) P-code interpreted by run-time machine

language program on Handy Board Built-in multi-tasking Built-in libraries to access Handy Board

hardware – sensor inputs, motor outputs, etc Additional features: persistent global variables,

built-in math functions, character arrays but no chars, one-dimensional arrays (multi-dimensional in some versions)

Page 10: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

DC Motor Output Commands

void motor(int m, int p)– Turns on the motor in port m– And runs the motor with power level p -- full

forward = 100, full backward = -100 (using pulse-width modulation)

void off(int m)– Turns off motor m.

void alloff(), void ao()– Turns off all motors

Page 11: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

Sensor Input Commands

int digital(int p)– Returns the value of the sensor in sensor

port p, as a true/false value (1 for true and 0 for false).

int analog(int p)– Returns value of sensor port numbered p.

Result is integer between 0 and 255.

Ports are numbered as marked on Handy Board

Page 12: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

Seeking/Avoiding Light

Motor(left_motor, left_light);

Motor(right_motor, right_light);

if (analog(left_light) <= light_thresh) motor(left, minimal_power);

(From Braitenberg)

Page 13: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

Buttons and Knob Inputs

int stop_button()– Returns value of button labeled STOP: 1 if

pressed and 0 if released.

int start_button()– Returns value of button labeled START.

int knob()– Returns the position of a knob as a value

from 0 to 255.

Page 14: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

Example pressing buttons program

void main(void) { printf("Press start!\n"); start_press();

motor(0, 50); stop_press();

ao();/* end main */}

Page 15: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

Example If-else

void main(void) { printf("Press start!\n"); start_press(); if (analog(0) > analog(1)) { motor(0, 50); } else { motor(1, 50); } stop_press(); ao();/* end main */}

Page 16: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

Example While Loop

void main(void) { printf("Press start!\n"); start_press(); while (!stop_button()) {

motor(0,50);}

ao();/* end main */}

Page 17: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

Light demo/* lightdemo.c - [email protected] */void main(void) { printf("Press start!\n"); start_press(); while (!stop_button()) { if (analog(0) > analog(1)) { motor(1, 0); motor(0, 50); } else { motor(0, 0); motor(1, 50); } } ao();/* end main */}

Page 18: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

Miscellaneous Commands

void sleep(float sec)– Waits for an amount of time equal to or slightly

greater than sec seconds. void msleep(long msec)

– Waits for an amount of time equal to or greater than msec milliseconds.

void beep()– Produces a tone of 500 Hertz for a period of 0.3

seconds. Math lib: sin, cos, tan, atan, sqrt, log10, log, exp10, exp,

^ 6811 machine language programs may be called

Page 19: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

Multi-tasking Overview Processes can be created and destroyed dynamically

during run-time. Any C function can be spawned as a separate task. Processes communicate through global variables. Each time a process runs, it executes for a certain

number of ticks. All processes are kept track of in a process table; each

time through the table, each process runs once (for an amount of time equal to its number of ticks).

When a process is created, it is assigned a unique process identification number or pid. This number can be used to kill a process.

Page 20: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

Multi-tasking Commands int start_process( function-call(: :) ,[TICKS],[STACK-

SIZE])– start process returns an integer, which is the process ID

assigned to the new process. int kill_process(int pid)

– kill process returns a value indicating if the operation was successful.

void hog_processor()– Allocates an additional 256 milliseconds of execution to the

currently running process. void defer()

– Makes a process swap out immediately after the function is called.

IC commands– Kill_all -- kills all currently running processes.– Ps -- prints out a list of the process status.

Page 21: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

Programming Tips

32K of RAM includes p-code interpreter and C libraries and program stack

Reduce redundant code by using functions

Comments don’t take up RAMWrite simple behaviors and then focus

on how to coordinate them rather than trying to write one big encompassing behavior

Page 22: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

Next Week Read on-line documents and answer questions

specified at http://plan.mcs.drexel.edu/robotlab/questions/question1.ps

Teams will be determined based on next week’s attendance

Lab 2: Structures and Drivetrains Lab fee ($50) and deposit ($50) due: Bring check

payable to “Drexel University” for $100. In memo field put “CS 280/680 Lab Fee/Deposit”

Sign up for class mailing list:– Send mail to [email protected] from your

desired email account, – with no subject and – the body “subscribe robotlab”

Page 23: Robot Building Lab: Introduction Lab 1 The Handy Board and Interactive-C (IC)  es/robotlab/labs/lab1.ps

Robot Building Lab: Introduction

Lab 1: The Handy Board and Interactive-C (IC)

Files:– http://plan.mcs.drexel.edu/courses/robotlab/labs/lab1.

pdf– http://plan.mcs.drexel.edu/projects/legorobots/

handyboard.html– http://plan.mcs.drexel.edu/courses/robotlab/readings/h

bmanual.pdf Lab overview:

– Connect HB to computer– Download IC, Start IC, experiment with command line

programming– Write and download programs to HB

• Print to LCD screen, using buttons, knob and loops to vary output• Control motor using knobs and buttons• Write motor experimentation program, parameterized by port,

allowing the user to run a motor at differing speeds/directions for differing durations

– Test program untethered