robotics tutorial fall, 2016 - ryerson university · the controller is based on the arduino...

16
Department of Computer Science Faculty of Science ROBOTICS TUTORIAL Fall, 2016 Office of Science Outreach and Enrichment 350 Victoria Street t: 416.979.5000 ext. 4100 Room: KHE 124 f: 416.979.5330 Toronto, ON, Canada M5B 2K3 [email protected] ryerson.ca/osoe

Upload: others

Post on 31-May-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Department of Computer Science

Faculty of Science

ROBOTICS TUTORIAL

Fall, 2016

Office of Science Outreach and Enrichment 350 Victoria Street t: 416.979.5000 ext. 4100 Room: KHE 124 f: 416.979.5330 Toronto, ON, Canada M5B 2K3 [email protected] ryerson.ca/osoe

Introduction The Office of Science Outreach and Engagement (OSOE) invites high school students for tours of the Ryerson University campus, along with participation in a “hands-on” workshops prepared by departments within the newly created Faculty of Science: Computer Science, Physics, Chemistry, Biology and Mathematics. The following workshop was prepared by the Department of Computer Science to introduce high school students to robotics and programming and to spark an interest in our Bachelor of Science program in computer science. For detailed information about this program, please visit the current Ryerson University calendar. The Workshop This workshop was designed by Jimmy Tran, who is a current PhD candidate here in the Graduate Program for Computer Science at Ryerson University. Jimmy obtained a Bachelor of Science degree in this department, and being intrigued by a course in robotics he decided to do graduate study in the area. He was one of the first graduates of our Master of Science degree program, writing a thesis titled “A Telepresence System for Canine Search in an Urban Search and Rescue Environment”. His PhD thesis continues this research in the area “Canine Assisted Robot Deployment for Urban Search and Rescue” under the guidance of Dr. Alex Ferworn. When Jimmy is not working on his PhD research, he is developing robot kits to support interest in the field. The kit that you will be working with is called Alphi, and is available from Jimmy’s company Curious Circuits. The workshop is led by two undergraduate robotics enthusiasts who will describe Alphi to you and show you how to wire and program it for three basic functions: (1) obstacle avoidance, (2) line following, and (3) light following. Along the way you will learn the difference between digital and analog signals. Can you guess already whether the bumper sensors for obstacle avoidance are digital or analog? How about the line sensors and light sensors which detect lines and light by differences in colour and intensity? You will also see a low-level application of programming in a language very similar to the C, C++ or Java that you are probably currently studying in high school. Best of all, you will have the fun of seeing your robot behave as you dictate through the code. You can modify the code and see the changes in behavior. The following pages are from the User Quick Guide which comes with the Alphi Robot Kit. In the guide you will see the wiring instructions and sample programs for the three functions mentioned above. The workshop leaders will guide you through the steps, as you do the wiring and programming. Have fun!

Curious Circuits Alphi Robot Kit

User Quick Guide

What’s Included in the Kit Robot

Battery and Charger

USB cable

Wires

1 red

9 black

8 yellow

2 Light Sensors

About Curious Circuits Alphi Curious Circuits Alphi is a fun and easy-to-use robot kit for your learning pleasure. The kit includes

easy to assemble hardware for a versatile robot while allowing certain degree of customization. The controller is based on the Arduino platform.

It has electrically driven motors capable of running up to approximately 4 hours when the

accompanying battery is fully recharged.

There are many built in sensors: obstacle detection using bumper switches and line sensing using

infrared diodes. Additional options include ability to mount light detectors.

Programming and uploading may be performed through the Arduino environment. Uploading may be

performed using the built in boot loader and USB cable. Example code for motor and sensor

control provided in this document. Updates may be available at www.curiouscircuits.com.

Overall

Dimensions (length x width x height): 127mm x 127mm x 65mm

Weight (robot with battery, depending on wiring and sensor configuration): 250g-265g

2 wheel differential steering, ball caster design

Microcontroller

Arduino compatible, highly integrated design with Atmega 328 MCU

14 channels digital i/o

6 PWM channels (pins 3, 5, 6, 9, 10, 11)

8 channels 10-bit analog i/o

USB programming interface and ICSP header for direct program download

Supports AREF, integrated sockets for RF module and Bluetooth module

5 I2C interface pin sets

7 key inputs

Serial interface TTL Level

Auto sensing/switching power input

2-way motor driver with 2A maximum current

DC supply: USB powered or external 7V-12V DC

DC Output:5V/3.3V DC

Wheels and Motors

