I was
hoping to write about this earlier but due to many reasons I missed it and
finally I got a chance to sit down continue on my post. In previous article I
discussed how to connect a LCD display to Arduino board through a shift
register. (74HC595N). I decided to have an LCD display in my robot for several
reasons.
- It’s very useful to know the readings that robot is taking during testing.
- I can’t access serial monitor when my robot is not connected to PC. I have to use a USB cable and it limits the robot’s movements.
- It’s cool to have an extra gadget on my robot. It makes it more interactive.
So
with the implementation of LCD display the robot consists of 4 main sections.
- Motors and the IC that controls motors.
- LCD display and shift register.
- Distance sensor
- Servo motor
In
order to control all these sections I had to write a program that controls each
of this section. I have talked about the motor controls in details in a
previous post and the basics of implementing the LCD display and Distance
sensor were discussed in last post. Therefore I would like to talk about the
rest of my program in details in upcoming posts.
In
this version the robot was expected to go along a path until it detects an
obstacle from a certain distance. The robot then stops and starts to scan the
surrounding area using the detector attached to a servo motor. Servo motor
turns the detector to left and right while the detector records 7 measurements
(distance to a particular obstacle and the direction i.e. angle to it with
respect to the initial position of the detector. ) then it’s chooses the most
suitable direction to go (direction which the distance to an obstacle is
maximum.).It may seems like an easy thing to do but implementing it was indeed
a difficult task.
Since
this is going to be a bit lengthy than a usual post I would like to split this
into small sections where I explain the functions that I implemented in order to
control my robot. Here is a list of functions that I have used in program.
Functions
associated with movements of robot
- Go_right ()
- Go_left ()
- Go_backward()
- Go_forward()
- robot_stop()
- turn_robot()
Functions
associated with distance sensor
- measure distance()
- sort()
Functions
associated with LCD
- write_LCD()
d Functions
associated with servo motor
- control_servo()
Then
there are setup () and main () functions that are being used to initialise and run
program.
Including required libraries and declaring variables
Header files used in program |
variables used in program |
x is
the variable that is being used to store the distance measurements by distance
sensor. It updates value of x every 100ms or any other given time period using
timer2 interrupts. It is declared as volatile since x gets updated outside of
the program. Therefore it indicates to the program that value of x can be
changed anytime.
direc
is variable type that is constructed exclusively for this program using structures
in C. structures allows programmer to construct user-defined data types that
can be used to store various types of data. In here my requirement was to have
a data type which is capable of storing both angle and distance to an obstacle
from robot position. In other words I wanted to use polar coordinates to detect
obstacles. My initial idea was to use two different arrays to store the angle
and the distance separately but it didn’t sound good because these two
parameters are actually related to each other. Therefore I decided to use a
user-defined data type to store these two parameters. After constructing the
data type you can use it to declare variables, just as you declare an integer
or any other default data types.
Structure used in program as a user-defined data type |
In
here I have declared an array called data which has 7 elements in it. Each
element can store both angle and direction since their data type is direc. Rest
of the variables are conventional variables that are being used to store
various parameters used by program.
The
next step was to initialize the setup() function. Setup() function is used to
assign pins in Arduino board to various inputs or outputs that are used in
program. pinMode() function is used to assign an Arduino GPIO to an input (ex:
echopin which is used to measure reflected ultra sound wave) or to an output
(ex: trigpin which is used to initiate an ultra sound wave) . myservo.attach ()
does the same.In here myservo is an object that is declared as a Servo.
New
thing in setup () is the way that timer interrupt has been set up. Previously I
was able to setup a timer interrupt from scratch but this time I used a
function that was built by Arduino community to setup a timer interrupt. It
made my life so easy and all I had to do was to give a value to the timer interrupt
period and an ISR to be called when timer overflows. Flash() is the ISR and
inside flash() I have called measure_distance() function. Therefore when ISR is
called by timer interrupt every 100ms, it will call measure_distance() that
will measure the distance to an obstacle. MsTimer2.h header file is responsible
for handling timer 2. You can use this link to learn more about this.
Lcd_home()
is used to initiate the lad display by clearing up the display and positioning
the cursor on left upper corner.
Myservo.write
(ini_angle) is used to make sure that sensor is facing forward direction when
robot is turned on. The ini_angle varible value could be any value but my case
it was around 70 degrees.
Now we’ll look at the functions I have used to in my
program.
Functions associated with robot movements
These
functions will depend on the type of robot you are going to build. In my case
it was a robot with wheels therefore it is expected that it should be able to
move forward, turn left or right and backward. There are 4 connectors that need
to be energised in order to rotate the motors. So I had to play with my robot a
bit to identify what combination of these will make my robot go forward,
backward, turn left and right. It’s something I had to do with trial and error.
The most important thing to remember is to never set all motor connectors to
high state since it will destroy the H bridge circuit in L293D IC.
functions that control movements of robots |
Functions associated with
distance sensor
Measure_distance() is the function that measure the
distance to an obstacle and store it in variable in every 100ms. It’s one of
the most critical functions. It generates an ultrasound wave and listen to it’s
reflection. Then it measures time delay between sending the signal and
receiving it back. This value is then used to calculate x value.
duration is the time delay measured and diving it by
58.2 this value is converted to a distance is cm.The speed of sound is 340 m/s
or 29.1 microseconds per cm. duration is the time for wave to return to the
sensor after reflection. Therefore duration value is halved when calculating
distance. Since x is a global variable any other function can access this x
value.
Function to measure displacement |
Sort() function is used to arrange collected distance
and angle data from smallest value to largest value. It takes the measured
values that are stored in array called data and compare an element with it’s previous
value. If the previous value is higher than the chosen element it will copy the
previous value to another allocated memory place temporally. Then it moves the
selected element to the position of previous element .Then copies the values in
temporary memory into the next element. This process is done until all the
values have been compared with it’s adjacent element. The following chart will
explain it better.
The technique that I used in here is called nested
for loop. It means a for loop inside another for loop. These types of loops are
very useful when you are trying to manipulate things like 2dimentional arrays
or matrices. In this case the inner most loop is the loop responsible of
carrying out the comparison and copying elements. Outer most for loop controls
the iterations occurred therefore the inner most loop.
Flow chart for sorting algorithm |
Function used to arrange data in ascending order |
I hope to explain the rest of the function from my next post.
No comments:
Post a Comment