dd

Dependencies:   xtoff2 RF24Network mbed

Fork of xtoff3 by pieter Berteloot

Files at this revision

API Documentation at this revision

Comitter:
gimohd
Date:
Wed Sep 05 07:50:27 2018 +0000
Parent:
15:f8aad6d1db68
Commit message:
test

Changed in this revision

LoadCell.cpp Show annotated file Show diff for this revision Revisions of this file
LoadCell.h Show annotated file Show diff for this revision Revisions of this file
Maths.cpp Show annotated file Show diff for this revision Revisions of this file
Maths.h Show annotated file Show diff for this revision Revisions of this file
Ontvanger.cpp Show diff for this revision Revisions of this file
Ontvanger.h Show diff for this revision Revisions of this file
Transmitter.cpp Show annotated file Show diff for this revision Revisions of this file
Transmitter.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r f8aad6d1db68 -r 691649d8a3da LoadCell.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LoadCell.cpp	Wed Sep 05 07:50:27 2018 +0000
@@ -0,0 +1,87 @@
+#include "LoadCell.h"
+
+LoadCell::LoadCell(PinName pin): ain(pin)
+{
+    SAMPLE_AMOUNT = 2000;
+    CALIBRATION_MASS = 843.56; // 274.19
+    TARE_VALUE = 0.743027;
+    CALIBRATION_VALUE = 0.725960;
+    OFFSET = TARE_VALUE-CALIBRATION_VALUE;
+    DOWN_OFFSET = 0.023591;
+};
+
+
+float LoadCell::analogRead()
+{
+    float samples [SAMPLE_AMOUNT];
+    for(int i = 0; i < SAMPLE_AMOUNT; i++) {
+        float read =ain.read();
+        samples [i] =read;
+    }
+    return Maths::mean(samples,0,SAMPLE_AMOUNT);
+}
+
+float LoadCell::simpleAnalogRead()
+{
+    return ain.read();
+}
+
+float LoadCell::calculateMass(float value)
+{
+    return ((TARE_VALUE - value)*(CALIBRATION_MASS))/(OFFSET);
+}
+
+
+float LoadCell::tare()
+{
+    TARE_VALUE = 0;
+    float values[20];
+    for (int a = 0; a<20; a++) {
+        values[a] = analogRead();
+    }
+
+    TARE_VALUE = Maths::mean(values,0,20);
+    return TARE_VALUE;
+}
+
+float LoadCell::tareDown()
+{
+    TARE_VALUE = 0;
+    float values[20];
+    for (int a = 0; a<20; a++) {
+        values[a] = analogRead();
+    }
+
+    TARE_VALUE = Maths::mean(values,0,20)- DOWN_OFFSET;
+    return TARE_VALUE;
+}
+
+float LoadCell::callibrate()
+{
+    CALIBRATION_VALUE = analogRead();
+    return CALIBRATION_VALUE;
+}
+
+
+float LoadCell::mass()
+{
+    return calculateMass(analogRead());
+}
+
+float LoadCell::simpleMass()
+{
+    return calculateMass(simpleAnalogRead());
+}
+
+
+void LoadCell::setCalibrationMass(int mass)
+{
+    CALIBRATION_MASS = mass;
+}
+
+void LoadCell::setSampleAmount(int samples)
+{
+    SAMPLE_AMOUNT = samples;
+}
+
+
diff -r f8aad6d1db68 -r 691649d8a3da LoadCell.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LoadCell.h	Wed Sep 05 07:50:27 2018 +0000
@@ -0,0 +1,39 @@
+#ifndef LoadCell_h
+#define LoadCell_h
+
+#include "mbed.h"
+#include "Maths.h"
+
+class LoadCell
+{
+private:
+    AnalogIn ain;
+    int SAMPLE_AMOUNT;
+    float CALIBRATION_MASS;
+    float TARE_VALUE;
+    float CALIBRATION_VALUE;  
+    float OFFSET;  
+    float DOWN_OFFSET;  
+
+
+public:
+    LoadCell(PinName pin);
+    float analogRead();
+    float simpleAnalogRead();
+    float calculateMass(float value);
+    float tare();
+    float tareDown();
+    float callibrate();
+    float mass();
+    float simpleMass();
+    void setCalibrationMass(int);
+    void setSampleAmount(int);
+          
+};
+
+#endif
+
+
+
+
+
diff -r f8aad6d1db68 -r 691649d8a3da Maths.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Maths.cpp	Wed Sep 05 07:50:27 2018 +0000
@@ -0,0 +1,55 @@
+#include "Maths.h"
+#include "mbed.h"
+
+int Maths::comp(const void* elem1, const void* elem2)
+{
+    if(*(const float*)elem1 < *(const float*)elem2)
+        return -1;
+    return *(const float*)elem1 > *(const float*)elem2;
+}
+
+float Maths::mean(float *samples, int n, int n2)
+{
+    float sum = 0.0;
+    for (int i=0; i<n2-n; i++) {
+        sum += samples[n+i];
+    }
+    return sum / (float)(n2-n);
+}
+
+float Maths::median(float *samples, int n)
+{
+    qsort(samples, n, sizeof(float), Maths::comp);
+
+    return mean(samples, (n - 1)/2 - 990,(n - 1)/2 + 990);
+}
+
+float Maths::stdev(float data[] , int n)
+{
+    float sum = 0.0, mean2, standardDeviation = 0.0;
+    int i;
+    mean2 = Maths::mean(data,0,n);
+
+    for(i = 0; i < n; ++i)
+        standardDeviation += pow(data[i] - mean2, 2);
+
+    return sqrt(standardDeviation / n);
+}
+
+
+float Maths::meanNoOutliers(float data[] , int n)
+{
+    float mean = Maths::mean(data,0,n);
+    float stdev = Maths::stdev(data,n);
+    float amount = 0;
+    float sum  = 0;
+    for(int i = 0; i < n; ++i) {
+        if (data[i] <= mean + 1*stdev && data[i] >= mean - 1*stdev) {
+            amount ++;
+            sum += data[i];
+        }
+    }
+    return sum/amount;
+
+
+}
diff -r f8aad6d1db68 -r 691649d8a3da Maths.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Maths.h	Wed Sep 05 07:50:27 2018 +0000
@@ -0,0 +1,23 @@
+#ifndef Maths_h
+#define Maths_h
+
+class Maths
+{
+private:
+
+
+public:
+    static int comp(const void* elem1, const void* elem2);
+    static float mean(float *samples, int n, int n2);
+    static float median(float *samples, int n);
+    static float stdev(float * samples, int n); 
+    static float meanNoOutliers(float data[] , int n);  
+        
+};
+
+#endif
+
+
+
+
+
diff -r f8aad6d1db68 -r 691649d8a3da Ontvanger.cpp
--- a/Ontvanger.cpp	Fri Jul 13 06:28:33 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,92 +0,0 @@
-#include "Ontvanger.h"
-
-Serial pc2(USBTX, USBRX);
-RF24 radio(spi_MOSI, spi_MISO, spi_SCK, nrf_CE, nrf_CSN );
-RF24Network network(radio);
-
-RF24NetworkHeader header_rx;
-
-Ontvanger::Ontvanger()
-{
-    radio.begin();
-    network.begin(90, this_node);
-    wait_ms(2000);
-    radio.setPALevel(RF24_PA_MIN);
-    radio.setDataRate(RF24_250KBPS);
-};
-
-void Ontvanger::update()
-{
-    network.update();
-}
-
-bool Ontvanger::available()
-{
-    return network.available();
-}
-
-void Ontvanger::printDetails()
-{
-    radio.printDetails();
-}
-
-payload_t Ontvanger::read()
-{
-    payload_t payload_rx;
-    network.read(header_rx,&payload_rx,sizeof(payload_rx));
-    
-    if(payload_rx.messageAvailable == true)
-        printMessage(payload_rx.messageIndex);
-    
-    return payload_rx;
-}
-
-bool Ontvanger::write(payload_t message)
-{
-    RF24NetworkHeader header_tx(other_node);
-    payload_t packet;
-    packet = message;
-    
-    return network.write(header_tx,&packet,sizeof(packet));
-}
-
-bool Ontvanger::isValid()
-{
-    return radio.isValid();
-}
-
-bool Ontvanger::testRPD()
-{
-    return radio.testRPD();
-}
-
-void Ontvanger::printMessage(char index){
-    switch (index) {
-      case '1': 
-        pc2.printf("Message: Initializing...\n\r");
-        break;
-      case '2': 
-        pc2.printf("Message: Taring...\n\r");
-        break;
-      case '3': 
-        pc2.printf("Message: Error: Tare first\n\r");
-        break;
-      case '4': 
-        pc2.printf("Message: Tare completed\n\r");
-        break;
-      case '5': 
-        pc2.printf("Message: Error: Tare value to low... Retry\n\r");
-        break;
-      case '6': 
-        pc2.printf("Message: Waiting on the right position...\n\r");
-        break;
-      case '7': 
-        pc2.printf("Message: Wait 5 Seconds on POSITION...\n\r");
-        break;
-      case '8': 
-        pc2.printf("Message: ERROR: Not on position\n\r");
-        break; 
-}
-    }
-
-
diff -r f8aad6d1db68 -r 691649d8a3da Ontvanger.h
--- a/Ontvanger.h	Fri Jul 13 06:28:33 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,72 +0,0 @@
-#ifndef Ontvanger_h
-#define Ontvanger_h
-#include "mbed.h"
-#include <RF24Network.h>
-#include <RF24.h>
-
-#define nrf_CE      p9
-#define nrf_CSN     p8
-#define spi_SCK     p7
-#define spi_MOSI    p5
-#define spi_MISO    p6
-
-#ifdef PRINT_ENABLE
-    #define IF_PRINT_ENABLE(x) ({x;})
-    #else
-    #define IF_PRINT_ENABLE(x)
-    #endif
-
-
-//Identification verzender/ontvanger
-const uint16_t this_node = 00;
-const uint16_t other_node = 01;
-
-
-//
-struct payload_t {
-    bool reedsensor;
-    float gram;
-    bool messageAvailable;
-    char messageIndex;
-};
-
-enum State2 {init_state,
-             testConnection_state,
-             send_state,
-             receive_state,
-            };
-
-enum State {State_init,
-            State_tare,
-            State_position,
-            State_read,
-            State_send,
-            State_receive,
-            State_calibrate
-           };
-
-
-
-class Ontvanger
-{
-private:
-
-
-public:
-    Ontvanger();
-    void update();
-    bool available();
-    void printDetails();
-    bool write(payload_t message);
-    payload_t read();
-    bool isValid();
-    bool testRPD();
-    void printMessage(char index);
-};
-
-#endif
-
-
-
-
-
diff -r f8aad6d1db68 -r 691649d8a3da Transmitter.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Transmitter.cpp	Wed Sep 05 07:50:27 2018 +0000
@@ -0,0 +1,107 @@
+#include "Transmitter.h"
+
+Serial pc2(USBTX, USBRX);
+RF24 radio(spi_MOSI, spi_MISO, spi_SCK, nrf_CE, nrf_CSN );
+RF24Network network(radio);
+
+RF24NetworkHeader header_rx;
+
+Ontvanger::Ontvanger()
+{
+    radio.begin();
+    network.begin(90, this_node);
+    wait_ms(2000);
+    radio.setPALevel(RF24_PA_MIN);
+    radio.setDataRate(RF24_250KBPS);
+};
+
+void Ontvanger::update()
+{
+    network.update();
+}
+
+bool Ontvanger::available()
+{
+    return network.available();
+}
+
+void Ontvanger::printDetails()
+{
+    radio.printDetails();
+}
+
+char * Ontvanger::readArray()
+{
+    char * payload_rx;
+    network.read(header_rx,&payload_rx,sizeof(payload_rx));
+    return payload_rx;
+}
+
+bool Ontvanger::writeArray(char * message)
+{
+    RF24NetworkHeader header_tx(other_node);
+    char packet[128];
+    strcpy(packet,message);
+    
+    return network.write(header_tx,&packet,sizeof(packet));
+}
+
+
+payload_t Ontvanger::read()
+{
+    payload_t payload_rx;
+    network.read(header_rx,&payload_rx,sizeof(payload_rx));
+    
+    
+    return payload_rx;
+}
+
+bool Ontvanger::write(payload_t message)
+{
+    RF24NetworkHeader header_tx(other_node);
+    payload_t packet;
+    packet = message;
+    
+    return network.write(header_tx,&packet,sizeof(packet));
+}
+
+bool Ontvanger::isValid()
+{
+    return radio.isValid();
+}
+
+bool Ontvanger::testRPD()
+{
+    return radio.testRPD();
+}
+
+void Ontvanger::printMessage(char index){
+    switch (index) {
+      case '1': 
+        pc2.printf("Message: Initializing...\n\r");
+        break;
+      case '2': 
+        pc2.printf("Message: Taring...\n\r");
+        break;
+      case '3': 
+        pc2.printf("Message: Error: Tare first\n\r");
+        break;
+      case '4': 
+        pc2.printf("Message: Tare completed\n\r");
+        break;
+      case '5': 
+        pc2.printf("Message: Error: Tare value to low... Retry\n\r");
+        break;
+      case '6': 
+        pc2.printf("Message: Waiting on the right position...\n\r");
+        break;
+      case '7': 
+        pc2.printf("Message: Wait 5 Seconds on POSITION...\n\r");
+        break;
+      case '8': 
+        pc2.printf("Message: ERROR: Not on position\n\r");
+        break; 
+}
+    }
+
+
diff -r f8aad6d1db68 -r 691649d8a3da Transmitter.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Transmitter.h	Wed Sep 05 07:50:27 2018 +0000
@@ -0,0 +1,73 @@
+#ifndef Transmitter_h
+#define Transmitter_h
+#include "mbed.h"
+#include <RF24Network.h>
+#include <RF24.h>
+
+#define nrf_CE      D4
+#define nrf_CSN     D3
+#define spi_SCK     A1
+#define spi_MOSI    A6
+#define spi_MISO    A5
+
+#ifdef PRINT_ENABLE
+    #define IF_PRINT_ENABLE(x) ({x;})
+    #else
+    #define IF_PRINT_ENABLE(x)
+    #endif
+
+
+//Identification verzender/ontvanger
+const uint16_t this_node = 01;
+const uint16_t other_node = 00;
+
+
+//
+struct payload_t {
+    char command;
+    float mass;
+    bool messageAvailable;
+};
+
+enum State2 {init_state,
+             testConnection_state,
+             send_state,
+             receive_state,
+            };
+
+enum State {State_init,
+            State_tare,
+            State_position,
+            State_read,
+            State_send,
+            State_receive,
+            State_calibrate
+           };
+
+
+
+class Ontvanger
+{
+private:
+
+
+public:
+    Ontvanger();
+    void update();
+    bool available();
+    void printDetails();
+    bool write(payload_t message);
+    payload_t read();
+    bool writeArray(char *);
+    char * readArray();
+    bool isValid();
+    bool testRPD();
+    void printMessage(char index);
+};
+
+#endif
+
+
+
+
+
diff -r f8aad6d1db68 -r 691649d8a3da main.cpp
--- a/main.cpp	Fri Jul 13 06:28:33 2018 +0000
+++ b/main.cpp	Wed Sep 05 07:50:27 2018 +0000
@@ -1,102 +1,240 @@
-/**
-    Main.cpp
-    Purpose: main class
-
-    @author Pieter Berteloot
-
-    RF24 PINOUT:
-     ---- -----
-    |GND | VCC |
-    |----      |
-    |CE    CSN |
-    |SCK   MOSI|
-    |MISO  IRQ |
-     ---- -----
-
-     ---- -----
-    |GND | Vout|
-    |----      |
-    |p9    p8  |
-    |p7    p5  |
-    |p6    NC  |
-     ---- -----
-*/
-//uncommend #define PRINT_ENABLE to see informational prints
-#define PRINT_ENABLE
-
 #include "mbed.h"
