Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: CommonTables FastAtan2 FastMathFunctions Magnetic
FastECompass.h@3:5eb51c7b0ca3, 2016-06-20 (annotated)
- Committer:
- Cotzo
- Date:
- Mon Jun 20 17:53:52 2016 +0000
- Revision:
- 3:5eb51c7b0ca3
- Parent:
- 0:a3affe6b4fe8
Bug fixed in the metodo updateSensors
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cotzo | 0:a3affe6b4fe8 | 1 | /* |
Cotzo | 0:a3affe6b4fe8 | 2 | * FastECompass Library |
Cotzo | 0:a3affe6b4fe8 | 3 | * Autor: Federico Pinna |
Cotzo | 0:a3affe6b4fe8 | 4 | * Date: 29 de may.de 2016 |
Cotzo | 0:a3affe6b4fe8 | 5 | */ |
Cotzo | 0:a3affe6b4fe8 | 6 | #include "mbed.h" |
Cotzo | 0:a3affe6b4fe8 | 7 | #include "arm_math.h" |
Cotzo | 0:a3affe6b4fe8 | 8 | #include "magnetic.h" |
Cotzo | 0:a3affe6b4fe8 | 9 | #include "MotionSensor.h" |
Cotzo | 0:a3affe6b4fe8 | 10 | #include "FastAtan2.h" |
Cotzo | 0:a3affe6b4fe8 | 11 | |
Cotzo | 0:a3affe6b4fe8 | 12 | |
Cotzo | 0:a3affe6b4fe8 | 13 | #ifndef FASTECOMPASS_FASTECOMPASS_H_ |
Cotzo | 0:a3affe6b4fe8 | 14 | #define FASTECOMPASS_FASTECOMPASS_H_ |
Cotzo | 0:a3affe6b4fe8 | 15 | |
Cotzo | 0:a3affe6b4fe8 | 16 | /** FastECompass class. |
Cotzo | 0:a3affe6b4fe8 | 17 | * |
Cotzo | 0:a3affe6b4fe8 | 18 | * Example: |
Cotzo | 0:a3affe6b4fe8 | 19 | * @code |
Cotzo | 0:a3affe6b4fe8 | 20 | * #include "mbed.h" |
Cotzo | 0:a3affe6b4fe8 | 21 | * #include "MAG3110.h" |
Cotzo | 0:a3affe6b4fe8 | 22 | * #include "MMA8451Q.h" |
Cotzo | 0:a3affe6b4fe8 | 23 | * #include "FastECompass.h" |
Cotzo | 0:a3affe6b4fe8 | 24 | * |
Cotzo | 0:a3affe6b4fe8 | 25 | * #define MMA8451_I2C_ADDRESS (0x1d<<1) |
Cotzo | 0:a3affe6b4fe8 | 26 | * |
Cotzo | 0:a3affe6b4fe8 | 27 | * MAG3110 mag( PTE25, PTE24); |
Cotzo | 0:a3affe6b4fe8 | 28 | * MMA8451Q acc( PTE25, PTE24, MMA8451_I2C_ADDRESS); |
Cotzo | 0:a3affe6b4fe8 | 29 | * DigitalOut lred(LED_RED,1); |
Cotzo | 0:a3affe6b4fe8 | 30 | * |
Cotzo | 0:a3affe6b4fe8 | 31 | * FastECompass ec(&mag,&acc); |
Cotzo | 0:a3affe6b4fe8 | 32 | * Serial pc(USBTX,USBRX); |
Cotzo | 0:a3affe6b4fe8 | 33 | * |
Cotzo | 0:a3affe6b4fe8 | 34 | * int main(){ |
Cotzo | 0:a3affe6b4fe8 | 35 | * |
Cotzo | 0:a3affe6b4fe8 | 36 | * ec.enableSensors(); |
Cotzo | 0:a3affe6b4fe8 | 37 | * ec.calibrateSensors(); |
Cotzo | 0:a3affe6b4fe8 | 38 | * |
Cotzo | 0:a3affe6b4fe8 | 39 | * lred=0; |
Cotzo | 0:a3affe6b4fe8 | 40 | * |
Cotzo | 0:a3affe6b4fe8 | 41 | * while(true){ |
Cotzo | 0:a3affe6b4fe8 | 42 | * ec.updateSensors(); |
Cotzo | 0:a3affe6b4fe8 | 43 | * pc.printf("Pitch: %d Roll: %d Yaw: %d\n",ec.getPitch(),ec.getRoll(),ec.getYaw()); |
Cotzo | 0:a3affe6b4fe8 | 44 | * wait(0.5); |
Cotzo | 0:a3affe6b4fe8 | 45 | * } |
Cotzo | 0:a3affe6b4fe8 | 46 | * |
Cotzo | 0:a3affe6b4fe8 | 47 | * } |
Cotzo | 0:a3affe6b4fe8 | 48 | * @endcode |
Cotzo | 0:a3affe6b4fe8 | 49 | **/ |
Cotzo | 0:a3affe6b4fe8 | 50 | |
Cotzo | 0:a3affe6b4fe8 | 51 | |
Cotzo | 0:a3affe6b4fe8 | 52 | class FastECompass{ |
Cotzo | 0:a3affe6b4fe8 | 53 | |
Cotzo | 0:a3affe6b4fe8 | 54 | MotionSensor *magsensor; |
Cotzo | 0:a3affe6b4fe8 | 55 | MotionSensor *accsensor; |
Cotzo | 0:a3affe6b4fe8 | 56 | MotionSensorDataCounts mag_raw; |
Cotzo | 0:a3affe6b4fe8 | 57 | MotionSensorDataCounts acc_raw; |
Cotzo | 0:a3affe6b4fe8 | 58 | MagCalibration magcal; |
Cotzo | 0:a3affe6b4fe8 | 59 | i16MagCalibration i16magcal; |
Cotzo | 0:a3affe6b4fe8 | 60 | MagneticBuffer magbuf; |
Cotzo | 0:a3affe6b4fe8 | 61 | int32_t pitch; |
Cotzo | 0:a3affe6b4fe8 | 62 | int32_t roll; |
Cotzo | 0:a3affe6b4fe8 | 63 | int32_t yaw; |
Cotzo | 0:a3affe6b4fe8 | 64 | |
Cotzo | 0:a3affe6b4fe8 | 65 | public: |
Cotzo | 0:a3affe6b4fe8 | 66 | /** Create and Initialize FastEComppas instance |
Cotzo | 0:a3affe6b4fe8 | 67 | *@param magsensor Pointer to object Magnetometer sensor derived from the MotionSensor class |
Cotzo | 0:a3affe6b4fe8 | 68 | *@param accsensor Pointer to object Accelerometer sensor derived from the MotionSensor class |
Cotzo | 0:a3affe6b4fe8 | 69 | */ |
Cotzo | 0:a3affe6b4fe8 | 70 | FastECompass(MotionSensor *magsensor, MotionSensor *accsensor); |
Cotzo | 0:a3affe6b4fe8 | 71 | |
Cotzo | 0:a3affe6b4fe8 | 72 | /**Calibrates hard-iron magnetometer sensor. |
Cotzo | 0:a3affe6b4fe8 | 73 | * Takes a sample every 40 milliseconds. |
Cotzo | 0:a3affe6b4fe8 | 74 | */ |
Cotzo | 0:a3affe6b4fe8 | 75 | void calibrateSensors(); |
Cotzo | 0:a3affe6b4fe8 | 76 | /** |
Cotzo | 0:a3affe6b4fe8 | 77 | * Enables magnetometer and accelerometer sensors. |
Cotzo | 0:a3affe6b4fe8 | 78 | */ |
Cotzo | 0:a3affe6b4fe8 | 79 | void enableSensors(); |
Cotzo | 0:a3affe6b4fe8 | 80 | /** |
Cotzo | 0:a3affe6b4fe8 | 81 | * Update magnetometer and accelerometer sensors. |
Cotzo | 0:a3affe6b4fe8 | 82 | * Calculates pitch, roll and yaw using the Tilt-Compensated algorithm. |
Cotzo | 0:a3affe6b4fe8 | 83 | */ |
Cotzo | 0:a3affe6b4fe8 | 84 | void updateSensors(); |
Cotzo | 0:a3affe6b4fe8 | 85 | /** Gets the value of the roll angle |
Cotzo | 3:5eb51c7b0ca3 | 86 | * @return An integer representing the roll angle in deg the range of -90 to 90. |
Cotzo | 0:a3affe6b4fe8 | 87 | */ |
Cotzo | 0:a3affe6b4fe8 | 88 | int32_t getRoll(); |
Cotzo | 0:a3affe6b4fe8 | 89 | /** Gets the value of the pitch angle |
Cotzo | 3:5eb51c7b0ca3 | 90 | * @return An integer representing the pitch angle in deg the range of -90 to 90. |
Cotzo | 0:a3affe6b4fe8 | 91 | */ |
Cotzo | 0:a3affe6b4fe8 | 92 | int32_t getPitch(); |
Cotzo | 0:a3affe6b4fe8 | 93 | /** Gets the value of the yaw angle |
Cotzo | 3:5eb51c7b0ca3 | 94 | * @return An integer representing the yaw angle in deg in the range of 0 to 360. |
Cotzo | 0:a3affe6b4fe8 | 95 | */ |
Cotzo | 0:a3affe6b4fe8 | 96 | int32_t getYaw(); |
Cotzo | 0:a3affe6b4fe8 | 97 | |
Cotzo | 0:a3affe6b4fe8 | 98 | }; |
Cotzo | 0:a3affe6b4fe8 | 99 | |
Cotzo | 0:a3affe6b4fe8 | 100 | |
Cotzo | 0:a3affe6b4fe8 | 101 | |
Cotzo | 0:a3affe6b4fe8 | 102 | #endif /* FASTECOMPASS_FASTECOMPASS_H_ */ |