37 #include "physical_constants.h"
38 #include "modulesettings.h"
39 #include "flightbatterysettings.h"
40 #include "flightbatterystate.h"
41 #include "gpsposition.h"
42 #include "manualcontrolcommand.h"
43 #include "attitudeactual.h"
44 #include "airspeedactual.h"
45 #include "actuatordesired.h"
46 #include "flightstatus.h"
47 #include "systemstats.h"
48 #include "homelocation.h"
49 #include "baroaltitude.h"
56 #include "custom_types.h"
67 #if defined(PIOS_MAVLINK_STACK_SIZE)
68 #define STACK_SIZE_BYTES PIOS_MAVLINK_STACK_SIZE
70 #define STACK_SIZE_BYTES 696
73 #define TASK_PRIORITY PIOS_THREAD_PRIO_LOW
74 #define TASK_RATE_HZ 10
77 { [MAV_DATA_STREAM_RAW_SENSORS]=0x02,
78 [MAV_DATA_STREAM_EXTENDED_STATUS]=0x02,
79 [MAV_DATA_STREAM_RC_CHANNELS]=0x05,
80 [MAV_DATA_STREAM_POSITION]=0x02,
81 [MAV_DATA_STREAM_EXTRA1]=0x0A,
82 [MAV_DATA_STREAM_EXTRA2]=0x02 };
84 #define MAXSTREAMS sizeof(mav_rates)
148 uint16_t
msg_length = MAVLINK_NUM_NON_PAYLOAD_BYTES +
163 FlightBatterySettingsData batSettings = {};
165 if (FlightBatterySettingsHandle() != NULL )
166 FlightBatterySettingsGet(&batSettings);
168 SystemStatsData systemStats;
174 FlightBatteryStateData batState = {};
176 if (FlightBatteryStateHandle() != NULL )
177 FlightBatteryStateGet(&batState);
179 SystemStatsGet(&systemStats);
181 int8_t battery_remaining = 0;
182 if (batSettings.Capacity != 0) {
183 if (batState.ConsumedEnergy < batSettings.Capacity) {
184 battery_remaining = 100 - lroundf(batState.ConsumedEnergy / batSettings.Capacity * 100);
189 if (batSettings.VoltagePin != FLIGHTBATTERYSETTINGS_VOLTAGEPIN_NONE)
190 voltage = lroundf(batState.Voltage * 1000);
193 if (batSettings.CurrentPin != FLIGHTBATTERYSETTINGS_CURRENTPIN_NONE)
194 current = lroundf(batState.Current * 100);
196 mavlink_msg_sys_status_pack(0, 200,
mav_msg,
204 (uint16_t)systemStats.CPULoad * 10,
228 ManualControlCommandData manualState;
229 FlightStatusData flightStatus;
231 ManualControlCommandGet(&manualState);
232 FlightStatusGet(&flightStatus);
233 SystemStatsGet(&systemStats);
236 mavlink_msg_rc_channels_raw_pack(0, 200,
mav_msg,
238 systemStats.FlightTime,
242 manualState.Channel[0],
244 manualState.Channel[1],
246 manualState.Channel[2],
248 manualState.Channel[3],
250 manualState.Channel[4],
252 manualState.Channel[5],
254 manualState.Channel[6],
256 manualState.Channel[7],
264 GPSPositionData gpsPosData = {};
266 SystemStatsGet(&systemStats);
268 if (GPSPositionHandle() != NULL )
269 GPSPositionGet(&gpsPosData);
270 if (HomeLocationHandle() != NULL )
271 HomeLocationGet(&homeLocation);
272 SystemStatsGet(&systemStats);
274 uint8_t gps_fix_type;
275 switch (gpsPosData.Status)
277 case GPSPOSITION_STATUS_NOGPS:
280 case GPSPOSITION_STATUS_NOFIX:
283 case GPSPOSITION_STATUS_FIX2D:
286 case GPSPOSITION_STATUS_FIX3D:
287 case GPSPOSITION_STATUS_DIFF3D:
295 mavlink_msg_gps_raw_int_pack(0, 200,
mav_msg,
297 (uint64_t)systemStats.FlightTime * 1000,
303 gpsPosData.Longitude,
305 gpsPosData.Altitude * 1000,
307 gpsPosData.HDOP * 100,
309 gpsPosData.VDOP * 100,
311 gpsPosData.Groundspeed * 100,
313 gpsPosData.Heading * 100,
315 gpsPosData.Satellites);
319 mavlink_msg_gps_global_origin_pack(0, 200,
mav_msg,
321 homeLocation.Latitude,
323 homeLocation.Longitude,
325 homeLocation.Altitude * 1000);
341 AttitudeActualData attActual;
342 SystemStatsData systemStats;
344 AttitudeActualGet(&attActual);
345 SystemStatsGet(&systemStats);
347 mavlink_msg_attitude_pack(0, 200,
mav_msg,
349 systemStats.FlightTime,
351 attActual.Roll * DEG2RAD,
353 attActual.Pitch * DEG2RAD,
355 attActual.Yaw * DEG2RAD,
367 ActuatorDesiredData actDesired;
368 AttitudeActualData attActual;
369 AirspeedActualData airspeedActual = {};
370 GPSPositionData gpsPosData = {};
371 BaroAltitudeData baroAltitude = {};
372 FlightStatusData flightStatus;
374 if (AirspeedActualHandle() != NULL )
375 AirspeedActualGet(&airspeedActual);
376 if (GPSPositionHandle() != NULL )
377 GPSPositionGet(&gpsPosData);
378 if (BaroAltitudeHandle() != NULL )
379 BaroAltitudeGet(&baroAltitude);
380 ActuatorDesiredGet(&actDesired);
381 AttitudeActualGet(&attActual);
382 FlightStatusGet(&flightStatus);
385 if (BaroAltitudeHandle() != NULL)
386 altitude = baroAltitude.Altitude;
387 else if (GPSPositionHandle() != NULL)
388 altitude = gpsPosData.Altitude;
391 int16_t
heading = lroundf(attActual.Yaw);
395 mavlink_msg_vfr_hud_pack(0, 200,
mav_msg,
397 airspeedActual.TrueAirspeed,
399 gpsPosData.Groundspeed,
403 actDesired.Thrust * 100,
411 uint8_t armed_mode = 0;
412 if (flightStatus.Armed == FLIGHTSTATUS_ARMED_ARMED)
413 armed_mode |= MAV_MODE_FLAG_SAFETY_ARMED;
415 uint8_t custom_mode = CUSTOM_MODE_STAB;
417 switch (flightStatus.FlightMode) {
418 case FLIGHTSTATUS_FLIGHTMODE_MANUAL:
419 case FLIGHTSTATUS_FLIGHTMODE_VIRTUALBAR:
420 case FLIGHTSTATUS_FLIGHTMODE_HORIZON:
422 custom_mode = CUSTOM_MODE_SPORT;
424 case FLIGHTSTATUS_FLIGHTMODE_ACRO:
425 case FLIGHTSTATUS_FLIGHTMODE_AXISLOCK:
426 custom_mode = CUSTOM_MODE_ACRO;
428 case FLIGHTSTATUS_FLIGHTMODE_STABILIZED1:
429 case FLIGHTSTATUS_FLIGHTMODE_STABILIZED2:
430 case FLIGHTSTATUS_FLIGHTMODE_STABILIZED3:
433 case FLIGHTSTATUS_FLIGHTMODE_LEVELING:
434 custom_mode = CUSTOM_MODE_STAB;
436 case FLIGHTSTATUS_FLIGHTMODE_AUTOTUNE:
437 custom_mode = CUSTOM_MODE_DRIFT;
439 case FLIGHTSTATUS_FLIGHTMODE_ALTITUDEHOLD:
440 custom_mode = CUSTOM_MODE_ALTH;
442 case FLIGHTSTATUS_FLIGHTMODE_RETURNTOHOME:
443 custom_mode = CUSTOM_MODE_RTL;
445 case FLIGHTSTATUS_FLIGHTMODE_TABLETCONTROL:
446 case FLIGHTSTATUS_FLIGHTMODE_POSITIONHOLD:
447 custom_mode = CUSTOM_MODE_POSH;
449 case FLIGHTSTATUS_FLIGHTMODE_FAILSAFE:
451 case FLIGHTSTATUS_FLIGHTMODE_PATHPLANNER:
452 custom_mode = CUSTOM_MODE_AUTO;
456 mavlink_msg_heartbeat_pack(0, 200,
mav_msg,
460 MAV_AUTOPILOT_GENERIC,
500 ModuleSettingsMavlinkSpeedGet(&speed);
uint32_t PIOS_Thread_Systime(void)
static mavlink_message_t * mav_msg
bool PIOS_Modules_IsEnabled(enum pios_modules module)
void * PIOS_malloc(size_t size)
static const uint8_t mav_rates[]
static struct pios_thread * uavoMavlinkBridgeTaskHandle
void * PIOS_malloc_no_dma(size_t size)
void PIOS_HAL_ConfigureSerialSpeed(uintptr_t com_id, HwSharedSpeedBpsOptions speed)
static int32_t uavoMavlinkBridgeStart(void)
static uint8_t * stream_ticks
#define MODULE_INITCALL(ifn, sfn)
struct pios_thread * PIOS_Thread_Create(void(*fp)(void *), const char *namep, size_t stack_bytes, void *argp, enum pios_thread_prio_e prio)
static bool stream_trigger(enum MAV_DATA_STREAM stream_num)
int32_t TaskMonitorAdd(TaskInfoRunningElem task, struct pios_thread *threadp)
static HomeLocationData homeLocation
static void send_message()
void PIOS_Thread_Sleep_Until(uint32_t *previous_ms, uint32_t increment_ms)
static bool module_enabled
PIOS_COM_SendBuffer(shub_global->frsky_port, shub_global->serial_buf, msg_length)
static int32_t uavoMavlinkBridgeInitialize(void)
Includes PiOS and core architecture components.
static void updateSettings()
static uint32_t lastSysTime
static void uavoMavlinkBridgeTask(void *parameters)
static uint32_t mavlink_port