-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautodrive.py
89 lines (82 loc) · 4.73 KB
/
autodrive.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
#!/usr/bin/env python
# Import libraries
import base64
from io import BytesIO
import cv2
import numpy as np
from PIL import Image
class F1TENTH:
def __init__(self):
# F1TENTH data
self.id = None
self.throttle = None
self.steering = None
self.encoder_ticks = None
self.encoder_angles = None
self.position = None
self.orientation_quaternion = None
self.orientation_euler_angles = None
self.angular_velocity = None
self.linear_acceleration = None
self.lidar_scan_rate = None
self.lidar_range_array = None
self.lidar_intensity_array = None
self.front_camera_image = None
# F1TENTH commands
self.throttle_command = None
self.steering_command = None
# Parse F1TENTH sensor data
def parse_data(self, data, verbose=False):
for k, v in data.items():
if ',' in v:
data[k] = v.replace(',', '.')
# Actuator feedbacks
self.throttle = float(data[self.id + " Throttle"])
self.steering = float(data[self.id + " Steering"])
# Wheel encoders
self.encoder_ticks = np.fromstring(data[self.id + " Encoder Ticks"], dtype=int, sep=' ')
self.encoder_angles = np.fromstring(data[self.id + " Encoder Angles"], dtype=float, sep=' ')
# IPS
self.position = np.fromstring(data[self.id + " Position"], dtype=float, sep=' ')
# IMU
self.orientation_quaternion = np.fromstring(data[self.id + " Orientation Quaternion"], dtype=float, sep=' ')
self.orientation_euler_angles = np.fromstring(data[self.id + " Orientation Euler Angles"], dtype=float, sep=' ')
self.angular_velocity = np.fromstring(data[self.id + " Angular Velocity"], dtype=float, sep=' ')
self.linear_acceleration = np.fromstring(data[self.id + " Linear Acceleration"], dtype=float, sep=' ')
# LIDAR
self.lidar_scan_rate = float(data[self.id + " LIDAR Scan Rate"])
self.lidar_range_array = np.fromstring(data[self.id + " LIDAR Range Array"], dtype=float, sep=' ')
self.lidar_intensity_array = np.fromstring(data[self.id + " LIDAR Intensity Array"], dtype=float, sep=' ')
# Cameras
self.front_camera_image = cv2.cvtColor(np.asarray(Image.open(BytesIO(base64.b64decode(data[self.id + " Front Camera Image"])))), cv2.COLOR_RGB2BGR)
if verbose:
print('\n--------------------------------')
print('Receive Data from F1TENTH: ' + self.id)
print('--------------------------------\n')
# Monitor F1TENTH data
print('Throttle: {}'.format(self.throttle))
print('Steering: {}'.format(self.steering))
print('Encoder Ticks: {} {}'.format(self.encoder_ticks[0],self.encoder_ticks[1]))
print('Encoder Angles: {} {}'.format(self.encoder_angles[0],self.encoder_angles[1]))
print('Position: {} {} {}'.format(self.position[0],self.position[1],self.position[2]))
print('Orientation [Quaternion]: {} {} {} {}'.format(self.orientation_quaternion[0],self.orientation_quaternion[1],self.orientation_quaternion[2],self.orientation_quaternion[3]))
print('Orientation [Euler Angles]: {} {} {}'.format(self.orientation_euler_angles[0],self.orientation_euler_angles[1],self.orientation_euler_angles[2]))
print('Angular Velocity: {} {} {}'.format(self.angular_velocity[0],self.angular_velocity[1],self.angular_velocity[2]))
print('Linear Acceleration: {} {} {}'.format(self.linear_acceleration[0],self.linear_acceleration[1],self.linear_acceleration[2]))
print('LIDAR Scan Rate: {}'.format(self.lidar_scan_rate))
print('LIDAR Range Array: \n{}'.format(self.lidar_range_array))
print('LIDAR Intensity Array: \n{}'.format(self.lidar_intensity_array))
# cv2.imshow(self.id + ' Front Camera Preview', cv2.resize(self.front_camera_image, (640, 360)))
# cv2.waitKey(1)
# Generate F1TENTH control commands
def generate_commands(self, verbose=False):
throttle = str(self.throttle_command).replace('.', ',')
steering = str(self.steering_command).replace('.', ',')
if verbose:
print('\n-------------------------------')
print('Transmit Data to F1TENTH: ' + self.id)
print('-------------------------------\n')
# Monitor F1TENTH control commands
print('Throttle Command: {}'.format(throttle))
print('Steering Command: {}'.format(steering))
return {str(self.id) + ' Throttle': throttle, str(self.id) + ' Steering': steering}