Accelerometer simulator

Dependencies:   mbed

main.cpp

Committer:
rschiano
Date:
2020-11-03
Revision:
3:b87d5296015c
Parent:
2:fbb699c3994f

File content as of revision 3:b87d5296015c:

#include "mbed.h"
#include <stdlib.h>

union MYFLOAT {
    float value;
    uint8_t byte[4];
} 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;
// Clock frequency 84MHz 12ns Instruction clock
unsigned int cnt;
float elapTime,value = 17.1234567;

int main()
{
    // 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");
//    timer.start();
    while(1) {
        for(cnt=0; cnt<2; cnt++)
            nSamples.byte[cnt] = pc.getc();
//        pc.scanf("%u",&nSamples);
        pc.printf("Entered %d.\r\n",nSamples.value);
//        pc.printf("Please, provide the sampling period [s] ");
        for(cnt=0; cnt<4; cnt++)
            sampPer.byte[cnt] = pc.getc();
//        pc.scanf("%f",&sampPer);
        pc.printf("Entered %e.\r\n",sampPer.value);
        for(cnt=0; cnt<nSamples.value; cnt++) {
            timer.reset();
            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(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);
        }
    }
}