ROBOTICS SUMMER CAMP Aug. 16-20, 2010 M-F, 1:00...

32
Electrical and Computer Engineering Department Kettering University IME-100 ECE Lab 5 5-1 G. Tewolde, IME100-ECE, 2017

Transcript of ROBOTICS SUMMER CAMP Aug. 16-20, 2010 M-F, 1:00...

Electrical and Computer Engineering Department

Kettering University

IME-100

ECE

Lab 5

5-1 G. Tewolde, IME100-ECE, 2017

Getting Started 1. Laboratory Computers

i. Log-in with User Name: Kettering Student (no password

required)

ii. IME-100 information (Lab presentation, files, etc.) in folder on

desktop

iii. Arduino programming software on desktop

iv. At the end of lab, Logout of computer; arrange keyboard and

mouse

2. Laboratory kit

i. Robotic kit with Arduino, motor shield, ultrasonic sensor, light

sensor, 9V battery

ii. At the end of lab, disconnect battery from robot and Arduino, turn

other instruments power off, unplug USB cable from PC, neatly

arrange kit on bench

5-2 G. Tewolde, IME100-ECE, 2017

IME-100, ECE Lab5

Robotics In this laboratory exercise, you will do the following:

• Robotics – what and why?

• Identify the main components of mobile robot

• Use Arduino motor driver library to drive motors and robot

• Target distance measurement using ultrasonic sensor

• Display data to serial terminal

• Use ultrasonic sensor for obstacle detection

• Exercise 1: Sonic wander bot challenge

• Exercise 2: Target Tracking robot

• Line following robot

• Exercise 3: Line following robot with safety feature

• Final Project Opportunity

5-3 G. Tewolde, IME100-ECE, 2017

Robots – What?

• A machine capable of:

– carrying out series of complex actions automatically

or by remote control

– Aware of its environment

– programmable using computer

Example:

• Assemble cars

• Vacuum floors

• Space exploration

• Surgical operations

• Entertainment

5-4 G. Tewolde, IME100-ECE, 2017

Robots – Why? • Why do we use robots?

– For increased productivity

• Manufacturing plants

– For high precision tasks

• Electronics assembly

• Surgical robotics

• Prosthetics

– For tasks too boring for humans

• Assembly, welding, vacuuming

• The robot works 24/7 without break

• It never gets tired, nor gets distracted

– For tasks too dangerous for humans:

• Exploration - Volcanoes, Nuclear reactors, Landmines, Space, etc.

– For education and entertainment:

5-5 G. Tewolde, IME100-ECE, 2017

Industrial robots in a car factory

Google’s self driving car Mars rover Curiosity

da Vinci surgical robot

Application Examples

Warehouse robots

High way platooning

5-6 G. Tewolde, IME100-ECE, 2017

What are robots made of?

• Simple example – Arduino based robot platform

- Processor or control module - Brain

- Power supply (battery)

- Mechanical and structural parts

- External interface

- Input ports

- Output ports

- Programming and debugging

- Actuators:

- Motors, hydraulics, pistons, grippers

- Sensors

- ‘See’ what is in the surroundings

5-7 G. Tewolde, IME100-ECE, 2017

Why Motor driver board? Arduino does not provide

sufficient current to directly

drive typical DC or stepper

motors

The motor driver circuit board

accepts the low power signals

from the Arduino and converts

them to levels appropriate for

driving motors

We can control:

• Direction of rotation

• Speed or rotation (using PWM)

Motor Driver Board

TB6612 motor driver

Motor

Power

Supply

(Battery)

MotorB

(Left)

MotorA

(Right)

5-8 G. Tewolde, IME100-ECE, 2017

• The instructor will assist you

in installing the motor driver

board (shield) onto your

Arduino board.

• Connect the wires coming

from the two motors into the

two sets of screw terminals

of the motor shield as

shown in the picture. Then

tighten the screws.

• Connect the positive wire

from the 9V battery to the

positive (+) terminal of the

