2012-07-08 22:49:26 -03:00
/*
APM_OBC . cpp
Outback Challenge Failsafe module
This library is free software ; you can redistribute it and / or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation ; either version 2.1
of the License , or ( at your option ) any later version .
*/
2012-11-12 17:34:14 -04:00
# include <AP_HAL.h>
2012-07-08 22:49:26 -03:00
# include <APM_OBC.h>
2012-11-12 17:34:14 -04:00
extern const AP_HAL : : HAL & hal ;
2012-07-08 22:49:26 -03:00
// table of user settable parameters
const AP_Param : : GroupInfo APM_OBC : : var_info [ ] PROGMEM = {
// @Param: MAN_PIN
// @DisplayName: Manual Pin
// @Description: This sets a digital output pin to set high when in manual mode
// @User: Advanced
AP_GROUPINFO ( " MAN_PIN " , 0 , APM_OBC , _manual_pin , - 1 ) ,
// @Param: HB_PIN
// @DisplayName: Heartbeat Pin
// @Description: This sets a digital output pin which is cycled at 10Hz when termination is not activated
// @User: Advanced
AP_GROUPINFO ( " HB_PIN " , 1 , APM_OBC , _heartbeat_pin , - 1 ) ,
// @Param: WP_COMMS
// @DisplayName: Comms Waypoint
// @Description: Waypoint number to navigate to on comms loss
// @User: Advanced
AP_GROUPINFO ( " WP_COMMS " , 2 , APM_OBC , _wp_comms_hold , 0 ) ,
// @Param: GPS_LOSS
// @DisplayName: GPS Loss Waypoint
// @Description: Waypoint number to navigate to on GPS lock loss
// @User: Advanced
AP_GROUPINFO ( " WP_GPS_LOSS " , 4 , APM_OBC , _wp_gps_loss , 0 ) ,
// @Param: TERMINATE
// @DisplayName: Force Terminate
// @Description: Can be set in flight to force termination of the heartbeat signal
// @User: Advanced
AP_GROUPINFO ( " TERMINATE " , 5 , APM_OBC , _terminate , 0 ) ,
2012-08-28 02:51:32 -03:00
// @Param: TERM_ACTION
// @DisplayName: Terminate action
// @Description: This can be used to force an action on flight termination. Normally this is handled by an external failsafe board, but you can setup APM to handle it here. If set to 0 (which is the default) then no extra action is taken. If set to the magic value 42 then the plane will deliberately crash itself by setting maximum throws on all surfaces, and zero throttle
// @User: Advanced
AP_GROUPINFO ( " TERM_ACTION " , 6 , APM_OBC , _terminate_action , 0 ) ,
2012-09-06 22:44:02 -03:00
// @Param: TERM_PIN
// @DisplayName: Terminate Pin
// @Description: This sets a digital output pin to set high on flight termination
// @User: Advanced
AP_GROUPINFO ( " TERM_PIN " , 7 , APM_OBC , _terminate_pin , - 1 ) ,
2012-07-08 22:49:26 -03:00
AP_GROUPEND
} ;
// access to geofence state
extern bool geofence_breached ( void ) ;
// function to change waypoint
extern void change_command ( uint8_t cmd_index ) ;
// check for Failsafe conditions. This is called at 10Hz by the main
// ArduPlane code
void
APM_OBC : : check ( APM_OBC : : control_mode mode ,
uint32_t last_heartbeat_ms ,
uint32_t last_gps_fix_ms )
{
// we always check for fence breach
if ( geofence_breached ( ) ) {
if ( ! _terminate ) {
2012-11-12 17:34:14 -04:00
hal . console - > println_P ( PSTR ( " Fence TERMINATE " ) ) ;
2012-07-08 22:49:26 -03:00
_terminate . set ( 1 ) ;
}
}
// tell the failsafe board if we are in manual control
// mode. This tells it to pass through controls from the
// receiver
if ( _manual_pin ! = - 1 ) {
2012-08-25 02:02:21 -03:00
if ( _manual_pin ! = _last_manual_pin ) {
2012-11-12 17:34:14 -04:00
hal . gpio - > pinMode ( _manual_pin , GPIO_OUTPUT ) ;
2012-08-25 02:02:21 -03:00
_last_manual_pin = _manual_pin ;
}
2012-11-12 17:34:14 -04:00
hal . gpio - > write ( _manual_pin , mode = = OBC_MANUAL ) ;
2012-07-08 22:49:26 -03:00
}
2012-11-12 17:34:14 -04:00
uint32_t now = hal . scheduler - > millis ( ) ;
2012-07-08 22:49:26 -03:00
bool gcs_link_ok = ( ( now - last_heartbeat_ms ) < 10000 ) ;
bool gps_lock_ok = ( ( now - last_gps_fix_ms ) < 3000 ) ;
switch ( _state ) {
case STATE_PREFLIGHT :
// we startup in preflight mode. This mode ends when
// we first enter auto.
if ( mode = = OBC_AUTO ) {
2012-11-12 17:34:14 -04:00
hal . console - > println_P ( PSTR ( " Starting OBC_AUTO " ) ) ;
2012-07-08 22:49:26 -03:00
_state = STATE_AUTO ;
}
break ;
case STATE_AUTO :
// this is the normal mode.
if ( ! gcs_link_ok ) {
2012-11-12 17:34:14 -04:00
hal . console - > println_P ( PSTR ( " State DATA_LINK_LOSS " ) ) ;
2012-07-08 22:49:26 -03:00
_state = STATE_DATA_LINK_LOSS ;
if ( _wp_comms_hold ) {
if ( _command_index ! = NULL ) {
_saved_wp = _command_index - > get ( ) ;
}
change_command ( _wp_comms_hold ) ;
}
break ;
}
if ( ! gps_lock_ok ) {
2012-11-12 17:34:14 -04:00
hal . console - > println_P ( PSTR ( " State GPS_LOSS " ) ) ;
2012-07-08 22:49:26 -03:00
_state = STATE_GPS_LOSS ;
if ( _wp_gps_loss ) {
if ( _command_index ! = NULL ) {
2012-09-26 07:15:39 -03:00
_saved_wp = _command_index - > get ( ) ;
2012-07-08 22:49:26 -03:00
}
change_command ( _wp_gps_loss ) ;
}
break ;
}
break ;
case STATE_DATA_LINK_LOSS :
if ( ! gps_lock_ok ) {
// losing GPS lock when data link is lost
// leads to termination
2012-11-12 17:34:14 -04:00
hal . console - > println_P ( PSTR ( " Dual loss TERMINATE " ) ) ;
2012-07-08 22:49:26 -03:00
_terminate . set ( 1 ) ;
} else if ( gcs_link_ok ) {
_state = STATE_AUTO ;
2012-11-12 17:34:14 -04:00
hal . console - > println_P ( PSTR ( " GCS OK " ) ) ;
2012-08-27 22:59:03 -03:00
if ( _saved_wp ! = 0 ) {
change_command ( _saved_wp ) ;
_saved_wp = 0 ;
}
2012-07-08 22:49:26 -03:00
}
break ;
case STATE_GPS_LOSS :
if ( ! gcs_link_ok ) {
// losing GCS link when GPS lock lost
// leads to termination
2012-11-12 17:34:14 -04:00
hal . console - > println_P ( PSTR ( " Dual loss TERMINATE " ) ) ;
2012-07-08 22:49:26 -03:00
_terminate . set ( 1 ) ;
} else if ( gps_lock_ok ) {
2012-11-12 17:34:14 -04:00
hal . console - > println_P ( PSTR ( " GPS OK " ) ) ;
2012-07-08 22:49:26 -03:00
_state = STATE_AUTO ;
2012-08-27 22:59:03 -03:00
if ( _saved_wp ! = 0 ) {
change_command ( _saved_wp ) ;
_saved_wp = 0 ;
}
2012-07-08 22:49:26 -03:00
}
break ;
}
2012-09-07 06:55:51 -03:00
// if we are not terminating or if there is a separate terminate
// pin configured then toggle the heartbeat pin at 10Hz
if ( _heartbeat_pin ! = - 1 & & ( _terminate_pin ! = - 1 | | ! _terminate ) ) {
2012-08-25 02:02:21 -03:00
if ( _heartbeat_pin ! = _last_heartbeat_pin ) {
2012-11-12 17:34:14 -04:00
hal . gpio - > pinMode ( _heartbeat_pin , GPIO_OUTPUT ) ;
2012-08-25 02:02:21 -03:00
_last_heartbeat_pin = _heartbeat_pin ;
}
2012-07-08 22:49:26 -03:00
_heartbeat_pin_value = ! _heartbeat_pin_value ;
2012-11-12 17:34:14 -04:00
hal . gpio - > write ( _heartbeat_pin , _heartbeat_pin_value ) ;
2012-07-08 22:49:26 -03:00
}
2012-09-06 22:44:02 -03:00
// set the terminate pin
if ( _terminate_pin ! = - 1 ) {
if ( _terminate_pin ! = _last_terminate_pin ) {
2012-11-12 17:34:14 -04:00
hal . gpio - > pinMode ( _terminate_pin , GPIO_OUTPUT ) ;
2012-09-06 22:44:02 -03:00
_last_terminate_pin = _terminate_pin ;
}
2012-11-12 17:34:14 -04:00
hal . gpio - > write ( _terminate_pin , _terminate ? 1 : 0 ) ;
2012-09-06 22:44:02 -03:00
}
2012-07-08 22:49:26 -03:00
}