![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
dd
Dependencies: xtoff2 RF24Network mbed
Fork of xtoff3 by
Diff: main.cpp
- Revision:
- 16:691649d8a3da
- Parent:
- 15:f8aad6d1db68
--- 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; + }*/ + } } + }