-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wheel.py
executable file
·118 lines (82 loc) · 3.12 KB
/
Wheel.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
from Controller import Controller
import time
import os
import RPi.GPIO as GPIO
debug_info = os.path.basename(__file__)
class Rear_Wheel(object):
'''Create a wheel object controlled by the Controller PCA9685'''
controller_address = 0x40
def __init__(self, PCAchannel, pin, debug = False):
self.debug = debug
self.controller = Controller(self.controller_address, debug)
self.PCAchannel = PCAchannel
self.pin = pin
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.pin, GPIO.OUT)
GPIO.output(self.pin, True)
def speed_converter(self, speed):
'''Converts speed from 0-100 to 0-4095'''
if 0 <= speed <= 100:
duty_cycle = 40.95*speed
else:
duty_cycle = 0
print ('Speed values must be between 0 and 100')
if self.debug:
print (debug_info, 'Exiting program due to invalid speed value')
GPIO.cleanup()
exit()
duty_cycle = int(duty_cycle)
if self.debug:
print (debug_info, 'Speed ', speed, 'converted to ', duty_cycle)
return duty_cycle
def forward(self, speed):
'''Sets the movement of the wheel forwards to a given speed'''
GPIO.output(self.pin, False)
duty_cycle = self.speed_converter(speed)
self.controller.set_duty_cycle(self.PCAchannel, duty_cycle)
if self.debug:
print (debug_info, 'Wheel connected to pin', self.pin, 'in the Raspberry Pi GPIO and channel', self.PCAchannel,'in the PCA9685 is moving forwards at speed', speed)
def backwards(self, speed):
'''Sets the movement of the wheel backwards to a given speed'''
GPIO.output(self.pin, True)
duty_cycle = self.speed_converter(speed)
self.controller.set_duty_cycle(self.PCAchannel, duty_cycle)
if self.debug:
print (debug_info, 'Wheel connected to pin', self.pin, 'in the Raspberry Pi GPIO and channel', self.PCAchannel,'in the PCA9685 is moving backwards at speed', speed)
def stop(self):
'''Stops the wheel'''
self.controller.set_duty_cycle(self.PCAchannel, 0)
if self.debug:
print (debug_info, 'Wheel connected to pin', self.pin, 'in the Raspberry Pi GPIO and channel', self.PCAchannel,'in the PCA9685 has stopped')
def test_wheel():
speed = 100 #Values from 0 to 100
wait = 0.5
left_PCAchannel = 5 #Values from 0 to 15
left_pin = 17 #Left wheel = 17; right wheel = 27
right_PCAchannel = 4 #Values from 0 to 15
right_pin = 27 #Left wheel = 17; right wheel = 27
try:
left_wheel = Rear_Wheel(left_PCAchannel, left_pin, debug = debug)
right_wheel = Rear_Wheel(right_PCAchannel, right_pin, debug = debug)
left_wheel.forward(speed)
time.sleep(wait)
left_wheel.backwards(speed)
time.sleep(wait)
left_wheel.stop()
right_wheel.forward(speed)
time.sleep(wait)
right_wheel.backwards(speed)
time.sleep(wait)
right_wheel.stop()
GPIO.cleanup()
except KeyboardInterrupt:
print ('Stopping wheels')
left_wheel.stop()
right_wheel.stop()
GPIO.cleanup()
print ('Test interrupted by user')
if __name__ == '__main__':
debug = True
print ('Testing file ', debug_info)
print ('Testing Wheel class')
test_wheel()