![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
code projet M1 + ensil
Dependencies: ADXL345 HMC5843 ITG3200 mbed
Fork of 9DOF-Stick by
main.cpp@0:9410b3e6aea5, 2011-10-07 (annotated)
- Committer:
- Digixx
- Date:
- Fri Oct 07 21:31:36 2011 +0000
- Revision:
- 0:9410b3e6aea5
- Child:
- 1:18f68a0b0793
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Digixx | 0:9410b3e6aea5 | 1 | #include "mbed.h" |
Digixx | 0:9410b3e6aea5 | 2 | #include "HMC5843.h" |
Digixx | 0:9410b3e6aea5 | 3 | #include "ADXL345.h" |
Digixx | 0:9410b3e6aea5 | 4 | #include "ITG3200.h" |
Digixx | 0:9410b3e6aea5 | 5 | |
Digixx | 0:9410b3e6aea5 | 6 | DigitalOut myled(LED1); |
Digixx | 0:9410b3e6aea5 | 7 | |
Digixx | 0:9410b3e6aea5 | 8 | HMC5843 cmp(p28, p27); // sda, scl |
Digixx | 0:9410b3e6aea5 | 9 | ADXL345 acc(p28, p27); // sda, scl |
Digixx | 0:9410b3e6aea5 | 10 | ITG3200 gyr(p28, p27); // sda, scl |
Digixx | 0:9410b3e6aea5 | 11 | Serial pc(USBTX, USBRX); // tx, rx |
Digixx | 0:9410b3e6aea5 | 12 | |
Digixx | 0:9410b3e6aea5 | 13 | int main() { |
Digixx | 0:9410b3e6aea5 | 14 | pc.baud(115200); |
Digixx | 0:9410b3e6aea5 | 15 | //values x,y,z |
Digixx | 0:9410b3e6aea5 | 16 | int readings[3]; |
Digixx | 0:9410b3e6aea5 | 17 | //ID Buffer |
Digixx | 0:9410b3e6aea5 | 18 | char buffer[3]; |
Digixx | 0:9410b3e6aea5 | 19 | |
Digixx | 0:9410b3e6aea5 | 20 | pc.printf("%c" ,13,10,13,10,13,10); |
Digixx | 0:9410b3e6aea5 | 21 | |
Digixx | 0:9410b3e6aea5 | 22 | // do init stuff |
Digixx | 0:9410b3e6aea5 | 23 | //Continuous mode, , 10Hz measurement rate. |
Digixx | 0:9410b3e6aea5 | 24 | // HMC5843_CONTINUOUS, HMC5843_10HZ_NORMAL HMC5843_1_0GA |
Digixx | 0:9410b3e6aea5 | 25 | cmp.setDefault(); |
Digixx | 0:9410b3e6aea5 | 26 | //Wait some time(Need at least 5ms) |
Digixx | 0:9410b3e6aea5 | 27 | wait(0.1); |
Digixx | 0:9410b3e6aea5 | 28 | cmp.getAddress(buffer); |
Digixx | 0:9410b3e6aea5 | 29 | pc.printf("cmp Id=%c%c%c \n\r",buffer[0],buffer[1],buffer[2]); |
Digixx | 0:9410b3e6aea5 | 30 | |
Digixx | 0:9410b3e6aea5 | 31 | // These are here to test whether any of the initialization fails. It will print the failure |
Digixx | 0:9410b3e6aea5 | 32 | if (acc.setPowerControl(0x00)){ |
Digixx | 0:9410b3e6aea5 | 33 | pc.printf("acc: didn't intitialize power control\n"); |
Digixx | 0:9410b3e6aea5 | 34 | return 0; } |
Digixx | 0:9410b3e6aea5 | 35 | wait(.001); |
Digixx | 0:9410b3e6aea5 | 36 | |
Digixx | 0:9410b3e6aea5 | 37 | //Full resolution, +/-16g, 4mg/LSB. |
Digixx | 0:9410b3e6aea5 | 38 | if(acc.setDataFormatControl(0x0B)){ |
Digixx | 0:9410b3e6aea5 | 39 | pc.printf("didn't set data format\n"); |
Digixx | 0:9410b3e6aea5 | 40 | return 0; } |
Digixx | 0:9410b3e6aea5 | 41 | wait(.001); |
Digixx | 0:9410b3e6aea5 | 42 | |
Digixx | 0:9410b3e6aea5 | 43 | //3.2kHz data rate. |
Digixx | 0:9410b3e6aea5 | 44 | if(acc.setDataRate(ADXL345_3200HZ)){ |
Digixx | 0:9410b3e6aea5 | 45 | pc.printf("didn't set data rate\n"); |
Digixx | 0:9410b3e6aea5 | 46 | return 0; } |
Digixx | 0:9410b3e6aea5 | 47 | wait(.001); |
Digixx | 0:9410b3e6aea5 | 48 | |
Digixx | 0:9410b3e6aea5 | 49 | //Measurement mode. |
Digixx | 0:9410b3e6aea5 | 50 | if(acc.setPowerControl(MeasurementMode)) { |
Digixx | 0:9410b3e6aea5 | 51 | pc.printf("didn't set the power control to measurement\n"); |
Digixx | 0:9410b3e6aea5 | 52 | return 0; } |
Digixx | 0:9410b3e6aea5 | 53 | |
Digixx | 0:9410b3e6aea5 | 54 | pc.printf("Acc Id=%x \n\r", acc.getDeviceID()); |
Digixx | 0:9410b3e6aea5 | 55 | pc.printf("%c" ,13,10); |
Digixx | 0:9410b3e6aea5 | 56 | |
Digixx | 0:9410b3e6aea5 | 57 | //Set highest bandwidth. |
Digixx | 0:9410b3e6aea5 | 58 | gyr.setLpBandwidth(LPFBW_256HZ); |
Digixx | 0:9410b3e6aea5 | 59 | pc.printf("Gyro Id=%x \n\r", gyr.getWhoAmI()); |
Digixx | 0:9410b3e6aea5 | 60 | pc.printf("%c" ,13,10); |
Digixx | 0:9410b3e6aea5 | 61 | |
Digixx | 0:9410b3e6aea5 | 62 | wait(1); |
Digixx | 0:9410b3e6aea5 | 63 | |
Digixx | 0:9410b3e6aea5 | 64 | while (1) { |
Digixx | 0:9410b3e6aea5 | 65 | |
Digixx | 0:9410b3e6aea5 | 66 | cmp.readData(readings); |
Digixx | 0:9410b3e6aea5 | 67 | pc.printf("C %+4i %+4i %+4i",(int16_t)readings[0],(int16_t)readings[1],(int16_t)readings[2]); |
Digixx | 0:9410b3e6aea5 | 68 | wait(0.05); |
Digixx | 0:9410b3e6aea5 | 69 | |
Digixx | 0:9410b3e6aea5 | 70 | acc.getOutput(readings); |
Digixx | 0:9410b3e6aea5 | 71 | pc.printf(" A %+5i %+5i %+5i",(int16_t)readings[0],(int16_t)readings[1],(int16_t)readings[2]); |
Digixx | 0:9410b3e6aea5 | 72 | wait(0.05); |
Digixx | 0:9410b3e6aea5 | 73 | |
Digixx | 0:9410b3e6aea5 | 74 | pc.printf(" G %+5i %+5i %+5i",(int16_t)gyr.getGyroX(),(int16_t)gyr.getGyroY(),(int16_t)gyr.getGyroX()); |
Digixx | 0:9410b3e6aea5 | 75 | pc.printf("%c" ,13,10); |
Digixx | 0:9410b3e6aea5 | 76 | wait(0.05); |
Digixx | 0:9410b3e6aea5 | 77 | |
Digixx | 0:9410b3e6aea5 | 78 | if (myled) { |
Digixx | 0:9410b3e6aea5 | 79 | myled=0; |
Digixx | 0:9410b3e6aea5 | 80 | } else { |
Digixx | 0:9410b3e6aea5 | 81 | myled=1; |
Digixx | 0:9410b3e6aea5 | 82 | } |
Digixx | 0:9410b3e6aea5 | 83 | |
Digixx | 0:9410b3e6aea5 | 84 | } |
Digixx | 0:9410b3e6aea5 | 85 | } |