Rosario Schiano Lo Moriello
/
Example11AccelerometerSimulator
Accelerometer simulator
main.cpp@3:b87d5296015c, 2020-11-03 (annotated)
- Committer:
- rschiano
- Date:
- Tue Nov 03 16:20:44 2020 +0000
- Revision:
- 3:b87d5296015c
- Parent:
- 2:fbb699c3994f
Accelerometer simulator
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rschiano | 0:8ef83d7dc88d | 1 | #include "mbed.h" |
rschiano | 2:fbb699c3994f | 2 | #include <stdlib.h> |
rschiano | 0:8ef83d7dc88d | 3 | |
rschiano | 2:fbb699c3994f | 4 | union MYFLOAT { |
rschiano | 2:fbb699c3994f | 5 | float value; |
rschiano | 2:fbb699c3994f | 6 | uint8_t byte[4]; |
rschiano | 3:b87d5296015c | 7 | } sendData,sampPer; |
rschiano | 2:fbb699c3994f | 8 | |
rschiano | 2:fbb699c3994f | 9 | union MYINT { |
rschiano | 2:fbb699c3994f | 10 | uint16_t value; |
rschiano | 2:fbb699c3994f | 11 | uint8_t byte[2]; |
rschiano | 2:fbb699c3994f | 12 | } nSamples; |
rschiano | 2:fbb699c3994f | 13 | |
rschiano | 3:b87d5296015c | 14 | float bx,by,bz,ax,ay,az; |
rschiano | 2:fbb699c3994f | 15 | Serial pc(PA_2,PA_3,921600); |
rschiano | 1:a31564d0b86c | 16 | DigitalIn myBtn(PC_13); |
rschiano | 1:a31564d0b86c | 17 | Timer timer; |
rschiano | 1:a31564d0b86c | 18 | // Clock frequency 84MHz 12ns Instruction clock |
rschiano | 2:fbb699c3994f | 19 | unsigned int cnt; |
rschiano | 2:fbb699c3994f | 20 | float elapTime,value = 17.1234567; |
rschiano | 0:8ef83d7dc88d | 21 | |
rschiano | 1:a31564d0b86c | 22 | int main() |
rschiano | 1:a31564d0b86c | 23 | { |
rschiano | 3:b87d5296015c | 24 | // Nominal acceleration values expressed in terms of mg |
rschiano | 3:b87d5296015c | 25 | ax = 0; |
rschiano | 3:b87d5296015c | 26 | ay = 0; |
rschiano | 3:b87d5296015c | 27 | az = 1000; |
rschiano | 3:b87d5296015c | 28 | // Bias values, also expressed in terms of mg +-60mg |
rschiano | 3:b87d5296015c | 29 | bx = rand()%120 - 60; // rand() [0,1[; rand()%120 [0,120[; rand()%120 -60 [-60,60[ |
rschiano | 3:b87d5296015c | 30 | by = rand()%120 - 60; |
rschiano | 3:b87d5296015c | 31 | bz = rand()%120 - 60; |
rschiano | 3:b87d5296015c | 32 | // Update nominal value with the bias |
rschiano | 3:b87d5296015c | 33 | ax += bx; //ax.value = ax.value + bx |
rschiano | 3:b87d5296015c | 34 | ay += by; |
rschiano | 3:b87d5296015c | 35 | az += bz; |
rschiano | 3:b87d5296015c | 36 | |
rschiano | 1:a31564d0b86c | 37 | while(myBtn); |
rschiano | 1:a31564d0b86c | 38 | while(!myBtn); |
rschiano | 2:fbb699c3994f | 39 | pc.printf("Hello world!!!\r\n"); |
rschiano | 2:fbb699c3994f | 40 | // timer.start(); |
rschiano | 2:fbb699c3994f | 41 | while(1) { |
rschiano | 2:fbb699c3994f | 42 | for(cnt=0; cnt<2; cnt++) |
rschiano | 2:fbb699c3994f | 43 | nSamples.byte[cnt] = pc.getc(); |
rschiano | 2:fbb699c3994f | 44 | // pc.scanf("%u",&nSamples); |
rschiano | 2:fbb699c3994f | 45 | pc.printf("Entered %d.\r\n",nSamples.value); |
rschiano | 2:fbb699c3994f | 46 | // pc.printf("Please, provide the sampling period [s] "); |
rschiano | 2:fbb699c3994f | 47 | for(cnt=0; cnt<4; cnt++) |
rschiano | 2:fbb699c3994f | 48 | sampPer.byte[cnt] = pc.getc(); |
rschiano | 2:fbb699c3994f | 49 | // pc.scanf("%f",&sampPer); |
rschiano | 2:fbb699c3994f | 50 | pc.printf("Entered %e.\r\n",sampPer.value); |
rschiano | 2:fbb699c3994f | 51 | for(cnt=0; cnt<nSamples.value; cnt++) { |
rschiano | 1:a31564d0b86c | 52 | timer.reset(); |
rschiano | 3:b87d5296015c | 53 | sendData.value = ax + (float)(rand()%500000)/100000.0f; // [0, 5[ mg |
rschiano | 3:b87d5296015c | 54 | for(int i=0; i<4; i++) { |
rschiano | 3:b87d5296015c | 55 | pc.putc(sendData.byte[i]); |
rschiano | 3:b87d5296015c | 56 | } |
rschiano | 3:b87d5296015c | 57 | sendData.value = ay + (float)(rand()%500000)/100000.0f; // [0, 5[ mg |
rschiano | 2:fbb699c3994f | 58 | for(int i=0; i<4; i++) { |
rschiano | 3:b87d5296015c | 59 | pc.putc(sendData.byte[i]); |
rschiano | 3:b87d5296015c | 60 | } |
rschiano | 3:b87d5296015c | 61 | sendData.value = az + (float)(rand()%500000)/100000.0f; // [0, 5[ mg |
rschiano | 3:b87d5296015c | 62 | for(int i=0; i<4; i++) { |
rschiano | 3:b87d5296015c | 63 | pc.putc(sendData.byte[i]); |
rschiano | 2:fbb699c3994f | 64 | } |
rschiano | 1:a31564d0b86c | 65 | elapTime = timer.read(); |
rschiano | 2:fbb699c3994f | 66 | wait(sampPer.value - elapTime); |
rschiano | 1:a31564d0b86c | 67 | } |
rschiano | 1:a31564d0b86c | 68 | } |
rschiano | 0:8ef83d7dc88d | 69 | } |