Wheels (diameter x width): 42mm x 19mm

Motor electrical: voltage 6V, no-load current 80mA, rated current 160mA

Motor mechanical: gear ratio 1:30, no-load speed 320rpm, rated torque 300g·cm

Battery

Lithium polymer, protection circuit included

Voltage 3.7V, capacity 1000 mAH, discharge 5C

With provided charger, approximately 1 hour to charge.

Approximately 4 hours operation when new and fully charged.

DC-DC Boost Converter

Input voltage: 3.7V-34V, output voltage: 3.7V-34V (set to 7.4V default by variable resister)

Maximum input current 3A, maximum power 15W, efficiency 90%

Note: The output voltage is set to 7.4V as the recommended operating voltage for Alphi. Input

voltage should be less than output voltage. Therefor a battery pack should output less than 7.4V. It is recommended to be used with the 3.7V LiPo battery provided with the kit.

Support

Software updates may be available at www.curiouscircuits.com.

For assistance, we may be contacted at [email protected].

Project Ideas

Alphi is versatile with many options to be reconfigured for various tasks. Three possibilities, based

on sensors, are included in the kit as part of the quick start.

Obstacle/object avoidance using built-in bumper sensors.

Line following using built-in line sensors. This may also be programmed for use with surface

edge detection.

Light sensing using mountable light sensors. Light sensing may also be used for distant object

detection based on luminosity.

The following sections of this document provides the necessary wiring configuration and sample code

for the 3 ready-made project ideas.

Specifications

V 1.0

Operation Basics

Robot controller.

USB port. See Programming Instructions.

Reset switch. Restarts loaded program if present.

Battery slot. JST type connector plug.

ON-OFF switch. Slide the switch... ...as shown.

Switch needs to be set to

ON position and charged battery connected for robot components, including controller, to operate.

Robot body.

ON

OFF

Recharging the Battery The supplied battery charger is

versatile in the types of lithium battery types that can be used with it.

With the supplied battery, this

JST connector is used for recharging.

Collapsible AC plug at back. Use

90-240V sockets.

When recharging, always first ensure polarity is correct. Check polarity * by pressing the left button. If correct, the CON LED will

be lit green. If not, it will remain dark.

When connected to AC, the PW LED is steady red.

When charging, the CH LED is flashing green.

When charging is complete, FUL LED is steady green.

To * change polarity, toggle the right button by pressing it.

RIGHT

Robot body.

LEFT FRONT

BACK

Programming Instructions

1. If not already available on your workstation, install the Arduino Integrated Development

Environment (IDE) on your workstation. The IDE may be obtained as a download from www.arduino.cc. Open the IDE at your workstation. 2. Connect the robot and computer using a USB cable.

When you connect the robot and computer with a USB cable, you may be notified by the operating system (OS) of your workstation that a new device for a USB serial port is being installed. Accept the installation if prompted. Most modern OS already have a driver for a USB serial port. A USB serial port is required to upload programs into the robot.

4. With the USB serial port number identified, navigate to the Tools/Serial Port in the Arduino IDE. Select the serial port that was noted in the

previous step. In the example, it is COM20. This lets the IDE know where to send the program once complied.

3. Discover the COM port that

the OS assigned to the USB serial port. At your workstation, navigate to the Device Manager

(or equivalent depending on the OS). Open the Ports section. Look for USB Serial Port. Note the COM port number beside it. In the example screenshot below, it is COM20.

5. Select the appropriate controller board from the available list in the IDE. Navigate

to Tools/Boards. Select Arduino Duermilanove w/ATmega328. This lets the IDE know how to complete the program.

6. You may write your program in the IDE. Sample code is provided in subsequent sections of this document following each wiring configuration for sensor type.

7. When ready, compile and upload the program. This may be performed in a single step by clicking this action button

(or equivalent depending on version of the IDE). Compile and upload functionality is also available through the menu.

V 1.0

Wiring Configuration for Bumper Sensors

Bumper sensors are used to detect collision with obstacles, operating on contact like light switches.

To enable the Robot’s bumper sensors so that they may be addressable when programming, ensure the following wiring between sensors and controller is present. Bumper sensor provides a binary signal, thus connected to the controller’s digital input ports rather than analogue.

Bumper sensors assemblies, each consisting of 1 bumper and 2 sensors.

There are 14 digital input ports available, D0 to D13. D0-D1 are doubled for serial transmission and reception. D4-D7 are reserved for motor control. Any remaining digital ports may be used for other

