-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathoscc-check.py
executable file
·439 lines (349 loc) · 14.5 KB
/
oscc-check.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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/usr/bin/python3
"""Usage: oscc-check.py (-V <vehicle>) [-hdelv] [-b <bustype>] [-c <channel>]
Options:
-h --help Display this information
-V <vehicle>, --vehicle <vehicle> Specify your vehicle. Required.
(kia_soul_ev / kia_soul_petrol / kia_niro)
-d --disable Disable modules only, no further checks (overrides enable)
-e --enable Enable modules only, no further checks
-l --loop Repeat all checks, run continuously
-b --bustype <bustype> CAN bus type [default: socketcan_native]
(for more see https://python-can.readthedocs.io/en/2.1.0/interfaces.html)
-c <channel>, --channel <channel> Specify CAN channel, [default: can0]
-v --version Display version information
"""
# This module lets us sleep intermittently. We're not in a hurry and want to see how the car behaves
# when commands are spaced out a bit.
import time
# This module makes it easier to print colored text to stdout
# `Fore`` is used to set the color and `Style`` is used to reset to default.
import colorama
from colorama import Fore, Style
# This is a requirement for this tool's command line argument handling.
from docopt import docopt
# These are the local modules you would import if you wanted to use this tool as a library rather
# than an executable.
from oscccan.canbus import CanBus
from oscccan.canbus import Report
from oscccan import OsccModule
class DebugModules(object):
"""
The 'DebugModules' class contains references to each of the OsccModules,
brake, steering, and throttle. It is used to manage a majority of the stdout reporting this
tool relies on.
"""
def __init__(self, bus, brake, steering, throttle):
"""
Initialize references to modules and CAN bus as well as the 'last_measurement' variable
that allows this class to track whether expected increases and decreases occurred.
"""
self.bus = bus
self.brake = brake
self.steering = steering
self.throttle = throttle
self.last_measurement = None
def enable(self):
"""
Enable all OSCC modules.
"""
while True:
success = self.enable_module(self.brake)
if not success:
continue
success = self.enable_module(self.steering)
if not success:
continue
success = self.enable_module(self.throttle)
if not success:
continue
break
def disable(self):
"""
Disable all OSCC modules.
"""
self.disable_module(self.brake)
self.disable_module(self.steering)
self.disable_module(self.throttle)
def enable_module(self, module):
"""
Enable a single OSCC modules. Print status, success and failure reports.
"""
print(
Fore.MAGENTA + ' status: ',
Style.RESET_ALL,
'attempting to enable',
module.module_name,
'module')
# Attempt to enable the module parameter. Under the hood, this sends the enable brakes CAN
# frame to OSCC/DriveKit over its CAN gateway.
self.bus.enable_module(module)
# Verify the module parameter is enabled by listening to the OSCC/DriveKit CAN gateway for
# a status message that confirms it. Set the `success` flag so we can report and handle
# failure
success = self.bus.check_module_enabled_status(
module,
expect=True)
if success:
print(Fore.GREEN + ' success:', Style.RESET_ALL,
module.module_name, 'module enabled')
else:
print(
Fore.RED +
' error: ',
Style.RESET_ALL,
module.module_name,
'module could not be enabled')
self.bus.reading_sleep()
return success
def disable_module(self, module):
"""
Disable a single OSCC modules. Print status, success and failure reports.
"""
print(
Fore.MAGENTA + ' status: ',
Style.RESET_ALL,
'attempting to disable',
module.module_name,
'module')
# Attempt to disable the module parameter. Under the hood, this sends the disable brakes CAN
# frame to OSCC/DriveKit over its CAN gateway.
self.bus.disable_module(module)
self.bus.reading_sleep()
# Verify the module parameter is disabled by listening to the OSCC/DriveKit CAN gateway for
# a status message that confirms it. Set the `success` flag so we can report and handle
# failure
success = self.bus.check_module_enabled_status(
module,
expect=False)
if success:
print(Fore.GREEN + ' success:', Style.RESET_ALL,
module.module_name, 'module disabled')
return True
else:
print(
Fore.RED + ' error: ',
Style.RESET_ALL,
module.module_name,
'module could not be disabled')
return False
def command_brake_module(self, cmd_value, expect=None):
"""
Command OSCC brake module and verify resulting behavior. Print status, success and
failure reports.
"""
if expect is not None:
print(
Fore.MAGENTA + ' status: ',
Style.RESET_ALL,
'attempting to',
expect,
'brake pressure from',
self.last_measurement, 'bar')
print(
Fore.MAGENTA + ' status: ',
Style.RESET_ALL,
'sending command value',
cmd_value,
'to brake module')
self.bus.send_command(self.brake, cmd_value, timeout=1.0)
self.bus.reading_sleep()
print(Fore.MAGENTA + ' status: ',
Style.RESET_ALL, 'measuring brake pressure')
if expect == 'increase':
report = self.bus.check_brake_pressure(
timeout=1.0, increase_from=self.last_measurement)
elif expect == 'decrease':
report = self.bus.check_brake_pressure(
timeout=1.0, decrease_from=self.last_measurement)
else:
report = self.bus.check_brake_pressure(timeout=1.0)
if report.success is True:
print(
Fore.GREEN + ' success:',
Style.RESET_ALL,
'brake pressure measured at',
report.value,
'bar')
else:
if report.value is not None and expect is not None:
print(
Fore.YELLOW + ' unexpected:',
Style.RESET_ALL,
'brake pressure measured at',
report.value,
'(did not measure',
expect,
'from last measurement',
self.last_measurement,
')')
else:
print(Fore.RED + ' error: ', Style.RESET_ALL,
'failed to read brake pressure')
self.last_measurement = report.value
self.bus.reading_sleep()
def command_steering_module(self, cmd_value, expect=None):
"""
Command OSCC steering module and verify resulting behavior. Print status, success and
failure reports.
"""
if expect is not None:
direction = 'positive' if expect == 'increase' else 'negative'
print(
Fore.MAGENTA + ' status: ',
Style.RESET_ALL,
direction,
'torque applied to steering wheel')
print(
Fore.MAGENTA + ' status: ',
Style.RESET_ALL,
'sending command value',
cmd_value,
'to steering module')
self.bus.send_command(self.steering, cmd_value, timeout=1.0)
self.bus.reading_sleep()
print(Fore.MAGENTA + ' status: ', Style.RESET_ALL,
'measuring steering wheel angle')
report = Report()
report.success = False
if expect == 'increase':
report = self.bus.check_steering_wheel_angle(
timeout=1.0,
increase_from=self.last_measurement)
elif expect == 'decrease':
report = self.bus.check_steering_wheel_angle(
timeout=1.0,
decrease_from=self.last_measurement)
else:
report = self.bus.check_steering_wheel_angle(timeout=1.0)
if report.success is True:
print(
Fore.GREEN + ' success:',
Style.RESET_ALL,
'steering wheel angle measured at',
str(report.value) + '°')
else:
if report.value is not None and expect is not None:
print(
Fore.YELLOW + ' unexpected:',
Style.RESET_ALL,
'steering wheel angle measured at',
report.value,
'(did not measure', expect,
'from last measurement',
str(self.last_measurement) + '°)')
else:
print(
Fore.RED + ' error: ',
Style.RESET_ALL,
'failed to read steering wheel angle')
self.last_measurement = report.value
self.bus.reading_sleep()
def command_throttle_module(self, cmd_value, expect=None):
"""
Command OSCC throttle module and verify resulting behavior. Print status, success and
failure reports.
"""
if expect is not None:
print(
Fore.RED + ' unexpected:',
Style.RESET_ALL,
'no logic to measure',
expect,
'in wheel speed')
print(
Fore.MAGENTA + ' status: ',
Style.RESET_ALL,
'sending command value',
cmd_value,
'to throttle module')
self.bus.send_command(self.throttle, cmd_value, timeout=1.0)
print(Fore.MAGENTA + ' status:',
Style.RESET_ALL, ' measuring wheel speed')
report = self.bus.check_wheel_speed(timeout=1.0)
if report.success is True and report.value is not None:
print(
Fore.GREEN + ' success:', Style.RESET_ALL,
'wheel speeds measured at lf', str(report.value[0]) +
', rf', str(report.value[1]) +
', lr', str(report.value[2]) +
', rr', str(report.value[3]))
else:
print(Fore.RED + ' error:', Style.RESET_ALL,
' failed to read wheel speeds')
self.last_measurement = report.value
self.bus.reading_sleep()
def check_vehicle_arg(arg):
"""
Sanity check the optional vehicle argument.
"""
vehicles = ['kia_soul_ev', 'kia_soul_petrol', 'kia_niro', None]
if arg not in vehicles:
raise ValueError('Unable to target vehicle',
arg + '. Options are', vehicles)
def main(args):
if args['--version']:
print('oscc-check 0.0.2')
return
check_vehicle_arg(args['--vehicle'])
bus = CanBus(
vehicle=args['--vehicle'],
bustype=args['--bustype'],
channel=args['--channel'])
brakes = OsccModule(base_arbitration_id=0x70, module_name='brake')
steering = OsccModule(base_arbitration_id=0x80, module_name='steering')
throttle = OsccModule(base_arbitration_id=0x90, module_name='throttle')
modules = DebugModules(bus, brakes, steering, throttle)
# Initialize module for printing colored text to stdout
colorama.init()
if args['--disable']:
modules.disable()
return
elif args['--enable']:
modules.enable()
return
# Each section or step of the following loop is distinguished from the next by this separator.
# The output begins with this separator for visually consistent output.
print("|Enable Modules -----------------------------------------------------------------|")
# This `while` loop repeats the same basic steps on each iteration, they are as follows:
# 1. Enable each OSCC module.
# 2. Send commands to increase and decrease brake pressure.
# 3. Verify that brake pressure reported by vehicle increased or decreased accordingly.
# 4. Send commands to apply positive or negative torque to steering wheel.
# 5. Verify that the steering wheel angle increased or decreased accordingly.
# 6. Disable each OSCC module.
while True:
modules.enable()
# Visually distinguish enable steps from the following brake validation
print("|Brake Test --------------------------------------------------------------------|")
pressure_cmd = 0.0
modules.command_brake_module(pressure_cmd, expect=None)
pressure_cmd = 0.5
modules.command_brake_module(pressure_cmd, expect='increase')
pressure_cmd = 0.0
modules.command_brake_module(pressure_cmd, expect='decrease')
pressure_cmd = 0.3
modules.command_brake_module(pressure_cmd, expect='increase')
pressure_cmd = 0.0
modules.command_brake_module(pressure_cmd, expect='decrease')
# Visually distinguish brake validation from the following steering wheel validation
print("|Steering Test ------------------------------------------------------------------|")
torque_cmd = -0.1
modules.command_steering_module(torque_cmd, expect=None)
torque_cmd = 0.1
modules.command_steering_module(torque_cmd, expect=None)
torque_cmd = 0.15
modules.command_steering_module(torque_cmd, expect='increase')
torque_cmd = -0.15
modules.command_steering_module(torque_cmd, expect='decrease')
# Visually distinguish throttle validation from the following disable steps
print("|Disable Modules ----------------------------------------------------------------|")
modules.disable()
if not args['--loop']:
break
# Visually distinguish disable steps from the following enable steps
print("|Enable Modules -----------------------------------------------------------------|")
if __name__ == "__main__":
"""
The program's entry point if run as an executable.
"""
main(docopt(__doc__))