Skip to content

Commit 7b1d385

Browse files
committed
Merge pull request #269 from dronekit/improve_px4_native_support
Improve px4 native support
2 parents 39a98a4 + 2778978 commit 7b1d385

22 files changed

+439
-499
lines changed

ServiceApp/src/org/droidplanner/services/android/api/DroneApi.java

-1
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,6 @@ public void onDroneEvent(DroneInterfaces.DroneEventsType event, MavLinkDrone dro
408408
droneEvent = AttributeEvent.STATE_VEHICLE_MODE;
409409
break;
410410

411-
case NAVIGATION:
412411
case ATTITUDE:
413412
case ORIENTATION:
414413
droneEvent = AttributeEvent.ATTITUDE_UPDATED;

ServiceApp/src/org/droidplanner/services/android/core/MAVLink/MavLinkMsgHandler.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void receiveData(MAVLinkMessage msg) {
3333
break;
3434

3535
/*
36-
TODO: find more reliable method to better determine the vehicle firmware.
36+
TODO: find more reliable method to determine the vehicle firmware.
3737
case msg_statustext.MAVLINK_MSG_ID_STATUSTEXT:
3838
msg_statustext msg_statustext = (msg_statustext) msg;
3939
handleStatusText(msg_statustext.getText());
@@ -72,6 +72,7 @@ private void handleHeartbeat(msg_heartbeat heartbeat) {
7272
break;
7373

7474
case MAV_AUTOPILOT.MAV_AUTOPILOT_PX4:
75+
droneMgr.onVehicleTypeReceived(FirmwareType.PX4_NATIVE);
7576
break;
7677
}
7778

ServiceApp/src/org/droidplanner/services/android/core/drone/DroneInterfaces.java

-5
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ public enum DroneEventsType {
3737
*/
3838
GUIDEDPOINT,
3939

40-
/**
41-
*
42-
*/
43-
NAVIGATION,
44-
4540
/**
4641
* Denotes vehicle attitude change event.
4742
*/

ServiceApp/src/org/droidplanner/services/android/core/drone/DroneManager.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.droidplanner.services.android.core.drone.autopilot.apm.ArduPlane;
5454
import org.droidplanner.services.android.core.drone.autopilot.apm.ArduRover;
5555
import org.droidplanner.services.android.core.drone.autopilot.apm.ArduSolo;
56+
import org.droidplanner.services.android.core.drone.autopilot.px4.Px4Native;
5657
import org.droidplanner.services.android.core.drone.companion.solo.SoloComp;
5758
import org.droidplanner.services.android.core.drone.variables.HeartBeat;
5859
import org.droidplanner.services.android.core.drone.variables.calibration.MagnetometerCalibrationImpl;
@@ -244,28 +245,33 @@ public void postDelayed(Runnable thread, long timeout) {
244245
switch (type) {
245246
case ARDU_COPTER:
246247
if (isCompanionComputerEnabled()) {
247-
Timber.d("Instantiating ArduSolo autopilot.");
248+
Timber.i("Instantiating ArduSolo autopilot.");
248249
this.drone = new ArduSolo(context, mavClient, dpHandler, dpPrefs, new AndroidApWarningParser(), this, this);
249250
} else {
250-
Timber.d("Instantiating ArduCopter autopilot.");
251+
Timber.i("Instantiating ArduCopter autopilot.");
251252
this.drone = new ArduCopter(context, mavClient, dpHandler, dpPrefs, new AndroidApWarningParser(), this, this);
252253
}
253254
break;
254255

255256
case ARDU_SOLO:
256-
Timber.d("Instantiating ArduCopter autopilot.");
257+
Timber.i("Instantiating ArduCopter autopilot.");
257258
this.drone = new ArduSolo(context, mavClient, dpHandler, dpPrefs, new AndroidApWarningParser(), this, this);
258259
break;
259260

260261
case ARDU_PLANE:
261-
Timber.d("Instantiating ArduPlane autopilot.");
262+
Timber.i("Instantiating ArduPlane autopilot.");
262263
this.drone = new ArduPlane(context, mavClient, dpHandler, dpPrefs, new AndroidApWarningParser(), this, this);
263264
break;
264265

265266
case ARDU_ROVER:
266-
Timber.d("Instantiating ArduPlane autopilot.");
267+
Timber.i("Instantiating ArduPlane autopilot.");
267268
this.drone = new ArduRover(context, mavClient, dpHandler, dpPrefs, new AndroidApWarningParser(), this, this);
268269
break;
270+
271+
case PX4_NATIVE:
272+
Timber.i("Instantiating PX4 Native autopilot.");
273+
this.drone = new Px4Native(dpHandler, mavClient);
274+
break;
269275
}
270276

271277
this.drone.getStreamRates().setRates(dpPrefs.getRates());

ServiceApp/src/org/droidplanner/services/android/core/drone/Preferences.java

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
public interface Preferences {
88

9-
public abstract FirmwareType getVehicleType();
10-
119
public abstract VehicleProfile loadVehicleProfile(FirmwareType firmwareType);
1210

1311
public StreamRates.Rates getRates();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package org.droidplanner.services.android.core.drone.autopilot;
2+
3+
import android.text.TextUtils;
4+
5+
import com.MAVLink.Messages.MAVLinkMessage;
6+
import com.MAVLink.common.msg_attitude;
7+
import com.MAVLink.common.msg_heartbeat;
8+
import com.MAVLink.common.msg_radio_status;
9+
import com.o3dr.services.android.lib.drone.attribute.AttributeType;
10+
import com.o3dr.services.android.lib.drone.property.Altitude;
11+
import com.o3dr.services.android.lib.drone.property.Attitude;
12+
import com.o3dr.services.android.lib.drone.property.Battery;
13+
import com.o3dr.services.android.lib.drone.property.DroneAttribute;
14+
import com.o3dr.services.android.lib.drone.property.Signal;
15+
import com.o3dr.services.android.lib.drone.property.Speed;
16+
import com.o3dr.services.android.lib.util.MathUtils;
17+
18+
import org.droidplanner.services.android.core.MAVLink.MAVLinkStreams;
19+
import org.droidplanner.services.android.core.drone.DroneEvents;
20+
import org.droidplanner.services.android.core.drone.DroneInterfaces;
21+
import org.droidplanner.services.android.core.drone.variables.Type;
22+
import org.droidplanner.services.android.utils.CommonApiUtils;
23+
24+
/**
25+
* Base drone implementation.
26+
* Supports mavlink messages belonging to the common set: https://pixhawk.ethz.ch/mavlink/
27+
*
28+
* Created by Fredia Huya-Kouadio on 9/10/15.
29+
*/
30+
public abstract class CommonMavLinkDrone implements MavLinkDrone {
31+
32+
private final MAVLinkStreams.MAVLinkOutputStream MavClient;
33+
private final DroneEvents events;
34+
protected final Type type;
35+
36+
protected final Altitude altitude = new Altitude();
37+
protected final Speed speed = new Speed();
38+
protected final Battery battery = new Battery();
39+
protected final Signal signal = new Signal();
40+
protected final Attitude attitude = new Attitude();
41+
42+
protected CommonMavLinkDrone(DroneInterfaces.Handler handler, MAVLinkStreams.MAVLinkOutputStream mavClient) {
43+
this.MavClient = mavClient;
44+
45+
events = new DroneEvents(this, handler);
46+
this.type = new Type(this);
47+
}
48+
49+
@Override
50+
public boolean isConnected() {
51+
return MavClient.isConnected();
52+
}
53+
54+
@Override
55+
public void addDroneListener(DroneInterfaces.OnDroneListener listener) {
56+
events.addDroneListener(listener);
57+
}
58+
59+
@Override
60+
public void removeDroneListener(DroneInterfaces.OnDroneListener listener) {
61+
events.removeDroneListener(listener);
62+
}
63+
64+
@Override
65+
public void notifyDroneEvent(final DroneInterfaces.DroneEventsType event) {
66+
switch (event) {
67+
case DISCONNECTED:
68+
signal.setValid(false);
69+
break;
70+
}
71+
72+
events.notifyDroneEvent(event);
73+
}
74+
75+
@Override
76+
public MAVLinkStreams.MAVLinkOutputStream getMavClient() {
77+
return MavClient;
78+
}
79+
80+
@Override
81+
public DroneAttribute getAttribute(String attributeType) {
82+
if (TextUtils.isEmpty(attributeType))
83+
return null;
84+
85+
switch (attributeType) {
86+
case AttributeType.SPEED:
87+
return speed;
88+
89+
case AttributeType.BATTERY:
90+
return battery;
91+
92+
case AttributeType.SIGNAL:
93+
return signal;
94+
95+
case AttributeType.ATTITUDE:
96+
return attitude;
97+
98+
case AttributeType.ALTITUDE:
99+
return altitude;
100+
}
101+
102+
return null;
103+
}
104+
105+
@Override
106+
public void onMavLinkMessageReceived(MAVLinkMessage message) {
107+
switch (message.msgid) {
108+
case msg_radio_status.MAVLINK_MSG_ID_RADIO_STATUS:
109+
msg_radio_status m_radio_status = (msg_radio_status) message;
110+
processSignalUpdate(m_radio_status.rxerrors, m_radio_status.fixed, m_radio_status.rssi,
111+
m_radio_status.remrssi, m_radio_status.txbuf, m_radio_status.noise, m_radio_status.remnoise);
112+
break;
113+
114+
case msg_attitude.MAVLINK_MSG_ID_ATTITUDE:
115+
msg_attitude m_att = (msg_attitude) message;
116+
processAttitude(m_att);
117+
break;
118+
119+
case msg_heartbeat.MAVLINK_MSG_ID_HEARTBEAT:
120+
msg_heartbeat msg_heart = (msg_heartbeat) message;
121+
setType(msg_heart.type);
122+
break;
123+
}
124+
}
125+
126+
protected void setType(int type) {
127+
this.type.setType(type);
128+
}
129+
130+
@Override
131+
public int getType() {
132+
return type.getType();
133+
}
134+
135+
private void processAttitude(msg_attitude m_att) {
136+
attitude.setRoll(CommonApiUtils.fromRadToDeg(m_att.roll));
137+
attitude.setRollSpeed(CommonApiUtils.fromRadToDeg(m_att.rollspeed));
138+
139+
attitude.setPitch(CommonApiUtils.fromRadToDeg(m_att.pitch));
140+
attitude.setPitchSpeed(CommonApiUtils.fromRadToDeg(m_att.pitchspeed));
141+
142+
attitude.setYaw(CommonApiUtils.fromRadToDeg(m_att.yaw));
143+
attitude.setYawSpeed(CommonApiUtils.fromRadToDeg(m_att.yawspeed));
144+
145+
notifyDroneEvent(DroneInterfaces.DroneEventsType.ATTITUDE);
146+
}
147+
148+
protected void processSignalUpdate(int rxerrors, int fixed, short rssi, short remrssi, short txbuf,
149+
short noise, short remnoise) {
150+
signal.setValid(true);
151+
signal.setRxerrors(rxerrors & 0xFFFF);
152+
signal.setFixed(fixed & 0xFFFF);
153+
signal.setRssi(SikValueToDB(rssi & 0xFF));
154+
signal.setRemrssi(SikValueToDB(remrssi & 0xFF));
155+
signal.setNoise(SikValueToDB(noise & 0xFF));
156+
signal.setRemnoise(SikValueToDB(remnoise & 0xFF));
157+
signal.setTxbuf(txbuf & 0xFF);
158+
159+
signal.setSignalStrength(MathUtils.getSignalStrength(signal.getFadeMargin(), signal.getRemFadeMargin()));
160+
161+
notifyDroneEvent(DroneInterfaces.DroneEventsType.RADIO);
162+
}
163+
164+
/**
165+
* Scalling done at the Si1000 radio More info can be found at:
166+
* http://copter.ardupilot.com/wiki/common-using-the-3dr-radio-for-telemetry-with-apm-and-px4/#Power_levels
167+
*/
168+
protected double SikValueToDB(int value) {
169+
return (value / 1.9) - 127;
170+
}
171+
172+
}

ServiceApp/src/org/droidplanner/services/android/core/drone/autopilot/MavLinkDrone.java

-26
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
package org.droidplanner.services.android.core.drone.autopilot;
22

33
import com.MAVLink.Messages.MAVLinkMessage;
4-
import com.MAVLink.common.msg_heartbeat;
54

65
import org.droidplanner.services.android.core.MAVLink.MAVLinkStreams;
76
import org.droidplanner.services.android.core.MAVLink.WaypointManager;
87
import org.droidplanner.services.android.core.drone.DroneInterfaces;
98
import org.droidplanner.services.android.core.drone.Preferences;
109
import org.droidplanner.services.android.core.drone.profiles.Parameters;
1110
import org.droidplanner.services.android.core.drone.profiles.VehicleProfile;
12-
import org.droidplanner.services.android.core.drone.variables.Altitude;
13-
import org.droidplanner.services.android.core.drone.variables.Battery;
1411
import org.droidplanner.services.android.core.drone.variables.Camera;
1512
import org.droidplanner.services.android.core.drone.variables.GPS;
1613
import org.droidplanner.services.android.core.drone.variables.GuidedPoint;
1714
import org.droidplanner.services.android.core.drone.variables.Home;
1815
import org.droidplanner.services.android.core.drone.variables.Magnetometer;
1916
import org.droidplanner.services.android.core.drone.variables.MissionStats;
20-
import org.droidplanner.services.android.core.drone.variables.Navigation;
21-
import org.droidplanner.services.android.core.drone.variables.RC;
22-
import org.droidplanner.services.android.core.drone.variables.Speed;
2317
import org.droidplanner.services.android.core.drone.variables.State;
2418
import org.droidplanner.services.android.core.drone.variables.StreamRates;
2519
import org.droidplanner.services.android.core.drone.variables.calibration.AccelCalibration;
@@ -47,14 +41,10 @@ public interface MavLinkDrone extends Drone {
4741

4842
public byte getCompid();
4943

50-
public void onHeartbeat(msg_heartbeat msg_heart);
51-
5244
public State getState();
5345

5446
public Parameters getParameters();
5547

56-
public void setType(int type);
57-
5848
public int getType();
5949

6050
public FirmwareType getFirmwareType();
@@ -69,16 +59,8 @@ public interface MavLinkDrone extends Drone {
6959

7060
public WaypointManager getWaypointManager();
7161

72-
public Speed getSpeed();
73-
74-
public Battery getBattery();
75-
7662
public Home getHome();
7763

78-
public Altitude getAltitude();
79-
80-
public Navigation getNavigation();
81-
8264
public Mission getMission();
8365

8466
public StreamRates getStreamRates();
@@ -91,18 +73,10 @@ public interface MavLinkDrone extends Drone {
9173

9274
public MagnetometerCalibrationImpl getMagnetometerCalibration();
9375

94-
public RC getRC();
95-
9676
public Magnetometer getMagnetometer();
9777

98-
public void setAltitudeGroundAndAirSpeeds(double altitude, double groundSpeed, double airSpeed, double climb);
99-
100-
public void setDisttowpAndSpeedAltErrors(double disttowp, double alt_error, double aspd_error);
101-
10278
public String getFirmwareVersion();
10379

104-
public void setFirmwareVersion(String message);
105-
10680
public Camera getCamera();
10781

10882
public void logMessage(int mavSeverity, String message);

ServiceApp/src/org/droidplanner/services/android/core/drone/autopilot/apm/ArduCopter.java

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.droidplanner.services.android.core.drone.DroneInterfaces;
77
import org.droidplanner.services.android.core.drone.LogMessageListener;
88
import org.droidplanner.services.android.core.drone.Preferences;
9+
import org.droidplanner.services.android.core.firmware.FirmwareType;
910
import org.droidplanner.services.android.core.model.AutopilotWarningParser;
1011

1112
/**
@@ -16,4 +17,9 @@ public class ArduCopter extends ArduPilot {
1617
public ArduCopter(Context context, MAVLinkStreams.MAVLinkOutputStream mavClient, DroneInterfaces.Handler handler, Preferences pref, AutopilotWarningParser warningParser, LogMessageListener logListener, DroneInterfaces.AttributeEventListener listener) {
1718
super(context, mavClient, handler, pref, warningParser, logListener, listener);
1819
}
20+
21+
@Override
22+
public FirmwareType getFirmwareType() {
23+
return FirmwareType.ARDU_COPTER;
24+
}
1925
}

0 commit comments

Comments
 (0)