RF24Network Send example program.

Dependencies:   xtoff RF24Network mbed

Fork of RF24Network_Send by Akash Vibhute

Revision:
11:2aa84e063c49
Parent:
10:875812a04307
Child:
12:38c5efed7950
--- a/main.cpp	Mon Mar 12 11:02:12 2018 +0000
+++ b/main.cpp	Wed Mar 21 16:22:34 2018 +0000
@@ -5,8 +5,9 @@
 #include "PowerControl/PowerControl.h"
 #include "PowerControl/EthernetPowerControl.h"
 
-#define NUM_SAMPLES 2000 // size of sample series
-#define USR_POWERDOWN    (0x104)
+#define NUM_SAMPLES 2000        // size of sample series
+#define USR_POWERDOWN   (0x104)
+#define MIN_TARE_VALUE 0.7575   //2.5 / 3.3 (2.5V is  zero load and adc is between 0 and 1)µ
 
 Verzender sent;
 Serial pc(USBTX, USBRX);
@@ -21,7 +22,10 @@
 float calibration = 0;
 bool reed = false;
 bool tareDone = false;
-State nextState;
+char nextState;
+float calibrationMass = 1003;
+float AVERAGE_TARE = 0;
+float CALIBRATION_OFFSET = 0.0199754;
 
 
 /**
@@ -59,43 +63,76 @@
     @param amount of samples
     @return average of the analog input
 */
