Accelerometer simulator

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
rschiano
Date:
Tue Nov 03 16:20:44 2020 +0000
Parent:
2:fbb699c3994f
Commit message:
Accelerometer simulator

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Nov 03 15:23:36 2020 +0000
+++ b/main.cpp	Tue Nov 03 16:20:44 2020 +0000
@@ -4,13 +4,14 @@
 union MYFLOAT {
     float value;
     uint8_t byte[4];
-} example,sampPer;
+} sendData,sampPer;
 
 union MYINT {
     uint16_t value;
     uint8_t byte[2];
 } nSamples;
 
+float bx,by,bz,ax,ay,az;
 Serial pc(PA_2,PA_3,921600);
 DigitalIn myBtn(PC_13);
 Timer timer;
@@ -20,17 +21,19 @@
 
 int main()
 {
-    /*    example.value = 2.343456795e1;
-        pc.printf("The value is %.9f.\r\n",example.value);
-    //    pc.printf("The value is ");
-        for(int j=0;j<10;j++){
-            for(int i=0;i<4;i++){
-                pc.putc(example.byte[i]);
-            }
-            example.value = example.value + 1.0f;
-        }
-    //    pc.printf("\r\n");
-    */
+    // Nominal acceleration values expressed in terms of mg
+    ax = 0;
+    ay = 0;
+    az = 1000; 
+    // Bias values, also expressed in terms of mg +-60mg
+    bx = rand()%120 - 60; // rand() [0,1[; rand()%120 [0,120[; rand()%120 -60 [-60,60[ 
+    by = rand()%120 - 60;
+    bz = rand()%120 - 60;
+    // Update nominal value with the bias
+    ax += bx;  //ax.value = ax.value + bx
+    ay += by;
+    az += bz;  
+    
     while(myBtn);
     while(!myBtn);
     pc.printf("Hello world!!!\r\n");
@@ -47,9 +50,17 @@
         pc.printf("Entered %e.\r\n",sampPer.value);
         for(cnt=0; cnt<nSamples.value; cnt++) {
             timer.reset();
-            example.value = value + (float)(rand()%100)/100;
+            sendData.value = ax + (float)(rand()%500000)/100000.0f; // [0, 5[ mg
+            for(int i=0; i<4; i++) {
+                pc.putc(sendData.byte[i]);
+            }
+            sendData.value = ay + (float)(rand()%500000)/100000.0f; // [0, 5[ mg
             for(int i=0; i<4; i++) {
-                pc.putc(example.byte[i]);
+                pc.putc(sendData.byte[i]);
+            }
+            sendData.value = az + (float)(rand()%500000)/100000.0f; // [0, 5[ mg
+            for(int i=0; i<4; i++) {
+                pc.putc(sendData.byte[i]);
             }
             elapTime = timer.read();
             wait(sampPer.value - elapTime);