-//#include "rtos.h"
-#include "Ontvanger.h"
+#include "Transmitter.h"
+#include "Maths.h"
+#include "LoadCell.h"
 
 Serial pc(USBTX, USBRX);
-Ontvanger receiver;
+Ontvanger tx;
+LoadCell test(A0);
+InterruptIn reedSensor(D9);
+Ticker sampler;
 Timer t;
 
-DigitalOut myled1(LED1);
-DigitalOut myled2(LED2);
-DigitalOut myled3(LED3);
-DigitalOut myled4(LED4);
+
+int reed = 0;
+
+int DEBUG_ENABLED = 0;
+int meet = 0;
+float val2[10000];
+int VAL_AM = 0;
+
+
+const int BEFORE_AMOUNT = 1;
+float BEFORE_VALUES[BEFORE_AMOUNT];
+int BEFORE_COUNTER = 0;
+
+const int REED_AMOUNT = 1;
+float REED_VALUES[REED_AMOUNT][BEFORE_AMOUNT];
+int REED_COUNTER = 0;
+int REED_ENABLE = 0;
+
+int MAGNET_COUNTER = 0;
 
-State2 current_state = init_state;
+void log(const char* format, ...)
+{
+    if (DEBUG_ENABLED) {
+        char buffer[256];
+        va_list args;
+        va_start(args, format);
+        vsprintf(buffer, format, args);
+        pc.printf("DEBUG - %s \r\n", buffer);
+        va_end(args);
+    }
+}
+
+void send(float mass)
+{
+    tx.update();
+    payload_t payload;
+    payload.command = 0x01;
+    payload.mass = mass;
+    log("Writing now");
+    bool ok = tx.write(payload);
+    log("Done writing");
+    if (ok)
+        log("Ok");
+    else
+        log("Failed");
+}
 
 
-//Thread thread;
-//Mutex stdio_mutex;
-//Mutex nrf_mutex;
+void sendCommand (char c)
+{
+    switch (c) {
+        case 't':
+            float tare = test.tare();
+            log("Tare %f", tare);
+            send(tare);
+            break;
+        case 'T':
+            float tare2 = test.tareDown();
+            log("Tare %f", tare2);
+            send(tare2);
+            break;
+        case 'c':
+            float cali = test.callibrate();
+            log("Callibrate %f", cali);
+            send(cali);
+            break;
+        case 'y':
+            float volt = test.analogRead();
+            log("Voltage %f", volt);
+            send(volt);
+            break;
+        case 'm':
+            float mass = test.mass();
+            log("MASS %f", mass);
+            send(mass);
+            break;
+            case 'M':
+            float mass2 = test.simpleMass();
+            log("MASS %f", mass2);
+            send(mass2);
+            break;
+        case 'P':
+            for (int i = 0; i<10000; i++) {
+                send(val2[i]);
+                wait_ms(25);
+            }
+            break;
+        case 'p':
+            float test[REED_AMOUNT];
+            float test2[REED_AMOUNT];
+
+            for (int i = 0; i<REED_AMOUNT; i++) {
+                for (int j = 0; j<BEFORE_AMOUNT; j++) {
+                    send(REED_VALUES[i][j]);
+                    printf("%f,",REED_VALUES[i][j]);
+                }
+                printf("\r\n");
+                send(999);
+
+                test[i] = Maths::mean(REED_VALUES[i],0,BEFORE_AMOUNT-1);
+                test2[i] = Maths::meanNoOutliers(REED_VALUES[i],BEFORE_AMOUNT-1);
+            }
+            send(999);
+            send(Maths::mean(test,0,REED_AMOUNT));
+            send(999);
+            send(Maths::meanNoOutliers(test,REED_AMOUNT));
+            send(999);
+            send(Maths::meanNoOutliers(test2,REED_AMOUNT));
+            send(999);
+
+            break;
+        case 'r':
+            REED_ENABLE = !REED_ENABLE;
+            memset( REED_VALUES, 0, sizeof( REED_VALUES ) );
+            memset( BEFORE_VALUES, 0, sizeof( BEFORE_VALUES) );
+            REED_COUNTER = 0;
+            break;
+    };
+}
+void readNF24()
+{
+    tx.update();
+    if (tx.available()) {
+        payload_t payload;
+        payload = tx.read();
+        pc.printf("%c\r\n", payload.command);
+        sendCommand(payload.command);
+    }
+}
+
+void reedS()
+{
+    if (REED_ENABLE)
+        reed = 1;
+}
+
+void getSample()
+{
+    if (REED_ENABLE) {
+        val2[VAL_AM] = test.simpleAnalogRead();
+        VAL_AM = (VAL_AM + 1) %10000;
+    }
+}
+
+void init()
+{
+    DEBUG_ENABLED = 0;
+    log("INITIALISE");
+    log("DEBUGGING IS: ON");
+    log("Load cell using: Analog Pin A2");
+    log("Transmitter using: D4, D3, A1, A6, A5");
+    tx.printDetails();
+
+    log("INIT REED SENSOR");
+
+    reedSensor.fall(&reedS);
+    reedSensor.mode(PullUp);
+    sampler.attach(&getSample, 0.00004);
+
+}
+
 
 void receive()
 {
     while (true) {
         pc.printf("");//print niet weg doen, één of andere reden werkt het niet zonder
-        receiver.update();
-        if (receiver.available()) {
+        tx.update();
+        if (tx.available()) {
             payload_t payload;
-            payload = receiver.read();
+            payload = tx.read();
             if(!payload.messageAvailable) {
-                pc.printf("%f\r\n", payload.gram);
+                pc.printf("%f\r\n", payload.mass);
             }
         }
     }
 }
 
-void send()
-{
-    //bool ok = receive.write(packet);
-    bool ok = true;
-    if (ok)
-        pc.printf("Vok.\n\r");
-    else
-        pc.printf("Vfailed.\n\r");
-}
+
+
 
 
-/**
-char c = 'a';
-void led2_thread()
+void readPc()
+{
+    if(pc.readable()) {
+        sendCommand(pc.getc());
+    }
+}
+
+void readReed()
 {
-    while (true) {
-        stdio_mutex.lock();
-        //c = pc.getc();
-        pc.printf("Character %c\r\n", c);
-        stdio_mutex.unlock();
+    if(reed) {
+        /*for (int i = 0; i <BEFORE_AMOUNT ; i++)
+            REED_VALUES[REED_COUNTER][i] = BEFORE_VALUES[(i+BEFORE_COUNTER) % BEFORE_AMOUNT];
+        REED_COUNTER=(REED_COUNTER+1) % REED_AMOUNT;
+
+        MAGNET_COUNTER++;
+        */
+        REED_ENABLE = 0;
+        send(VAL_AM);
+        reed = 0;
+
     }
-}**/
+}
 
 int main()
 {
-    float massa = 69;
-    //thread.start(receive);
-    while (true) {
-        pc.printf("");
-        wait_ms(100);
-        receiver.update();
-        payload_t payload;
-        massa++;
-        payload.gram = massa;
-        bool ok = receiver.write(payload);
-        if (ok)
-            pc.printf("ok.\r\n");
-        else
-            pc.printf("failed.\r\n");
+    init();
+    while (1) {
+        //If reed is enabled
+        if(!REED_ENABLE) {
+            readPc();
+            readNF24();
+        } else {
+            readReed();
+            /*val2[VAL_AM] = test.simpleAnalogRead();
+            VAL_AM = (VAL_AM + 1) %10000;
+            */
+            /*BEFORE_VALUES[BEFORE_COUNTER] = test.mass();
+            BEFORE_COUNTER ++;
+            BEFORE_COUNTER = BEFORE_COUNTER % BEFORE_AMOUNT;
+            if(MAGNET_COUNTER >= REED_AMOUNT) {
+                MAGNET_COUNTER = 0;
+                REED_ENABLE = !REED_ENABLE;
+            }*/
+        }
     }
+
 }