purposes such with bumper sensors. D2-D3 and D8-D13 are available. Sample code uses D8-D11 for the 4 bumper sensors.

For ease of programming and debugging, it is suggested the order of bumper sensors and D input ports correspond left to right.

Any GND pins on the controller may be used. It is suggested that those directly in line with corresponding D input ports be used as pairs for organization.

On the bumper sensor, any of the 2 pins may be used for either D input or GND. The connections in the diagram are examples only.

Eight of the supplied black

wires are suggested for connection between bumper sensors and controller for both GND and D input.

Robot controller.

Pins for digital input. GND row is ground.

D is digital input port. VCC is unused for bumper sensors.

Sample Code for Obstacle Avoidance using Bumper Sensors

//This is an example sketch that test motor and bumper sensor functions.

//Robot Wiring:

//D8 <-> Far Right Bumper

//D9 <-> Middle Right Bumper

//D10 <-> Middle Left Bumper

//D11 <-> Far Left Bumper

int E1 = 5; //M1 Speed Control

int E2 = 6; //M2 Speed Control

int M1 = 4; //M1 Direction Control

int M2 = 7; //M1 Direction Control

char bumper0, bumper1, bumper2, bumper3;

//Function that controls the left and right motors:

//-255-Full Reverse; 0-Stop; 255 Full Forward

void motor(int leftSpeed, int rightSpeed) {

char lSpeed, rSpeed;

char lDir, rDir;

if (leftSpeed < 0) {

lSpeed = (char) -leftSpeed;

lDir = LOW;

}

else {

lSpeed = (char) leftSpeed;

lDir = HIGH;

}

if (rightSpeed < 0) {

rSpeed = (char) -rightSpeed;

rDir = LOW;

}

else {

rSpeed = (char) rightSpeed;

rDir = HIGH;

}

analogWrite (E1,lSpeed); //PWM Speed Control.

digitalWrite(M1,lDir);

analogWrite (E2,rSpeed);

digitalWrite(M2,rDir);

}

void setup(void)

{

int i;

for(i=4;i<=7;i++) {

pinMode(i, OUTPUT); //Set motor pin as output.

}

motor(0,0); //Stop both motors.

//Flash LED on Romeoboard to indicate start of program.

pinMode(13, OUTPUT);

digitalWrite(13, HIGH);

delay(500);

digitalWrite(13, LOW);

delay(500);

for (i=8;i<=11;i++) {

pinMode(i, INPUT);// Set bumper pin as input.

digitalWrite(i, HIGH); //Set pull-up resistor on bumper pin.

}

//Read in the states of the bumper sensor.

bumper0 = digitalRead(8);

bumper1 = digitalRead(9);

bumper2 = digitalRead(10);

bumper3 = digitalRead(11);

//Wait until one of the bumper sensor is hit before beginning the loop.

while (bumper0 == HIGH && bumper1 == HIGH && bumper2 == HIGH && bumper3 == HIGH) {

bumper0 = digitalRead(8);

bumper1 = digitalRead(9);

bumper2 = digitalRead(10);

bumper3 = digitalRead(11);

}

}

//Infinite loop

void loop(void)

{

int motorSpeedLeft = 100;

int motorSpeedRight = 100;

int reverseDelay = 100;

int delayTime = 400;

//Read in bumper sensor state.

bumper0 = digitalRead(8);

bumper1 = digitalRead(9);

bumper2 = digitalRead(10);

bumper3 = digitalRead(11);

if (bumper0 == LOW) { //Far right bumper is hit, turn right.

motor(motorSpeedLeft, -motorSpeedRight);

delay(delayTime);

}

else if (bumper1 == LOW) { //Middle right bumper is hit, go forward.

motor(motorSpeedLeft, motorSpeedRight);

delay(delayTime);

}

else if (bumper2 == LOW) { //Middle left bumper is hit, go backward.

motor(-motorSpeedLeft, -motorSpeedRight);

delay(delayTime);

}

else if (bumper3 == LOW) { //Far left bumper is hit, turn left.

motor(-motorSpeedLeft, motorSpeedRight);

delay(delayTime);

}

else { //No bumper is hit, stop both motor.

motor(0, 0);

}

}

V 1.0

Wiring Configuration for Line Sensors

Located at the bottom of the robot, line sensors are used to detect lines by differences in colour.

To enable the Robot’s line sensors so that they may be addressable when programming, ensure the following wiring between sensors and controller is present. Line sensor provides an analog signal, thus connected to the controller’s analogue input ports rather than digital.

