In my last post I have discussed several functions that I have used. From this post I would like to discuss the rest of the functions.
Functions associated with LCD Display
Write_LCD()
Write_LCD() handles displaying the current distance
measurement the ultra sound sensor is taking. Since the sensor measures
distance every 50ms that resolution is too fast for humans to be able grasp any
information. Therefore the actual display doesn’t show the distance in real
time. It has an interval about 200ms.
First challenge I faced was that variable x is an
integer so I had to convert it to a string before I can send it to LCD display.
By using following code I was able to convert x into a string (i.e.an array of
characters).
Converting an integer into an array |
Another problem I faced was since x varies from
single digit to up to 3 digits the display showed meaningless characters when
there are blanks. For example let’s at the beginning x was a 3 digit number. When
it changes to a 3 digit number the leftmost digit will turn in to a meaningless
character. My solution was to use if statements to split the case into 3
scenarios. So if x is between 0 and 9, 2 zeros will be displayed in front of x,
if x is between 10 and 99, a zero will be displayed in front of x, if x is
larger than 100 no zeros will be displayed in front of x. you will be able to
understand my method clearly.
Writing into LCD Display |
Functions
associated with servo motor
Control_servo()
Servo
motor is used when robot has stopped due to detection of an obstacle and needs
to find an alternative path to go. I used a polar coordination system to identify
the angle and distance to an obstacle. The following diagram will help you to
understand my method.
In this
picture r denotes the displacement to the obstacle and theta denotes the angle from left side
horizontal axis. When the sensor facing forward direction value of theta must be 90
degrees. But I found that the minimum value for theta was about 20 degrees and
maximum was about 150 degrees. Therefore when initializing the servo it was fed
with 70 degrees not 90 degrees. Servo library takes the angle difference from
it’s current position to move to a new position.
When
the robot got a x value which is less than 20cm it stops and main function
calls control_servo() function . Control servo() function first moves sensor
from 70 degrees to 140 degrees. While doing this it records the distance when angle
is 90,110 and 130 degrees. Then again sensor moves from 130 degrees to 0
degrees. Sensor records the distance when angle is 50, 30, 15 and 0 degrees.
One important thing to remember is to have a delay when changing angle. Since
the actual servo takes some time to move to a new position from current
position a delay is required. I have used a delay of 10ms for this. By accurately
measuring the angular speed of the servo this time delay can be fine-tuned. Data
array is used to record relevant angle and displacement.
Function to control Servo motor |
Now I think it’s ok to explain functionality of
turn_robot() function. The purpose of this function is to turn the robot in to
a suitable direction based on the values in data array. When turn robot is
called, first it calls control_servo() function in order to collect distance
and angle measurements. Then it calls sort() function to arrange obtained data
from smallest distance value to highest distance value. After this data[6]
element will have the highest distance measurement and corresponding angle.70
degrees is subtracted from this angle. If this value is a negative value it
means this angle (i.e. the obstacle that is situated furthest from robot) is
positioned from left side of the robot. If this value is positive it means this
angle (i.e. the obstacle that is situated furthest from robot) is positioned
from right side of the robot. Then this angle is used to turn the robot to that
direction.
equation 2 |
According to equation 2 there is a time period which is proportional
to a given angle theta. Assuming omega is a constant which
is the angular velocity of the robot turning then t can be used to turn the
robot. By calculating a time period which corresponds to theta and using that
time period as a delay period I was able to achieve this. We only need the
magnitude of theta since we already know the direction of the turn. The angular
velocity of the vehicle had to be measured experimentally and also this
constant value is valid only when the surface that robot moves is uniform. So
this method is not very accurate method at the moment but I am planning to
implement a control system to keep the speed of the motors at a constant rate. By
that we omega value will stay the same. In here I have multiplied turn_time
variable by 1000 in order to convert it in to milliseconds.
Function to turn robot |
Now I have covered all the functions I have
constructed to build my program. The main program or loop() doesn’t do much
except coordinating the all these functionalities and making robot move
forward. Inside loop() it checks x value
.If x is larger than 20cm(or any arbitrary value) robot will move forward and
if x is less than that value it will stop robot and call turn_robot() function.
After that turn_robot() function will turn the robot in a suitable direction
and starts main program from beginning.
Main function loop() |
This is my complete program for my robot. Although this
program works it still need lots of debugging. There are some unexpected behaviours
that need to be fixed.It might take some time and I hope to write my progress
on my blog. But I’m glad finally I was able to make a working prototype of my
robot. It may sound funny but building this robot was like raising a child for
me. You need to be both knowledgeable and passionate at what you are doing. Sometimes
you get frustrated when things are not going well or not giving the desired
output but you won’t give up until you are satisfied. You will realised even
the simplest things in life like going on a straight line is not simple as it
seems for somebody as simple as a robot. It takes lot of time and patience to
achieve what you want but at the end of the day you can see that little
creature is actually wondering in your room and this will make you feel you achieved
what you deserve.
I think if you read these articles it might be
helpful to you in your own projects. I have posted a link to the source code at
the end of this article. Please feel free to download it and use it if you
need.