
Code for the project of LELEC2811 2017
Dependencies: FreescaleIAP MMA8491Q_PG mbed
Fork of LELEC_2811_Accelerometer by
Diff: main.cpp
- Revision:
- 6:4ea2ba88338f
- Parent:
- 5:79b8cd191fa8
--- a/main.cpp Fri Dec 01 10:30:27 2017 +0000 +++ b/main.cpp Sat Dec 02 21:14:53 2017 +0000 @@ -30,8 +30,8 @@ #define ACQ_TIMER_PERIOD 0.005 // Time between 2 acquisitions (here 5 mSec) #define N_PTS 100 // Number of points for each axis used to detect mvt #define N_MVTS 5 // Number of mvts detected -#define THRESHOLD_MVT 0.5 // threshold to validate a mvt -#define THRESHOLD_SHOCK 0.5 // threshold to detect shock +#define THRESHOLD_MVT 0.4 // threshold to validate a mvt +#define THRESHOLD_SHOCK 0.45 // threshold to detect shock MMA8491Q my8491(PTE0, PTE1, MMA8491_I2C_ADDRESS); // Setup I2C for MMA8491 @@ -83,14 +83,20 @@ int flash_base_address = RESERVED_SECTOR * SECTOR_SIZE ; // Store Flash Base Address int flash_next_address; // next address for saving data in flash int flash_base_address_cmd; // base address where the parameters are saved -/* -const float Weights[3*N_PTS*N_MVTS] = { - #include "Weights.txt" + +const float w_s_l [3*N_PTS] = { + #include "weights_samples_layer.txt" +}; +const float b_s_l [N_PTS] = { + #include "biases_samples_layer.txt" }; -const float Biases[N_MVTS] = { - #include "Biases.txt" +const float w_o_l [N_PTS*N_MVTS] = { + #include "weights_output_layer.txt" }; -*/ +const float b_o_l [N_MVTS] = { + #include "biases_output_layer.txt" +}; + // ------------------------ Function Declaration ------------------------ void Init(void); void DisplayFlashInfos(void); // display memory use @@ -111,7 +117,10 @@ void Log(void); // read data, detect shock and movement void Rotate(int16_t *AccDataLog, int amount, int16_t *inputs); // inputs = AccDataLog rotated of amount void PrintSet(MvtSet mvtSet); // display set of data -void Softmax (float *inputs, float *result); // softmax function (used by neural network) + +float Map (float x); // inputs mapped between 0 and 1 +float Sigmoid (float x); // sigmoid function +void Softmax (float *inputs, float *result); // softmax function Mvt SelectMvt(int16_t *inputs); // compute probabilities for each mvt based on the inputs // ------------------------------------------------------------------------------------------------------- @@ -260,9 +269,18 @@ { char cmd = Host_Comm.getc(); if ((cmd == 'E') || (cmd == 'e')) { - EraseAllSectors(); - flash_next_address = flash_base_address; - Host_Comm.printf("Erase done.\n\r"); + Host_Comm.printf("Press 'E' again to confirm.\n\r"); + wait_ms(1000); + if(Host_Comm.readable()) { + cmd = Host_Comm.getc(); + if ((cmd == 'E') || (cmd == 'e')) { + EraseAllSectors(); + flash_next_address = flash_base_address; + Host_Comm.printf("Erase done.\n\r"); + } + } + else + Host_Comm.printf("Erase aborded.\n\r"); } else if ((cmd == 'C') || (cmd == 'c')) { mode = CONSOLE; @@ -555,6 +573,15 @@ Host_Comm.printf("------- End Set -------\n\n\r"); } +// ------------------------------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------------- + +// -------------------------------- Map --------------------------------- +float Map (float x) { return (x+OFFSET)/RANGE; } + +// ------------------------------ Sigmoid ------------------------------- +float Sigmoid (float x) { return 1/(1+exp(-x)); } + // ------------------------------ Softmax ------------------------------- void Softmax (float *inputs, float *result) { @@ -573,26 +600,28 @@ // ----------------------------- SelectMvt ------------------------------ Mvt SelectMvt(int16_t *inputs) { - /* int i, j; - float selection [N_MVTS] = {}; + + float samples [N_PTS]; + for (i = 0; i < N_PTS; i++) + samples[i] = Sigmoid( Map(inputs[i*3])*w_s_l[i*3] + Map(inputs[i*3+1])*w_s_l[i*3+1] + Map(inputs[i*3+2])*w_s_l[i*3+2] + b_s_l[i] ); + + float probabilities [N_MVTS] = {}; for (j = 0; j < N_MVTS; j++) { - for (i = 0; i < N_PTS*3; i++) - selection[j] += (inputs[i]+OFFSET)/RANGE * Weights[i*N_MVTS+j]; - selection[j] = selection[j]+Biases[j]; + for (i = 0; i < N_PTS; i++) + probabilities[j] += samples[i] * w_o_l[i*N_MVTS+j]; + probabilities[j] = probabilities[j] + b_o_l[j]; } - Softmax(selection,selection); + Softmax(probabilities,probabilities); Mvt mvt = Undefined; Host_Comm.printf("Proba mvt : "); for (i = 0; i < N_MVTS; i++) { - if (selection[i] > THRESHOLD_MVT) + if (probabilities[i] > THRESHOLD_MVT) mvt = static_cast<Mvt>(i+1); - Host_Comm.printf("%f ",selection[i]); + Host_Comm.printf("%f ",probabilities[i]); } Host_Comm.printf("\n\r"); return mvt; - */ - return SmashShot; }