Vcc (5V), GND and output from line sensors.

Eight analogue input ports available, A0 to A7. Note order on the controller is 6, 7, 0, 1, 2, 3, 4, 5 left to right viewed in

orientation with alphanumeric

characters true side up. There are 8 line sensors on the line sensor module. Thus, up to all 8 may be used with available A input ports. However, 2 light sensors

supplied in the kit also requires analogue ports. If both light sensors are used, up to 6 line sensors may be used. If that is the case, it is suggest A0-A5 be used for line sensors.

For ease of programming and

debugging, it is suggested the order of line sensors and A input ports correspond left to right. Any ground pin on the controller may be used. Any Vcc (5V) pin may be used.

Choice of how many and which sensors depends on application. Drawing shows 6 middle ones

used. Sample code addreses 2 but

not specifically which physical 2. Eight of the supplied yellow wires are suggested for connection between line sensors and analogue input ports on the controller. A red

wire is suggested for Vcc (5V). A black wire is suggested for GND.

Robot controller.

Pins for analogue input. A row is analogue input port. GND is ground. 5V is required as reference.

L R

L

R

Sample Code for Line Following using Line Sensors

//This is an example sketch of simple line.

//following behaviour using only 2 line sensor.

//Robot Wiring:

//A7 <-> Line Sensor 8

//A0 <-> Line Sensor 1

//GND <-> GND

//VCC <-> VCC/5V

int E1 = 5; //M1 Speed Control

int E2 = 6; //M2 Speed Control

int M1 = 4; //M1 Direction Control

int M2 = 7; //M1 Direction Control

//Function that controls the left and right motors:

//-255-Full Reverse; 0-Stop; 255 Full Forward

void motor(int leftSpeed, int rightSpeed)

{

char lSpeed, rSpeed;

char lDir, rDir;

if (leftSpeed < 0) {

lSpeed = (char) -leftSpeed;

lDir = LOW;

}

else {

lSpeed = (char) leftSpeed;

lDir = HIGH;

}

if (rightSpeed < 0) {

rSpeed = (char) -rightSpeed;

rDir = LOW;

}

else {

rSpeed = (char) rightSpeed;

rDir = HIGH;

}

analogWrite (E1,lSpeed); //PWM Speed Control.

digitalWrite(M1,lDir);

analogWrite (E2,rSpeed);

digitalWrite(M2,rDir);

}

char bumper0, bumper1, bumper2, bumper3;

void setup(void)

{

int i;

for(i=4;i<=7;i++) {

pinMode(i, OUTPUT); //Set motor pin as output.

}

motor(0,0); //Stop both motors.

//Flash LED on Romeoboard to indicate start of program.

pinMode(13, OUTPUT);

digitalWrite(13, HIGH);

delay(500);

digitalWrite(13, LOW);

delay(500);

for (i=8;i<=11;i++) {

pinMode(i, INPUT);// Set bumper pin as input.

digitalWrite(i, HIGH); //Set pull-up resistor on bumper pin.

}

//Read in the states of the bumper sensor.

bumper0 = digitalRead(8);

bumper1 = digitalRead(9);

bumper2 = digitalRead(10);

bumper3 = digitalRead(11);

//Wait until one of the bumper sensor is hit before beginning the loop.

while (bumper0 == HIGH && bumper1 == HIGH && bumper2 == HIGH && bumper3 == HIGH) {

bumper0 = digitalRead(8);

bumper1 = digitalRead(9);

bumper2 = digitalRead(10);

bumper3 = digitalRead(11);

}

}

//Infinite Loop

void loop(void)

{

//Offset the left and right motor speed so that the robot drive forward straight.

This may very from robot to robot.

int motorSpeedLeft = 65;

int motorSpeedRight = 58;

int turnDelay = 50;

//Read in the analog input

int lineLeft = analogRead(7);

int lineRight = analogRead(0);

if (lineLeft > 500) { //Line is found on the left.

motor(-motorSpeedLeft, motorSpeedRight); //Turn left slightly.

delay(turnDelay);

}

else if (lineRight >500) { //Line is found on the right.

motor(motorSpeedLeft, -motorSpeedRight); //Turn right slightly.

delay(turnDelay);

}

else { //No line found, go forward.

motor(motorSpeedLeft, motorSpeedRight);

}

}

V 1.0

Wiring Configuration for Light Sensors

Mounted at sides of the robot above the main board, light sensors are used to detect colour diffe.

