Lab #3

 

In this lab an ultrasonic sensor is combined with an Arduino.  Ultrasonic sensors are very useful for detecting the distance of objects.  The object should be directly in front of the sensor within a +/-15 deg angle (see the datasheet below for details).  The detection range is typically 2 cm to 500 cm with resolution of 3 mm.  Applications include distance measurement/control and obstacle detection/avoidance in robotics, mechatronics, and automotive applications.

 

Note: For Lab #3 the ultrasonic sensor is now optional in order to reduce the workload/difficulty for some students.  Therefore, everything in this lab is now optional except question 3) of the lab assignment (where you make a robot).  That question has been updated including the lab #3 due date and submission requirements.

 

Ultrasonic sensors

 

The principle of an ultrasonic sensor like sonar is as follows.  A short ultrasonic sound (typically 40 kHz frequency) is emitted by the sensor.  The duration time is then measured for the sound to propagate forward, reflect (i.e. echo) off the object of interest, and return to the sensor.  This process is indicated in the diagram below.

 

 

The following equation then holds between the distance of the sensor to the object, the speed of sound c, and the duration.

 

2*distance = c * duration

distance = c * duration / 2

 

This duration can be accurately measured using an Arduino and the distance calculated.  Note that an echo pulse with a time width equal to the duration is produced by a microcontroller on the ultrasonic sensor board.  The echo pulse is not directly produced by the sensor itself.

 

The datasheet of the HC-SR04 Ultrasonic Range Finder sensor used in this lab is given by

http://www.robotshop.com/media/files/pdf/datasheet-sen026.pdf

 

The following diagram shows how to wire the ultrasonic sensor to the Arduino.

 

 

The Arduino measures the duration in microseconds.  This value (after unit conversion) can be used to calculate the distance. A large duration value (>38,000 us) will be returned by the sensor when the object is out of range (or acoustically absorbent).  Furthermore, if the sensor is measured too frequently (>20 times per second) errors can occur since the echo pulses don’t have enough time to return and be processed.  Careful testing should be performed to ensure the sensors are working properly for a given object type, geometry, and measurement rate.  Note when using more than one ultrasonic sensor together there is the potential for errors due to interactions if they are mounted incorrectly (too close, pointing towards each other, etc.).  The object to be located must also be capable of reflecting sound (i.e. not acoustically absorbent). 

 

 

Programming the Arduino

 

This video covers the main programming part of the lab

http://users.encs.concordia.ca/~bwgordon/arduino_lab3.mp4

 

This video provides some additional example / exercise problems

http://users.encs.concordia.ca/~bwgordon/arduino_lab3_examples.mp4

 

A rar file that contains the source code files for this lesson is provided here

http://users.encs.concordia.ca/~bwgordon/arduino_lab3.rar

 

 

Lab assignment

 

Note that the first two questions are optional, but the third question is to be handed and demonstrated in week #12 (see the Requirements section below).

 

1) Write a function called read_ultrasonic() that returns the distance in cm from the ultrasonic sensor.

 

2) Attach a small arm (e.g. 11 cm foam or cardboard arm) to a servo motor and attach the ultrasonic sensor to the ground facing the arm.  Write a program that slowly adjusts the servo angle in order to achieve a given distance (e.g. 7 cm) from the sensor.  This can be achieved by slowly increasing the servo angle in a while loop and measuring the sensor distance until the desired distance is achieved. 

 

Try improving your program to make it go faster and address the situation where the distance becomes too close (i.e. when you overshoot the destination).

 

3) Use the ideas and solutions in the labs to develop your own robot.  Start with an objective and try to develop a robot and Arduino program to achieve that objective.  The objective is flexible / open and depends on your interests and ideas.  Possible robot types include two link robot arms and mobile robots with sensors to guide the motion of your robot.  Some marks are for originality so try to be original.  See the objectives and requirements section below for more details about the robot.

 

It’s also possible to add more servos and sensors if you want but it’s not necessary.  Here is a list of good vendors below for mechatronics equipment.  Note that Spikenzielabs is near Concordia and Abra Electronics is within driving distance if you don’t want to pay for shipping.  Shipping is fast and not so expensive ($8 to free for >$75 in orders) for Robotshop.

http://users.encs.concordia.ca/~bwgordon/vendor_information.html

 

Note that the bookstore has new stock for the Arduino labs.

 

Objectives and requirements

 

This section provides more details for the objectives and requirements for your robot. 

 

Modification of Servos for Continuous Rotation -- follow this optional section if you want continuous (360+ degrees) servo rotation for wheeled mobile robots, etc.

 

Requirements and Evaluation Criteria for Lab #3 – make sure to check this section.

 

Note that for the purposes of this lab a robot is defined as a collection of actuators, sensors, mechanism, Arduino board, other components, and program that moves to perform some task.  

 

The overall objective of this task is to develop a robot like system that employs two or more servo actuators and one or more sensors (light and/or ultrasonic) in order to sense it’s position / environment and guide its motion with an Arduino program that you develop. This is analogous to the light maximization problem in lab #2 except using two or more servos in a robot like configuration of your choosing.

 

There are three main issues for your group to address:

 

1) Selecting a motion task or objective

 

A motion task / objective should be defined which the sensors can guide / verify.  It should be interesting and somewhat practical but it doesn’t have to be too complex.  It’s best to quickly pick an objective so you can start working on it soon.  Examples include:

 

a) Maximizing light.  In lab #2 you maximized light with respect to one servo.  You would be expected to generalize that for two or more servos in a two link arm, turret, 5-bar, mobile robot, or some other configuration.  You could also try to follow a moving light source after you have solved the stationary problem.  Note that maximizing light is not an end to itself.  It is normally used to find or move closer to an object of interest.

 

b) Robot positioning.  In this case you want your robot to move with some relative motion with respect to the environment.  For example, following an object at some specific distance or moving in some pattern with respect to an object / environment such as a circle.  This can be achieved by adjusting the servos until some specific value of the sensors is achieved (e.g. a certain light level or ultrasonic distance).  Note the relationship between light sensor and distance is not linear so a table / equation relating distance to light level is needed for this type of sensor.

 

2) Developing a robot and program to achieve the task

 

Since you don’t receive many marks for robot quality you should construct your robot quickly using easy to work with materials such as foam, cardboard, glue, tape, wood, etc.  Most of your time should be spent on programming and testing your program.  In general, your program will involve various if statements, loops, equations, etc. that read the sensors and command the servos based on that information.

 

3) Testing, debugging, and optimization of your system

 

Your robot system may not initially be able to perform it’s task perhaps due to hardware problems or program errors.  The robot might also be too slow or inconsistent.  In both cases you should carefully check and improve your system to obtain better performance and reliability.