motor shield’s power screw

terminal. Then tighten the

screw.

Motor Driver Board …

5-9 G. Tewolde, IME100-ECE, 2017

• The next step is to look at the programming aspect of the

motor driver shield.

• A library is a piece of program that you can use to accomplish

a specific task. Here, you will use a motor driver library to

enable to you drive the robot motors.

• The motor driver circuit for each motor needs two Arduino

pins. One of them is used for direction control while the second

is used for speed control.

• When you use the library you need to define the actual two

pins to be used for each of the motors.

• For better understanding of how the library works, open the

example program from the Arduino menu: File -> Examples ->

Motor -> DriveRobotExample

Motor Driver Library

5-10 G. Tewolde, IME100-ECE, 2017

#include <Motor.h>

//initialize left motor attached to

//Arduino pins 2 and 3

//pin 2 is for direction control

//pin 3 is for speed control

Motor motorLeft(2, 3);

//initialize left motor attached to

//Arduino pins 4 and 5

//pin 4 is for direction control

//pin 5 is for speed control

Motor motorRight(4, 5);

void setup()

{

//wait 3 seconds after power on

//before driving robot

delay(3000);

}

void loop()

{

//drive robot forward for 2 seconds

//enter speed in the range 0 .. 100

motorLeft.forward(80);

motorRight.forward(80);

delay(2000);

//drive robot backward for 2 seconds

//enter speed in the range 0 .. 100

motorLeft.backward(80);

motorRight.backward(80);

delay(2000);

//turn robot in clockwise direction for 1 second

motorLeft.forward(50);

motorRight.backward(50);

delay(1000);

//turn robot in counter-clockwise direction for 1 second

motorLeft.backward(50);

motorRight.forward(50);

delay(1000);

//stop robot for 1 second

motorLeft.stop();

motorRight.stop();

delay(1000);

}

DriveRobotExample.pde

5-11 G. Tewolde, IME100-ECE, 2017

• Write a program for your Arduino to drive the robot on a

square path as shown below.

3 ft

3 ft

Is your robot following the square path accurately? Explain why.

5-12 G. Tewolde, IME100-ECE, 2017

Exercise 1:

Basic Navigation

Introduction to Robotic Sensor

• Just like the sensing organs in humans, sensors in a robot collect information about its surroundings

• Sensors are electrical/mechanical/chemical devices that convert environmental attributes to quantitative measurements

• Robots use their sensors to detect and respond to their environment

– Touch sensors

– Ultrasonic sensors

– Light sensors

– Infrared sensors

– Wheel encoders

– Speed sensors

– GPS

• To navigate in an obstacle course, for example, the robot needs to use its sensors

5-13 G. Tewolde, IME100-ECE, 2017

• SPA is a robot control procedure with three critical capabilities for

effective operation:

• SENSE: ability to sense important things about its environment, like

the presence of obstacles or navigation aids.

What information does your robot need about its surroundings, and

how will it gather that information?

• PLAN: take the sensed data and figure out how to respond

appropriately to it, based on a pre-existing strategy.

Do you have a strategy? Does your program determine the

appropriate response, based on that strategy and the sensed data?

• ACT: carry out the actions that the plan calls for.

Have you built your robot so that it can do what it needs to,

physically? Does it actually do it when told?

Sense-plan-act algorithm (SPA)

5-14 G. Tewolde, IME100-ECE, 2017

• Ultrasonic sensor measures distances using sound, the same

principle that bats and submarines use to find their way around.

– The ultrasonic sensor then sends information to the robot about the

distance to the nearest object in front of the sensor.

– The wiring between the Arduino and the ultrasonic sensor needs one

wire as an output from the Arduino for sending trigger pulses, and a

second wire as an input to the Arduino for capturing echoes.

– The sensor also needs power (VCC and GND).

Ultrasonic Sensor Basic Operation

Arduino

5-15 G. Tewolde, IME100-ECE, 2017

• The motor driver shield provides female

