1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-17 02:52:12 +01:00

OP-908 added pios_constants.h containing some (more or less) useful physical constants, added other float constants to pios_math.h fixes for code style.

+review OPReview-436
This commit is contained in:
Alessio Morale 2013-04-18 21:03:25 +02:00
parent cc6d19e40a
commit 36c0ec100b
10 changed files with 205 additions and 53 deletions

View File

@ -57,7 +57,8 @@
//#define MALLOC(x) malloc(x)
//#define FREE(x) free(x)
// const should hopefully keep them in the flash region
// http://reviews.openpilot.org/cru/OPReview-436#c6476 :
// first column not used but it will be optimized out by compiler
static const float CoeffFile[91][6] = { {0, 0, 0, 0, 0, 0},
{1, 0, -29496.6, 0.0, 11.6, 0.0},
{1, 1, -1586.3, 4944.4, 16.5, -25.9},
@ -204,11 +205,11 @@ int WMM_GetMagVector(float Lat, float Lon, float AltEllipsoid, uint16_t Month, u
// ***********
// range check supplied params
if (Lat < -90) return -1; // error
if (Lat > 90) return -2; // error
if (Lat < -90.0f) return -1; // error
if (Lat > 90.0f) return -2; // error
if (Lon < -180) return -3; // error
if (Lon > 180) return -4; // error
if (Lon < -180.0f) return -3; // error
if (Lon > 180.0f) return -4; // error
// ***********
// allocated required memory

View File

@ -82,7 +82,7 @@ int sin_lookup_initalize()
return -1;
for(uint32_t i = 0; i < 180; i++)
sin_table[i] = sinf((float)i * 2 * M_PI_F / 360.0f);
sin_table[i] = sinf(DEG2RAD((float)i));
return 0;
}

View File

@ -94,7 +94,7 @@ static void path_endpoint( float * start_point, float * end_point, float * cur_p
dist_diff = sqrtf( diff_north * diff_north + diff_east * diff_east );
dist_path = sqrtf( path_north * path_north + path_east * path_east );
if(dist_diff < 1e-6f ) {
if (dist_diff < 1e-6f ) {
status->fractional_progress = 1;
status->error = 0;
status->path_direction[0] = status->path_direction[1] = 0;
@ -135,7 +135,7 @@ static void path_vector( float * start_point, float * end_point, float * cur_poi
dot = path_north * diff_north + path_east * diff_east;
dist_path = sqrtf( path_north * path_north + path_east * path_east );
if(dist_path < 1e-6f) {
if (dist_path < 1e-6f){
// if the path is too short, we cannot determine vector direction.
// Fly towards the endpoint to prevent flying away,
// but assume progress=1 either way.

View File

@ -58,6 +58,8 @@
#include "manualcontrolcommand.h"
#include "CoordinateConversions.h"
#include <pios_board_info.h>
#include <pios_math.h>
// Private constants
#define STACK_SIZE_BYTES 540
@ -67,9 +69,6 @@
#define UPDATE_RATE 25.0f
#define GYRO_NEUTRAL 1665
#define F_PI ((float)M_PI)
#define PI_MOD(x) (fmod(x + M_PI, M_PI * 2) - M_PI)
// Private types
// Private variables
@ -336,7 +335,7 @@ static int32_t updateSensors(AccelsData * accels, GyrosData * gyros)
float throttle;
FlightStatusArmedGet(&armed);
ManualControlCommandThrottleGet(&throttle); // Until flight status indicates airborne
if ((armed == FLIGHTSTATUS_ARMED_ARMED) && (throttle > 0)) {
if ((armed == FLIGHTSTATUS_ARMED_ARMED) && (throttle > 0.0f)) {
trim_samples++;
// Store the digitally scaled version since that is what we use for bias
trim_accels[0] += accels->x;
@ -515,10 +514,10 @@ static void updateAttitude(AccelsData * accelsData, GyrosData * gyrosData)
// Work out time derivative from INSAlgo writeup
// Also accounts for the fact that gyros are in deg/s
float qdot[4];
qdot[0] = (-q[1] * gyros[0] - q[2] * gyros[1] - q[3] * gyros[2]) * dT * (F_PI / 180.0f / 2.0f);
qdot[1] = (q[0] * gyros[0] - q[3] * gyros[1] + q[2] * gyros[2]) * dT * (F_PI / 180.0f / 2.0f);
qdot[2] = (q[3] * gyros[0] + q[0] * gyros[1] - q[1] * gyros[2]) * dT * (F_PI / 180.0f / 2.0f);
qdot[3] = (-q[2] * gyros[0] + q[1] * gyros[1] + q[0] * gyros[2]) * dT * (F_PI / 180.0f / 2.0f);
qdot[0] = (-q[1] * gyros[0] - q[2] * gyros[1] - q[3] * gyros[2]) * dT * (M_PI_F / 180.0f / 2.0f);
qdot[1] = (q[0] * gyros[0] - q[3] * gyros[1] + q[2] * gyros[2]) * dT * (M_PI_F / 180.0f / 2.0f);
qdot[2] = (q[3] * gyros[0] + q[0] * gyros[1] - q[1] * gyros[2]) * dT * (M_PI_F / 180.0f / 2.0f);
qdot[3] = (-q[2] * gyros[0] + q[1] * gyros[1] + q[0] * gyros[2]) * dT * (M_PI_F / 180.0f / 2.0f);
// Take a time step
q[0] = q[0] + qdot[0];

View File

@ -76,8 +76,6 @@
#define TASK_PRIORITY (tskIDLE_PRIORITY+3)
#define FAILSAFE_TIMEOUT_MS 10
#define PI_MOD(x) (fmodf(x + M_PI_F, M_PI_F * 2) - M_PI_F)
// low pass filter configuration to calculate offset
// of barometric altitude sensor
// reasoning: updates at: 10 Hz, tau= 300 s settle time

View File

@ -67,12 +67,11 @@
#include "velocityactual.h"
#include "CoordinateConversions.h"
#include <pios_math.h>
#include <pios_constants.h>
// Private constants
#define MAX_QUEUE_SIZE 4
#define STACK_SIZE_BYTES 1548
#define TASK_PRIORITY (tskIDLE_PRIORITY+2)
#define GEE 9.81f
// Private types
// Private variables
static bool followerEnabled = false;

View File

@ -45,46 +45,46 @@
*/
uint16_t PIOS_MPXV_Measure(PIOS_MPXV_descriptor *desc)
{
if (desc)
return PIOS_ADC_PinGet(desc->airspeedADCPin);
return 0;
if (desc)
return PIOS_ADC_PinGet(desc->airspeedADCPin);
return 0;
}
/*
*Returns zeroPoint so that the user can inspect the calibration vs. the sensor value
*/
uint16_t PIOS_MPXV_Calibrate(PIOS_MPXV_descriptor *desc,uint16_t measurement){
desc->calibrationSum += measurement;
desc->calibrationCount++;
desc->zeroPoint = (uint16_t)(((float)desc->calibrationSum) / desc->calibrationCount);
return desc->zeroPoint;
uint16_t PIOS_MPXV_Calibrate(PIOS_MPXV_descriptor *desc,uint16_t measurement) {
desc->calibrationSum += measurement;
desc->calibrationCount++;
desc->zeroPoint = (uint16_t)(((float)desc->calibrationSum) / desc->calibrationCount);
return desc->zeroPoint;
}
/*
* Reads the airspeed and returns CAS (calibrated airspeed) in the case of success.
* In the case of a failed read, returns -1.
*/
float PIOS_MPXV_CalcAirspeed(PIOS_MPXV_descriptor *desc,uint16_t measurement)
{
//Calculate dynamic pressure, as per docs
float Qc = 3.3f/4096.0f * (float)(measurement - desc->zeroPoint);
//Calculate dynamic pressure, as per docs
float Qc = 3.3f/4096.0f * (float)(measurement - desc->zeroPoint);
//Saturate Qc on the lower bound, in order to make sure we don't have negative airspeeds. No need
// to saturate on the upper bound, we'll handle that later with calibratedAirspeed.
if (Qc < 0) {
Qc=0;
}
//Compute calibrated airspeed, as per http://en.wikipedia.org/wiki/Calibrated_airspeed
float calibratedAirspeed = A0*sqrtf(5.0f*(powf(Qc/P0+1.0f,POWER)-1.0f));
//Upper bound airspeed. No need to lower bound it, that comes from Qc
if (calibratedAirspeed > desc->maxSpeed) { //in [m/s]
calibratedAirspeed=desc->maxSpeed;
}
return calibratedAirspeed;
//Saturate Qc on the lower bound, in order to make sure we don't have negative airspeeds. No need
// to saturate on the upper bound, we'll handle that later with calibratedAirspeed.
if (Qc < 0) {
Qc=0;
}
//Compute calibrated airspeed, as per http://en.wikipedia.org/wiki/Calibrated_airspeed
float calibratedAirspeed = A0 * sqrtf( 5.0f * (powf(Qc / P0 + 1.0f, POWER) - 1.0f));
//Upper bound airspeed. No need to lower bound it, that comes from Qc
//in [m/s]
if (calibratedAirspeed > desc->maxSpeed) {
calibratedAirspeed=desc->maxSpeed;
}
return calibratedAirspeed;
}
#endif /* PIOS_INCLUDE_MPXV */

View File

@ -0,0 +1,134 @@
/**
******************************************************************************
*
* @file pios_constants.h
* @author The OpenPilot Team, http://www.openpilot.org Copyright_F (Cf) 2013.
* @brief Shared phisical constants
* --
* @see The GNU Public License_F (GPLf) Version 3
*
*****************************************************************************/
/*
* 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
*_F (at your optionf) 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, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef PIOS_CONSTANTS_H
#define PIOS_CONSTANTS_H
// Constants borrowed straight from GSL http://www.gnu.org/software/gsl/
#define PIOS_CONST_MKS_SPEED_OF_LIGHT_F (2.99792458e8f) /* m / s */
#define PIOS_CONST_MKS_GRAVITATIONAL_CONSTANT_F (6.673e-11f) /* m^3 / kg s^2 */
#define PIOS_CONST_MKS_PLANCKS_CONSTANT_H_F (6.62606896e-34f) /* kg m^2 / s */
#define PIOS_CONST_MKS_PLANCKS_CONSTANT_HBAR_F (1.05457162825e-34f) /* kg m^2 / s */
#define PIOS_CONST_MKS_ASTRONOMICAL_UNIT_F (1.49597870691e11f) /* m */
#define PIOS_CONST_MKS_LIGHT_YEAR_F (9.46053620707e15f) /* m */
#define PIOS_CONST_MKS_PARSEC_F (3.08567758135e16f) /* m */
#define PIOS_CONST_MKS_GRAV_ACCEL_F (9.80665e0f) /* m / s^2 */
#define PIOS_CONST_MKS_ELECTRON_VOLT_F (1.602176487e-19f) /* kg m^2 / s^2 */
#define PIOS_CONST_MKS_MASS_ELECTRON_F (9.10938188e-31f) /* kg */
#define PIOS_CONST_MKS_MASS_MUON_F (1.88353109e-28f) /* kg */
#define PIOS_CONST_MKS_MASS_PROTON_F (1.67262158e-27f) /* kg */
#define PIOS_CONST_MKS_MASS_NEUTRON_F (1.67492716e-27f) /* kg */
#define PIOS_CONST_MKS_RYDBERG_F (2.17987196968e-18f) /* kg m^2 / s^2 */
#define PIOS_CONST_MKS_BOLTZMANN_F (1.3806504e-23f) /* kg m^2 / K s^2 */
#define PIOS_CONST_MKS_MOLAR_GAS_F (8.314472e0f) /* kg m^2 / K mol s^2 */
#define PIOS_CONST_MKS_STANDARD_GAS_VOLUME_F (2.2710981e-2f) /* m^3 / mol */
#define PIOS_CONST_MKS_MINUTE_F (6e1f) /* s */
#define PIOS_CONST_MKS_HOUR_F (3.6e3f) /* s */
#define PIOS_CONST_MKS_DAY_F (8.64e4f) /* s */
#define PIOS_CONST_MKS_WEEK_F (6.048e5f) /* s */
#define PIOS_CONST_MKS_INCH_F (2.54e-2f) /* m */
#define PIOS_CONST_MKS_FOOT_F (3.048e-1f) /* m */
#define PIOS_CONST_MKS_YARD_F (9.144e-1f) /* m */
#define PIOS_CONST_MKS_MILE_F (1.609344e3f) /* m */
#define PIOS_CONST_MKS_NAUTICAL_MILE_F (1.852e3f) /* m */
#define PIOS_CONST_MKS_FATHOM_F (1.8288e0f) /* m */
#define PIOS_CONST_MKS_MIL_F (2.54e-5f) /* m */
#define PIOS_CONST_MKS_POINT_F (3.52777777778e-4f) /* m */
#define PIOS_CONST_MKS_TEXPOINT_F (3.51459803515e-4f) /* m */
#define PIOS_CONST_MKS_MICRON_F (1e-6f) /* m */
#define PIOS_CONST_MKS_ANGSTROM_F (1e-10f) /* m */
#define PIOS_CONST_MKS_HECTARE_F (1e4f) /* m^2 */
#define PIOS_CONST_MKS_ACRE_F (4.04685642241e3f) /* m^2 */
#define PIOS_CONST_MKS_BARN_F (1e-28f) /* m^2 */
#define PIOS_CONST_MKS_LITER_F (1e-3f) /* m^3 */
#define PIOS_CONST_MKS_US_GALLON_F (3.78541178402e-3f) /* m^3 */
#define PIOS_CONST_MKS_QUART_F (9.46352946004e-4f) /* m^3 */
#define PIOS_CONST_MKS_PINT_F (4.73176473002e-4f) /* m^3 */
#define PIOS_CONST_MKS_CUP_F (2.36588236501e-4f) /* m^3 */
#define PIOS_CONST_MKS_FLUID_OUNCE_F (2.95735295626e-5f) /* m^3 */
#define PIOS_CONST_MKS_TABLESPOON_F (1.47867647813e-5f) /* m^3 */
#define PIOS_CONST_MKS_TEASPOON_F (4.92892159375e-6f) /* m^3 */
#define PIOS_CONST_MKS_CANADIAN_GALLON_F (4.54609e-3f) /* m^3 */
#define PIOS_CONST_MKS_UK_GALLON_F (4.546092e-3f) /* m^3 */
#define PIOS_CONST_MKS_MILES_PER_HOUR_F (4.4704e-1f) /* m / s */
#define PIOS_CONST_MKS_KILOMETERS_PER_HOUR_F (2.77777777778e-1f) /* m / s */
#define PIOS_CONST_MKS_KNOT_F (5.14444444444e-1f) /* m / s */
#define PIOS_CONST_MKS_POUND_MASS_F (4.5359237e-1f) /* kg */
#define PIOS_CONST_MKS_OUNCE_MASS_F (2.8349523125e-2f) /* kg */
#define PIOS_CONST_MKS_TON_F (9.0718474e2f) /* kg */
#define PIOS_CONST_MKS_METRIC_TON_F (1e3f) /* kg */
#define PIOS_CONST_MKS_UK_TON_F (1.0160469088e3f) /* kg */
#define PIOS_CONST_MKS_TROY_OUNCE_F (3.1103475e-2f) /* kg */
#define PIOS_CONST_MKS_CARAT_F (2e-4f) /* kg */
#define PIOS_CONST_MKS_UNIFIED_ATOMIC_MASS_F (1.660538782e-27f) /* kg */
#define PIOS_CONST_MKS_GRAM_FORCE_F (9.80665e-3f) /* kg m / s^2 */
#define PIOS_CONST_MKS_POUND_FORCE_F (4.44822161526e0f) /* kg m / s^2 */
#define PIOS_CONST_MKS_KILOPOUND_FORCE_F (4.44822161526e3f) /* kg m / s^2 */
#define PIOS_CONST_MKS_POUNDAL_F (1.38255e-1f) /* kg m / s^2 */
#define PIOS_CONST_MKS_CALORIE_F (4.1868e0f) /* kg m^2 / s^2 */
#define PIOS_CONST_MKS_BTU_F (1.05505585262e3f) /* kg m^2 / s^2 */
#define PIOS_CONST_MKS_THERM_F (1.05506e8f) /* kg m^2 / s^2 */
#define PIOS_CONST_MKS_HORSEPOWER_F (7.457e2f) /* kg m^2 / s^3 */
#define PIOS_CONST_MKS_BAR_F (1e5f) /* kg / m s^2 */
#define PIOS_CONST_MKS_STD_ATMOSPHERE_F (1.01325e5f) /* kg / m s^2 */
#define PIOS_CONST_MKS_TORR_F (1.33322368421e2f) /* kg / m s^2 */
#define PIOS_CONST_MKS_METER_OF_MERCURY_F (1.33322368421e5f) /* kg / m s^2 */
#define PIOS_CONST_MKS_INCH_OF_MERCURY_F (3.38638815789e3f) /* kg / m s^2 */
#define PIOS_CONST_MKS_INCH_OF_WATER_F (2.490889e2f) /* kg / m s^2 */
#define PIOS_CONST_MKS_PSI_F (6.89475729317e3f) /* kg / m s^2 */
#define PIOS_CONST_MKS_POISE_F (1e-1f) /* kg m^-1 s^-1 */
#define PIOS_CONST_MKS_STOKES_F (1e-4f) /* m^2 / s */
#define PIOS_CONST_MKS_STILB_F (1e4f) /* cd / m^2 */
#define PIOS_CONST_MKS_LUMEN_F (1e0f) /* cd sr */
#define PIOS_CONST_MKS_LUX_F (1e0f) /* cd sr / m^2 */
#define PIOS_CONST_MKS_PHOT_F (1e4f) /* cd sr / m^2 */
#define PIOS_CONST_MKS_FOOTCANDLE_F (1.076e1f) /* cd sr / m^2 */
#define PIOS_CONST_MKS_LAMBERT_F (1e4f) /* cd sr / m^2 */
#define PIOS_CONST_MKS_FOOTLAMBERT_F (1.07639104e1f) /* cd sr / m^2 */
#define PIOS_CONST_MKS_CURIE_F (3.7e10f) /* 1 / s */
#define PIOS_CONST_MKS_ROENTGEN_F (2.58e-4f) /* A s / kg */
#define PIOS_CONST_MKS_RAD_F (1e-2f) /* m^2 / s^2 */
#define PIOS_CONST_MKS_SOLAR_MASS_F (1.98892e30f) /* kg */
#define PIOS_CONST_MKS_BOHR_RADIUS_F (5.291772083e-11f) /* m */
#define PIOS_CONST_MKS_NEWTON_F (1e0f) /* kg m / s^2 */
#define PIOS_CONST_MKS_DYNE_F (1e-5f) /* kg m / s^2 */
#define PIOS_CONST_MKS_JOULE_F (1e0f) /* kg m^2 / s^2 */
#define PIOS_CONST_MKS_ERG_F (1e-7f) /* kg m^2 / s^2 */
#define PIOS_CONST_MKS_STEFAN_BOLTZMANN_CONSTANT_F (5.67040047374e-8f) /* kg / K^4 s^3 */
#define PIOS_CONST_MKS_THOMSON_CROSS_SECTION_F (6.65245893699e-29f) /* m^2 */
#define PIOS_CONST_MKS_BOHR_MAGNETON_F (9.27400899e-24f) /* A m^2 */
#define PIOS_CONST_MKS_NUCLEAR_MAGNETON_F (5.05078317e-27f) /* A m^2 */
#define PIOS_CONST_MKS_ELECTRON_MAGNETIC_MOMENT_F (9.28476362e-24f) /* A m^2 */
#define PIOS_CONST_MKS_PROTON_MAGNETIC_MOMENT_F (1.410606633e-26f) /* A m^2 */
#define PIOS_CONST_MKS_FARADAY_F (9.64853429775e4f) /* A s / mol */
#define PIOS_CONST_MKS_ELECTRON_CHARGE_F (1.602176487e-19f) /* A s */
#define PIOS_CONST_MKS_VACUUM_PERMITTIVITY_F (8.854187817e-12f) /* A^2 s^4 / kg m^3 */
#define PIOS_CONST_MKS_VACUUM_PERMEABILITY_F (1.25663706144e-6f) /* kg m / A^2 s^2 */
#define PIOS_CONST_MKS_DEBYE_F (3.33564095198e-30f) /* A s^2 / m^2 */
#define PIOS_CONST_MKS_GAUSS_F (1e-4f) /* kg / A s^2 */
#endif /* PIOS_CONSTANTS_H */

View File

@ -24,8 +24,8 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef PIOS_HELPERS_H_
#define PIOS_HELPERS_H_
#ifndef PIOS_HELPERS_H
#define PIOS_HELPERS_H
/**
@ -37,4 +37,4 @@
#define NELEMENTS(x) (sizeof(x) / sizeof((x)[0]))
#endif // PIOS_HELPERS_H_
#endif // PIOS_HELPERS_H

View File

@ -24,12 +24,33 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef PIOS_MATH_H_
#define PIOS_MATH_H_
#ifndef PIOS_MATH_H
#define PIOS_MATH_H
// Generic float math constants
#define M_E_F 2.71828182845904523536028747135f /* e */
#define M_LOG2E_F 1.44269504088896340735992468100f /* log_2 (e) */
#define M_LOG10E_F 0.43429448190325182765112891892f /* log_10 (e) */
#define M_SQRT2_F 1.41421356237309504880168872421f /* sqrt(2) */
#define M_SQRT1_2_F 0.70710678118654752440084436210f /* sqrt(1/2) */
#define M_SQRT3_F 1.73205080756887729352744634151f /* sqrt(3) */
#define M_PI_F 3.14159265358979323846264338328f /* pi */
#define M_PI_2_F 1.57079632679489661923132169164f /* pi/2 */
#define M_PI_4_F 0.78539816339744830961566084582f /* pi/4 */
#define M_SQRTPI_F 1.77245385090551602729816748334f /* sqrt(pi) */
#define M_2_SQRTPI_F 1.12837916709551257389615890312f /* 2/sqrt(pi) */
#define M_1_PI_F 0.31830988618379067153776752675f /* 1/pi */
#define M_2_PI_F 0.63661977236758134307553505349f /* 2/pi */
#define M_LN10_F 2.30258509299404568401799145468f /* ln(10) */
#define M_LN2_F 0.69314718055994530941723212146f /* ln(2) */
#define M_LNPI_F 1.14472988584940017414342735135f /* ln(pi) */
#define M_EULER_F 0.57721566490153286060651209008f /* Euler constant */
#define M_PI_F ((float)M_PI)
// Conversion macro
#define RAD2DEG(rad) ((rad)*(180.0f/M_PI_F))
#define DEG2RAD(deg) ((deg)*(M_PI_F/180.0f))
// Useful math macros
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif // PIOS_MATH_H_
#endif // PIOS_MATH_H