The 1DUltraBot is now finished, apart from the final 3D printed flat top piece. The circuit diagram is illustrated below and is very simple. One digital output (D2) from the Nano to control the servo motor and two (D6 and D7) to control the ultrasonic rangefinder module. The servo motor is connected directly to the battery as is the Nano, whereas the ultrasonic rangefinder is connected to the 5V created by the Nano. I wasn't sure if the ultrasonic module would work properly from the battery voltage so I didn't take the chance.
The programme has been changed so that it takes a measurement using the ultrasonic rangefinder (HC-4) as listed below. First a 12 us trigger pulse is generated to start the ultrasonic module. The ultrasonic module creates a burst of high frequency audio waves for a short period. When the burst of ultrasound has ended the echo signal is activated so that timing can begin. It is not possible to start timing before the ultrasonic burst has completed as it messes up all the receiver signals as it is effectively a very loud noise next to a microphone. This means that a while loop is used to find when the echo signal goes high. This loop is a bit prone to errors because if the echo signal is not detected it will stay in the while loop forever. I could fix this by putting in a counter and exiting on some maximum count value but it works so I didn't bother. Then another while loop counts in increments of 10 us until the echo signal goes low. This while loop also doesn't have a counter to exit in case there is a problem but it would e easy enough to add, but it works, so I didn't. I went for 10 us increments as with anything smaller, the instructions to implement the while loop take up proportionally more time in the loop and the timing is not accurate. I did not perform an accurate calibration of the rangefinder as it isn't necessary for this project but I roughly checked and converted the time count into distance (cm) by dividing by 4.5. This creates a floating point number so I used the int() cast to chop it back into an integer. This value is then returned by this function.
int range(void)
{
int distance;
distance = 0;
digitalWrite(trigger, HIGH); // Trigger pulse of 12 us
delayMicroseconds(12);
digitalWrite(trigger, LOW);
while (digitalRead(echo) == 0)
{
//Do nothing - waiting for start of echo pulse
}
while (digitalRead(echo) == 1)
{
distance++;
delayMicroseconds(10);
}
distance = (int) (distance / 4.5) ; //Div by 4.5 converts to cm
delay(800);
return(distance);
} /* range */
This range() function is used in a while loop as listed below to take a measurement to the nearest object. Just in case there is a problem I have limited the maximum range to 100. This is about 100 cm, roughly. Then I used my own servo motor driver function called myservo() to make the chassis go forwards for almost the measured distance (it is a bit shorter to avoid crashing into the object). The first value provided to myservo() is the direction (2000 is forward maximum speed, 0 is backwards at the maximum speed), the second value is the specific servo motor (in this case number 2), and the final value is the length of time the servo motor is to be driven. This is set as twice the distance obtained from the rangefinder. This is just a try and see constant but it seems to be OK.
while (1)
{
dist_value = 0;
dist_value = range();
Serial.print("Distance is ");
Serial.println(dist_value) ;
// } /* while */
// Just going forwards and backwards
//while (1)
// {
if (dist_value > 100) // Ensure the distance is no more than 100
dist_value = 100;
myservo(fl_shoulder, 2000, 2 * dist_value); // Forwards
delay(1000);
myservo(fl_shoulder, 0, 2 * dist_value); // Backwards
delay(2000);
} /* while */
The finished and working 1DUltrabot is illustrated in the following video. I changed the battery holder from 4 x AA to 4 x AAA and the Nano breadboard to a much smaller one, as I realised that if I did this I would be able to squeeze everything back inside the original chassis top. I then 3D printed two side panels and a front panel to hold the ultrasonic rangefinder module, which as clipped in, in order to make a more attractive and complete mobile robot. All it needs is the final top piece 3D printing and all the electronics will be hidden away. I think it looks quite nice and it is a lot more 'finished' than most of my projects.
It works and it does what it is supposed to: nothing useful just an adaptable mobile robot using ultrasonics to determine the distance moved forwards and backwards following a track guide rail.
Dubbie
Top Comments