2012-07-15 22:21:20 -03:00
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
2013-08-29 02:34:34 -03:00
/*
This program is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
2012-07-15 22:21:20 -03:00
/*
2012-08-17 03:08:43 -03:00
* APM_Airspeed . cpp - airspeed ( pitot ) driver
*/
2012-07-15 22:21:20 -03:00
2013-08-29 02:34:34 -03:00
2013-06-02 22:40:06 -03:00
# include <AP_HAL.h>
2012-12-06 19:58:40 -04:00
# include <AP_Math.h>
2012-07-15 22:21:20 -03:00
# include <AP_Common.h>
2013-06-02 22:40:06 -03:00
# include <AP_ADC.h>
2012-07-15 22:21:20 -03:00
# include <AP_Airspeed.h>
2012-10-09 21:01:22 -03:00
extern const AP_HAL : : HAL & hal ;
2013-06-02 22:40:06 -03:00
# if CONFIG_HAL_BOARD == HAL_BOARD_APM1
# include <AP_ADC_AnalogSource.h>
# define ARSPD_DEFAULT_PIN 64
# elif CONFIG_HAL_BOARD == HAL_BOARD_APM2
# define ARSPD_DEFAULT_PIN 0
# elif CONFIG_HAL_BOARD == HAL_BOARD_AVR_SITL
# define ARSPD_DEFAULT_PIN 0
# elif CONFIG_HAL_BOARD == HAL_BOARD_PX4
# include <sys/stat.h>
# include <sys/types.h>
# include <fcntl.h>
# include <unistd.h>
# include <systemlib/airspeed.h>
# include <drivers/drv_airspeed.h>
# include <uORB/topics/differential_pressure.h>
2013-09-12 21:48:35 -03:00
# ifdef CONFIG_ARCH_BOARD_PX4FMU_V1
2013-06-02 22:40:06 -03:00
# define ARSPD_DEFAULT_PIN 11
2013-09-12 21:48:35 -03:00
# else
# define ARSPD_DEFAULT_PIN 15
# endif
2013-09-23 00:09:18 -03:00
# elif CONFIG_HAL_BOARD == HAL_BOARD_FLYMAPLE
# define ARSPD_DEFAULT_PIN 16
2013-09-28 05:40:29 -03:00
# elif CONFIG_HAL_BOARD == HAL_BOARD_LINUX
# define ARSPD_DEFAULT_PIN 65
2013-06-02 22:40:06 -03:00
# else
# define ARSPD_DEFAULT_PIN 0
# endif
2012-07-15 22:21:20 -03:00
// table of user settable parameters
const AP_Param : : GroupInfo AP_Airspeed : : var_info [ ] PROGMEM = {
2012-07-16 10:48:47 -03:00
// @Param: ENABLE
2012-07-15 22:21:20 -03:00
// @DisplayName: Airspeed enable
// @Description: enable airspeed sensor
// @Values: 0:Disable,1:Enable
2012-08-06 22:02:14 -03:00
AP_GROUPINFO ( " ENABLE " , 0 , AP_Airspeed , _enable , 1 ) ,
2012-07-15 22:21:20 -03:00
2012-07-16 10:48:47 -03:00
// @Param: USE
2012-07-15 22:21:20 -03:00
// @DisplayName: Airspeed use
// @Description: use airspeed for flight control
2012-08-17 21:22:51 -03:00
// @Values: 1:Use,0:Don't Use
2012-08-06 22:02:14 -03:00
AP_GROUPINFO ( " USE " , 1 , AP_Airspeed , _use , 0 ) ,
2012-07-15 22:21:20 -03:00
2012-07-16 10:48:47 -03:00
// @Param: OFFSET
2012-07-15 22:21:20 -03:00
// @DisplayName: Airspeed offset
// @Description: Airspeed calibration offset
// @Increment: 0.1
2012-08-06 22:02:14 -03:00
AP_GROUPINFO ( " OFFSET " , 2 , AP_Airspeed , _offset , 0 ) ,
2012-07-15 22:21:20 -03:00
2012-07-16 10:48:47 -03:00
// @Param: RATIO
2012-07-15 22:21:20 -03:00
// @DisplayName: Airspeed ratio
// @Description: Airspeed calibration ratio
// @Increment: 0.1
2013-01-10 14:42:24 -04:00
AP_GROUPINFO ( " RATIO " , 3 , AP_Airspeed , _ratio , 1.9936f ) ,
2012-07-15 22:21:20 -03:00
2013-06-02 22:40:06 -03:00
// @Param: PIN
// @DisplayName: Airspeed pin
2013-09-12 21:48:35 -03:00
// @Description: The analog pin number that the airspeed sensor is connected to. Set this to 0..9 for the APM2 analog pins. Set to 64 on an APM1 for the dedicated airspeed port on the end of the board. Set to 11 on PX4 for the analog airspeed port. Set to 15 on the Pixhawk for the analog airspeed port. Set to 65 on the PX4 or Pixhawk for an EagleTree or MEAS I2C airspeed sensor.
2013-06-02 22:40:06 -03:00
// @User: Advanced
AP_GROUPINFO ( " PIN " , 4 , AP_Airspeed , _pin , ARSPD_DEFAULT_PIN ) ,
2013-07-19 19:14:58 -03:00
// @Param: AUTOCAL
// @DisplayName: Automatic airspeed ratio calibration
// @Description: If this is enabled then the APM will automatically adjust the ARSPD_RATIO during flight, based upon an estimation filter using ground speed and true airspeed. The automatic calibration will save the new ratio to EEPROM every 2 minutes if it changes by more than 5%
// @User: Advanced
AP_GROUPINFO ( " AUTOCAL " , 5 , AP_Airspeed , _autocal , 0 ) ,
2012-07-15 22:21:20 -03:00
AP_GROUPEND
} ;
2013-06-02 22:40:06 -03:00
2013-03-07 19:01:15 -04:00
/*
this scaling factor converts from the old system where we used a
2013-05-13 02:13:19 -03:00
0 to 4095 raw ADC value for 0 - 5 V to the new system which gets the
2013-03-07 19:01:15 -04:00
voltage in volts directly from the ADC driver
*/
2013-05-13 02:13:19 -03:00
# define SCALING_OLD_CALIBRATION 819 // 4095/5
2013-03-07 19:01:15 -04:00
2013-06-02 22:40:06 -03:00
void AP_Airspeed : : init ( )
{
_last_pressure = 0 ;
2013-08-28 09:35:07 -03:00
_calibration . init ( _ratio ) ;
_last_saved_ratio = _ratio ;
_counter = 0 ;
2013-09-28 05:40:29 -03:00
analog . init ( ) ;
digital . init ( ) ;
2013-06-02 22:40:06 -03:00
}
// read the airspeed sensor
float AP_Airspeed : : get_pressure ( void )
{
if ( ! _enable ) {
return 0 ;
}
2013-09-28 05:40:29 -03:00
float pressure = 0 ;
if ( _pin = = 65 ) {
_healthy = digital . get_differential_pressure ( pressure ) ;
} else {
_healthy = analog . get_differential_pressure ( pressure ) ;
2013-06-02 22:40:06 -03:00
}
2013-09-28 05:40:29 -03:00
return pressure ;
2013-06-02 22:40:06 -03:00
}
2012-07-15 22:21:20 -03:00
// calibrate the airspeed. This must be called at least once before
// the get_airspeed() interface can be used
2012-10-09 21:01:22 -03:00
void AP_Airspeed : : calibrate ( )
2012-07-15 22:21:20 -03:00
{
2012-08-17 03:08:43 -03:00
float sum = 0 ;
2013-09-28 05:40:29 -03:00
uint8_t count = 0 ;
2012-08-17 03:08:43 -03:00
if ( ! _enable ) {
return ;
}
2013-06-02 22:40:06 -03:00
// discard first reading
get_pressure ( ) ;
2013-09-28 05:40:29 -03:00
for ( uint8_t i = 0 ; i < 10 ; i + + ) {
2012-10-09 21:01:22 -03:00
hal . scheduler - > delay ( 100 ) ;
2013-09-28 05:40:29 -03:00
float p = get_pressure ( ) ;
if ( _healthy ) {
sum + = p ;
count + + ;
}
}
if ( count = = 0 ) {
// unhealthy sensor
hal . console - > printf_P ( PSTR ( " Airspeed sensor unhealthy \n " ) ) ;
_offset . set ( 0 ) ;
return ;
2012-08-17 03:08:43 -03:00
}
2013-09-28 05:40:29 -03:00
float raw = sum / count ;
2013-03-07 19:01:15 -04:00
_offset . set_and_save ( raw ) ;
2012-08-17 03:08:43 -03:00
_airspeed = 0 ;
2013-06-02 22:40:06 -03:00
_raw_airspeed = 0 ;
2012-07-15 22:21:20 -03:00
}
// read the airspeed sensor
void AP_Airspeed : : read ( void )
{
2012-08-17 03:08:43 -03:00
float airspeed_pressure ;
if ( ! _enable ) {
return ;
}
2013-09-28 05:40:29 -03:00
airspeed_pressure = get_pressure ( ) ;
airspeed_pressure = max ( airspeed_pressure - _offset , 0 ) ;
2013-11-02 06:25:44 -03:00
_last_pressure = airspeed_pressure ;
2013-06-02 22:40:06 -03:00
_raw_airspeed = sqrtf ( airspeed_pressure * _ratio ) ;
_airspeed = 0.7f * _airspeed + 0.3f * _raw_airspeed ;
2012-07-15 22:21:20 -03:00
}