2012-08-04 19:12:36 -03:00
/****************************************************************************
*
* Copyright ( C ) 2012 PX4 Development Team . All rights reserved .
* Author : Thomas Gubler < thomasgubler @ student . ethz . ch >
* Julian Oes < joes @ student . ethz . ch >
*
* Redistribution and use in source and binary forms , with or without
* modification , are permitted provided that the following conditions
* are met :
*
* 1. Redistributions of source code must retain the above copyright
* notice , this list of conditions and the following disclaimer .
* 2. Redistributions in binary form must reproduce the above copyright
* notice , this list of conditions and the following disclaimer in
* the documentation and / or other materials provided with the
* distribution .
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission .
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* " AS IS " AND ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT
* LIMITED TO , THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT , INDIRECT ,
* INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING ,
* BUT NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS
* OF USE , DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT
* LIABILITY , OR TORT ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE , EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE .
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2012-08-06 18:43:09 -03:00
/**
* @ file state_machine_helper . c
* State machine helper functions implementations
*/
2012-08-04 19:12:36 -03:00
# include <stdio.h>
2012-08-27 17:57:20 -03:00
# include <unistd.h>
2012-08-04 19:12:36 -03:00
# include <uORB/uORB.h>
# include <uORB/topics/vehicle_status.h>
2012-09-02 06:33:52 -03:00
# include <uORB/topics/actuator_controls.h>
2012-08-04 19:12:36 -03:00
# include <systemlib/systemlib.h>
2012-10-23 22:02:36 -03:00
# include <drivers/drv_hrt.h>
2012-08-13 13:53:37 -03:00
# include <mavlink/mavlink_log.h>
2012-08-04 19:12:36 -03:00
2012-08-27 17:57:20 -03:00
# include "state_machine_helper.h"
2013-01-11 02:42:09 -04:00
static const char * system_state_txt [ ] = {
2012-08-04 19:12:36 -03:00
" SYSTEM_STATE_PREFLIGHT " ,
" SYSTEM_STATE_STANDBY " ,
" SYSTEM_STATE_GROUND_READY " ,
" SYSTEM_STATE_MANUAL " ,
" SYSTEM_STATE_STABILIZED " ,
" SYSTEM_STATE_AUTO " ,
" SYSTEM_STATE_MISSION_ABORT " ,
" SYSTEM_STATE_EMCY_LANDING " ,
" SYSTEM_STATE_EMCY_CUTOFF " ,
" SYSTEM_STATE_GROUND_ERROR " ,
" SYSTEM_STATE_REBOOT " ,
} ;
2012-08-13 13:53:37 -03:00
/**
* Transition from one state to another
*/
int do_state_update ( int status_pub , struct vehicle_status_s * current_status , const int mavlink_fd , commander_state_machine_t new_state )
2012-08-04 19:12:36 -03:00
{
int invalid_state = false ;
2012-08-13 13:53:37 -03:00
int ret = ERROR ;
2012-08-04 19:12:36 -03:00
2012-08-06 18:43:09 -03:00
commander_state_machine_t old_state = current_status - > state_machine ;
2012-08-04 19:12:36 -03:00
switch ( new_state ) {
case SYSTEM_STATE_MISSION_ABORT : {
/* Indoor or outdoor */
2012-08-27 17:57:20 -03:00
// if (flight_environment_parameter == PX4_FLIGHT_ENVIRONMENT_OUTDOOR) {
2013-01-11 02:42:09 -04:00
ret = do_state_update ( status_pub , current_status , mavlink_fd , ( commander_state_machine_t ) SYSTEM_STATE_EMCY_LANDING ) ;
2012-08-04 19:12:36 -03:00
2012-08-27 17:57:20 -03:00
// } else {
// ret = do_state_update(status_pub, current_status, mavlink_fd, (commander_state_machine_t)SYSTEM_STATE_EMCY_CUTOFF);
// }
2012-08-04 19:12:36 -03:00
}
break ;
case SYSTEM_STATE_EMCY_LANDING :
/* Tell the controller to land */
2012-08-18 11:48:43 -03:00
/* set system flags according to state */
current_status - > flag_system_armed = true ;
2012-08-04 19:12:36 -03:00
2013-01-11 03:24:28 -04:00
warnx ( " EMERGENCY LANDING! \n " ) ;
mavlink_log_critical ( mavlink_fd , " EMERGENCY LANDING! " ) ;
2012-08-04 19:12:36 -03:00
break ;
case SYSTEM_STATE_EMCY_CUTOFF :
2012-08-13 13:53:37 -03:00
/* Tell the controller to cutoff the motors (thrust = 0) */
2012-08-18 11:48:43 -03:00
/* set system flags according to state */
current_status - > flag_system_armed = false ;
2013-01-11 03:24:28 -04:00
warnx ( " EMERGENCY MOTOR CUTOFF! \n " ) ;
mavlink_log_critical ( mavlink_fd , " EMERGENCY MOTOR CUTOFF! " ) ;
2012-08-04 19:12:36 -03:00
break ;
case SYSTEM_STATE_GROUND_ERROR :
2012-08-18 11:48:43 -03:00
/* set system flags according to state */
/* prevent actuators from arming */
current_status - > flag_system_armed = false ;
2013-01-11 03:24:28 -04:00
warnx ( " GROUND ERROR, locking down propulsion system \n " ) ;
mavlink_log_critical ( mavlink_fd , " GROUND ERROR, locking down system " ) ;
2012-08-04 19:12:36 -03:00
break ;
case SYSTEM_STATE_PREFLIGHT :
2012-08-06 18:43:09 -03:00
if ( current_status - > state_machine = = SYSTEM_STATE_STANDBY
2013-01-11 02:42:09 -04:00
| | current_status - > state_machine = = SYSTEM_STATE_PREFLIGHT ) {
2012-08-18 11:48:43 -03:00
/* set system flags according to state */
2012-09-02 06:20:36 -03:00
current_status - > flag_system_armed = false ;
2013-01-11 03:24:28 -04:00
mavlink_log_critical ( mavlink_fd , " Switched to PREFLIGHT state " ) ;
2013-01-11 02:42:09 -04:00
2012-08-06 18:43:09 -03:00
} else {
invalid_state = true ;
2013-01-11 03:24:28 -04:00
mavlink_log_critical ( mavlink_fd , " REFUSED to switch to PREFLIGHT state " ) ;
2012-08-06 18:43:09 -03:00
}
2013-01-11 02:42:09 -04:00
2012-08-04 19:12:36 -03:00
break ;
case SYSTEM_STATE_REBOOT :
2012-08-13 13:53:37 -03:00
if ( current_status - > state_machine = = SYSTEM_STATE_STANDBY
2013-01-13 19:22:35 -04:00
| | current_status - > state_machine = = SYSTEM_STATE_PREFLIGHT
| | current_status - > flag_hil_enabled ) {
2012-08-13 13:53:37 -03:00
invalid_state = false ;
2012-08-18 11:48:43 -03:00
/* set system flags according to state */
current_status - > flag_system_armed = false ;
2013-01-11 03:24:28 -04:00
mavlink_log_critical ( mavlink_fd , " REBOOTING SYSTEM " ) ;
2012-08-13 13:53:37 -03:00
usleep ( 500000 ) ;
2012-10-28 02:42:43 -03:00
up_systemreset ( ) ;
2012-08-13 13:53:37 -03:00
/* SPECIAL CASE: NEVER RETURNS FROM THIS FUNCTION CALL */
2013-01-11 02:42:09 -04:00
2012-08-13 13:53:37 -03:00
} else {
invalid_state = true ;
2013-01-11 03:24:28 -04:00
mavlink_log_critical ( mavlink_fd , " REFUSED to REBOOT " ) ;
2012-08-13 13:53:37 -03:00
}
2013-01-11 02:42:09 -04:00
2012-08-04 19:12:36 -03:00
break ;
case SYSTEM_STATE_STANDBY :
2012-08-18 11:48:43 -03:00
/* set system flags according to state */
/* standby enforces disarmed */
current_status - > flag_system_armed = false ;
2013-01-11 03:24:28 -04:00
mavlink_log_critical ( mavlink_fd , " Switched to STANDBY state " ) ;
2012-08-04 19:12:36 -03:00
break ;
case SYSTEM_STATE_GROUND_READY :
2012-08-18 11:48:43 -03:00
/* set system flags according to state */
/* ground ready has motors / actuators armed */
current_status - > flag_system_armed = true ;
2013-01-11 03:24:28 -04:00
mavlink_log_critical ( mavlink_fd , " Switched to GROUND READY state " ) ;
2012-08-04 19:12:36 -03:00
break ;
case SYSTEM_STATE_AUTO :
2012-08-18 11:48:43 -03:00
/* set system flags according to state */
/* auto is airborne and in auto mode, motors armed */
current_status - > flag_system_armed = true ;
2013-01-11 03:24:28 -04:00
mavlink_log_critical ( mavlink_fd , " Switched to FLYING / AUTO mode " ) ;
2012-08-04 19:12:36 -03:00
break ;
case SYSTEM_STATE_STABILIZED :
2012-08-18 11:48:43 -03:00
/* set system flags according to state */
current_status - > flag_system_armed = true ;
2013-01-11 03:24:28 -04:00
mavlink_log_critical ( mavlink_fd , " Switched to FLYING / STABILIZED mode " ) ;
2012-08-04 19:12:36 -03:00
break ;
case SYSTEM_STATE_MANUAL :
2012-08-18 11:48:43 -03:00
/* set system flags according to state */
current_status - > flag_system_armed = true ;
2013-01-11 03:24:28 -04:00
mavlink_log_critical ( mavlink_fd , " Switched to FLYING / MANUAL mode " ) ;
2012-08-04 19:12:36 -03:00
break ;
default :
invalid_state = true ;
break ;
}
2012-08-06 18:43:09 -03:00
if ( invalid_state = = false | | old_state ! = new_state ) {
2012-08-04 19:12:36 -03:00
current_status - > state_machine = new_state ;
2012-08-13 13:53:37 -03:00
state_machine_publish ( status_pub , current_status , mavlink_fd ) ;
2012-09-05 06:37:17 -03:00
publish_armed_status ( current_status ) ;
2012-08-27 17:57:20 -03:00
ret = OK ;
2012-08-04 19:12:36 -03:00
}
2013-01-11 02:42:09 -04:00
2012-08-16 08:09:35 -03:00
if ( invalid_state ) {
2013-01-11 03:24:28 -04:00
mavlink_log_critical ( mavlink_fd , " REJECTING invalid state transition " ) ;
2012-08-27 17:57:20 -03:00
ret = ERROR ;
2012-08-16 08:09:35 -03:00
}
2013-01-11 02:42:09 -04:00
2012-08-27 17:57:20 -03:00
return ret ;
2012-08-04 19:12:36 -03:00
}
2013-01-11 02:42:09 -04:00
void state_machine_publish ( int status_pub , struct vehicle_status_s * current_status , const int mavlink_fd )
{
2012-08-06 18:43:09 -03:00
/* publish the new state */
current_status - > counter + + ;
current_status - > timestamp = hrt_absolute_time ( ) ;
2012-11-14 10:18:16 -04:00
/* assemble state vector based on flag values */
if ( current_status - > flag_control_rates_enabled ) {
current_status - > onboard_control_sensors_present | = 0x400 ;
2013-01-11 02:42:09 -04:00
2012-11-14 10:18:16 -04:00
} else {
current_status - > onboard_control_sensors_present & = ~ 0x400 ;
}
2013-01-11 02:42:09 -04:00
2012-11-14 10:18:16 -04:00
current_status - > onboard_control_sensors_present | = ( current_status - > flag_control_attitude_enabled ) ? 0x800 : 0 ;
current_status - > onboard_control_sensors_present | = ( current_status - > flag_control_attitude_enabled ) ? 0x1000 : 0 ;
current_status - > onboard_control_sensors_present | = ( current_status - > flag_control_velocity_enabled | | current_status - > flag_control_position_enabled ) ? 0x2000 : 0 ;
current_status - > onboard_control_sensors_present | = ( current_status - > flag_control_velocity_enabled | | current_status - > flag_control_position_enabled ) ? 0x4000 : 0 ;
current_status - > onboard_control_sensors_enabled | = ( current_status - > flag_control_rates_enabled ) ? 0x400 : 0 ;
current_status - > onboard_control_sensors_enabled | = ( current_status - > flag_control_attitude_enabled ) ? 0x800 : 0 ;
current_status - > onboard_control_sensors_enabled | = ( current_status - > flag_control_attitude_enabled ) ? 0x1000 : 0 ;
current_status - > onboard_control_sensors_enabled | = ( current_status - > flag_control_velocity_enabled | | current_status - > flag_control_position_enabled ) ? 0x2000 : 0 ;
current_status - > onboard_control_sensors_enabled | = ( current_status - > flag_control_velocity_enabled | | current_status - > flag_control_position_enabled ) ? 0x4000 : 0 ;
2012-08-06 18:43:09 -03:00
orb_publish ( ORB_ID ( vehicle_status ) , status_pub , current_status ) ;
2012-12-27 13:27:08 -04:00
printf ( " [cmd] new state: %s \n " , system_state_txt [ current_status - > state_machine ] ) ;
2012-08-06 18:43:09 -03:00
}
2013-01-11 02:42:09 -04:00
void publish_armed_status ( const struct vehicle_status_s * current_status )
{
2012-09-05 06:37:17 -03:00
struct actuator_armed_s armed ;
armed . armed = current_status - > flag_system_armed ;
2012-11-09 11:30:00 -04:00
/* lock down actuators if required, only in HIL */
armed . lockdown = ( current_status - > flag_hil_enabled ) ? true : false ;
2012-09-05 06:37:17 -03:00
orb_advert_t armed_pub = orb_advertise ( ORB_ID ( actuator_armed ) , & armed ) ;
orb_publish ( ORB_ID ( actuator_armed ) , armed_pub , & armed ) ;
}
2012-08-04 19:12:36 -03:00
/*
* Private functions , update the state machine
*/
2012-08-13 13:53:37 -03:00
void state_machine_emergency_always_critical ( int status_pub , struct vehicle_status_s * current_status , const int mavlink_fd )
2012-08-04 19:12:36 -03:00
{
2013-01-11 03:24:28 -04:00
warnx ( " EMERGENCY HANDLER \n " ) ;
2012-08-04 19:12:36 -03:00
/* Depending on the current state go to one of the error states */
if ( current_status - > state_machine = = SYSTEM_STATE_PREFLIGHT | | current_status - > state_machine = = SYSTEM_STATE_STANDBY | | current_status - > state_machine = = SYSTEM_STATE_GROUND_READY ) {
2012-08-13 13:53:37 -03:00
do_state_update ( status_pub , current_status , mavlink_fd , ( commander_state_machine_t ) SYSTEM_STATE_GROUND_ERROR ) ;
2012-08-04 19:12:36 -03:00
} else if ( current_status - > state_machine = = SYSTEM_STATE_AUTO | | current_status - > state_machine = = SYSTEM_STATE_MANUAL ) {
2013-01-11 02:42:09 -04:00
2012-11-20 11:50:55 -04:00
// DO NOT abort mission
//do_state_update(status_pub, current_status, mavlink_fd, (commander_state_machine_t)SYSTEM_STATE_MISSION_ABORT);
2012-08-04 19:12:36 -03:00
} else {
2013-01-11 03:24:28 -04:00
warnx ( " Unknown system state: #%d \n " , current_status - > state_machine ) ;
2012-08-04 19:12:36 -03:00
}
}
2012-08-13 13:53:37 -03:00
void state_machine_emergency ( int status_pub , struct vehicle_status_s * current_status , const int mavlink_fd ) //do not call state_machine_emergency_always_critical if we are in manual mode for these errors
2012-08-04 19:12:36 -03:00
{
if ( current_status - > state_machine ! = SYSTEM_STATE_MANUAL ) { //if we are in manual: user can react to errors themself
2012-08-13 13:53:37 -03:00
state_machine_emergency_always_critical ( status_pub , current_status , mavlink_fd ) ;
2012-08-04 19:12:36 -03:00
} else {
2012-12-27 13:27:08 -04:00
//global_data_send_mavlink_statustext_message_out("[cmd] ERROR: take action immediately! (did not switch to error state because the system is in manual mode)", MAV_SEVERITY_CRITICAL);
2012-08-04 19:12:36 -03:00
}
}
// /*
// * Wrapper functions (to be used in the commander), all functions assume lock on current_status
// */
// /* These functions decide if an emergency exits and then switch to SYSTEM_STATE_MISSION_ABORT or SYSTEM_STATE_GROUND_ERROR
// *
// * START SUBSYSTEM/EMERGENCY FUNCTIONS
// * */
// void update_state_machine_subsystem_present(int status_pub, struct vehicle_status_s *current_status, subsystem_type_t *subsystem_type)
// {
// current_status->onboard_control_sensors_present |= 1 << *subsystem_type;
// current_status->counter++;
// current_status->timestamp = hrt_absolute_time();
// orb_publish(ORB_ID(vehicle_status), status_pub, current_status);
// }
// void update_state_machine_subsystem_notpresent(int status_pub, struct vehicle_status_s *current_status, subsystem_type_t *subsystem_type)
// {
// current_status->onboard_control_sensors_present &= ~(1 << *subsystem_type);
// current_status->counter++;
// current_status->timestamp = hrt_absolute_time();
// orb_publish(ORB_ID(vehicle_status), status_pub, current_status);
// /* if a subsystem was removed something went completely wrong */
// switch (*subsystem_type) {
// case SUBSYSTEM_TYPE_GYRO:
// //global_data_send_mavlink_statustext_message_out("Commander: gyro not present", MAV_SEVERITY_EMERGENCY);
// state_machine_emergency_always_critical(status_pub, current_status);
// break;
// case SUBSYSTEM_TYPE_ACC:
// //global_data_send_mavlink_statustext_message_out("Commander: accelerometer not present", MAV_SEVERITY_EMERGENCY);
// state_machine_emergency_always_critical(status_pub, current_status);
// break;
// case SUBSYSTEM_TYPE_MAG:
// //global_data_send_mavlink_statustext_message_out("Commander: magnetometer not present", MAV_SEVERITY_EMERGENCY);
// state_machine_emergency_always_critical(status_pub, current_status);
// break;
// case SUBSYSTEM_TYPE_GPS:
// {
// uint8_t flight_env = global_data_parameter_storage->pm.param_values[PARAM_FLIGHT_ENV];
// if (flight_env == PX4_FLIGHT_ENVIRONMENT_OUTDOOR) {
// //global_data_send_mavlink_statustext_message_out("Commander: GPS not present", MAV_SEVERITY_EMERGENCY);
// state_machine_emergency(status_pub, current_status);
// }
// }
// break;
// default:
// break;
// }
// }
// void update_state_machine_subsystem_enabled(int status_pub, struct vehicle_status_s *current_status, subsystem_type_t *subsystem_type)
// {
// current_status->onboard_control_sensors_enabled |= 1 << *subsystem_type;
// current_status->counter++;
// current_status->timestamp = hrt_absolute_time();
// orb_publish(ORB_ID(vehicle_status), status_pub, current_status);
// }
// void update_state_machine_subsystem_disabled(int status_pub, struct vehicle_status_s *current_status, subsystem_type_t *subsystem_type)
// {
// current_status->onboard_control_sensors_enabled &= ~(1 << *subsystem_type);
// current_status->counter++;
// current_status->timestamp = hrt_absolute_time();
// orb_publish(ORB_ID(vehicle_status), status_pub, current_status);
// /* if a subsystem was disabled something went completely wrong */
// switch (*subsystem_type) {
// case SUBSYSTEM_TYPE_GYRO:
// //global_data_send_mavlink_statustext_message_out("Commander: EMERGENCY - gyro disabled", MAV_SEVERITY_EMERGENCY);
// state_machine_emergency_always_critical(status_pub, current_status);
// break;
// case SUBSYSTEM_TYPE_ACC:
// //global_data_send_mavlink_statustext_message_out("Commander: EMERGENCY - accelerometer disabled", MAV_SEVERITY_EMERGENCY);
// state_machine_emergency_always_critical(status_pub, current_status);
// break;
// case SUBSYSTEM_TYPE_MAG:
// //global_data_send_mavlink_statustext_message_out("Commander: EMERGENCY - magnetometer disabled", MAV_SEVERITY_EMERGENCY);
// state_machine_emergency_always_critical(status_pub, current_status);
// break;
// case SUBSYSTEM_TYPE_GPS:
// {
// uint8_t flight_env = (uint8_t)(global_data_parameter_storage->pm.param_values[PARAM_FLIGHT_ENV]);
// if (flight_env == PX4_FLIGHT_ENVIRONMENT_OUTDOOR) {
// //global_data_send_mavlink_statustext_message_out("Commander: EMERGENCY - GPS disabled", MAV_SEVERITY_EMERGENCY);
// state_machine_emergency(status_pub, current_status);
// }
// }
// break;
// default:
// break;
// }
// }
// void update_state_machine_subsystem_healthy(int status_pub, struct vehicle_status_s *current_status, subsystem_type_t *subsystem_type)
// {
// current_status->onboard_control_sensors_health |= 1 << *subsystem_type;
// current_status->counter++;
// current_status->timestamp = hrt_absolute_time();
// orb_publish(ORB_ID(vehicle_status), status_pub, current_status);
// switch (*subsystem_type) {
// case SUBSYSTEM_TYPE_GYRO:
// //TODO state machine change (recovering)
// break;
// case SUBSYSTEM_TYPE_ACC:
// //TODO state machine change
// break;
// case SUBSYSTEM_TYPE_MAG:
// //TODO state machine change
// break;
// case SUBSYSTEM_TYPE_GPS:
// //TODO state machine change
// break;
// default:
// break;
// }
// }
// void update_state_machine_subsystem_unhealthy(int status_pub, struct vehicle_status_s *current_status, subsystem_type_t *subsystem_type)
// {
// bool previosly_healthy = (bool)(current_status->onboard_control_sensors_health & 1 << *subsystem_type);
// current_status->onboard_control_sensors_health &= ~(1 << *subsystem_type);
// current_status->counter++;
// current_status->timestamp = hrt_absolute_time();
// orb_publish(ORB_ID(vehicle_status), status_pub, current_status);
// /* if we received unhealthy message more than *_HEALTH_COUNTER_LIMIT, switch to error state */
// switch (*subsystem_type) {
// case SUBSYSTEM_TYPE_GYRO:
// //global_data_send_mavlink_statustext_message_out("Commander: gyro unhealthy", MAV_SEVERITY_CRITICAL);
// if (previosly_healthy) //only throw emergency if previously healthy
// state_machine_emergency_always_critical(status_pub, current_status);
// break;
// case SUBSYSTEM_TYPE_ACC:
// //global_data_send_mavlink_statustext_message_out("Commander: accelerometer unhealthy", MAV_SEVERITY_CRITICAL);
// if (previosly_healthy) //only throw emergency if previously healthy
// state_machine_emergency_always_critical(status_pub, current_status);
// break;
// case SUBSYSTEM_TYPE_MAG:
// //global_data_send_mavlink_statustext_message_out("Commander: magnetometer unhealthy", MAV_SEVERITY_CRITICAL);
// if (previosly_healthy) //only throw emergency if previously healthy
// state_machine_emergency_always_critical(status_pub, current_status);
// break;
// case SUBSYSTEM_TYPE_GPS:
// // //TODO: remove this block
// // break;
// // ///////////////////
// //global_data_send_mavlink_statustext_message_out("Commander: GPS unhealthy", MAV_SEVERITY_CRITICAL);
// // printf("previosly_healthy = %u\n", previosly_healthy);
// if (previosly_healthy) //only throw emergency if previously healthy
// state_machine_emergency(status_pub, current_status);
// break;
// default:
// break;
// }
// }
/* END SUBSYSTEM/EMERGENCY FUNCTIONS*/
2012-08-13 13:53:37 -03:00
void update_state_machine_got_position_fix ( int status_pub , struct vehicle_status_s * current_status , const int mavlink_fd )
2012-08-04 19:12:36 -03:00
{
/* Depending on the current state switch state */
if ( current_status - > state_machine = = SYSTEM_STATE_PREFLIGHT ) {
2012-08-13 13:53:37 -03:00
do_state_update ( status_pub , current_status , mavlink_fd , ( commander_state_machine_t ) SYSTEM_STATE_STANDBY ) ;
2012-08-04 19:12:36 -03:00
}
}
2012-08-13 13:53:37 -03:00
void update_state_machine_no_position_fix ( int status_pub , struct vehicle_status_s * current_status , const int mavlink_fd )
2012-08-04 19:12:36 -03:00
{
/* Depending on the current state switch state */
if ( current_status - > state_machine = = SYSTEM_STATE_STANDBY | | current_status - > state_machine = = SYSTEM_STATE_GROUND_READY | | current_status - > state_machine = = SYSTEM_STATE_AUTO ) {
2012-08-13 13:53:37 -03:00
state_machine_emergency ( status_pub , current_status , mavlink_fd ) ;
2012-08-04 19:12:36 -03:00
}
}
2012-08-13 13:53:37 -03:00
void update_state_machine_arm ( int status_pub , struct vehicle_status_s * current_status , const int mavlink_fd )
2012-08-04 19:12:36 -03:00
{
if ( current_status - > state_machine = = SYSTEM_STATE_STANDBY ) {
2012-12-27 13:27:08 -04:00
printf ( " [cmd] arming \n " ) ;
2012-08-13 13:53:37 -03:00
do_state_update ( status_pub , current_status , mavlink_fd , ( commander_state_machine_t ) SYSTEM_STATE_GROUND_READY ) ;
2012-08-18 11:48:43 -03:00
}
2012-08-04 19:12:36 -03:00
}
2012-08-13 13:53:37 -03:00
void update_state_machine_disarm ( int status_pub , struct vehicle_status_s * current_status , const int mavlink_fd )
2012-08-04 19:12:36 -03:00
{
if ( current_status - > state_machine = = SYSTEM_STATE_GROUND_READY | | current_status - > state_machine = = SYSTEM_STATE_MANUAL | | current_status - > state_machine = = SYSTEM_STATE_PREFLIGHT ) {
2012-12-27 13:27:08 -04:00
printf ( " [cmd] going standby \n " ) ;
2012-08-13 13:53:37 -03:00
do_state_update ( status_pub , current_status , mavlink_fd , ( commander_state_machine_t ) SYSTEM_STATE_STANDBY ) ;
2012-08-04 19:12:36 -03:00
} else if ( current_status - > state_machine = = SYSTEM_STATE_STABILIZED | | current_status - > state_machine = = SYSTEM_STATE_AUTO ) {
2012-12-27 13:27:08 -04:00
printf ( " [cmd] MISSION ABORT! \n " ) ;
2012-08-13 13:53:37 -03:00
do_state_update ( status_pub , current_status , mavlink_fd , ( commander_state_machine_t ) SYSTEM_STATE_STANDBY ) ;
2012-08-04 19:12:36 -03:00
}
}
2012-08-13 13:53:37 -03:00
void update_state_machine_mode_manual ( int status_pub , struct vehicle_status_s * current_status , const int mavlink_fd )
2012-08-04 19:12:36 -03:00
{
2012-08-13 13:53:37 -03:00
int old_mode = current_status - > flight_mode ;
current_status - > flight_mode = VEHICLE_FLIGHT_MODE_MANUAL ;
2012-12-27 13:27:08 -04:00
2012-10-13 07:25:30 -03:00
current_status - > flag_control_manual_enabled = true ;
2012-12-30 04:53:45 -04:00
/* set behaviour based on airframe */
2012-12-30 06:01:09 -04:00
if ( ( current_status - > system_type = = VEHICLE_TYPE_QUADROTOR ) | |
2013-01-11 02:42:09 -04:00
( current_status - > system_type = = VEHICLE_TYPE_HEXAROTOR ) | |
( current_status - > system_type = = VEHICLE_TYPE_OCTOROTOR ) ) {
2012-12-30 04:53:45 -04:00
/* assuming a rotary wing, set to SAS */
2012-12-30 06:01:09 -04:00
current_status - > manual_control_mode = VEHICLE_MANUAL_CONTROL_MODE_SAS ;
current_status - > flag_control_attitude_enabled = true ;
current_status - > flag_control_rates_enabled = true ;
2013-01-11 02:42:09 -04:00
2012-12-30 04:53:45 -04:00
} else {
/* assuming a fixed wing, set to direct pass-through */
2012-12-30 06:01:09 -04:00
current_status - > manual_control_mode = VEHICLE_MANUAL_CONTROL_MODE_DIRECT ;
current_status - > flag_control_attitude_enabled = false ;
current_status - > flag_control_rates_enabled = false ;
2012-12-30 04:53:45 -04:00
}
2012-08-13 13:53:37 -03:00
if ( old_mode ! = current_status - > flight_mode ) state_machine_publish ( status_pub , current_status , mavlink_fd ) ;
2012-08-04 19:12:36 -03:00
if ( current_status - > state_machine = = SYSTEM_STATE_GROUND_READY | | current_status - > state_machine = = SYSTEM_STATE_STABILIZED | | current_status - > state_machine = = SYSTEM_STATE_AUTO ) {
2012-12-27 13:27:08 -04:00
printf ( " [cmd] manual mode \n " ) ;
2012-08-13 13:53:37 -03:00
do_state_update ( status_pub , current_status , mavlink_fd , ( commander_state_machine_t ) SYSTEM_STATE_MANUAL ) ;
2012-08-04 19:12:36 -03:00
}
}
2012-08-13 13:53:37 -03:00
void update_state_machine_mode_stabilized ( int status_pub , struct vehicle_status_s * current_status , const int mavlink_fd )
2012-08-04 19:12:36 -03:00
{
2012-12-30 19:41:11 -04:00
if ( current_status - > state_machine = = SYSTEM_STATE_GROUND_READY | | current_status - > state_machine = = SYSTEM_STATE_STABILIZED | | current_status - > state_machine = = SYSTEM_STATE_MANUAL | | current_status - > state_machine = = SYSTEM_STATE_AUTO ) {
2012-12-27 13:27:08 -04:00
int old_mode = current_status - > flight_mode ;
2012-12-30 19:41:11 -04:00
int old_manual_control_mode = current_status - > manual_control_mode ;
current_status - > flight_mode = VEHICLE_FLIGHT_MODE_MANUAL ;
current_status - > manual_control_mode = VEHICLE_MANUAL_CONTROL_MODE_SAS ;
2012-12-27 13:27:08 -04:00
current_status - > flag_control_attitude_enabled = true ;
current_status - > flag_control_rates_enabled = true ;
2012-12-30 19:41:11 -04:00
current_status - > flag_control_manual_enabled = true ;
2013-01-11 02:42:09 -04:00
2012-12-30 19:41:11 -04:00
if ( old_mode ! = current_status - > flight_mode | |
2013-01-11 02:42:09 -04:00
old_manual_control_mode ! = current_status - > manual_control_mode ) {
2013-01-06 09:43:10 -04:00
printf ( " [cmd] att stabilized mode \n " ) ;
2012-12-30 19:41:11 -04:00
do_state_update ( status_pub , current_status , mavlink_fd , ( commander_state_machine_t ) SYSTEM_STATE_MANUAL ) ;
state_machine_publish ( status_pub , current_status , mavlink_fd ) ;
}
2012-12-27 13:27:08 -04:00
}
}
2012-08-13 13:53:37 -03:00
2012-12-30 19:41:11 -04:00
void update_state_machine_mode_guided ( int status_pub , struct vehicle_status_s * current_status , const int mavlink_fd )
2012-12-27 13:27:08 -04:00
{
if ( ! current_status - > flag_vector_flight_mode_ok ) {
2012-12-30 19:41:11 -04:00
mavlink_log_critical ( mavlink_fd , " NO POS LOCK, REJ. GUIDED MODE " ) ;
tune_error ( ) ;
2012-12-27 13:27:08 -04:00
return ;
}
2013-01-11 02:42:09 -04:00
2012-08-04 19:12:36 -03:00
if ( current_status - > state_machine = = SYSTEM_STATE_GROUND_READY | | current_status - > state_machine = = SYSTEM_STATE_MANUAL | | current_status - > state_machine = = SYSTEM_STATE_AUTO ) {
2012-12-30 19:41:11 -04:00
printf ( " [cmd] position guided mode \n " ) ;
2012-12-27 13:27:08 -04:00
int old_mode = current_status - > flight_mode ;
2012-12-30 19:41:11 -04:00
current_status - > flight_mode = VEHICLE_FLIGHT_MODE_STAB ;
2012-12-27 13:27:08 -04:00
current_status - > flag_control_manual_enabled = false ;
current_status - > flag_control_attitude_enabled = true ;
current_status - > flag_control_rates_enabled = true ;
2012-08-13 13:53:37 -03:00
do_state_update ( status_pub , current_status , mavlink_fd , ( commander_state_machine_t ) SYSTEM_STATE_STABILIZED ) ;
2013-01-11 02:42:09 -04:00
2012-12-27 13:27:08 -04:00
if ( old_mode ! = current_status - > flight_mode ) state_machine_publish ( status_pub , current_status , mavlink_fd ) ;
2012-12-30 19:41:11 -04:00
2012-08-04 19:12:36 -03:00
}
}
2012-08-13 13:53:37 -03:00
void update_state_machine_mode_auto ( int status_pub , struct vehicle_status_s * current_status , const int mavlink_fd )
2012-08-04 19:12:36 -03:00
{
2012-12-27 13:27:08 -04:00
if ( ! current_status - > flag_vector_flight_mode_ok ) {
mavlink_log_critical ( mavlink_fd , " NO POS LOCK, REJ. AUTO MODE " ) ;
return ;
}
2013-01-11 02:42:09 -04:00
2012-08-04 19:12:36 -03:00
if ( current_status - > state_machine = = SYSTEM_STATE_GROUND_READY | | current_status - > state_machine = = SYSTEM_STATE_MANUAL | | current_status - > state_machine = = SYSTEM_STATE_STABILIZED ) {
2012-12-27 13:27:08 -04:00
printf ( " [cmd] auto mode \n " ) ;
int old_mode = current_status - > flight_mode ;
current_status - > flight_mode = VEHICLE_FLIGHT_MODE_AUTO ;
current_status - > flag_control_manual_enabled = false ;
current_status - > flag_control_attitude_enabled = true ;
current_status - > flag_control_rates_enabled = true ;
2012-08-13 13:53:37 -03:00
do_state_update ( status_pub , current_status , mavlink_fd , ( commander_state_machine_t ) SYSTEM_STATE_AUTO ) ;
2013-01-11 02:42:09 -04:00
2012-12-27 13:27:08 -04:00
if ( old_mode ! = current_status - > flight_mode ) state_machine_publish ( status_pub , current_status , mavlink_fd ) ;
2012-08-04 19:12:36 -03:00
}
}
2012-08-13 13:53:37 -03:00
uint8_t update_state_machine_mode_request ( int status_pub , struct vehicle_status_s * current_status , const int mavlink_fd , uint8_t mode )
2012-08-04 19:12:36 -03:00
{
uint8_t ret = 1 ;
2013-01-11 02:42:09 -04:00
2012-12-27 13:27:08 -04:00
/* Switch on HIL if in standby and not already in HIL mode */
if ( ( mode & VEHICLE_MODE_FLAG_HIL_ENABLED )
2013-01-11 02:42:09 -04:00
& & ! current_status - > flag_hil_enabled ) {
2012-12-27 13:27:08 -04:00
if ( ( current_status - > state_machine = = SYSTEM_STATE_STANDBY ) ) {
/* Enable HIL on request */
current_status - > flag_hil_enabled = true ;
ret = OK ;
state_machine_publish ( status_pub , current_status , mavlink_fd ) ;
publish_armed_status ( current_status ) ;
printf ( " [cmd] Enabling HIL, locking down all actuators for safety. \n \t (Arming the system will not activate them while in HIL mode) \n " ) ;
2013-01-11 02:42:09 -04:00
2012-12-27 13:27:08 -04:00
} else if ( current_status - > state_machine ! = SYSTEM_STATE_STANDBY & &
2013-01-11 02:42:09 -04:00
current_status - > flag_system_armed ) {
2012-12-27 13:27:08 -04:00
2013-01-11 03:24:28 -04:00
mavlink_log_critical ( mavlink_fd , " REJECTING HIL, disarm first! " )
2013-01-11 02:42:09 -04:00
2012-12-27 13:27:08 -04:00
} else {
2013-01-11 03:24:28 -04:00
mavlink_log_critical ( mavlink_fd , " REJECTING HIL, not in standby. " )
2012-12-27 13:27:08 -04:00
}
2012-12-01 11:28:53 -04:00
}
2013-01-11 02:42:09 -04:00
2012-12-01 11:28:53 -04:00
/* switch manual / auto */
if ( mode & VEHICLE_MODE_FLAG_AUTO_ENABLED ) {
update_state_machine_mode_auto ( status_pub , current_status , mavlink_fd ) ;
2013-01-11 02:42:09 -04:00
2012-12-01 11:28:53 -04:00
} else if ( mode & VEHICLE_MODE_FLAG_STABILIZED_ENABLED ) {
update_state_machine_mode_stabilized ( status_pub , current_status , mavlink_fd ) ;
2013-01-11 02:42:09 -04:00
2012-12-30 19:41:11 -04:00
} else if ( mode & VEHICLE_MODE_FLAG_GUIDED_ENABLED ) {
update_state_machine_mode_guided ( status_pub , current_status , mavlink_fd ) ;
2013-01-11 02:42:09 -04:00
2012-12-01 11:28:53 -04:00
} else if ( mode & VEHICLE_MODE_FLAG_MANUAL_INPUT_ENABLED ) {
update_state_machine_mode_manual ( status_pub , current_status , mavlink_fd ) ;
}
2012-08-04 19:12:36 -03:00
2012-08-08 13:47:46 -03:00
/* vehicle is disarmed, mode requests arming */
2012-08-18 11:48:43 -03:00
if ( ! ( current_status - > flag_system_armed ) & & ( mode & VEHICLE_MODE_FLAG_SAFETY_ARMED ) ) {
2012-08-08 13:47:46 -03:00
/* only arm in standby state */
// XXX REMOVE
if ( current_status - > state_machine = = SYSTEM_STATE_STANDBY | | current_status - > state_machine = = SYSTEM_STATE_PREFLIGHT ) {
2012-08-13 13:53:37 -03:00
do_state_update ( status_pub , current_status , mavlink_fd , ( commander_state_machine_t ) SYSTEM_STATE_GROUND_READY ) ;
2012-08-04 19:12:36 -03:00
ret = OK ;
2012-12-27 13:27:08 -04:00
printf ( " [cmd] arming due to command request \n " ) ;
2012-08-04 19:12:36 -03:00
}
2012-08-08 13:47:46 -03:00
}
2012-08-04 19:12:36 -03:00
2012-08-08 13:47:46 -03:00
/* vehicle is armed, mode requests disarming */
2012-08-18 11:48:43 -03:00
if ( current_status - > flag_system_armed & & ! ( mode & VEHICLE_MODE_FLAG_SAFETY_ARMED ) ) {
2012-08-08 13:47:46 -03:00
/* only disarm in ground ready */
2012-08-18 11:48:43 -03:00
if ( current_status - > state_machine = = SYSTEM_STATE_GROUND_READY ) {
do_state_update ( status_pub , current_status , mavlink_fd , ( commander_state_machine_t ) SYSTEM_STATE_STANDBY ) ;
ret = OK ;
2012-12-27 13:27:08 -04:00
printf ( " [cmd] disarming due to command request \n " ) ;
2012-08-18 11:48:43 -03:00
}
}
2012-08-04 19:12:36 -03:00
2012-08-08 13:47:46 -03:00
/* NEVER actually switch off HIL without reboot */
2012-08-18 11:48:43 -03:00
if ( current_status - > flag_hil_enabled & & ! ( mode & VEHICLE_MODE_FLAG_HIL_ENABLED ) ) {
2013-01-11 03:24:28 -04:00
warnx ( " DENYING request to switch off HIL. Please power cycle (safety reasons) \n " ) ;
mavlink_log_critical ( mavlink_fd , " Power-cycle to exit HIL " ) ;
2012-08-08 13:47:46 -03:00
ret = ERROR ;
}
2012-08-04 19:12:36 -03:00
return ret ;
}
2012-08-13 13:53:37 -03:00
uint8_t update_state_machine_custom_mode_request ( int status_pub , struct vehicle_status_s * current_status , const int mavlink_fd , uint8_t custom_mode ) //TODO: add more checks to avoid state switching in critical situations
2012-08-04 19:12:36 -03:00
{
commander_state_machine_t current_system_state = current_status - > state_machine ;
uint8_t ret = 1 ;
switch ( custom_mode ) {
case SYSTEM_STATE_GROUND_READY :
break ;
case SYSTEM_STATE_STANDBY :
break ;
case SYSTEM_STATE_REBOOT :
printf ( " try to reboot \n " ) ;
2013-01-13 18:43:30 -04:00
if ( current_system_state = = SYSTEM_STATE_STANDBY
| | current_system_state = = SYSTEM_STATE_PREFLIGHT
| | current_status - > flag_hil_enabled ) {
2012-08-04 19:12:36 -03:00
printf ( " system will reboot \n " ) ;
2013-01-11 03:24:28 -04:00
mavlink_log_critical ( mavlink_fd , " Rebooting.. " ) ;
2012-12-27 13:27:08 -04:00
usleep ( 200000 ) ;
2012-08-13 13:53:37 -03:00
do_state_update ( status_pub , current_status , mavlink_fd , ( commander_state_machine_t ) SYSTEM_STATE_REBOOT ) ;
2012-08-04 19:12:36 -03:00
ret = 0 ;
}
break ;
case SYSTEM_STATE_AUTO :
printf ( " try to switch to auto/takeoff \n " ) ;
if ( current_system_state = = SYSTEM_STATE_GROUND_READY | | current_system_state = = SYSTEM_STATE_MANUAL ) {
2012-08-13 13:53:37 -03:00
do_state_update ( status_pub , current_status , mavlink_fd , ( commander_state_machine_t ) SYSTEM_STATE_AUTO ) ;
2012-08-04 19:12:36 -03:00
printf ( " state: auto \n " ) ;
ret = 0 ;
}
break ;
case SYSTEM_STATE_MANUAL :
printf ( " try to switch to manual \n " ) ;
if ( current_system_state = = SYSTEM_STATE_GROUND_READY | | current_system_state = = SYSTEM_STATE_AUTO ) {
2012-08-13 13:53:37 -03:00
do_state_update ( status_pub , current_status , mavlink_fd , ( commander_state_machine_t ) SYSTEM_STATE_MANUAL ) ;
2012-08-04 19:12:36 -03:00
printf ( " state: manual \n " ) ;
ret = 0 ;
}
break ;
default :
break ;
}
return ret ;
}