headers for installing the ultrasonic sensors

in. It is labeled as JP4-SONAR on the board.

• The VCC and GND side are indicated on the

board.

• The sensor should be installed facing in the

forward direction of the robot.

Connecting Ultrasonic Sensor

Insert ultrasonic sensor here

5-16 G. Tewolde, IME100-ECE, 2017

Displaying Ultrasonic

Measurements on PC Monitor • An open source library called NewPing is available for

interfacing to the ultasonic sensor.

http://playground.arduino.cc/Code/NewPing

• An example program is available from the Arduino IDE. Open from

File -> Examples -> NewPing -> NewPingExample

• A modified version of the example is given in the next slide. The

changes are:

– The serial monitor baud rate is changed to 9600

– The pin assignment for the ECHO pin of the sensor is changed to 13

– The function sonar.ping_cm() is used to directly read the distance in cm

• Create a new Arduino sketch (program) and copy->paste the code

given in the next slide. Upload the program to your Arduino and

observe the ultrasonic sensor measurements in the Serial monitor

window.

5-17 G. Tewolde, IME100-ECE, 2017

G. Tewolde, IME100-ECE, 2017

Displaying Ultrasonic

Measurements on PC Monitor void loop() {

// Wait 50ms between pings (about 20

// pings/sec). 29ms should be the shortest

// delay between pings.

delay(50);

//unsigned int uS = sonar.ping();

//Send ping, get ping distance in cm or 0 if

//no ping echo within set maximum distance

dist = sonar.ping_cm();

//output measured distance

Serial.print("Distance: ");

Serial.print(dist);

Serial.println("cm");

}

// Example NewPing library sketch that

// does a ping about 20 times per second.

#include <NewPing.h>

#define TRIG_PIN 12

#define ECHO_PIN 13

// Maximum distance we want to ping for

//(in centimeters). Maximum sensor

//distance is rated at 400-500cm.

#define MAX_DIST 400

//variable for storing measured distance

unsigned int dist;

// NewPing setup of pins and max distance.

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DIST);

void setup() {

// Open serial monitor

// at the default 9600 baud to see ping results.

Serial.begin(9600);

} 5-18

Exercise 2:

Sonic Wander Bot Program your robot to wander around on the floor. That is, it moves straight ahead as long as it does not sense an obstacle in front of it within a distance of 30 cm. When it detects an object within the defined range it should avoid it by turning about 90 degrees to the left and then continue to go straight ahead. Bonus: Instead of always turning to the left, choose your turning direction at random. Hint: Arduino offers a random number generation function, called random(). See the reference at: https://www.arduino.cc/en/Reference/Random

5-19 G. Tewolde, IME100-ECE, 2017

Exercise 2+ (Extra Credit):

Target tracking robot In this exercise, you will modify your Wander Bot program so that the robot tracks a target that it finds within a distance of 60 cm. The movement behavior is as follows:

- If there is no object in front of the robot within 60 cm distance, the robot stands idle.

- When the robot detects an object within 60 cm distance it drives forward until it gets within 30 cm distance to the object. If the object moves further away, the robot should follow it to keep its distance within 30 cm. When it reaches at the 30 cm distance the robot will stop.

5-20 G. Tewolde, IME100-ECE, 2017

• We will use a photoresistor as a light sensor

• A photoresistor’s resistance to electrical current

changes with the intensity of light illumination. It is also

called a light dependent resistance (LDR).

• A photoresistor is made of cadmium sulfide. It

responds to visible light similarly to the human eye.

• Photoresistor resistance could range between about

200 KOhm in the dark and 500 ohm in a brightly lit

room.

• Application: light sensitive switches – e.g. streetlights,

vehicle headlights, garden lights, that automatically

turn on at dusk

Light Sensor Basic Operation

5-21 G. Tewolde, IME100-ECE, 2017

Light Sensor Measuring Brightness

Photoresistor

• Voltage reading is inversely proportional to light levels.

