-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathCarConnector.py
132 lines (111 loc) · 4.71 KB
/
CarConnector.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
import os
import sys
import time
import json
import numpy as np
python_path = os.path.abspath('AirSim/PythonClient')
sys.path.append(python_path)
from AirSimClient import *
class CarConnector:
def __init__(self):
with open("config.json") as config_buffer:
self.config = json.loads(config_buffer.read())
self.max_speed = self.config['driver']['max_speed']
self.min_speed = self.config['driver']['min_speed']
self.max_actions = self.config['driver']['max_actions']
self.modes = self.config['driver']['selected_modes']
self.throttle = self.config['driver']['throttle']
self.max_steering = self.config['driver']['max_steering']
self.index = 0
self.trajectory = 0
self.client = CarClient()
self.client.confirmConnection()
self.client.enableApiControl(True)
self.car_controls = CarControls()
self.car_controls.brake = 0
self.car_controls.throttle = 0
self.car_controls.steering = 0
def disconnect(self):
self.client.enableApiControl(False)
def get_position(self):
state = self.client.getCarState()
pos = state.kinematics_true.position
return pos
def get_orientation(self):
state = self.client.getCarState()
ort = state.kinematics_true.orientation
return self.client.getPitchRollYaw(ort)
def get_position_and_orientation(self):
state = self.client.getCarState()
pos = state.kinematics_true.position
ort = state.kinematics_true.orientation
return pos, self.client.toEulerianAngle(ort)
def reset(self):
self.car_controls.brake = 1
self.client.setCarControls(self.car_controls)
time.sleep(1)
self.index = 0
self.car_controls.brake = 0
self.car_controls.throttle = 0
self.car_controls.steering = 0
self.client.reset()
self.client.enableApiControl(True)
pos, ort = self.get_position_and_orientation()
return pos, ort
def drive(self):
self.get_controls()
self.client.setCarControls(self.car_controls)
def get_controls(self):
if self.index % self.max_actions == 0:
self.mode = np.random.choice(self.modes, 1)
if self.mode=="still":
self.car_controls.brake = 0
self.car_controls.throttle = 0
self.car_controls.steering = 0
if self.mode=="acc":
if self.car_controls.is_manual_gear:
self.car_controls.is_manual_gear = False
self.car_controls.manual_gear = 0
self.car_controls.brake = 0
self.car_controls.throttle = self.throttle
self.car_controls.steering = 0
if self.mode=="deacc":
self.car_controls.throttle = -1*self.throttle
self.car_controls.is_manual_gear = True
self.car_controls.manual_gear = -1
if self.mode=="brake":
if self.car_controls.is_manual_gear:
self.car_controls.is_manual_gear = False
self.car_controls.manual_gear = 0
self.car_controls.brake = 1
self.car_controls.throttle = 0
self.car_controls.steering = 0
if self.mode=="turn":
if self.car_controls.is_manual_gear:
self.car_controls.is_manual_gear = False
self.car_controls.manual_gear = 0
self.car_controls.brake = 0
self.car_controls.throttle = self.throttle
self.car_controls.steering = (np.random.sample()*2 - 1)*self.max_steering
elif self.mode=="random":
if self.index % 4 == 0:
x = np.random.randint(1, high=4)
if x==1:
if self.client.getCarState().speed >= self.max_speed-5.0:
self.car_controls.brake = 1
self.car_controls.throttle = 0
else:
self.car_controls.brake = 0
self.car_controls.throttle = self.throttle
elif x==2:
self.car_controls.brake = 0
self.car_controls.throttle = 0
else:
if self.client.getCarState().speed >= self.max_speed/2.0:
self.car_controls.brake = 1
self.car_controls.throttle = 0
else:
self.car_controls.brake = 0
self.car_controls.throttle = self.throttle
self.car_controls.steering = (np.random.sample()*2 - 1.0)*self.throttle
self.index += 1