-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobot.py
More file actions
395 lines (330 loc) · 14.3 KB
/
Copy pathRobot.py
File metadata and controls
395 lines (330 loc) · 14.3 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# Importing main libraries
import numpy as np
import RPi.GPIO as GPIO
import curses
import math
import csv
import matplotlib.pyplot as plt
# Initializing global variables
pi = np.pi
# Initializing variables and function for printing
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
GPIO.setmode(GPIO.BOARD) # initializing the Raspberry Pi GPIO pin nomenclature
GPIO.setwarnings(False) # Disables unwanted warnings
# Custom library containing all motor params and functions
class Motor:
def __init__(self, name, in1, in2, pwm_pin, EncA, EncB, CPR, wheel_dia):
# All motor params
self.name = name
self.in1 = in1
self.in2 = in2
self.pwm_pin = pwm_pin
self.EncA = EncA
self.EncB = EncB
self.count = 0
self.rev = 0
self.dist_data = 0
self.count_data = [0]
self.unit_vel = [0]
self.vel = 0
self.total = 0
self.CPR = CPR
self.wheel_dia = wheel_dia
self.pwm_check = 0
self.sign_check = 0
def Motor_setup(self): # Initializes motor pins on the Raspberry Pi
GPIO.setup(self.in1, GPIO.OUT)
GPIO.setup(self.in2, GPIO.OUT)
GPIO.setup(self.EncA, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(self.EncB, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(self.pwm_pin, GPIO.OUT)
def Motor_pwm(self): # Initialized pwm pin
self.m_pwm = GPIO.PWM(self.pwm_pin, 100)
def edge_detect(self): # Initializes events to help get feedback through encoders
GPIO.add_event_detect(self.EncA, GPIO.BOTH, self.rotation_decode)
GPIO.add_event_detect(self.EncB, GPIO.BOTH, self.rotation_decode)
def count_reset(self): # Count reset
self.count = 0
def rotation_decode(self, tag): # X4 decoding
if tag == self.EncA and self.pwm_check != 0:
if GPIO.input(self.EncA) == GPIO.HIGH:
if GPIO.input(self.EncB) == GPIO.LOW and self.sign_check == 1:
self.count += 1
elif GPIO.input(self.EncB) == GPIO.HIGH and self.sign_check == 0:
self.count -= 1
elif GPIO.input(self.EncA) == GPIO.LOW:
if GPIO.input(self.EncB) == GPIO.HIGH and self.sign_check == 1:
self.count += 1
elif GPIO.input(self.EncB) == GPIO.LOW and self.sign_check == 0:
self.count -= 1
elif tag == self.EncB and self.pwm_check != 0:
if GPIO.input(self.EncB) == GPIO.HIGH:
if GPIO.input(self.EncA) == GPIO.HIGH and self.sign_check == 1:
self.count += 1
elif GPIO.input(self.EncA) == GPIO.LOW and self.sign_check == 0:
self.count -= 1
elif GPIO.input(self.EncB) == GPIO.LOW:
if GPIO.input(self.EncA) == GPIO.LOW and self.sign_check == 1:
self.count += 1
elif GPIO.input(self.EncA) == GPIO.HIGH and self.sign_check == 0:
self.count -= 1
def Motor_run(self): # Converting encoder pulses to real world params
self.dist_data = self.count * np.pi * self.wheel_dia / self.CPR
self.rev = self.count / self.CPR
self.count_data.append(self.count)
def pwm(self, duty, sign): # Runs motor in the required pwm and direction
self.pwm_check = duty
self.sign_check = sign
self.m_pwm.start(0)
if sign == 1:
GPIO.output(self.in1, GPIO.LOW)
GPIO.output(self.in2, GPIO.HIGH)
self.m_pwm.ChangeDutyCycle(duty)
else:
GPIO.output(self.in1, GPIO.HIGH)
GPIO.output(self.in2, GPIO.LOW)
self.m_pwm.ChangeDutyCycle(duty)
def STOP_pwm(self): # Stops motor
self.m_pwm.ChangeDutyCycle(0)
GPIO.output(self.in1, GPIO.LOW)
GPIO.output(self.in2, GPIO.LOW)
def Vel(self, t, interval): # Calculates velocity of the motor
if t != 0:
self.unit_vel.append(2 * pi * (self.count_data[-1] - self.count_data[-2]) / (self.CPR * interval * 4))
if self.unit_vel[-1] != 0:
self.vel = sum(self.unit_vel) / len(self.unit_vel)
else:
self.vel = 0
def data_print(self, t, motor_number): # prints data
stdscr.addstr(motor_number + 1, 0, 'Runtime: %0.1f || %s: Count: %d | Rev: %d | Dist: %0.2f | Vel: %0.2f\t' % (
t, self.name, self.count, self.rev, self.dist_data, self.vel))
stdscr.refresh()
def stop_print(self): # stops printing
curses.echo()
curses.nocbreak()
curses.endwin()
# Custom library containing all robot kinematics params and functions
class Kinematics:
def __init__(self, r, d):
# All robot kinematic params
self.r = r
self.d = d
self.w1 = 0
self.w2 = 0
self.w3 = 0
self.pwm1 = 0
self.pwm2 = 0
self.pwm3 = 0
self.Vx = 0
self.Vy = 0
self.Wz = 0
def ikine(self, iVx, iVy, iWz): # Inverse kinematics
h_mat = [[-self.d / self.r, 1 / self.r, 0],
[-self.d / self.r, -1 / (2 * self.r), -(np.sin(np.pi / 3) / self.r)],
[-self.d / self.r, -1 / (2 * self.r), (np.sin(np.pi / 3) / self.r)]
]
robot_vel = [[iWz], [iVx], [iVy]]
[[self.w1], [self.w2], [self.w3]] = np.dot(h_mat, robot_vel)
def wheel_pwm(self): # PWM publisher
# Convert wheel velocity in rad/s to pwm in %
self.pwm1 = np.round(self.w1, 2) / 14.75 * 100
self.pwm2 = np.round(self.w2, 2) / 14.25 * 100
self.pwm3 = np.round(self.w3, 2) / 14.25 * 100
if self.pwm1 < 0:
sign1 = 0
else:
sign1 = 1
if self.pwm2 < 0:
sign2 = 0
else:
sign2 = 1
if self.pwm3 < 0:
sign3 = 0
else:
sign3 = 1
return abs(self.pwm1), sign1, abs(self.pwm2), sign2, abs(self.pwm3), sign3
def fkine(self, v1, v2, v3): # Forward kinematics
h_mat = [[-self.d / self.r, 1 / self.r, 0],
[-self.d / self.r, -1 / (2 * self.r), -(np.sin(np.pi / 3) / self.r)],
[-self.d / self.r, -1 / (2 * self.r), (np.sin(np.pi / 3) / self.r)]
]
h_mat = np.linalg.inv(h_mat)
wheel_vel = [[v1], [v2], [v3]]
[[self.Wz], [self.Vx], [self.Vy]] = np.dot(h_mat, wheel_vel)
def kine_print(self): # Prints data
stdscr.addstr(0, 0, '\t\t * * * BETA * * *')
stdscr.addstr(6, 0, ' w1: %0.2f | w2: %0.2f | w3: %0.2f\t\t' % (self.w1, self.w2, self.w3))
stdscr.addstr(7, 0,
' pwm1: %0.3f | pwm2: %0.3f | pwm3: %0.3f\t\t' % (self.pwm1, self.pwm2, self.pwm3))
stdscr.addstr(8, 0, '\t Vx: %0.3f | Vy: %0.3f | Wz: %0.3f\t\t' % (self.Vx, self.Vy, self.Wz))
stdscr.refresh()
def end_print(self): # Prints data
stdscr.addstr(12, 0, '\t\t Press p to exit\t')
stdscr.refresh()
def wait(self): # Prints data
stdscr.addstr(12, 0, '\t\t Please Wait!\t')
stdscr.refresh()
def qr_scan(self): # Prints data
stdscr.addstr(3, 0, '\t Please scan QR code')
stdscr.refresh()
def robot_stop(self): # stops printing
curses.echo()
curses.nocbreak()
curses.endwin()
# Custom library containing all robot trajectory params and functions
class Trajectory:
def __init__(self, start_position, goal_position, sample_time, robot_max_vel):
# All trajectory params
self.start_position = start_position
self.goal_position = goal_position
self.sample_time = sample_time
self.var_x = []
self.var_y = []
self.dist = 0
self.robot_max_vel = robot_max_vel
self.max_time = 0
self.tarr = []
self.x_dot = 0
self.y_dot = 0
self.r_vel_x = []
self.r_vel_y = []
self.theta = 0
self.robot_pose = []
self.robot_vel = []
self.x = 0
self.y = 0
self.intercept = 0
def elu_dist(self): # Calculates enclidean distance between start and goal points
self.dist = (np.sqrt(
(np.square(self.goal_position[0] - self.start_position[0]) + np.square(
self.goal_position[1] - self.start_position[1]))))
self.max_time = self.dist / self.robot_max_vel
self.tarr = np.arange(0, self.max_time, self.sample_time)
def planner(self): # Plans a straight line path and a ramp-up steady ramp-down trajectory
self.theta = math.atan2((self.goal_position[1] - self.start_position[1]),
(self.goal_position[0] - self.start_position[0]))
self.x_dot = self.robot_max_vel * np.cos(self.theta)
self.y_dot = self.robot_max_vel * np.sin(self.theta)
try:
self.r_vel_x = np.append(np.linspace(0, self.x_dot, 20),
np.append(np.linspace(self.x_dot, self.x_dot, len(self.tarr) - 40),
np.linspace(self.x_dot, 0, 20)))
self.r_vel_y = np.append(np.linspace(0, self.y_dot, 20),
np.append(np.linspace(self.y_dot, self.y_dot, len(self.tarr) - 40),
np.linspace(self.y_dot, 0, 20)))
except ValueError:
self.r_vel_x = np.linspace(self.x_dot, self.x_dot, len(self.tarr))
self.r_vel_y = np.linspace(self.y_dot, self.y_dot, len(self.tarr))
for ax_x, ax_y in zip(self.r_vel_x, self.r_vel_y):
self.robot_vel.append([ax_x, ax_y, 0])
def traj_print(self): # pints data
stdscr.addstr(10, 0, '\t No. of waypoints: %d | Max time: %0.2f' % (len(self.tarr), self.max_time))
stdscr.refresh()
# Custom library containing all robot measurement params and functions
class Measure:
def __init__(self):
# All measured params
self.time = []
self.x_in_vel = []
self.y_in_vel = []
self.x_out_vel = []
self.y_out_vel = []
self.A = []
self.B = []
self.C = []
self.in_VelA = []
self.in_VelB = []
self.in_VelC = []
self.out_VelA = []
self.out_VelB = []
self.out_VelC = []
self.fields = ['time', 'x_in_vel', 'y_in_vel',
'x_out_vel', 'y_out_vel',
'EncA', 'EncB', 'EncC',
'InVelA', 'InVelB', 'InVelC',
'OutVelA', 'OutVelB', 'OutVelC']
self.filename = "datalog.csv"
self.rows = []
# All functions below append data received into an array
def time_data(self, t):
self.time.append(t)
def robot_vel_in_data(self, in_x, in_y):
self.x_in_vel.append(round(in_x, 2))
self.y_in_vel.append(round(in_y, 2))
def robot_vel_out_data(self, out_x, out_y):
self.x_out_vel.append(round(out_x, 2))
self.y_out_vel.append(round(out_y, 2))
def encoder_data(self, a, b, c):
self.A.append(a)
self.B.append(b)
self.C.append(c)
def motor_vel_in_data(self, in_velA, in_velB, in_velC):
self.in_VelA.append(round(in_velA, 2))
self.in_VelB.append(round(in_velB, 2))
self.in_VelC.append(round(in_velC, 2))
def motor_vel_out_data(self, out_velA, out_velB, out_velC):
self.out_VelA.append(round(out_velA, 2))
self.out_VelB.append(round(out_velB, 2))
self.out_VelC.append(round(out_velC, 2))
def data_logger(self): # Takes all data arrays and arranges it into a spreadsheet
self.rows = np.transpose(
[self.time, self.x_in_vel, self.y_in_vel,
self.x_out_vel, self.y_out_vel,
self.A, self.B, self.C,
self.in_VelA, self.in_VelB, self.in_VelC,
self.out_VelA, self.out_VelB, self.out_VelC])
with open(self.filename, 'w') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow(self.fields)
csvwriter.writerows(self.rows)
def save_plots(self): # Plots the stored data and saves them into as images
plt.plot(self.time, self.x_in_vel, label="Robot In Vel")
plt.plot(self.time, self.x_out_vel, label="Robot Out Vel")
plt.xlabel('Time (s)')
plt.ylabel('Robot Velocity (m/s)')
plt.title('Robot Velocity in X-axis')
plt.legend()
plt.savefig('Robot Velocity in X-axis.png', dpi=300, bbox_inches='tight')
plt.figure()
plt.plot(self.time, self.y_in_vel, label="Robot In Vel")
plt.plot(self.time, self.y_out_vel, label="Robot Out Vel")
plt.xlabel('Time (s)')
plt.ylabel('Robot Velocity (m/s)')
plt.title('Robot Velocity in Y-axis')
plt.legend()
plt.savefig('Robot Velocity in Y-axis.png', dpi=300, bbox_inches='tight')
plt.figure()
plt.plot(self.time, self.A, label="Motor 1 Encoder")
plt.plot(self.time, self.B, label="Motor 2 Encoder")
plt.plot(self.time, self.C, label="Motor 3 Encoder")
plt.xlabel('Time (s)')
plt.ylabel('Encoder Counts (No.)')
plt.title('Encoder Counts')
plt.legend()
plt.savefig('Encoder Counts.png', dpi=300, bbox_inches='tight')
plt.figure()
plt.plot(self.time, self.in_VelA, label="Motor 1 In Vel")
plt.plot(self.time, self.out_VelA, label="Motor 1 Out Vel")
plt.xlabel('Time (s)')
plt.ylabel('Motor 1 Vel (rad/s)')
plt.title('Motor 1 Velocity')
plt.legend()
plt.savefig('Motor 1 Velocity.png', dpi=300, bbox_inches='tight')
plt.figure()
plt.plot(self.time, self.in_VelB, label="Motor 2 In Vel")
plt.plot(self.time, self.out_VelB, label="Motor 2 Out Vel")
plt.xlabel('Time (s)')
plt.ylabel('Motor 2 Vel (rad/s)')
plt.title('Motor 2 Velocity')
plt.legend()
plt.savefig('Motor 2 Velocity.png', dpi=300, bbox_inches='tight')
plt.figure()
plt.plot(self.time, self.in_VelC, label="Motor 3 In Vel")
plt.plot(self.time, self.out_VelC, label="Motor 3 Out Vel")
plt.xlabel('Time (s)')
plt.ylabel('Motor 3 Vel (rad/s)')
plt.title('Motor 3 Velocity')
plt.legend()
plt.savefig('Motor 3 Velocity.png', dpi=300, bbox_inches='tight')