-
Notifications
You must be signed in to change notification settings - Fork 0
/
car_dif.py
233 lines (189 loc) · 7.7 KB
/
car_dif.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/python
#
# Python Module to externalise all Initio/PiRoCon specific hardware
#
# Created by Gareth Davies, Sep 2013
# Updated May 2014, Feb 2015
# Copyright 4tronix
#
# This code is in the public domain and may be freely copied and used
# No warranty is provided or implied
#
#======================================================================
#======================================================================
# General Functions
#
# init(). Initialises GPIO pins, switches motors off, etc
# cleanup(). Sets all motors off and sets GPIO to standard values
# version(). Returns 1. Invalid until after init() has been called
#======================================================================
#======================================================================
# Motor Functions
#
# stop(): Stops both motors
# forward(speed): Sets both motors to move forward at speed. 0 <= speed <= 100
# reverse(speed): Sets both motors to reverse at speed. 0 <= speed <= 100
# spinLeft(speed): Sets motors to turn opposite directions at speed. 0 <= speed <= 100
# spinRight(speed): Sets motors to turn opposite directions at speed. 0 <= speed <= 100
# turnForward(leftSpeed, rightSpeed): Moves forwards in an arc by setting different speeds. 0 <= leftSpeed,rightSpeed <= 100
# turnreverse(leftSpeed, rightSpeed): Moves backwards in an arc by setting different speeds. 0 <= leftSpeed,rightSpeed <= 100
#======================================================================
# Import all necessary libraries
import RPi.GPIO as GPIO, sys, threading, time, os, subprocess
import math
import logging
#use physical pin numbering
GPIO.setmode(GPIO.BOARD)
#======================================================================
# General Functions
#
# init(). Initialises GPIO pins, switches motors and LEDs Off, etc
#======================================================================
# Wheel Encoder Functions
#
class WheelEncoder():
def __init__(self):
self.radius = 5.5
self.circum = self.radius * math.pi
self.ticks = 0
self.startTime = time.time()
self.lastTime = self.startTime
self.elapsedTime = 0.0
self.distance = 0.0
self.acceleration = 0.0
self.totalDistance = 0.0
self.speed = 0.0
def updateRevolutions(self):
self.revolutions = int(self.ticks/36)
self.ticks = 0
def updateElapsedTime(self):
thisTime = time.time()
self.elapsedTime = thisTime - self.lastTime
self.lastTime = thisTime
def updateDistance(self):
self.distance = self.circum * self.revolutions
self.totalDistance += self.distance
def updateSpeed(self,speed):
self.speed = speed
def getSpeed(self):
speed = self.distance/self.elapsedTime
return speed
def updateAcceleration(self):
self.updateRevolutions()
self.updateElapsedTime()
self.updateDistance()
currentSpeed = self.getSpeed()
self.acceleration = (currentSpeed-self.speed)/self.elapsedTime
self.updateSpeed(currentSpeed)
def updateState(self,ticks):
self.ticks = ticks
self.updateAcceleration()
return self.getState()
def getState(self):
self.state = {"elapsedTime":self.elapsedTime, "distance (m)":self.distance/100, "totalDistance (m)": self.totalDistance/100, "speed (m/s)": self.speed/100, "acceleration (m/s^2)":self.acceleration/100, "revolutions": self.revolutions}
return self.state
class Initio():
def init(self):
#Pins
# Pins 24, 26 Right Motor
# Pins 19, 21 Left Motor
self.R1 = 13
self.R2 = 15
self.L1 = 11
self.L2 = 12
#Setup
GPIO.setmode(GPIO.BOARD)
GPIO.setup(self.R1, GPIO.OUT)
GPIO.setup(self.R2, GPIO.OUT)
GPIO.setup(self.L1, GPIO.OUT)
GPIO.setup(self.L2, GPIO.OUT)
#Setup PWM
self.leftF = GPIO.PWM(self.R1,50)
self.leftB = GPIO.PWM(self.R2,50)
self.ightF = GPIO.PWM(self.L1,50)
self.rightB = GPIO.PWM(self.L2,50)
# Initialise the PWM device using the default address
#print GPIO.RPI_REVISION
#use pwm on inputs so motors don't go too fast
GPIO.setup(self.L1, GPIO.OUT)
self.p = GPIO.PWM(self.L1, 20)
self.p.start(0)
GPIO.setup(self.L2, GPIO.OUT)
self.q = GPIO.PWM(self.L2, 20)
self.q.start(0)
GPIO.setup(self.R1, GPIO.OUT)
self.a = GPIO.PWM(self.R1, 20)
self.a.start(0)
GPIO.setup(self.R2, GPIO.OUT)
self.b = GPIO.PWM(self.R2, 20)
self.b.start(0)
# create log file to save monitor informations
# cleanup(). Sets all motors off and sets GPIO to standard values
def cleanup(self):
self.stop()
GPIO.cleanup()
# version(). Returns 1. Invalid until after init() has been called
def version(self):
return 1
# End of General Functions
#======================================================================
#======================================================================
# Motor Functions
#
# stop(): Stops both motors
def stop(self):
self.p.ChangeDutyCycle(0)
self.q.ChangeDutyCycle(0)
self.a.ChangeDutyCycle(0)
self.b.ChangeDutyCycle(0)
# forward(speed): Sets both motors to move forward at speed. 0 <= speed <= 100
def forward(self,speed):
self.p.ChangeDutyCycle(speed)
self.q.ChangeDutyCycle(0)
self.a.ChangeDutyCycle(speed)
self.b.ChangeDutyCycle(0)
self.p.ChangeFrequency(speed + 5)
self.a.ChangeFrequency(speed + 5)
# reverse(speed): Sets both motors to reverse at speed. 0 <= speed <= 100
def reverse(self,speed):
self.p.ChangeDutyCycle(0)
self.q.ChangeDutyCycle(speed)
self.a.ChangeDutyCycle(0)
self.b.ChangeDutyCycle(speed)
self.q.ChangeFrequency(speed + 5)
self.b.ChangeFrequency(speed + 5)
# spinLeft(speed): Sets motors to turn opposite directions at speed. 0 <= speed <= 100
def spinLeft(self,speed):
self.p.ChangeDutyCycle(0)
self.q.ChangeDutyCycle(speed)
self.a.ChangeDutyCycle(speed)
self.b.ChangeDutyCycle(0)
self.q.ChangeFrequency(speed + 5)
self.a.ChangeFrequency(speed + 5)
# spinRight(speed): Sets motors to turn opposite directions at speed. 0 <= speed <= 100
def spinRight(self,speed):
self.p.ChangeDutyCycle(speed)
self.q.ChangeDutyCycle(0)
self.a.ChangeDutyCycle(0)
self.b.ChangeDutyCycle(speed)
self.p.ChangeFrequency(speed + 5)
self.b.ChangeFrequency(speed + 5)
# turnForward(leftSpeed, rightSpeed): Moves forwards in an arc by setting different speeds. 0 <= leftSpeed,rightSpeed <= 100
def turnForward(self,leftSpeed, rightSpeed):
self.p.ChangeDutyCycle(leftSpeed)
self.q.ChangeDutyCycle(0)
self.a.ChangeDutyCycle(rightSpeed)
self.b.ChangeDutyCycle(0)
self.p.ChangeFrequency(leftSpeed + 5)
self.a.ChangeFrequency(rightSpeed + 5)
# turnReverse(leftSpeed, rightSpeed): Moves backwards in an arc by setting different speeds. 0 <= leftSpeed,rightSpeed <= 100
def turnReverse(self,leftSpeed, rightSpeed):
self.p.ChangeDutyCycle(0)
self.q.ChangeDutyCycle(leftSpeed)
self.a.ChangeDutyCycle(0)
self.b.ChangeDutyCycle(rightSpeed)
self.q.ChangeFrequency(leftSpeed + 5)
self.b.ChangeFrequency(rightSpeed + 5)
# End of Motor Functions
#======================================================================
#======================================================================