This is only a preview of the September 2020 issue of Practical Electronics. You can view 0 of the 72 pages in the full issue. Articles in this series:
|
Make it with Micromite
Phil Boyce – hands on with the mighty PIC-powered, BASIC microcontroller
Part 20: Ultrasonic obstacle avoidance
Fig.39. By adding an ultrasonic distance module, the MRB can be programmed to avoid crashing into objects.
W
e now come to the last
part of our Micromite Robot
Buggy (MRB) mini series. The
objective this month is to enable it to
roam autonomously without crashing
into anything, which means it will need
to have some form of obstacle avoidance
mechanism. So, this month we are going
to show you how to implement this
feature by using a low-cost ultrasonic
distance sensor mounted on the front
of the MRB (see Fig.39). This will
require us to make a simple hardware
modification to the TFT module that we
originally assembled back in Part 15.
For our MRB program code to interface
with the ultrasonic distance sensor,
we will use the Micromite DISTANCE
command. As usual, MMBASIC greatly
48
simplifies everything for us; in this case,
the DISTANCE command communicates
directly with the ultrasonic sensor,
returning a numeric value that represents
the distance (in centimetres (cm)) to an
object positioned in front of it.
We will start this month by working
through the hardware modification to
the TFT module, and then we will test
that it works correctly by writing a fiveline demo program that incorporates
the DISTANCE command. This demo
program will perform the function of a
basic, yet highly accurate Digital Tape
Measure – a great little project in its
own right. We will then take this demo
program and modify it slightly so that
it forms the basis of our MRB obstacle
avoidance code.
Concept of obstacle avoidance
The ultrasonic (US) distance sensor
hardware, along with the MMBASIC
DISTANCE software command, enables
our program code to measure the
distance (in cm) to any object that is
in front of the MRB. By continually
measuring the distance value while
the robot is moving forward, the
MRB program code can be made to
automatically ‘apply the brakes’ should
the distance value fall below a certain
threshold value.
Micromite code
The code in this article is available
for download from the PE website.
Practical Electronics | September | 2020
Cryst al
VCC
TRIG
ECH O
G ND
H C -S R 0 4
T
R
U ltrasonic
transmitter
U ltrasonic
receive r
5V
17
Vcc
Trig (Input pin)
G ND
Echo (O utput pin)
0V
18
Fig.40. The low-cost Ultrasonic distance
module used in the MRB has just four
pins. Two pins supply 5V power, and
the other two pins connect directly to
Micromite I/O pins.
For example, consider setting a
threshold value in the MRB code to
10cm. Whenever the distance measured
is greater than this 10cm threshold, then
the MRB can be allowed to continue
to move forward without the risk of
hitting anything. However, as soon as
the measured distance falls below the
10cm threshold, then the program code
can be written to automatically stop the
MRB from moving forward, and hence
prevent it from crashing into an object.
This distance-measuring method
of obstacle avoidance is much better
than implementing some fancy form
of front-mounted mechanical ‘bumperswitch’ (as used in some other mobile
robots). Implementing the bumperswitch method means that the robot
has to actually crash into an object
first in order to apply the brakes. For
this reason, we consider the bumperswitch method as collision detection
rather than obstacle avoidance.
Fig.41. A small piece of
stripboard acts as a miniriser board. This allows
the US module to have an
unobstructed view over the
animated LED eyes.
1 2 3 4
A
B
C
D
E
F
G
H
I
J
The HC-SR04 module
The US distance sensor module that we
are using here is the readily available
HC-SR04. It comprises a US transmitter,
and a US receiver, along with just a few
support components. It has four pins
presented on a 0.1-inch header (see
Fig.40). Two pins supply the required
5V power, and there are two control
pins. The control pin marked ‘TRIG’ is
an input trigger, and the ‘ECHO’ control
pin is an output. The module operates
as follows: when a pulse is sent to the
TRIG input pin from an external device
(in this case from the Micromite) the US
transmitter sends out a short pulse of
US waves. The US receiver then waits
for a short period of time to see if it can
detect these US waves being reflected
(ie, bounced) off an object directly in
front of it. If it sees a reflection, then
the ECHO output pin sends out a pulse
to an external device (the Micromite).
Practical Electronics | September | 2020
JS1
JP1
A
B
C
D
E
F
G
H
I
J
1 2 3 4
J
I
H
G
F
E
D
C
B
A
J
I
H
G
F
E
D
C
B
A
1 2 3 4
The time between the transmission
of the US wave and the reception of
any reflected waves (the time between
sending a TRIG pulse and receiving the
ECHO pulse) will infer the distance to
the object – much like the way radar
operates using electromagnetic waves.
The Micromite performs all of the timing
calculations from within firmware
(whenever the DISTANCE command
is used) and conveniently returns a
calculated distance as a numeric value
– more on this later.
Circuit diagram
The circuit connections for adding
obstacle avoidance to the MRB is also
shown in Fig.40. As always, it is just
a matter of connecting the external
hardware (ie, the US distance sensor) to
the relevant power pins and Micromite
I/O pins. It really could not be any
simpler as there are only four physical
connections required. Out of the four
connections, only two are required
to be connected to I/O pins on the
Micromite. Checking which Micromite
49
pins are still available for use on the MRB, we only have
pins 17 and 18 that are currently unassigned to anything
– therefore we will use these two I/O pins. Hence the four
connections that need to be made are:
Vcc connected to 5V (the 5V pin is physically to the right
of Micromite pin 15)
TRIG connected to Micromite pin 17
ECHO connected to Micromite pin 18
GND connected to 0V (which conveniently is on Micromite
pin 19).
All four of the US distance sensor module pins can be
brought together into a 4-way header on the TFT module;
however, we will need to make a slight modification to
the TFT stripboard first. We must also ensure that the US
distance module is physically mounted in such a way that
it can face forwards on our MRB without being impeded by
the animated eyes (if installed). This we can do by adding
a new mini-riser board (see Fig.41).
Modifications to the TFT module
The assembly of the TFT module was discussed in Part 15
(PE, April 2020). Referring back to Fig.5 from that article,
you can see that the Micromite pins that are required for the
US module (pins 17, 18, and 19) were not included in the
header (J2) that ultimately plugs into the MKC. Therefore, the
main modification to the TFT module requires the addition
of these three downward-facing pins into J2. The stripboard
layout from Part 15 is recreated here in Fig.42, and it has
been updated to show the required US modifications in red.
As can be seen, in addition to the three pins for J2, there
are seven track cuts, four wire links to insert, and also the
soldering of a new mini-riser board via a 4-way right-angle
header pin (see Fig.41).
Begin by making the seven track cuts on the MRB TFT
module; ensure you follow the usual practice of marking
out first, then checking the positions at least twice. Once
the cuts are made, do a quick visual check to ensure that
no copper swarf is bridging across neighbouring tracks.
Next, insert the three downward-facing pins into the
plastic housing of J2 into positions V15, V16 and V17. These
three new pins will ultimately be inserted into the MRB
daughterboard, so do check that their length matches the
existing downward-facing pins that are on the TFT module.
Next, install the four jumper links, as shown in Fig.42;
again, make sure you check for and remove any accidental
solder bridges with neighbouring tracks.
Mini-riser board
The mini-riser board is built from a small piece of stripboard
4-tracks wide, by 10-holes long (high), as shown in Fig.41. To
this we need to solder a 4-way socket (that the US distance
module plugs into), and a 4-way right-angle pin header (to
solder the mini-riser board directly to the TFT module).
Note that there are no track cuts or wire links needed on
the mini-riser board.
Begin by cutting out the required size stripboard and
solder the 4-way socket and the 4-way right-angle connector
as shown in the various photos in Fig.41. Perform a visual
check and ensure you remove any solder bridges between
neighbouring tracks. Last, carefully solder the mini-riser
board to the TFT module into positions A11-A14 (highlighted
in red in Fig.42).
It is now time to mount the modified TFT module (complete
with the new mini-riser board) onto the MRB daughterboard.
Insert the US distance sensor into the 4-way socket, ensuring
the US transmitter and receiver units are facing forwards.
Plug in all relevant modules so that the MRB can be powered
50
up as usual. We will now load a simple test program to check
that the US distance sensor is working correctly.
Testing the US distance sensor
Begin by downloading the file MRB_DigitalTapeMeasure.txt,
and then install it onto your MRB in the usual manner. On
running the program, you should see the TFT screen clear
to a blue screen with a thin green border. In the centre of
the screen there should be a number (displayed in white).
This number represents the distance being measured by
the US sensor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
AA
BB
CC
DD
EE
FF
GG
HH
II
JJ
KK
LS1
J1
J2
J3
R1
J4
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
AA
BB
CC
DD
EE
FF
GG
HH
II
JJ
KK
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
KK
JJ
II
HH
GG
FF
EE
DD
CC
BB
AA
Z
Y
X
W
V
U
T
S
R
Q
P
O
N
M
L
K
J
I
H
G
F
E
D
C
B
A
KK
JJ
II
HH
GG
FF
EE
DD
CC
BB
AA
Z
Y
X
W
V
U
T
S
R
Q
P
O
N
M
L
K
J
I
H
G
F
E
D
C
B
A
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Fig.42. The original MRB TFT module from Part 15. The required
US modifications are highlighted in red.
Practical Electronics | September | 2020
Fig.43. Side view of the completed Micromite Robot Buggy.
If the number displayed is −2, then
the Micromite is not seeing the US
sensor correctly. If this happens, first
check the US sensor is inserted correctly
into the 4-way socket. If this is fine,
then you will need to carefully check
that all modifications made to the TFT
module match Fig.42.
Once you observe a number on the
screen other than −2, then position your
hand in front of the US module so that
the US waves can bounce off it (or use
a book). Now move your hand back and
forth in front of the US sensor and you
should see that the number displayed
on the TFT changes – increasing in
value as you move your hand away
from the sensor, and reducing in value
as you move your hand towards the
sensor. If this appears to work, then
point your robot towards a nearby
wall and you should see the number
remain reasonably static. Now use a
tape measure to measure the actual
distance between the US sensor and the
wall – you should find the displayed
number on the TFT is within a couple
of centimetres of what you measured.
If not, then you may need to slightly
alter the angle of the US sensor so that
it is pointing at right-angles to the wall.
Practical Electronics | September | 2020
If all is working as described above,
point the MRB towards various other flat
objects and check the accuracy of the
distance shown on the TFT. Note that any
object made from a soft material (such
as a cushion or an item of clothing) may
not be able to reflect the US waves and
hence will not be detected; therefore,
ensure you use relatively solid objects
for this test, and you should find that
your MRB is now operating as an accurate
digital tape measure.
The DISTANCE command
Press C t r l _ C to stop the program
running, and type EDIT so that you
can take a look at the code. The Main
Program comprises of just five lines of
code, as follows:
DO
d=DISTANCE(17,18)
TEXT ……….,…STR$(INT(d))…, ……
PAUSE 100
LOOP
As you saw when you ran the
DigitalTapeMeasure program, the
above code continually displays the
measured distance on the TFT screen.
The code is just a simple DO…LOOP
with the magic happening in the line:
d=DISTANCE(17,18). The format of the
DISTANCE command requires two values
to be passed – these are the pin numbers
that connect to the TRIG pin and the
ECHO pin respectively. The MMBASIC
Firmware will then automatically send
a pulse to the TRIG pin (output), and
will wait for a reflection pulse on the
ECHO pin (input). The time taken to see
any reflected pulse is then converted
into a numeric value that represents
the true distance (in cm).
In the above code, this numeric value
is loaded into variable d. Note that the
DISTANCE command automatically
configures the two Micromite pins to
the relevant I/O state – in other words,
there is no need to use the SETPIN
command to configure pins 17 and 18.
The second line of code in the DO…
LOOP uses the TEXT, STR$(…) and INT
commands to display the integer value
of d on the TFT (effectively the integer
value drops any decimal places making
an easier-to-read display).
Finally, the P A U S E command is
included in the DO…LOOP to prevent
the TRIG pulse from being generated
too soon after the previous measurement
was triggered.
51
www.poscope.com/epe
Scrolling further down the code into
the subroutines section, you will see
that there are quite a few lines of code,
each containing many numbers. These
numbers are part of what is known as a
‘CFunction’. This topic is well beyond
the scope of this series; however, in
essence, they contain the code that
adds the DISTANCE command into
MMBASIC. For more information, refer
to the Distance.pdf file (available for
download from the September 2020
page of the PE website)
So, in summary, to measure the
distance of an object from the sensor, and
to then load the result into a variable,
you simply use the command syntax:
variable = DISTANCE(TRIG_pin_
number, ECHO_pin_number)
Adding obstacle avoidance to
the MRB
- USB
- Ethernet
- Web server
- Modbus
- CNC (Mach3/4)
- IO
- PWM
- Encoders
- LCD
- Analog inputs
- Compact PLC
- up to 256
- up to 32
microsteps
microsteps
- 50 V / 6 A
- 30 V / 2.5 A
- USB configuration
- Isolated
PoScope Mega1+
PoScope Mega50
- up to 50MS/s
- resolution up to 12bit
- Lowest power consumption
- Smallest and lightest
- 7 in 1: Oscilloscope, FFT, X/Y,
Recorder, Logic Analyzer, Protocol
decoder, Signal generator
52
We will now modify the code from last
month (MRB_IR_Control.txt) to include
obstacle avoidance. Currently, if you
run the MRB_IR_Control program and
press the blue arrow button on the 44button IR transmitter, then the MRB
will move forward at the set speed.
The MRB will continue moving forward
until you either:
Stop it
Select turn (to the left or right)
Or the MRB hits an object (in which
case there is a chance the motors can
be damaged by being put under too
much stress).
Now download MRB_IR_Control_with_
ObstaceAvoidance.txt (available from
the September 2020 page of the PE
website) and install it on your MRB.
R U N the program and point your
MRB towards a wall. Then press the
‘Forwards’ button on your IR remote
and observe what happens. If all is
well, your MRB will automatically stop
short of the wall with a notification on
the TFT screen, as well as an animated
eye effect being displayed. However,
you may feel more comfortable to be
on standby just in case it does not stop!
This version of the program simply
monitors the measured US distance
whenever the MRB is moving forward. If
this value is below a threshold distance,
the MRB is made to stop (as discussed
at the start of this article). As well as
stopping the motors, we also call an
animated eye-effect subroutine, as well
as displaying a warning message on the
TFT screen.
As usual, the Micromite’s program
code is commented throughout, so do
take a look and have a go at altering the
stopping distance (and see how close
you can get it to stop in front of objects
before there is a collision).
The code to do all of the abovementioned features should be reasonably
familiar to you by now, but if not, then
please read the comments included in
the code to help you understand how
it all works.
Allowing the MRB to explore
by itself
Finally, this month we will run one
more program that you should then
use as a basis to write your own robot
code. Download MRB_Self_Explore.txt
from the September 2020 page of the
PE website and install it on your MRB.
This program will allow your MRB to
roam freely without crashing into large
objects. The IR transmitter is used only
for two things: to start exploring (press
the blue forward arrow button) and for
the emergency stop (press any other
button on the IR transmitter).
RUN the program and press the blue
forward arrow button. This will make
your MRB move and it will appear to
go off and roam around by itself. Any
time you feel it necessary to apply the
emergency brakes, simply press any
other button on the IR remote – this
may be necessary if the MRB has run
into a small undetected object.
The point of this program is really
for it to be the basis for you to create
your own MRB functionality. By now
you know how to write code to:
Move the MRB
Avoid the MRB hitting objects
Draw and animate the eye images
Make beeping sounds (and play
simple tunes)
Write to the colour TFT touch screen
Control via an IR remote control
(or from your Terminal program via
Bluetooth).
The design concept of the MRB was
to bring together many of the topics
that we covered in the Make it with
Micromite series.
Do please have a go at writing your
own MRB program code. Start simple,
and continually add new features. Any
time you feel you’re struggling then
simply drop me an email and I will
gladly help you out.
Next month
That’s enough Robot Buggy for now –
but we may return to it. In the next issue
we will move on to a brand new topic:
Micromite UART communication. Until
then, have fun, and please do stay safe!
Questions? Please email Phil at:
contactus<at>micromite.org
Practical Electronics | September | 2020
|