LSM303DLHC Full Driver: Readings For Acc, Mag and Temp; Class Method for frequently-used 13 sensor parameters; Class Method to direct modify registers; Support Calibration (offset+scale);
main.cpp@4:8723c07d4c45, 2016-06-09 (annotated)
- Committer:
- Airium
- Date:
- Thu Jun 09 15:19:00 2016 +0000
- Revision:
- 4:8723c07d4c45
- Parent:
- 3:522d01930e6a
Add offset calibration for specific use in HPF-on situation and therefore remove (bool) HPF_state and isHPFEn() and associated code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Airium | 0:7864abfabe2f | 1 | #include "mbed.h" |
Airium | 0:7864abfabe2f | 2 | #include "LSM303DLHC.h" |
Airium | 0:7864abfabe2f | 3 | |
Airium | 0:7864abfabe2f | 4 | LSM303DLHC sensor(p9,p10); |
Airium | 0:7864abfabe2f | 5 | Serial pc(USBTX,USBRX); |
Airium | 0:7864abfabe2f | 6 | Ticker ticker; |
Airium | 0:7864abfabe2f | 7 | |
Airium | 0:7864abfabe2f | 8 | float acc[3] = { 0 }; |
Airium | 0:7864abfabe2f | 9 | float mag[3] = { 0 }; |
Airium | 0:7864abfabe2f | 10 | float tmp[1] = { 0 }; |
Airium | 0:7864abfabe2f | 11 | |
Airium | 0:7864abfabe2f | 12 | void CallBack(); |
Airium | 0:7864abfabe2f | 13 | |
Airium | 0:7864abfabe2f | 14 | int main(){ |
Airium | 0:7864abfabe2f | 15 | pc.baud(921600); |
Airium | 0:7864abfabe2f | 16 | |
Airium | 3:522d01930e6a | 17 | // example setup |
Airium | 3:522d01930e6a | 18 | // refer to LSM303DLHC.h for more overloaded details for ACtrl() MCtrl() TCtrl() |
Airium | 0:7864abfabe2f | 19 | sensor.MCtrl(LSM303DLHC::GN1); // set mag range +/-1.3Gausee |
Airium | 3:522d01930e6a | 20 | sensor.MCtrl(LSM303DLHC::MDR3); // set mag date rate 7.5Hz |
Airium | 0:7864abfabe2f | 21 | sensor.ACtrl(LSM303DLHC::G8); // set acc range +/-8g |
Airium | 3:522d01930e6a | 22 | sensor.ACtrl(LSM303DLHC::ADR2); // set acc date rate 10Hz |
Airium | 0:7864abfabe2f | 23 | sensor.ACtrl(LSM303DLHC::HPF_OFF); // disable internal acc HPF |
Airium | 3:522d01930e6a | 24 | |
Airium | 0:7864abfabe2f | 25 | float acc_offset[3] = { 6.5, 5, 12.5 }; // acc offset |
Airium | 0:7864abfabe2f | 26 | float acc_scale[3] = { 3.9, 4, 4 }; // acc scale |
Airium | 3:522d01930e6a | 27 | sensor.ACal(acc_offset,acc_scale); // set acc calibration |
Airium | 0:7864abfabe2f | 28 | |
Airium | 3:522d01930e6a | 29 | ticker.attach(&CallBack, 0.2); // read and refresh disp with 200ms interval |
Airium | 3:522d01930e6a | 30 | |
Airium | 0:7864abfabe2f | 31 | while (1) |
Airium | 0:7864abfabe2f | 32 | wait_us(1); |
Airium | 0:7864abfabe2f | 33 | } |
Airium | 0:7864abfabe2f | 34 | |
Airium | 0:7864abfabe2f | 35 | void CallBack(){ |
Airium | 0:7864abfabe2f | 36 | sensor.GetAcc(acc); |
Airium | 0:7864abfabe2f | 37 | sensor.GetMag(mag); |
Airium | 0:7864abfabe2f | 38 | sensor.GetTemp(tmp); |
Airium | 0:7864abfabe2f | 39 | |
Airium | 3:522d01930e6a | 40 | pc.printf("A %5f %5f %5f\n\r", acc[0], acc[1], acc[2]); |
Airium | 3:522d01930e6a | 41 | pc.printf("M %5f %5f %5f\n\r", mag[0], mag[1], mag[2]); |
Airium | 3:522d01930e6a | 42 | pc.printf("T %5f\n\r", tmp[0]); |
Airium | 0:7864abfabe2f | 43 | } |
Airium | 0:7864abfabe2f | 44 |