To enable the Robot’s light sensors so that they may be addressable when programming, ensure the following wiring between sensors and controller is present. Light sensor provides an analog signal, thus connected to the controller’s analogue input ports rather than digital.

Light sensors. Mounted on clips (shown outside of clips for clarity).

Eight analogue input ports

available, A0 to A7. Note order on the controller is 6, 7, 0, 1, 2, 3, 4, 5 left to right viewed in orientation with alphanumeric characters true side up.

If both light sensors are used, up to 6 line sensors may be used. If that is the case, it is suggest A6-A7 be used for light sensors. For ease of programming and

debugging, it is suggested the order of light sensors and A

input ports correspond left to right. Any ground pin on the controller may be used. Any Vcc (5V) pin may be used.

Robot controller.

Pins for analogue input. A row is analogue input port. GND is ground. 5V is required as reference.

Each supplied light sensor has all necessary wires

integrated with the sensor itself. The yellow wire must be connected to one of the A input ports such as A6 or A7. The red wire must be connected to any one of the 5V pins. The

black wire must be connected to any one of the GND pins.

Clip for light sensor.

Clip for light sensor.

Sample Code for Light Following using Light Sensors

//This is an example sketch of a simple light following behavior.

//Robot Wiring:

//A7 <-> Left Light Sensor

//A6 <-> Right Light Sensor

//GND <-> GND

//VCC <-> VCC/5V

int E1 = 5; //M1 Speed Control

int E2 = 6; //M2 Speed Control

int M1 = 4; //M1 Direction Control

int M2 = 7; //M1 Direction Control

//Function that controls the left and right motors:

//-255-Full Reverse; 0-Stop; 255 Full Forward

void motor(int leftSpeed, int rightSpeed) {

char lSpeed, rSpeed;

char lDir, rDir;

if (leftSpeed < 0) {

lSpeed = (char) -leftSpeed;

lDir = LOW;

}

else {

lSpeed = (char) leftSpeed;

lDir = HIGH;

}

if (rightSpeed < 0) {

rSpeed = (char) -rightSpeed;

rDir = LOW;

}

else {

rSpeed = (char) rightSpeed;

rDir = HIGH;

}

analogWrite (E1,lSpeed); //PWM Speed Control.

digitalWrite(M1,lDir);

analogWrite (E2,rSpeed);

digitalWrite(M2,rDir);

}

char bumper0, bumper1, bumper2, bumper3;

void setup(void)

{

int i;

for(i=4;i<=7;i++) {

pinMode(i, OUTPUT); //Set motor pin as output.

}

motor(0,0); //Stop both motors.

//Flash LED on Romeoboard to indicate start of program.

pinMode(13, OUTPUT);

digitalWrite(13, HIGH);

delay(500);

digitalWrite(13, LOW);

delay(500);

for (i=8;i<=11;i++) {

pinMode(i, INPUT);// Set bumper pin as input.

digitalWrite(i, HIGH); //Set pull-up resistor on bumper pin.

}

//Read in the states of the bumper sensor.

bumper0 = digitalRead(8);

bumper1 = digitalRead(9);

bumper2 = digitalRead(10);

bumper3 = digitalRead(11);

//Wait until one of the bumper sensor is hit before beginning the loop.

while (bumper0 == HIGH && bumper1 == HIGH && bumper2 == HIGH && bumper3 == HIGH) {

bumper0 = digitalRead(8);

bumper1 = digitalRead(9);

bumper2 = digitalRead(10);

bumper3 = digitalRead(11);

}

}

//Infinite Loop

void loop(void)

{

//Offset the left and right motor speed so that the robot drive forward straight.

This may vary from robot to robot.

int motorSpeedLeft = 65;

int motorSpeedRight = 58;

//Read in the analog light input, higher value = less light.

int lightSensorLeft = analogRead(7);

int lightSensorRight = analogRead(6);

//Calculate the difference between the left and right light value.

int lightDifference = lightSensorRight-lightSensorLeft;

//normalize the lightDifference value

lightDifference = lightDifference/5;

if (lightSensorRight >= lightSensorLeft) {

//lightDifference is positive. Subtract it from motorSpeedLeft to turn left.

motor(motorSpeedLeft-lightDifference, motorSpeedRight);

}

else if (lightSensorLeft >= lightSensorRight) {

//lightDifference is negative. Add it to motorSpeedRight to turn right.

motor(motorSpeedLeft, motorSpeedRight+lightDifference);

}

else {

//Light Sensors are balanced. Go Forward.

motor(motorSpeedLeft, motorSpeedRight);

}

}