• Analog voltage reading ranges from 0 ~ 5V based on the LDR value

• The green wire is the point you read analog voltage from.

• Let us try to measure the voltage in various illuminations.

• In your experiment, you can use the light sensor provided to you and

mounted in the front of your robot facing towards the floor.

5-22 G. Tewolde, IME100-ECE, 2017

Light Sensor: Hardware configuration

Wire the left, middle and right light

sensors to A0, A1, and A2, respectively.

Connect the power and GND lines to 5V

and GND on the Arduino.

LEDs are used to illuminate the surface

for the light sensors to detect.

LED

photoresistor The black tape around the sensors shields it from

Ambient light interference when it is used for line following

5-23 G. Tewolde, IME100-ECE, 2017

Display the light sensor

measurements #define lightRightPin A0 // Analog 0 #define lightMidPin A1 // Analog 1 #define lightLeftPin A2 // Analog 2 // variables for the light reading: 0 ~ 1023 int lightRight; int lightMid; int lightLeft; void setup() { Serial.begin(9600); pinMode(lightRightPin, INPUT); } void loop() { lightRight = analogRead(lightRightPin); Serial.println(lightRight); delay(1000); }

Modify this code example so that it measure all three light sensors and prints

them on the Serial monitor as shown in the output window.

5-24 G. Tewolde, IME100-ECE, 2017

Line Following Robot

Line following algorithm: 1. Calibrate the light sensors so the program

knows from the sensor readings when the

sensor is looking at a black tape of a

lighter background color

2. Use the three light sensors to detect if any

of the sensors is seeing the line or not.

3. If the middle sensor is on the line, drive

the robot straight

4. If the right sensor is on the line, drive the

robot slightly right and forward to bring the

middle sensor on the line

5. If the left sensor is on the line, drive the

robot slightly left and forward to bring the

middle sensor on the line.

6. Repeat steps 2 to 5

5-25 G. Tewolde, IME100-ECE, 2017

G. Tewolde, IME100-ECE, 2017

void loop() {

//line following algorithm

//read the sensor values

int lightLeft = analogRead(lightLeftPin);

int lightMid = analogRead(lightMidPin);

int lightRight = analogRead(lightRightPin);

//if mid sensor detect black color,

//drive straight forward

if (lightMid < midAvg) {

motorLeft.forward(30);

motorRight.forward(30);

delay(10);

}

//if left sensor detect black color,

//turn left a little bit

if (lightLeft < leftAvg) {

motorLeft.forward(10);

motorRight.forward(30);

delay(10);

}

//if right sensor detect black color,

//turn right a little bit

if (lightRight < rightAvg) {

motorLeft.forward(30);

motorRight.forward(10);

delay(10);

}

}

#include <Motor.h>

//initialize left motor attached to Arduino pins 2 and 3

//pin 2 is for direction control

//pin 3 is for speed control

Motor motorLeft(2, 3);

//initialize left motor attached to Arduino pins 4 and 5

//pin 4 is for direction control

//pin 5 is for speed control

Motor motorRight(4, 5);

#define lightRightPin A0 // Analog 0

#define lightMidPin A1 // Analog 1

#define lightLeftPin A2 // Analog 2

//average value for each of the three photoresistors

//found in calibration mode,

//used to compare with newly read photoresistor

//values to determine the color

int leftAvg;

int midAvg;

int rightAvg;

void setup() {

//Initialize Serial communications

Serial.begin(9600);

//do light sensor calibration for line following

lineFollowingCali();

} 5-26

