Requires Ultra Sonic sensor
In this tutorial we will create a obstacle avoidance robot using the HC-SR04 Ultra sonic sensor. This is a cheap sensor that uses sound waves to calculate distances to objects. The function sends out a 10 microsecond pulse, starts a timer then stops the timer when an echo of the pulse is received, Thge round trip time is then used to calculate the distance to the object.
[pastacode lang=”python” manual=”def%20sonar()%3A%20%20%23%20This%20function%20will%20get%20distance%20using%20the%20sonar%20module%0A%0A%20%20%20%20global%20SONAR%0A%0A%20%20%20%20start_time%20%3D%200%0A%20%20%20%20end_time%20%3D%200%0A%0A%20%20%20%20SONAR.write_digital(1)%20%23%20Send%2010us%20Ping%20pulse%0A%20%20%20%20sleep_us(10)%0A%20%20%20%20SONAR.write_digital(0)%0A%20%20%20%20SONAR.set_pull(SONAR.NO_PULL)%0A%20%20%20%20while%20SONAR.read_digital()%20%3D%3D%200%3A%20%23%20ensure%20Ping%20pulse%20has%20cleared%0A%20%20%20%20%20%20%20%20pass%0A%20%20%20%20start%20%3D%20ticks_us()%20%23%20define%20starting%20time%0A%20%20%20%20while%20SONAR.read_digital()%20%3D%3D%201%3A%20%23%20wait%20for%20Echo%20pulse%20to%20return%0A%20%20%20%20%20%20%20%20pass%0A%20%20%20%20end%20%3D%20ticks_us()%20%23%20define%20ending%20time%0A%20%20%20%20echo%20%3D%20end-start%0A%20%20%20%20distance%20%3D%20int(0.01715%20*%20echo)%20%23%20Calculate%20cm%20distance%0A%20%20%20%20return%20distance” message=”Using Ultrasound to Calculate Distance” highlight=”” provider=”manual”/]
Now we have a function to detect objects we need to create a simple path finding algorithm so the robot knows how to behave when an object is detected. This will be a simple path finding algorithm that will tell the robot to turn when an object is within 50cm of the sensor. The code below is the comlete code listing where the code under the while loop is the path finding algorithm.
[pastacode lang=”python” manual=”from%20microbit%20import%20*%20%20%23%20generic%20microbit%20functionality%0Aimport%20neopixel%20%20%23%20used%20to%20illuminate%20the%20LEDs%0Aimport%20radio%20%20%23%20used%20to%20communicate%20with%20other%20microbits%0Afrom%20utime%20import%20ticks_us%2C%20sleep_us%2C%20ticks_ms%20%20%23%20used%20to%20calc%20distance%0A%0ArobotType%20%3D%20%22%22%0ASONAR%20%3D%20pin15%0A%0AminDist%20%3D%2050%20%20%23%20Distance%20in%20CM%20when%20the%20robot%20will%20turn%0A%0Adef%20detectModel()%3A%20%20%23%20Detects%20which%20model%20were%20using%20XL%20or%20classic%0A%20%20%20%20global%20robotType%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20value%20%3D%20i2c.read(28%2C%201%2C%20repeat%3DFalse)%20%20%23%20Read%20i2c%20bus%0A%20%20%20%20%20%20%20%20robotType%20%3D%20%22XL%22%20%20%23%20If%20we%20can%20read%20it%20then%20it%20must%20be%20XL%0A%20%20%20%20%20%20%20%20display.show(%22X%22)%0A%20%20%20%20except%3A%0A%20%20%20%20%20%20%20%20robotType%20%3D%20%22classic%22%20%20%23%20If%20we%20can’t%20read%20it%20it%20must%20be%20classic%0A%20%20%20%20%20%20%20%20display.show(%22C%22)%20%20%20%20%20%20%23%20or%20Micro%3Abit%20is%20unplugged%0A%20%20%20%20sleep(1000)%20%20%23%20Do%20this%20so%20the%20user%20can%20see%20if%20the%20correct%20model%20is%20found%0A%0Adef%20sonar()%3A%20%20%23%20This%20function%20will%20get%20distance%20using%20the%20sonar%20module%0A%0A%20%20%20%20global%20SONAR%0A%0A%20%20%20%20start_time%20%3D%200%0A%20%20%20%20end_time%20%3D%200%0A%0A%20%20%20%20SONAR.write_digital(1)%20%23%20Send%2010us%20Ping%20pulse%0A%20%20%20%20sleep_us(10)%0A%20%20%20%20SONAR.write_digital(0)%0A%20%20%20%20SONAR.set_pull(SONAR.NO_PULL)%0A%20%20%20%20while%20SONAR.read_digital()%20%3D%3D%200%3A%20%23%20ensure%20Ping%20pulse%20has%20cleared%0A%20%20%20%20%20%20%20%20pass%0A%20%20%20%20start%20%3D%20ticks_us()%20%23%20define%20starting%20time%0A%20%20%20%20while%20SONAR.read_digital()%20%3D%3D%201%3A%20%23%20wait%20for%20Echo%20pulse%20to%20return%0A%20%20%20%20%20%20%20%20pass%0A%20%20%20%20end%20%3D%20ticks_us()%20%23%20define%20ending%20time%0A%20%20%20%20echo%20%3D%20end-start%0A%20%20%20%20distance%20%3D%20int(0.01715%20*%20echo)%20%23%20Calculate%20cm%20distance%0A%20%20%20%20return%20distance%0A%0Adef%20Drive(_leftSpeed%2C%20_rightSpeed%2C%20_leftDirection%2C%20_rightDirection)%3A%0A%20%20%20%20%23%20speed%20values%20between%201%20-%201023%0A%20%20%20%20%23%20smaller%20values%20%3D%3D%20faster%20speed%20moving%20backwards%0A%20%20%20%20%23%20Smaller%20values%20%3D%3D%20lower%20speeds%20when%20moving%20forwards%0A%20%20%20%20%23%20direction%200%20%3D%3D%20forwards%2C%201%20%3D%3D%20backwards%0A%20%20%20%20leftSpeed.write_analog(_leftSpeed)%20%20%23%20Set%20the%20speed%20of%20left%20motor%0A%20%20%20%20rightSpeed.write_analog(_rightSpeed)%20%20%23%20Set%20the%20speed%20of%20right%20motor%0A%20%20%20%20if%20(_leftDirection%20!%3D%202)%3A%0A%20%20%20%20%20%20%20%20leftDirection.write_digital(_leftDirection)%20%20%23%20left%20motor%0A%20%20%20%20%20%20%20%20rightDirection.write_digital(_rightDirection)%20%20%23%20right%20motor%0A%20%20%20%20%0AdetectModel()%0A%0A%23%20Motor%20pins%3B%20these%20tell%20the%20motor%20to%20go%0A%23%20forward%2C%20backwards%20or%20turn%0Aif%20robotType%20%3D%3D%20%22classic%22%3A%0A%20%20%20%20leftSpeed%20%3D%20pin0%0A%20%20%20%20leftDirection%20%3D%20pin8%0A%20%20%20%20rightSpeed%20%3D%20pin1%0A%20%20%20%20rightDirection%20%3D%20pin12%0A%0Aelse%3A%20%20%23%20Bit%3ABot%20XL%0A%20%20%20%20leftSpeed%20%3D%20pin16%0A%20%20%20%20leftDirection%20%3D%20pin8%0A%20%20%20%20rightSpeed%20%3D%20pin14%0A%20%20%20%20rightDirection%20%3D%20pin12%0A%20%20%20%20%0Awhile%20True%3A%0A%20%20%20%20%23%20path%20finding%20alogirthm%0A%20%20%20%20distance%20%3D%20sonar()%20%20%23%20get%20a%20distance%20measurement%0A%20%20%20%20if%20distance%20%3C%20minDist%3A%0A%20%20%20%20%20%20%20%20sleep(200)%0A%20%20%20%20%20%20%20%20Drive(1%2C%201023%2C%201%2C1)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20Drive(1023%2C1023%2C0%2C0)%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20″ message=”Full Code Listing for a Simple Path Finding Robot” highlight=”” provider=”manual”/]
Code Files