-float getAverageSamples(int samples)
+float getAverageSamples(int samples, int subSamples)
 {
     float AVERAGE = 0;
+    int num_sub_samples = 0;
     int num_samples = 0;
-    while (num_samples < samples) {
+
+    while (num_samples <samples) {
         float r = ain.read();
         AVERAGE += r;
         num_samples++;
+        if(num_sub_samples >= subSamples) {
+            wait_ms(10);
+            num_sub_samples = 0;
+        }
+
+        num_sub_samples++;
     }
-
     AVERAGE /= num_samples;
     num_samples = 0;
 
     return AVERAGE;
 }
 
-/**
-    Get the average from the analog input over a mount of time
 
-    @param amount of time in seconds
-    @return average of the analog input
-*/
-float getAverageTime(int time)
+float getAverageSamples2(int samples, int subSamples)
 {
-    t2.start();
-    t2.reset();
     float AVERAGE = 0;
+    float FinalAVERAGE = 0;
+    float SUB_AVERAGE = 0;
+    float SUB_AVERAGE_ARRAY[samples];
     int num_samples = 0;
-    while (num_samples <= NUM_SAMPLES & t2.read() <= time) {
-        float r = ain.read();
-        AVERAGE += r;
+    int num_subSamples = 0;
+    int count = 0;
+
+    //get average of 10 samples and store them in array
+    while( num_samples < samples) {
+        while (num_subSamples < subSamples) {
+            float r = ain.read();
+            SUB_AVERAGE += r;
+            AVERAGE += r;
+            num_subSamples++;
+            count++;
+        }
+        SUB_AVERAGE /= num_subSamples;
+        SUB_AVERAGE_ARRAY[num_samples] = SUB_AVERAGE;
+        SUB_AVERAGE = 0;
+        num_subSamples = 0;
         num_samples++;
     }
-    AVERAGE /= num_samples;
     num_samples = 0;
 
-    return AVERAGE;
+    //calculate total average of 2500 samples
+    AVERAGE /= count;
+    AVERAGE = ((AVERAGE - tare)*(calibrationMass))/(CALIBRATION_OFFSET);
+    count = 0;
+
+    //loop over array and check if the samples are within range of total average
+    for (int i=0; i< samples; i++) {
+        float massa = ((SUB_AVERAGE_ARRAY[i] - tare)*(calibrationMass))/(CALIBRATION_OFFSET);
+
+        if (massa < AVERAGE - 30 or massa > AVERAGE + 30) {
+            massa = 0;
+            count--;
+        }
+        FinalAVERAGE += massa;
+        count++;
+    }
+    FinalAVERAGE /= count;
+    IF_PRINT_ENABLE(pc.printf("FinalAVERAGE: %f , AVERAGE: %f, COUNT: %d\n\r", FinalAVERAGE, AVERAGE, count););
+    return FinalAVERAGE;
 }
 
 /**
@@ -107,6 +144,7 @@
             Read:   Read the mass when the paddle passes the read position and
                     send the data to the receiver.
             Receive: Check if there were messages
+            Calibration: Get calibration factor
 */
 int main()
 {
@@ -124,44 +162,65 @@
                 reedSensor.mode(PullUp);
                 setCurrentState(State_read);
                 payload_t payload;
+                sent.sendMessage(INIT);
                 break;
 
             case State_position:
+                sent.sendMessage(POSITION);
                 IF_PRINT_ENABLE(pc.printf("State: position\n\r"););
                 if (reed) {
+                    sent.sendMessage(POSITION_WAIT);
                     IF_PRINT_ENABLE(pc.printf("Waiting for 5 seconds\n\r"););
-                    wait(5);
-                    if (reed and nextState == State_tare) {
-                        IF_PRINT_ENABLE(pc.printf("Selected state\n\r"););
+                    wait(1);
+                    if (reed and nextState == 's') {
+                        IF_PRINT_ENABLE(pc.printf("Selecting tare state\n\r"););
                         setCurrentState(State_tare);
-                    } else if (reed and nextState == State_calibrate) {
+                        break;
+                    } else if (reed and nextState == 'c') {
+                        IF_PRINT_ENABLE(pc.printf("Selecting calibrate state\n\r"););
                         setCurrentState(State_calibrate);
+                        break;
                     } else {
+                        sent.sendMessage(POSITION_ERROR);
                         IF_PRINT_ENABLE(pc.printf("Error on position\n\r"););
+                        setCurrentState(State_read);
                     }
                 }
                 break;
 
 
             case State_tare:
+                sent.sendMessage(TARE);
                 IF_PRINT_ENABLE(pc.printf("State: tare\n\r"););
-                tare = getAverageSamples(50000);
-                IF_PRINT_ENABLE(pc.printf("tare = %f\r\n",tare*3.3););
-                tareDone = true;
+                tare = getAverageSamples(250,100);
+                if(MIN_TARE_VALUE <= tare) {
+                    sent.sendMessage(TARE_COMPLETE);
+                    IF_PRINT_ENABLE(pc.printf("tare = %f\r\n",tare*3.3););
+                    tareDone = true;
+                } else {
+                    sent.sendMessage(TARE_ERROR);
+                    IF_PRINT_ENABLE(pc.printf("ERROR: TARE VALUE TO LOW\n\r"););
+                    tareDone = false;
+                }
                 setCurrentState(State_read);
                 break;
 
             case State_read:
                 if (reed) {
-                    massa = getAverageTime(1) - tare;
-                    payload.reedsensor = 1;
-                    payload.milligram = massa * 3.3;
-                    IF_PRINT_ENABLE(pc.printf("Sent packet1 -- Reed: %d --- %f mg \r\n",payload.reedsensor, payload.milligram););
-                    bool ok = sent.write(payload);
-                    if (ok) {
-                        IF_PRINT_ENABLE(pc.printf("ok.\n\r"););
+                    if (tareDone == true) {
+                        massa = getAverageSamples2(25, 100);
+                        payload.reedsensor = 1;
+                        payload.gram = massa;
+                        IF_PRINT_ENABLE(pc.printf("Sent packet1 -- Reed: %d --- %f g \r\n",payload.reedsensor, payload.gram););
+                        bool ok = sent.write(payload);
+                        if (ok) {
+                            IF_PRINT_ENABLE(pc.printf("ok.\n\r"););
+                        } else {
+                            IF_PRINT_ENABLE(pc.printf("failed.\n\r"););
+                        }
                     } else {
-                        IF_PRINT_ENABLE(pc.printf("failed.\n\r"););
+                        sent.sendMessage(TARE_FIRST);
+                        IF_PRINT_ENABLE(pc.printf("Tare First.\n\r"););
                     }
                     setCurrentState(State_receive);
                 }
@@ -170,31 +229,39 @@
             case State_receive:
                 sent.update();
                 if (sent.available()) {
+                    IF_PRINT_ENABLE(pc.printf("Received something\n\r"););
                     state_Packet state;
                     state = sent.read();
-                    if(state.setstate == State_tare) {
+
+                    if( state.setstate == 's') {
                         IF_PRINT_ENABLE(pc.printf("Next state: Tare\n\r"););
-                        nextState = State_tare;
+                        nextState = 's';
                         setCurrentState(State_position);
                         break;
-                    } else if ( state.setstate == State_calibrate) {
+                    }
+
+                    if(state.setstate == 'c') {
                         IF_PRINT_ENABLE(pc.printf("Next state: Calibrate\n\r"););
-                        nextState = State_calibrate;
+                        nextState = 'c';
                         setCurrentState(State_position);
+                        break;
                     }
                 }
+
                 setCurrentState(State_read);
                 break;
 
             case State_calibrate:
+                setCurrentState(State_read);
                 if(tareDone == true) {
                     IF_PRINT_ENABLE(pc.printf("State: calibreren\n\r"););
                     IF_PRINT_ENABLE(pc.printf("Put 1kg on paddle...\n\r"););
                     IF_PRINT_ENABLE(pc.printf("Waiting: 10 seconds\n\r"););
-                    wait(10);
+                    wait(1);
                     IF_PRINT_ENABLE(pc.printf("Starting calibration\n\r"););
-                    calibration = getAverageSamples(50000);
-                    IF_PRINT_ENABLE(pc.printf("Calibration done: %f\n\r",  calibration););
+                    calibration = getAverageSamples(1000,10);
+                    IF_PRINT_ENABLE(pc.printf("Calibration=  %f\n\r",  calibration*3.3););
+
                 } else {
                     IF_PRINT_ENABLE(pc.printf("ERROR: TARE FIRST\n\r"););
                 }