53 #include "physical_constants.h"
57 #include "modulesettings.h"
58 #include "attitudeactual.h"
59 #include "pathdesired.h"
60 #include "positionactual.h"
61 #include "flightstatus.h"
62 #include "pathstatus.h"
63 #include "airspeedactual.h"
64 #include "gpsvelocity.h"
65 #include "gpsposition.h"
66 #include "fixedwingairspeeds.h"
67 #include "fixedwingpathfollowersettings.h"
68 #include "fixedwingpathfollowerstatus.h"
69 #include "homelocation.h"
70 #include "nedposition.h"
71 #include "stabilizationdesired.h"
72 #include "stabilizationsettings.h"
73 #include "systemsettings.h"
74 #include "velocitydesired.h"
75 #include "velocityactual.h"
80 #define MAX_QUEUE_SIZE 4
81 #define STACK_SIZE_BYTES 1548
82 #define TASK_PRIORITY PIOS_THREAD_PRIO_NORMAL
96 void *ctx,
void *obj,
int len);
100 void *ctx,
void *obj,
int len);
123 #ifdef MODULE_FixedWingPathFollower_BUILTIN
126 uint8_t module_state[MODULESETTINGS_ADMINSTATE_NUMELEM];
127 ModuleSettingsAdminStateGet(module_state);
128 if (module_state[MODULESETTINGS_ADMINSTATE_FIXEDWINGPATHFOLLOWER] == MODULESETTINGS_ADMINSTATE_ENABLED) {
146 if (FixedWingPathFollowerSettingsInitialize() == -1 \
147 || FixedWingAirspeedsInitialize() == -1 \
148 || FixedWingPathFollowerStatusInitialize() == -1 \
149 || PathDesiredInitialize() == -1 \
150 || PathStatusInitialize() == -1 \
151 || VelocityDesiredInitialize() == -1 \
152 || AirspeedActualInitialize() == -1 ){
181 SystemSettingsData systemSettings;
182 FlightStatusData flightStatus;
184 uint32_t lastUpdateTime;
195 path_desired_updated =
false;
208 SystemSettingsGet(&systemSettings);
209 if ( (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_FIXEDWING) &&
210 (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_FIXEDWINGELEVON) &&
211 (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_FIXEDWINGVTAIL) )
213 AlarmsSet(SYSTEMALARMS_ALARM_PATHFOLLOWER,SYSTEMALARMS_ALARM_CRITICAL);
221 static uint8_t last_flight_mode;
222 FlightStatusGet(&flightStatus);
225 PositionActualData positionActual;
227 static enum {FW_FOLLOWER_IDLE, FW_FOLLOWER_RUNNING, FW_FOLLOWER_ERR}
state = FW_FOLLOWER_IDLE;
232 bool process_path_desired_update =
233 (last_flight_mode == FLIGHTSTATUS_FLIGHTMODE_PATHPLANNER) &&
234 path_desired_updated;
235 path_desired_updated =
false;
241 if (flightStatus.FlightMode != last_flight_mode ||
242 process_path_desired_update) {
244 last_flight_mode = flightStatus.FlightMode;
246 switch(flightStatus.FlightMode) {
247 case FLIGHTSTATUS_FLIGHTMODE_RETURNTOHOME:
248 state = FW_FOLLOWER_RUNNING;
250 PositionActualGet(&positionActual);
252 pathDesired.Mode = PATHDESIRED_MODE_CIRCLEPOSITIONRIGHT;
265 case FLIGHTSTATUS_FLIGHTMODE_POSITIONHOLD:
266 state = FW_FOLLOWER_RUNNING;
268 PositionActualGet(&positionActual);
270 pathDesired.Mode = PATHDESIRED_MODE_CIRCLEPOSITIONRIGHT;
283 case FLIGHTSTATUS_FLIGHTMODE_PATHPLANNER:
284 case FLIGHTSTATUS_FLIGHTMODE_TABLETCONTROL:
285 state = FW_FOLLOWER_RUNNING;
289 case PATHDESIRED_MODE_ENDPOINT:
290 case PATHDESIRED_MODE_VECTOR:
291 case PATHDESIRED_MODE_CIRCLERIGHT:
292 case PATHDESIRED_MODE_CIRCLELEFT:
295 state = FW_FOLLOWER_ERR;
296 pathStatus.Status = PATHSTATUS_STATUS_CRITICAL;
298 AlarmsSet(SYSTEMALARMS_ALARM_PATHFOLLOWER,SYSTEMALARMS_ALARM_CRITICAL);
303 state = FW_FOLLOWER_IDLE;
309 case FW_FOLLOWER_RUNNING:
316 AlarmsSet(SYSTEMALARMS_ALARM_PATHFOLLOWER,SYSTEMALARMS_ALARM_WARNING);
321 case FW_FOLLOWER_IDLE:
323 northVelIntegral = 0;
330 airspeedErrorInt = 0;
333 case FW_FOLLOWER_ERR:
349 PositionActualData positionActual;
350 PositionActualGet(&positionActual);
351 VelocityActualData velocityActual;
352 VelocityActualGet(&velocityActual);
355 positionActual.North +
357 positionActual.East +
366 float altitudeSetpoint = 0;
368 case PATHDESIRED_MODE_CIRCLERIGHT:
369 case PATHDESIRED_MODE_CIRCLELEFT:
373 case PATHDESIRED_MODE_ENDPOINT:
374 case PATHDESIRED_MODE_VECTOR:
387 VelocityDesiredData velocityDesired;
397 float downError = altitudeSetpoint - positionActual.Down;
406 pathStatus.Status = PATHSTATUS_STATUS_INPROGRESS;
408 pathStatus.Status = PATHSTATUS_STATUS_COMPLETED;
412 VelocityDesiredSet(&velocityDesired);
429 VelocityDesiredData velocityDesired;
430 VelocityActualData velocityActual;
431 StabilizationDesiredData stabDesired;
432 AttitudeActualData attitudeActual;
433 FixedWingPathFollowerStatusData fixedwingpathfollowerStatus;
434 AirspeedActualData airspeedActual;
436 float groundspeedActual;
437 float groundspeedDesired;
438 float indicatedAirspeedActual;
439 float indicatedAirspeedDesired;
444 float descentspeedDesired;
445 float descentspeedError;
450 float bearingCommand;
452 FixedWingPathFollowerStatusGet(&fixedwingpathfollowerStatus);
454 VelocityActualGet(&velocityActual);
455 StabilizationDesiredGet(&stabDesired);
456 VelocityDesiredGet(&velocityDesired);
457 AttitudeActualGet(&attitudeActual);
458 AirspeedActualGet(&airspeedActual);
466 groundspeedActual = sqrtf( velocityActual.East*velocityActual.East + velocityActual.North*velocityActual.North );
473 groundspeedDesired = sqrtf(velocityDesired.North*velocityDesired.North + velocityDesired.East*velocityDesired.East);
474 indicatedAirspeedDesired =
bound_min_max( groundspeedDesired + indicatedAirspeedActualBias,
479 airspeedError = indicatedAirspeedDesired - indicatedAirspeedActual;
483 velocityDesired.Down,
486 descentspeedError = descentspeedDesired - velocityActual.Down;
489 fixedwingpathfollowerStatus.Errors[FIXEDWINGPATHFOLLOWERSTATUS_ERRORS_WIND] = 0;
490 if (groundspeedDesired - indicatedAirspeedActualBias <= 0 ) {
491 fixedwingpathfollowerStatus.Errors[FIXEDWINGPATHFOLLOWERSTATUS_ERRORS_WIND] = 1;
495 fixedwingpathfollowerStatus.Errors[FIXEDWINGPATHFOLLOWERSTATUS_ERRORS_HIGHSPEED] = 0;
496 fixedwingpathfollowerStatus.Errors[FIXEDWINGPATHFOLLOWERSTATUS_ERRORS_LOWSPEED] = 0;
498 fixedwingpathfollowerStatus.Errors[FIXEDWINGPATHFOLLOWERSTATUS_ERRORS_OVERSPEED] = 1;
502 fixedwingpathfollowerStatus.Errors[FIXEDWINGPATHFOLLOWERSTATUS_ERRORS_HIGHSPEED] = 1;
506 fixedwingpathfollowerStatus.Errors[FIXEDWINGPATHFOLLOWERSTATUS_ERRORS_LOWSPEED] = 1;
510 fixedwingpathfollowerStatus.Errors[FIXEDWINGPATHFOLLOWERSTATUS_ERRORS_STALLSPEED] = 1;
514 fixedwingpathfollowerStatus.Errors[FIXEDWINGPATHFOLLOWERSTATUS_ERRORS_STALLSPEED] = 1;
518 if (indicatedAirspeedActual<1e-6
f) {
521 fixedwingpathfollowerStatus.Errors[FIXEDWINGPATHFOLLOWERSTATUS_ERRORS_LOWSPEED] = 1;
531 powerError = -descentspeedError +
543 )*(1.0f-1.0f/(1.0f+30.0f/dT));
555 fixedwingpathfollowerStatus.Error[FIXEDWINGPATHFOLLOWERSTATUS_ERROR_POWER] = powerError;
556 fixedwingpathfollowerStatus.ErrorInt[FIXEDWINGPATHFOLLOWERSTATUS_ERRORINT_POWER] =
powerIntegral;
557 fixedwingpathfollowerStatus.Command[FIXEDWINGPATHFOLLOWERSTATUS_COMMAND_POWER] = powerCommand;
560 stabDesired.Thrust = powerCommand;
563 fixedwingpathfollowerStatus.Errors[FIXEDWINGPATHFOLLOWERSTATUS_ERRORS_LOWPOWER] = 0;
566 && velocityActual.Down > 0
567 && descentspeedDesired < 0
571 fixedwingpathfollowerStatus.Errors[FIXEDWINGPATHFOLLOWERSTATUS_ERRORS_LOWPOWER] = 1;
575 fixedwingpathfollowerStatus.Errors[FIXEDWINGPATHFOLLOWERSTATUS_ERRORS_HIGHPOWER] = 0;
578 && velocityActual.Down < 0
579 && descentspeedDesired > 0
583 fixedwingpathfollowerStatus.Errors[FIXEDWINGPATHFOLLOWERSTATUS_ERRORS_HIGHPOWER] = 1;
609 ) + verticalSpeedToPitchCommandComponent;
611 fixedwingpathfollowerStatus.Error[FIXEDWINGPATHFOLLOWERSTATUS_ERROR_SPEED] = airspeedError;
612 fixedwingpathfollowerStatus.ErrorInt[FIXEDWINGPATHFOLLOWERSTATUS_ERRORINT_SPEED] =
airspeedErrorInt;
613 fixedwingpathfollowerStatus.Command[FIXEDWINGPATHFOLLOWERSTATUS_COMMAND_SPEED] = pitchCommand;
621 fixedwingpathfollowerStatus.Errors[FIXEDWINGPATHFOLLOWERSTATUS_ERRORS_PITCHCONTROL] = 0;
624 && velocityActual.Down > 0
625 && descentspeedDesired < 0
628 fixedwingpathfollowerStatus.Errors[FIXEDWINGPATHFOLLOWERSTATUS_ERRORS_PITCHCONTROL] = 1;
635 if (groundspeedDesired> 1e-6
f) {
636 bearingError = RAD2DEG * (atan2f(velocityDesired.East,velocityDesired.North) - atan2f(velocityActual.East,velocityActual.North));
642 if (bearingError<-180.0
f) bearingError+=360.0f;
643 if (bearingError>180.0
f) bearingError-=360.0f;
651 fixedwingpathfollowerStatus.Error[FIXEDWINGPATHFOLLOWERSTATUS_ERROR_BEARING] = bearingError;
652 fixedwingpathfollowerStatus.ErrorInt[FIXEDWINGPATHFOLLOWERSTATUS_ERRORINT_BEARING] =
bearingIntegral;
653 fixedwingpathfollowerStatus.Command[FIXEDWINGPATHFOLLOWERSTATUS_COMMAND_BEARING] = bearingCommand;
669 stabDesired.ReprojectionMode = STABILIZATIONDESIRED_REPROJECTIONMODE_NONE;
671 stabDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_ROLL] = STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE;
672 stabDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_PITCH] = STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE;
673 stabDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_YAW] = STABILIZATIONDESIRED_STABILIZATIONMODE_MANUAL;
674 stabDesired.ThrustMode = STABILIZATIONDESIRED_THRUSTMODE_DIRECT;
676 if (isnan(stabDesired.Roll) || isnan(stabDesired.Pitch) || isnan(stabDesired.Yaw) || isnan(stabDesired.Thrust)) {
688 StabilizationDesiredSet(&stabDesired);
691 FixedWingPathFollowerStatusSet(&fixedwingpathfollowerStatus);
697 void *ctx,
void *obj,
int len)
699 (void) ctx; (void) obj; (void) len;
701 if (ev == NULL || ev->
obj == FixedWingPathFollowerSettingsHandle())
703 if (ev == NULL || ev->
obj == FixedWingAirspeedsHandle())
708 void *ctx,
void *obj,
713 AirspeedActualData airspeedActual;
715 AirspeedActualGet(&airspeedActual);
717 VelocityActualData velocityActual;
719 VelocityActualGet(&velocityActual);
721 float groundspeed = sqrtf(velocityActual.East*velocityActual.East + velocityActual.North*velocityActual.North );
uint32_t PIOS_Thread_Systime(void)
static float eastVelIntegral
static float accelIntegral
static PathDesiredData pathDesired
static void airspeedActualUpdatedCb(const UAVObjEvent *ev, void *ctx, void *obj, int len)
static void pathfollowerTask(void *parameters)
static float northVelIntegral
int32_t FixedWingPathFollowerInitialize()
void UAVObjCbSetFlag(const UAVObjEvent *objEv, void *ctx, void *obj, int len)
float fractional_progress
int32_t AlarmsSet(SystemAlarmsAlarmElem alarm, SystemAlarmsAlarmOptions severity)
static PathStatusData pathStatus
int32_t FixedWingPathFollowerStart()
static uint8_t updateFixedDesiredAttitude()
static float airspeedErrorInt
static bool path_desired_updated
static bool module_enabled
Path calculation library with common API.
bool PIOS_SENSORS_IsRegistered(enum pios_sensor_type type)
Checks if a sensor type is registered with the PIOS_SENSORS interface.
#define MODULE_INITCALL(ifn, sfn)
static float downVelIntegral
void path_progress(const PathDesiredData *pathDesired, const float *cur_point, struct path_status *status)
Compute progress along path and deviation from it.
struct pios_thread * PIOS_Thread_Create(void(*fp)(void *), const char *namep, size_t stack_bytes, void *argp, enum pios_thread_prio_e prio)
int32_t TaskMonitorAdd(TaskInfoRunningElem task, struct pios_thread *threadp)
float bound_min_max(float val, float min, float max)
Bound input value between min and max.
void PIOS_Thread_Sleep_Until(uint32_t *previous_ms, uint32_t increment_ms)
static float speedIntegral
void PIOS_Thread_Sleep(uint32_t time_ms)
Header for Coordinate conversions library in coordinate_conversions.c.
static float indicatedAirspeedActualBias
static float bearingIntegral
Includes PiOS and core architecture components.
static FixedWingAirspeedsData fixedWingAirspeeds
int32_t AlarmsClear(SystemAlarmsAlarmElem alarm)
float correction_direction[2]
static float powerIntegral
static FixedWingPathFollowerSettingsData fixedwingpathfollowerSettings
static void updatePathVelocity()
static struct pios_thread * pathfollowerTaskHandle
static void SettingsUpdatedCb(const UAVObjEvent *ev, void *ctx, void *obj, int len)