Created
April 8, 2019 19:04
-
-
Save RobertLucian/0640f59e5a7afab3716d8b51c3bd9db8 to your computer and use it in GitHub Desktop.
Remote Controlled LIDAR-based GiggleBot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from gigglebot import * | |
from distance_sensor import DistanceSensor | |
from microbit import sleep | |
from utime import ticks_us, sleep_us | |
import ustruct | |
import radio | |
# stop the robot if it's already moving | |
stop() | |
# enable radio | |
radio.on() | |
# distance sensor object | |
ds = DistanceSensor() | |
ds.start_continuous() | |
rotate_time = 0.7 # measured in seconds | |
rotate_span = 160 # measured in degrees | |
rotate_steps = 10 | |
overhead_compensation = 1.05 # defined in percentages | |
time_per_step = 10 ** 6 * rotate_time / (rotate_steps * overhead_compensation) | |
last_read_time = 0 | |
radar = bytearray(rotate_steps) | |
servo_rotate_direction = 0 # 0 for going upwards (0->160) and 1 otherwise | |
radar_index = 0 | |
set_servo(RIGHT, 0) | |
while True: | |
# read from the radar | |
if ticks_us() - last_read_time > time_per_step: | |
# read from the distance sensor | |
radar[radar_index] = int(ds.read_range_continuous() / 10) | |
last_read_time = ticks_us() | |
print(radar_index) | |
# do the logic for rotating the servo from left to right | |
if radar_index == rotate_steps - 1 and servo_rotate_direction == 0: | |
set_servo(RIGHT, 0) | |
servo_rotate_direction = 1 | |
elif radar_index == 0 and servo_rotate_direction == 1: | |
set_servo(RIGHT, rotate_span) | |
servo_rotate_direction = 0 | |
else: | |
radar_index += 1 if servo_rotate_direction == 0 else -1 | |
# and send the radar values | |
radio.send_bytes(radar) | |
try: | |
# read robot commands | |
lmotor, rmotor = ustruct.unpack('bb', radio.receive_bytes()) | |
# and actuate the motors should there be any received commands | |
set_speed(lmotor, rmotor) | |
drive() | |
except TypeError: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment