-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot.py
149 lines (117 loc) · 3.92 KB
/
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'''tank.py
The interface for the hardware parts of the robot.
Access motors, ultrasonics from here.
'''
from serial import Serial
import time
import camera
from settings import SERIAL_PORT, RGB_PINS
import drive
import atexit
import RPi.GPIO as GPIO
#from neopixel import Adafruit_NeoPixel
class Robot:
def __init__(self):
'''
All the hardware in the robot.
'''
self.last_left = 0
self.last_right = 0
try:
self.ultrasonic_ser = Serial(SERIAL_PORT)
except:
print("Failed to initialise ultrasonics")
try:
self.camera = camera.ConstantCamera()
self.camera.start()
self.camera.wait_for_ready()
except Exception as e:
self.camera = None
print("Failed to initialise camera")
self.driver = drive.Driver()
atexit.register(self.shutdown)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
self.pwm = GPIO.PWM(18, 100)
self.pwm.start(5)
#self.neopixel = Adafruit_NeoPixel(1, 12)
#self.neopixel.begin()
self.flywheels_pin = 17
GPIO.setup(self.flywheels_pin, GPIO.OUT)
self.servo_angle = 90
self.set_servo(75)
def set_tank(self, speed_left, speed_right):
'''
Manually set values for motors.
'''
self.driver.turn_motors(0, int(speed_left*255))
self.driver.turn_motors(1, int(speed_right*255))
self.last_left = speed_left
self.last_right = speed_right
def enable_flywheel(self):
'''Enables the flywheels'''
GPIO.output(self.flywheels_pin, True)
def disable_flywheel(self):
'''Disables the flywheels'''
GPIO.output(self.flywheels_pin, False)
def fire(self):
self.set_servo(-20)
time.sleep(0.5)
self.set_servo(75)
def set_colour(self, hex_value):
'''Change the LED colour'''
value = hex_value.lstrip('#')
colour = tuple(int(value[i:i + 2], 16) for i in range(0, 6, 2))
#self.neopixel.setPixelColorRGB(0, *colour)
#self.neopixel.show()
def get_distances(self):
'''
Uses UART Serial over USB to communicate.
'''
left = self.get_distance(0)
middle = self.get_distance(1)
right = self.get_distance(2)
return [right, middle, left]
def get_distance(self, index):
self.ultrasonic_ser.write(bytes([index]))
return ord(self.ultrasonic_ser.read())
def take_picture(self):
return self.camera.get_image()
def forwards(self, speed=1, duration=-1):
self.set_tank(speed, speed)
if duration > 0:
time.sleep(duration)
self.halt()
def halt(self):
self.set_tank(0, 0)
def left(self, speed=1, duration=-1):
self.set_tank(-speed, speed)
if duration > 0:
time.sleep(duration)
self.halt()
def right(self, speed=1, duration=-1):
self.set_tank(speed, -speed)
if duration > 0:
time.sleep(duration)
self.halt()
def bear_left(self, change=50, duration=-1, speed=1):
'''Change is a % of straight ahead'''
self.set_tank(speed*(1-change/100), speed*1)
def bear_right(self, change=50, duration=-1, speed=1):
'''Change is a % of straight ahead'''
self.set_tank(speed*1, speed*(1-change/100))
def backwards(self, speed=1, duration=-1):
self.set_tank(-speed, -speed)
if duration > 0:
time.sleep(duration)
self.halt()
def set_servo(self, angle):
'''Im only doing this to satisfy pylint'''
self.pwm.ChangeDutyCycle((angle/180) * 14 + 6)
self.servo_angle = angle
def shutdown(self):
self.camera.halt_capture()
self.halt()
self.ultrasonic_ser.close()
GPIO.cleanup()
ROBOT = Robot()