MPL3115A2 barometer implementation in MatrixPilot - Jeffem/MP5_QuadCopter GitHub Wiki
Introduction
This document deals with the integration of the MPL3115A2 barometer in the MatrixPilot and UDB5 autopilot.
Purpose
MPL3115A2 barometer description
The MPL3115A2 is a compact, piezoresistive, absolute pressure sensor with an I2C digital interface. MPL3115A2 has a wide operating range of 20 kPa to 110 kPa, a range that covers all surface elevations on earth. The MEMS is temperature compensated utilizing an on-chip temperature sensor. The pressure and temperature data is fed into a high resolution ADC to provide fully compensated and digitized outputs for pressure in Pascals and temperature in °C. The compensated pressure output can then be converted to altitude, provided in meters. The internal processing in MPL3115A2 removes compensation and unit conversion load from the system MCU, simplifying system design.
MPL3115A2's advanced ASIC has multiple user programmable modes such as power saving, interrupt and autonomous data acquisition modes, including programmed acquisition cycle timing, and poll-only modes. Typical active supply current is 40 μA per measurement-second for a stable 10 cm output resolution.
Over sampling and rate.
OS2 | OS1 | OS0 | Oversample ratio | Minimum time between data samples |
---|---|---|---|---|
0 | 0 | 0 | 1 | 6 ms |
0 | 0 | 1 | 2 | 10 ms |
0 | 1 | 0 | 4 | 18 ms |
0 | 1 | 1 | 8 | 34 ms |
1 | 0 | 0 | 16 | 66 ms |
1 | 0 | 1 | 32 | 130 ms |
1 | 1 | 0 | 64 | 258 ms |
1 | 1 | 1 | 128 | 512 ms |
MPL3115A2 Pros & Cons
Compare to the BMP085 or the BMP 180, this MPL3115 presents the great advantage to give directly the altitude. No need to convert the pressure to the altitude with a complex and time consuming equation.
Another advantage is to have integrated averaging, avoiding to filter the data.
Main inconvenient is the minimum data rate of 1Hz, except interfacing the device via its interruption protocol.
This way is used in the solution implemented here because we want a minimum of 4 Hz data rate.
The external interrupt INT1 is used to trigger the UDB when the MPL3115 is ready to send an altitude measurement.
N.B. : On the UDB5 PCB, INT1 is connected to RA12. Be careful of the other versions (UDB4 or AUAV3) to connect IN1 or IN2 properly.
Changes
This integration needs 15 files to be modified.
- options.h
To have the choice between barometers, a second value is let to USE_BAROMETER_ALTITUDE
#define USE_BAROMETER_ALTITUDE 2
MagYawDrift is set to 1 to call the magnetometer and check the I2C sharing between magnetometer and barometer.
Other modifications are preparing the QuadCopter configuration.
- gain_variables.h
ALTITUDE_GAINS_VARIABLE must be set to 1 to allow usage of library altitudeCntrlVariable which have the routine altitudeCntrl where calculate_sonar_height_above_ground() is called.
- Options_magnetometer.h
The magnetometer is defined and configured.
- Options_mavlink.h
Mavlink is used as telemetry to check the behavior of the barometer.
- estAltitude.h
As the MPL3115 gives directly the altitude, the udb_barometer_callback points on the altitude variable instead of the pressure.
- barometer.h
Declaration of the modified udb_barometer_callback.
Creation of MPL3115 init to initialize properly the MPL.
- mavlink.
In the global position message, the parameters “alt” and “relative_alt” are replaced for testing absolute and relative barometer measurement.
- altitudeCntrlVariable.c
Suppression of USE_SONAR condition to call calculate_sonar_height_above_ground().
- Navigate.c
The calibration of the altimeter(s) has no reason to be there. We can need altimetry without GPS.
If we need some filtering for this calibration, why not initialize the altimeter during the calibration of the other sensors.
Altimeter(s) calibration will be done in libDCM.
- gpsParseCommon.c
Same reason than for navigate.c, the telemetry functions are called by calculate_sonar_height_above_ground() implemented in sonarCntrl.c.
- SonarCntrl.c
Altitude computation are made by estAltitude() wich is called by calculate_sonar_height_above_ground().
- estAltitude.c
To calibrate the MPL3115, a filter is used for a better estimation of the altitude 0.
Two variables are used, barometer_altitude_gnd and barometer_altitude_gnd0 for this filtering.
The filter is a simple first order filter with a depth of 16 samples.
At the end of the calibration, the altitude of the aircraft during the calibration is recorded in barometer_altitude_gnd to be used for the computation of barometer_agl_altitude.
Udb_barometer callback is improved to read directly the altitude from the MPL3115.
And estAltitude gives the height above ground by substracting the barometer_altitude_gnd to the current barometer_altitude.
- libDCM.c
Instead of testing USE_BAROMETER_ALTITUDE with 1 to call the barometer, as the routine are identical for both barometers, the flag is tested positive.
The calibration is called during udb_heratbeat_callback while the calibrating phase is not completed.
- barometer.c
First implementation of the MPL was done with delays between exchanges on the I2C.
Thank to the new method developed by Pete to have both barometer and magnetometer speaking on the I2C bus, the reading of the MPL3115 data is now realized accordingly.
Interruption Int1 is initiated in the libUDB.c file and implemented in the barometer.c file.
The measurement is read at 4 Hz by reading the status and data registers in rxbarometer called by get_data_from_I2C_sensors() in libDCM. The OST is set, to obtain a new measurement.
Then, when data are ready, INT1 is triggered by the barometer.
The interruption service reset the OST.
- libUDB.c
Call I2C2_Reset at init.
Declare and configure the INT1 interrupt used by the MPL3115.
Validation
The validation is realized “on the table” with a UDB5 spare PCB.
The mezzanine PCB allows connecting the magnetometer, the barometer and the Lidar on the I2C bus. It has also the interface with a little XBee telemetry.
No radio, Lidar, GPS nor servos (or ESC) are connected to the UDB for the test. (The Lidar will not be connected to the I2C but on a servo input and a digital output (red wire with naked terminal)).
Test with the QGround Control Station V2.2.1
Barometer raw altitude is 106 m (QFE Paris CdG 1007 hPa, Altitude 118m, thus QNH=1020 hPa), barometer_altitude_gnd is quite 0 (0.4m).
The barometer agl altitude is noisy, even on the table, with nearly 2m peak to peak.
The lower step is around 1/3 of graduation, that means 200/3=60 mm.
The rate cannot be estimated here, due to the lack of time axis scale. The rate seems not constant.
The magnetometer gives measurements inside the attended magnitude order (<500 mG).
Test with the GFM Ground Control Station with telemetry rate 40 Hz
Upgrade to 6Hz
As the oversampling rate retained for the MPL is 32, which need 130 ms between data reads, the rate on the I2C bus could be increased up to 6Hz in case of use of this barometer.
The get_data_from_I2C_sensors() routine is modified to receive the I2C rate as a parameter.
This upgrade involve (mainly) files :
-
heartbeat.h
Definition of a constant I2C_SENSOR_RATE, set to 6 (Hz) -
libDCM
Definition of the constant TICKS = PID_HZ/I2C_SENSOR_RATE.
Parametrization of the get_data_from_I2C_sensors routine.
Replacement of the constant in “hard” 40 by PID_HZ when it is judicious.
The call to estAltitude() is reported in libDCM/udb_heartbeat_callback instead of being called in sonarCntrl. -
SonarCntrl
Transfer of the estAltitude call to libDCM.
Test with the GFM Ground Control Station with telemetry rate 40 Hz
CONCLUSION
This upgrade gives a gain of 50% in the measurement rate for both magnetometer and barometer. A better noise rejection is waiting and must be verified in flight.