void lineFollowingCali() {

//Initiallize the maximum and minimum

//values of the sensors

int leftmax = analogRead(lightLeftPin);

int leftmin = analogRead(lightLeftPin);

int midmax = analogRead(lightMidPin);

int midmin = analogRead(lightMidPin);

int rightmax = analogRead(lightRightPin);

int rightmin = analogRead(lightRightPin);

int compare;

//update the maximum and minimum values

//of the sensor readings

for (int i = 0; i < 1023; i++) {

compare = analogRead(lightLeftPin);

if (leftmax < compare) {

leftmax = compare;

}

if (leftmin > compare) {

leftmin = compare;

}

compare = analogRead(lightMidPin);

if (midmax < compare) {

midmax = compare;

}

if (midmin > compare) {

midmin = compare;

}

compare = analogRead(lightRightPin);

if (rightmax < compare) {

rightmax = compare;

}

if (rightmin > compare) {

rightmin = compare;

}

delay(5);

}

//calculate the average value of maximum and

//minimum read value to get the center points

leftAvg = (leftmax + leftmin) / 2;

midAvg = (midmax + midmin) / 2;

rightAvg = (rightmax + rightmin) / 2;

Serial.print(leftAvg);

Serial.print(" ");

Serial.print(midAvg);

Serial.print(" ");

Serial.println(rightAvg);

}

5-27 G. Tewolde, IME100-ECE, 2017

Exercise 3: Line Following Robot with Safety Feature

When the robot is moving with the line-following algorithm, we would like to incorporate a safety feature by using an ultrasonic sensor mounted on the top. So when this safety feature is enabled, the robot should immediately stop whenever it detects an obstacle within 15 cm range in front of it. When the obstacle is removed from the robot’s way, the robot should resume its line-following movement.

5-28 G. Tewolde, IME100-ECE, 2017

Homework: Making Connections and

For the Curious You … Due: One week from today

Making Connections

From the things you interact with every day (at home, in your car, at

school, workplace, shopping, restaurant, bank, stadium, the road, …)

pick one and identify the electrical and electronic components,

sensors, actuators, microcontrollers, robots, program applications, ...

For the Curious You

Research and brainstorm/painstorm with your lab partners and

present a specific application (existing or new) for a robotic like

system. Think outside the box to consider all kinds of robotic systems

(such as mobile, stationary, walking, flying, driving, humanoid). If

you choose existing system or application, discuss innovative ways of

improving it.

Turn in a 2-3 pages report for your answers.

5-29 G. Tewolde, IME100-ECE, 2017

Final Project Opportunity

• For the curious you: You are given an opportunity to come up with a cool innovative project that uses an Arduino microcontroller, one or more sensors, and wireless Bluetooth communication to a Smartphone or Tablet.

– Refer to the online tutorial material to learn how the Bluetooth communication works and how you can use it for your application

• Alternative project: Extend the robotics project you worked on in this week’s lab so that a user will have a wireless communication access to the robot from an Android device. The robot will have remote and autonomous modes of operations. In remote control mode a user will be able to control the robot’s movements using commands from the phone. The robot will support two autonomous drive modes, as chosen by the user. The first auto mode drives the robot around aimlessly while avoiding obstacles on its way. The second auto mode executes the safe line following algorithm.

5-30 G. Tewolde, IME100-ECE, 2017

Finishing Up (and to get full-credit in the lab)

1. Check-out with the instructor

i. Demonstrate your functioning exercises

2. Clean-up at bench – Leave it better than you found it!

i. Detangle cables and arrange Keyboard, Mouse and

Chairs

ii. Logout of computer

iii. Return Equipment (Arduino, Robot kit, sensors, and all

accessories)

3. Leave the check-out sheet with your group names at your

station

5-31 G. Tewolde, IME100-ECE, 2017

Lab 5 Check-Out Sheet (to be left at the bench at the end of lab)

Group Members (please print name clearly):

Instructor (check all that apply):

□ Exercise 1 Demo

□ Exercise 2 Demo

□ Exercise 3 Demo

□ Print-outs of program codes for all three

exercises. Include group member names in

comment at the top of your program file.

□ Computer Logout

□ Return of equipment

Arduino, motor control, ultrasonic sensor,

light sensor, Bluetooth module

Robot kit with motor wires, and battery

USB Cable

Additional Comments:

5-32 G. Tewolde, IME100-ECE, 2017