Logic
- move forward
- detect distance in front of the car
- if distance is too small, stop and rotate
- once distance is big enough, continue
Code
import Raspbot_Lib
from McLumk_Wheel_Sports import *
# McLumk_Wheel_Sports has already initialized a Raspbot() object called "bot"
def get_distance(bot):
desired_block_length = 1
low_bit_distance = bot.read_data_array(0x1A , 1)[0]
high_bit_distance = bot.read_data_array(0x1B, 1)[0]
return (high_bit_distance << 8)|low_bit_distance
if __name__ == "__main__":
speed = 75
rotation_multiplier = 0.05
bot.Ctrl_Ulatist_Switch(1)
time.sleep(0.1) # polling rate of 10Hz
try:
while True:
if get_distance(bot) < 500: # 50 centimeters
move_backward(rotation_multiplier*speed)
time.sleep(0.2)
stop_robot()
rotate_left(speed*rotation_multiplier)
time.sleep(0.5)
else:
move_forward(speed)
except:
stop_robot()
bot.Ctrl_Ulatist_Switch(0)
print("exiting the program...")