MAVLinkHILSystem.java
11.2 KB
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
package me.drton.jmavsim;
import me.drton.jmavlib.conversion.RotationConversion;
import me.drton.jmavlib.mavlink.MAVLinkMessage;
import me.drton.jmavlib.mavlink.MAVLinkSchema;
import me.drton.jmavsim.vehicle.AbstractVehicle;
import javax.vecmath.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.TimeZone;
/**
* MAVLinkHILSystem is MAVLink bridge between AbstractVehicle and autopilot connected via MAVLink.
* MAVLinkHILSystem should have the same sysID as the autopilot, but different componentId.
*/
public class MAVLinkHILSystem extends MAVLinkHILSystemBase {
// private Simulator simulator;
// private AbstractVehicle vehicle;
private boolean gotHeartBeat = false;
private boolean inited = false;
private boolean stopped = false;
private boolean gotHilActuatorControls = false;
private long hilStateUpdateInterval = -1; //don't publish by default
private long nextHilStatePub = 0;
private long timeThrottleCounter = 0;
private long lastHeartbeatMs = 0;
/**
* Create MAVLinkHILSimulator, MAVLink system that sends simulated sensors to autopilot and passes controls from
* autopilot to simulator
*
* @param sysId SysId of simulator should be the same as autopilot
* @param componentId ComponentId of simulator should be different from autopilot
* @param vehicle vehicle to connect
*/
public MAVLinkHILSystem(MAVLinkSchema schema, int sysId, int componentId, AbstractVehicle vehicle) {
super(schema, sysId, componentId, vehicle);
}
@Override
public boolean gotHilActuatorControls() {
return gotHilActuatorControls;
}
@Override
public void handleMessage(MAVLinkMessage msg) {
super.handleMessage(msg);
long t = simulator.getSimMillis();
if ("HIL_ACTUATOR_CONTROLS".equals(msg.getMsgName())) {
gotHilActuatorControls = true;
List<Double> control = new ArrayList<Double>();
for (int i = 0; i < 8; ++i) {
control.add(((Number)((Object[])msg.get("controls"))[i]).doubleValue());
}
// Get the system arming state if the mode
// field is valid
int mode = msg.getInt("mode");
boolean armed = true;
if (mode != 0) {
if ((mode & 128) > 0 /* armed */) {
armed = true;
} else {
armed = false;
}
}
simulator.advanceTime();
vehicle.setControl(control);
} else if ("HIL_CONTROLS".equals(msg.getMsgName()) &&
!gotHilActuatorControls) { //this is deprecated, but we still support it for now
List<Double> control = Arrays.asList(msg.getDouble("roll_ailerons"),
msg.getDouble("pitch_elevator"),
msg.getDouble("yaw_rudder"), msg.getDouble("throttle"), msg.getDouble("aux1"),
msg.getDouble("aux2"), msg.getDouble("aux3"), msg.getDouble("aux4"));
// Get the system arming state if the mode
// field is valid
int mode = msg.getInt("mode");
boolean armed = true;
if (mode != 0) {
if ((mode & 128) > 0 /* armed */) {
armed = true;
} else {
armed = false;
}
}
vehicle.setControl(control);
} else if ("COMMAND_LONG".equals(msg.getMsgName())) {
int command = msg.getInt("command");
if (command == 511) { //MAV_CMD_SET_MESSAGE_INTERVAL
int msg_id = (int)(msg.getFloat("param1") + 0.5);
if (msg_id == 115) { //HIL_STATE_QUATERNION
hilStateUpdateInterval = (int)(msg.getFloat("param2") + 0.5);
}
}
} else if ("HEARTBEAT".equals(msg.getMsgName())) {
long realMs = simulator.getRealMillis();
// We timeout after 3 seconds and do a reset.
long diffMs = realMs - lastHeartbeatMs;
if (diffMs > 3000) {
if (gotHeartBeat) {
System.out.println("Reseting after silence of " + diffMs + " ms");
}
reset();
}
if (!gotHeartBeat && !stopped) {
if (sysId < 0 || sysId == msg.systemID) {
gotHeartBeat = true;
if (sysId < 0) {
sysId = msg.systemID;
}
System.out.println("Init MAVLink");
initMavLink();
} else if (sysId > -1 && sysId != msg.systemID) {
System.out.println("WARNING: Got heartbeat from system #" + Integer.toString(msg.systemID) +
" but configured to only accept messages from system #" + Integer.toString(sysId) +
". Please change the system ID parameter to match in order to use HITL/SITL.");
}
}
if ((msg.getInt("base_mode") & 128) == 0) {
vehicle.setControl(Collections.<Double>emptyList());
}
lastHeartbeatMs = realMs;
} else if ("STATUSTEXT".equals(msg.getMsgName())) {
System.out.println("MSG: " + msg.getString("text"));
}
}
@Override
public void initMavLink() {
if (vehicle.getSensors().getGPSStartTime() == -1) {
vehicle.getSensors().setGPSStartTime(simulator.getSimMillis() + 1000);
}
stopped = false;
inited = true;
}
@Override
public void endSim() {
if (!inited) {
return;
}
inited = false;
gotHeartBeat = false;
stopped = true;
vehicle.getSensors().setGPSStartTime(-1);
}
@Override
public void update(long t, boolean paused) {
super.update(t, paused);
if (paused) {
return;
}
long tu = t * 1000; // Time in us
if (!this.inited) {
return;
}
Sensors sensors = vehicle.getSensors();
// Sensors
MAVLinkMessage msg_sensor = new MAVLinkMessage(schema, "HIL_SENSOR", sysId, componentId,
protocolVersion);
// sensor source bitmask
int sensor_source = 0;
msg_sensor.set("time_usec", tu);
Vector3d tv = sensors.getAcc();
msg_sensor.set("xacc", tv.x);
msg_sensor.set("yacc", tv.y);
msg_sensor.set("zacc", tv.z);
tv = sensors.getGyro();
sensor_source |= 0b111;
msg_sensor.set("xgyro", tv.x);
msg_sensor.set("ygyro", tv.y);
msg_sensor.set("zgyro", tv.z);
tv = sensors.getMag();
sensor_source |= 0b111000;
msg_sensor.set("xmag", tv.x);
msg_sensor.set("ymag", tv.y);
msg_sensor.set("zmag", tv.z);
sensor_source |= 0b111000000;
msg_sensor.set("pressure_alt", sensors.getPressureAlt());
msg_sensor.set("abs_pressure", sensors.getPressure() * 0.01); // Pa to millibar
sensor_source |= 0b1101000000000;
if (sensors.isReset()) {
msg_sensor.set("fields_updated", (1 << 31));
sensors.setReset(false);
} else {
msg_sensor.set("fields_updated", sensor_source);
}
sendMessage(msg_sensor);
/* ground truth */
if (hilStateUpdateInterval != -1 && nextHilStatePub <= tu) {
MAVLinkMessage msg_hil_state = new MAVLinkMessage(schema, "HIL_STATE_QUATERNION", sysId,
componentId, protocolVersion);
msg_hil_state.set("time_usec", tu);
Float[] q = RotationConversion.quaternionByEulerAngles(vehicle.attitude);
msg_hil_state.set("attitude_quaternion", q);
Vector3d v3d = vehicle.getRotationRate();
msg_hil_state.set("rollspeed", (float) v3d.x);
msg_hil_state.set("pitchspeed", (float) v3d.y);
msg_hil_state.set("yawspeed", (float) v3d.z);
int alt = (int)(1000 * vehicle.position.z);
msg_hil_state.set("alt", alt);
msg_hil_state.set("lat", (int)(sensors.getGlobalPosition().lat * 1.e7));
msg_hil_state.set("lon", (int)(sensors.getGlobalPosition().lon * 1.e7));
v3d = vehicle.getVelocity();
msg_hil_state.set("vx", (int)(v3d.x * 100));
msg_hil_state.set("vy", (int)(v3d.y * 100));
msg_hil_state.set("vz", (int)(v3d.z * 100));
Vector3d airSpeed = new Vector3d(vehicle.getVelocity());
airSpeed.scale(-1.0);
airSpeed.add(vehicle.getWorld().getEnvironment().getCurrentWind(vehicle.position));
float as_mag = (float) airSpeed.length();
msg_hil_state.set("true_airspeed", (int)(as_mag * 100));
v3d = vehicle.acceleration;
msg_hil_state.set("xacc", (int)(v3d.x * 1000));
msg_hil_state.set("yacc", (int)(v3d.y * 1000));
msg_hil_state.set("zacc", (int)(v3d.z * 1000));
sendMessage(msg_hil_state);
nextHilStatePub = tu + hilStateUpdateInterval;
}
// GPS
if (sensors.isGPSUpdated()) {
GNSSReport gps = sensors.getGNSS();
if (gps != null && gps.position != null && gps.velocity != null) {
MAVLinkMessage msg_gps = new MAVLinkMessage(schema, "HIL_GPS", sysId, componentId, protocolVersion);
msg_gps.set("time_usec", tu);
msg_gps.set("lat", (long)(gps.position.lat * 1e7));
msg_gps.set("lon", (long)(gps.position.lon * 1e7));
msg_gps.set("alt", (long)(gps.position.alt * 1e3));
msg_gps.set("vn", (int)(gps.velocity.x * 100));
msg_gps.set("ve", (int)(gps.velocity.y * 100));
msg_gps.set("vd", (int)(gps.velocity.z * 100));
msg_gps.set("eph", (int)(gps.eph * 100f));
msg_gps.set("epv", (int)(gps.epv * 100f));
msg_gps.set("vel", (int)(gps.getSpeed() * 100));
msg_gps.set("cog", (int) Math.toDegrees(gps.getCog()) * 100);
msg_gps.set("fix_type", gps.fix);
msg_gps.set("satellites_visible", 10);
sendMessage(msg_gps);
}
}
// SYSTEM TIME from host
if (timeThrottleCounter++ % 1000 == 0) {
MAVLinkMessage msg_system_time = new MAVLinkMessage(schema, "SYSTEM_TIME", sysId, componentId,
protocolVersion);
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
msg_system_time.set("time_unix_usec", cal.getTimeInMillis() * 1000);
msg_system_time.set("time_boot_ms", tu / 1000);
sendMessage(msg_system_time);
}
}
private void reset() {
gotHeartBeat = false;
inited = false;
stopped = false;
gotHilActuatorControls = false;
nextHilStatePub = 0;
}
}