-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDriver.py
282 lines (249 loc) · 8.53 KB
/
Driver.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Driver.py
#
# Solar-boat Project 2019
# created on: 2019/07/27
# Author: Tetsuro Ninomiya
#
import math
import sys
import time
import adafruit_ina260
import board
import yaml
from Logger import Logger
from Pid import PositionalPID
from PwmOut import PwmOut
from PwmRead import PwmRead
from Status import Status
from TimeManager import TimeManager
class Driver:
def __init__(self, filename):
self.log_time = time.time()
self._logger = Logger()
self._logger.open()
# load config
print("loading", filename)
with open(filename, "r") as f:
params = yaml.safe_load(f)
# setup time manager
self._time_manager = TimeManager()
self._time_manager.set_time_limit(params["time_limit"]) # Time Limit
self._sleep_time = params["sleep_time"]
# setup pid
self._pid = PositionalPID(params["pwm_range"])
P = params["P"]
I = params["I"]
D = params["D"]
self._pid.set_pid(P, I, D)
# setup waypoints
self._status = Status(params["wp_radius"])
for wp in params["waypoints"]:
name = wp["name"]
lat = wp["lat"]
lon = wp["lon"]
print(name, lat, lon)
self._status.waypoint.add_point(lat, lon)
# setup pwm read/write
self._pwm_read = PwmRead(
params["gpio"]["mode"]["in"],
params["gpio"]["servo"]["in"],
params["gpio"]["thruster"]["in"],
)
self._pwm_out = PwmOut(
params["gpio"]["servo"]["out"], params["gpio"]["thruster"]["out"]
)
self.run_ina = False
self.initial= False
self.current = 0
self.voltage = 0
self.power = 0
# setup for ina226
print("Configuring INA226..")
try:
i2c = board.I2C()
self.i_sensor = adafruit_ina260.INA260(i2c, 0x40)
except:
print("Error when configuring INA226")
time.sleep(1)
print("Configuration Done")
def check_mode_change(self):
print(
"Please set to AN mode and then switch to RC mode to start appropriately."
)
self._pwm_read.measure_pulse_width()
self._update_mode()
if self._status.mode == "AN":
print("Next procedure: Set to RC mode to start.")
while self._status.mode == "AN":
self._pwm_read.measure_pulse_width()
self._update_mode()
time.sleep(0.1)
elif self._status.mode == "RC":
print("Next procedure: set to AN mode and then switch to RC mode to start.")
while self._status.mode == "RC":
self._pwm_read.measure_pulse_width()
self._update_mode()
time.sleep(0.1)
print("Next procedure: Set to RC mode to start.")
while self._status.mode == "AN":
self._pwm_read.measure_pulse_width()
self._update_mode()
time.sleep(0.1)
print("Procedure confirmed.")
def do_operation(self):
while self._time_manager.in_time_limit():
self._pwm_read.measure_pulse_width()
self._status.read_gps()
self._update_output()
if time.time() - self.log_time > 0:
self.log_time = time.time()
# for test
self._pwm_read.print_pulse_width()
# ina226
# if hasattr(self, "i_sensor"):
# self.i_sensor.print_status()
self._print_log()
time.sleep(self._sleep_time)
return
def _update_output(self):
if self._status.has_finished:
mode = "RC"
else:
self._update_mode()
mode = self._status.mode
# RC mode
if mode == "RC":
self._rc_operation()
# AN mode
elif mode == "AN":
self._auto_navigation()
else:
print("Could not update output based on mode")
# update pwm output
self._pwm_out.update_pulse_width()
return
def _update_mode(self):
mode_duty_ratio = self._pwm_read.pins[self._pwm_read.pin_mode]["pulse_width"]
# RC mode
if 0 < mode_duty_ratio < 1500:
self._status.mode = "RC"
# AN mode
elif 1500 <= mode_duty_ratio:
self._status.mode = "AN"
else:
print("Error: mode updating failed", file=sys.stderr)
return
def _rc_operation(self):
# Set the readout signals from receiver as the output signals
self._pwm_out.servo_pulse_width = self._pwm_read.pins[self._pwm_read.pin_servo][
"pulse_width"
]
self._pwm_out.thruster_pulse_width = self._pwm_read.pins[
self._pwm_read.pin_thruster
]["pulse_width"]
return
def _auto_navigation(self):
if hasattr(self, "i_sensor"):
self.current = self.i_sensor.current
self.voltage = self.i_sensor.voltage
self.power = self.i_sensor.power
self.run_ina = True
else:
self.current = 0
self.voltage = 0
self.power = 0
# update status
status = self._status
status.calc_target_bearing()
status.calc_target_distance()
status.update_target()
target_bearing_relative = math.degrees(self._status.target_bearing_relative)
target_distance = self._status.target_distance
servo_pulse_width = self._pid.get_step_signal(
target_bearing_relative, target_distance
)
self._pwm_out.servo_pulse_width = servo_pulse_width
if self.run_ina:
self._optimize_thruster()
else:
self._pwm_out.thruster_pulse_width = 1900
return
def _optimize_thruster(self):
if self.voltage < 10.15:
self._pwm_out.thruster_pulse_width -= 5 * (12.15 - self.voltage)
elif self.current < 50 and not self.initial:
self._pwm_out.thruster_pulse_width = 1100
self.initial= True
elif self._pwm_out.thruster_pulse_width <= 1900:
self._pwm_out.thruster_pulse_width += 50
print(self._pwm_out.thruster_pulse_width)
self.initial= False
self._pwm_out.thruster_pulse_width = min(
max(self._pwm_out.thruster_pulse_width, 1100), 1900
)
def _print_log(self):
timestamp = self._status.timestamp_string
mode = self._status.mode
latitude = self._status.latitude
longitude = self._status.longitude
speed = self._status.speed
heading = math.degrees(self._status.boat_heading)
servo_pw = self._pwm_out.servo_pulse_width
thruster_pw = self._pwm_out.thruster_pulse_width
t_bearing = math.degrees(self._status.target_bearing)
t_bearing_rel = math.degrees(self._status.target_bearing_relative)
t_distance = self._status.target_distance
target = self._status.waypoint.get_point()
t_latitude = target[0]
t_longitude = target[1]
t_idx = self._status.waypoint._index
err = self._pid.err_back
# To print logdata
print(timestamp)
print(
f"Current: {self.current:.3f}mA, Voltage: {self.voltage:.3f}V, Power: {self.power:.3f}mW"
)
print(
f"[{mode} MODE] LAT={latitude:.7f}, LON={longitude:.7f}, SPEED={speed:.2f} [km/h], HEADING={heading:.2f}"
)
print(
f"DUTY (SERVO, THRUSTER): ({servo_pw:6.1f}, {thruster_pw:6.1f}) [us]"
)
print(f"TARGET INDEX: {t_idx}")
print(f"TARGET (LATITUDE, LONGITUDE): ({t_latitude:.7f}, {t_longitude:.7f})")
print(
f"TARGET (REL_BEARING, DISTANCE): ({t_bearing_rel:5.2f}, {t_distance:5.2f} [m])"
)
print("")
# To write logdata (csv file)
log_list = [
timestamp,
mode,
latitude,
longitude,
heading,
speed,
t_idx,
t_latitude,
t_longitude,
t_bearing,
t_distance,
servo_pw,
thruster_pw,
err,
self.current,
self.voltage,
self.power,
]
self._logger.write(log_list)
return
def end(self):
self._logger.close()
self._pwm_read.end()
self._pwm_out.end()
return
if __name__ == "__main__":
print("Driver")