This repository has been archived by the owner on Oct 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrun_robot.py
executable file
·69 lines (48 loc) · 1.88 KB
/
run_robot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
import logging
from random import randint
from robot import Robot
# default sleep timeout in sec
DEFAULT_SLEEP_TIMEOUT_IN_SEC = 0.1
# default speed
DEFAULT_SPEED = 300
# default threshold distance
DEFAULT_THRESHOLD_DISTANCE = 150
# config logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
def main():
logging.debug('Run robot, run!')
robot = Robot()
robot.set_speed(DEFAULT_SPEED)
robot.forward()
try:
while True:
time.sleep(DEFAULT_SLEEP_TIMEOUT_IN_SEC)
logging.debug('color value: %s' % str(robot.color_sensor.value()))
logging.debug('ultrasonic value: %s' % str(robot.ultrasonic_sensor.value()))
logging.debug('gyro value: %s' % str(robot.gyro_sensor.value()))
logging.debug('motor positions (r, l): %s, %s' % (str(robot.right_motor.position),
str(robot.left_motor.position)))
logging.debug('motor speed (r, l): %s, %s' % (str(robot.right_motor.speed),
str(robot.left_motor.speed)))
if robot.ultrasonic_sensor.value() < DEFAULT_THRESHOLD_DISTANCE:
logging.debug('object found: %s' % str(robot.ultrasonic_sensor.value()))
robot.forward(DEFAULT_SPEED * 0.5)
robot.turn(randint(90, 180))
robot.forward(DEFAULT_SPEED)
# doing a cleanup action just before program ends
# handle ctr+c and system exit
except (KeyboardInterrupt, SystemExit):
logging.exception("message")
# handle exceptions
except Exception:
logging.exception("message")
finally:
teardown(robot)
logging.shutdown()
def teardown(robot):
robot.brake()
if __name__ == "__main__":
main()