This is a continuation from Raspberry Pi Robot
After creating my Raspberry Pi robot, I decided I wanted to develop my robot to move autonomously, for this I purchased three HC-SR04 Ultrasonic sensors and connected them to the GPIO pins on the Raspberry Pi.
I used my existing python script and removed the user control, I then added a function to find the distance from each of the sensors by sending a pulse, timing how long it is until a response is received and then using the known constant speed of sound to calculate the distance, unfortunately this has drawbacks, particularly when used at an angle the sensors will be prone to vastly incorrect readings.
The Pi is programmed to move forwards and check the front distance every 0.5 seconds, if the distance is less than 20cm then the Pi will pivot, initially I programmed it so that the Pi would always pivot left when the distance was less than 20cm, however, I wanted to further develop this.
When the front sensor records a distance of less than 20cm, both side sensors will take a reading, the Pi will then pivot in the direction with the highest distance, due to the inaccuracies when used at angles this sometimes gives false readings and causes the Pi to head in the wrong direction, but it does work a lot of the time.
Overall, it has been very fun developing my robot, I now have an autonomous talking robot (I also switched out the microphone for a webcam since it had a better pickup range and now means I can stream video) and I will be thinking of more projects involving my Raspberry Pi for the future.