-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpeedTruckTarget.py
100 lines (76 loc) · 2.93 KB
/
SpeedTruckTarget.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
import Command
import Config
import SpeedNone
class SpeedTruckTarget(SpeedNone.SpeedNone):
def __init__(self):
self.current_target_speed_ms = 0.0
self.go_forward = True
#self.traction_force = 100
self.num_previous_speed = 4
self.previous_speed_array = []
self.max_traction = 100
if 'max_traction' in Config.config_json:
self.max_traction = Config.config_json['max_traction']
self.current_traction = self.max_traction
self.is_accelerating = False
for i in range(0, self.num_previous_speed):
self.previous_speed_array.append(1000000.0)
def run(self, current_speed_ms, target_speed_ms):
if self.current_target_speed_ms != target_speed_ms:
#self.traction_force = self.current_traction
self.current_traction = self.max_traction
self.current_target_speed_ms = target_speed_ms
if current_speed_ms < target_speed_ms:
self.traction_on(self.current_traction)
if self.is_accelerating is True:
self.current_traction = min(self.current_traction + 0.1, 100)
else:
self.current_traction = self.max_traction
self.is_accelerating = True
else:
if current_speed_ms > target_speed_ms + target_speed_ms * 0.1:
self.traction_off(self.current_traction)
else:
self.traction_off(0)
if self.is_accelerating is False:
self.current_traction = min(self.current_traction + 0.1, 100)
else:
self.current_traction = self.max_traction
self.is_accelerating = False
#self.traction_force -= 5
self.previous_speed_array.pop(0)
self.previous_speed_array.append(current_speed_ms)
prev_speed = 1000000.0
decelerate = True
for s in self.previous_speed_array:
if s <= prev_speed:
prev_speed = s
continue
else:
decelerate = False
#if decelerate is True:
# self.traction_force += 5
# if self.traction_force > self.max_traction:
# self.traction_force = self.max_traction
def forward(self):
self.go_forward = True
def backward(self):
self.go_forward = False
def traction_on(self, value):
if self.go_forward is True:
Command.TRUCK_BRAKE(0)
Command.TRUCK_ACCELERATE(value)
else:
Command.TRUCK_ACCELERATE(0)
Command.TRUCK_BRAKE(value)
def traction_off(self, value):
if self.go_forward is False:
Command.TRUCK_BRAKE(0)
Command.TRUCK_ACCELERATE(value)
else:
Command.TRUCK_ACCELERATE(0)
Command.TRUCK_BRAKE(value)
def reset(self):
Command.TRUCK_BRAKE(0)
Command.TRUCK_ACCELERATE(0)
self.__init__()