2015-08-11 03:28:44 -03:00
# include "AP_LandingGear.h"
# include <AP_Relay/AP_Relay.h>
# include <AP_Math/AP_Math.h>
2017-01-06 21:06:40 -04:00
# include <SRV_Channel/SRV_Channel.h>
2015-08-11 03:28:44 -03:00
# include <AP_HAL/AP_HAL.h>
2018-06-10 03:34:02 -03:00
# include <DataFlash/DataFlash.h>
2015-01-06 00:24:05 -04:00
extern const AP_HAL : : HAL & hal ;
2018-06-10 03:34:02 -03:00
# define MASK_LOG_CTUN (1<<4)
2015-10-25 14:03:46 -03:00
const AP_Param : : GroupInfo AP_LandingGear : : var_info [ ] = {
2015-01-06 00:24:05 -04:00
// @Param: SERVO_RTRACT
// @DisplayName: Landing Gear Servo Retracted PWM Value
2017-05-15 20:21:16 -03:00
// @Description: Servo PWM value in microseconds when landing gear is retracted
2015-01-06 00:24:05 -04:00
// @Range: 1000 2000
2017-05-02 10:45:24 -03:00
// @Units: PWM
2015-01-06 00:24:05 -04:00
// @Increment: 1
// @User: Standard
2015-01-06 11:25:43 -04:00
AP_GROUPINFO ( " SERVO_RTRACT " , 0 , AP_LandingGear , _servo_retract_pwm , AP_LANDINGGEAR_SERVO_RETRACT_PWM_DEFAULT ) ,
2015-01-06 00:24:05 -04:00
// @Param: SERVO_DEPLOY
// @DisplayName: Landing Gear Servo Deployed PWM Value
2017-05-15 20:21:16 -03:00
// @Description: Servo PWM value in microseconds when landing gear is deployed
2015-01-06 00:24:05 -04:00
// @Range: 1000 2000
2017-05-02 10:45:24 -03:00
// @Units: PWM
2015-01-06 00:24:05 -04:00
// @Increment: 1
// @User: Standard
2015-01-06 11:25:43 -04:00
AP_GROUPINFO ( " SERVO_DEPLOY " , 1 , AP_LandingGear , _servo_deploy_pwm , AP_LANDINGGEAR_SERVO_DEPLOY_PWM_DEFAULT ) ,
2015-01-06 00:24:05 -04:00
2017-08-04 03:28:33 -03:00
// @Param: STARTUP
// @DisplayName: Landing Gear Startup position
// @Description: Landing Gear Startup behaviour control
// @Values: 0:WaitForPilotInput, 1:Retract, 2:Deploy
// @User: Standard
AP_GROUPINFO ( " STARTUP " , 2 , AP_LandingGear , _startup_behaviour , ( uint8_t ) AP_LandingGear : : LandingGear_Startup_WaitForPilotInput ) ,
2018-06-10 03:34:02 -03:00
// @Param: DEPLOY_PIN
// @DisplayName: Chassis deployment feedback pin
// @Description: Pin number to use for feedback of gear deployment. If set to -1 feedback is disabled.
// @Values: -1:Disabled,50:PX4 AUX1,51:PX4 AUX2,52:PX4 AUX3,53:PX4 AUX4,54:PX4 AUX5,55:PX4 AUX6
// @User: Standard
// @RebootRequired: True
AP_GROUPINFO ( " DEPLOY_PIN " , 3 , AP_LandingGear , _pin_deployed , - 1 ) ,
// @Param: DEPLOY_POL
// @DisplayName: Chassis deployment feedback pin polarity
// @Description: Polarity for feedback pin. If this is 1 then the pin should be high when gear are deployed. If set to 0 then then deployed gear level is low.
// @Values: 0:Low,1:High
// @User: Standard
AP_GROUPINFO ( " DEPLOY_POL " , 4 , AP_LandingGear , _pin_deployed_polarity , 0 ) ,
// @Param: WOW_PIN
// @DisplayName: Weight on wheels feedback pin
// @Description: Pin number to use for feedback of weight on wheels condition. If set to -1 feedback is disabled.
// @Values: -1:Disabled,50:PX4 AUX1,51:PX4 AUX2,52:PX4 AUX3,53:PX4 AUX4,54:PX4 AUX5,55:PX4 AUX6
// @User: Standard
// @RebootRequired: True
AP_GROUPINFO ( " WOW_PIN " , 5 , AP_LandingGear , _pin_weight_on_wheels , DEFAULT_PIN_WOW ) ,
2017-08-04 03:28:33 -03:00
2018-06-10 03:34:02 -03:00
// @Param: WOW_POL
// @DisplayName: Weight on wheels feedback pin polarity
// @Description: Polarity for feedback pin. If this is 1 then the pin should be high when there is weight on wheels. If set to 0 then then weight on wheels level is low.
// @Values: 0:Low,1:High
// @User: Standard
AP_GROUPINFO ( " WOW_POL " , 6 , AP_LandingGear , _pin_weight_on_wheels_polarity , DEFAULT_PIN_WOW_POL ) ,
2015-01-06 00:24:05 -04:00
AP_GROUPEND
} ;
2018-06-10 03:34:02 -03:00
AP_LandingGear * AP_LandingGear : : _singleton ;
2017-08-04 03:28:33 -03:00
/// initialise state of landing gear
void AP_LandingGear : : init ( )
{
2018-06-10 03:34:02 -03:00
if ( _pin_deployed ! = - 1 ) {
hal . gpio - > pinMode ( _pin_deployed , HAL_GPIO_INPUT ) ;
hal . gpio - > write ( _pin_deployed , 1 ) ;
log_wow_state ( wow_state_current ) ;
}
if ( _pin_weight_on_wheels ! = - 1 ) {
hal . gpio - > pinMode ( _pin_weight_on_wheels , HAL_GPIO_INPUT ) ;
hal . gpio - > write ( _pin_weight_on_wheels , 1 ) ;
log_wow_state ( wow_state_current ) ;
}
2017-08-04 03:28:33 -03:00
switch ( ( enum LandingGearStartupBehaviour ) _startup_behaviour . get ( ) ) {
default :
case LandingGear_Startup_WaitForPilotInput :
// do nothing
break ;
case LandingGear_Startup_Retract :
retract ( ) ;
break ;
case LandingGear_Startup_Deploy :
deploy ( ) ;
break ;
}
}
2017-06-10 01:07:38 -03:00
/// set landing gear position to retract, deploy or deploy-and-keep-deployed
void AP_LandingGear : : set_position ( LandingGearCommand cmd )
2015-01-06 00:24:05 -04:00
{
2017-06-10 01:07:38 -03:00
switch ( cmd ) {
case LandingGear_Retract :
2018-08-02 22:25:58 -03:00
retract ( ) ;
2017-06-10 01:07:38 -03:00
break ;
case LandingGear_Deploy :
deploy ( ) ;
break ;
}
2015-01-06 00:24:05 -04:00
}
/// deploy - deploy landing gear
void AP_LandingGear : : deploy ( )
2015-01-06 11:25:43 -04:00
{
// set servo PWM to deployed position
2017-01-06 21:06:40 -04:00
SRV_Channels : : set_output_pwm ( SRV_Channel : : k_landing_gear_control , _servo_deploy_pwm ) ;
2015-01-06 11:25:43 -04:00
2015-01-06 00:24:05 -04:00
// set deployed flag
2017-06-10 01:07:38 -03:00
_deployed = true ;
2015-01-06 00:24:05 -04:00
}
/// retract - retract landing gear
void AP_LandingGear : : retract ( )
2017-06-10 01:07:38 -03:00
{
2015-01-06 11:25:43 -04:00
// set servo PWM to retracted position
2017-01-06 21:06:40 -04:00
SRV_Channels : : set_output_pwm ( SRV_Channel : : k_landing_gear_control , _servo_retract_pwm ) ;
2015-01-06 11:25:43 -04:00
2015-01-06 00:24:05 -04:00
// reset deployed flag
2017-06-10 01:07:38 -03:00
_deployed = false ;
2015-01-06 00:24:05 -04:00
}
2018-06-10 03:34:02 -03:00
bool AP_LandingGear : : deployed ( )
{
if ( _pin_deployed = = - 1 ) {
return _deployed ;
} else {
return hal . gpio - > read ( _pin_deployed ) = = _pin_deployed_polarity ? true : false ;
}
}
AP_LandingGear : : LG_WOW_State AP_LandingGear : : get_wow_state ( )
{
return wow_state_current ;
}
AP_LandingGear : : LG_LandingGear_State AP_LandingGear : : get_state ( )
{
return gear_state_current ;
}
uint32_t AP_LandingGear : : get_gear_state_duration_ms ( )
{
if ( last_gear_event_ms = = 0 ) {
return 0 ;
}
return AP_HAL : : millis ( ) - last_gear_event_ms ;
}
uint32_t AP_LandingGear : : get_wow_state_duration_ms ( )
{
if ( last_wow_event_ms = = 0 ) {
return 0 ;
}
return AP_HAL : : millis ( ) - last_wow_event_ms ;
}
void AP_LandingGear : : update ( )
{
if ( _pin_weight_on_wheels = = - 1 ) {
last_wow_event_ms = 0 ;
wow_state_current = LG_WOW_UNKNOWN ;
} else {
LG_WOW_State wow_state_new = hal . gpio - > read ( _pin_weight_on_wheels ) = = _pin_weight_on_wheels_polarity ? LG_WOW : LG_NO_WOW ;
if ( wow_state_new ! = wow_state_current ) {
// we changed states, lets note the time.
last_wow_event_ms = AP_HAL : : millis ( ) ;
log_wow_state ( wow_state_new ) ;
}
wow_state_current = wow_state_new ;
}
if ( _pin_deployed = = - 1 ) {
last_gear_event_ms = 0 ;
gear_state_current = ( _deployed = = true ? LG_DEPLOYED : LG_RETRACTED ) ;
} else {
LG_LandingGear_State gear_state_new ;
if ( _deployed ) {
gear_state_new = ( deployed ( ) = = true ? LG_DEPLOYED : LG_DEPLOYING ) ;
} else {
gear_state_new = ( deployed ( ) = = false ? LG_RETRACTED : LG_RETRACTING ) ;
}
if ( gear_state_new ! = gear_state_current ) {
// we changed states, lets note the time.
last_gear_event_ms = AP_HAL : : millis ( ) ;
log_wow_state ( wow_state_current ) ;
}
gear_state_current = gear_state_new ;
}
}
// log weight on wheels state
void AP_LandingGear : : log_wow_state ( LG_WOW_State state )
{
if ( DataFlash_Class : : instance ( ) - > should_log ( MASK_LOG_CTUN ) ) {
DataFlash_Class : : instance ( ) - > Log_Write ( " LGR " , " TimeUS,LandingGear,WeightOnWheels " , " Qbb " ,
AP_HAL : : micros64 ( ) ,
( int8_t ) gear_state_current , ( int8_t ) state ) ;
}
}