d

Dependencies:   xtoff RF24Network mbed

Files at this revision

API Documentation at this revision

Comitter:
gimohd
Date:
Thu Jul 12 12:02:35 2018 +0000
Commit message:
hoi

Changed in this revision

RF24.lib Show annotated file Show diff for this revision Revisions of this file
RF24Network.lib Show annotated file 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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RF24.lib	Thu Jul 12 12:02:35 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/pietor/code/xtoff/#5476ceebbb1f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RF24Network.lib	Thu Jul 12 12:02:35 2018 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/akashvibhute/code/RF24Network/#b1110d26a900
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Transmitter.cpp	Thu Jul 12 12:02:35 2018 +0000
@@ -0,0 +1,67 @@
+#include "Transmitter.h"
+
+RF24 radio(spi_MOSI, spi_MISO, spi_SCK, nrf_CE, nrf_CSN );
+RF24Network network(radio);
+unsigned long packets_sent;
+RF24NetworkHeader header_rx;
+char previous_char;
+
+/**
+    Constructor: Initialize RF24 and RF24Network
+*/
+Verzender::Verzender()
+{
+    wait_ms(2000);
+    radio.begin();
+    network.begin(90, this_node);
+    wait_ms(2000);
+    radio.setPALevel(RF24_PA_MIN);
+    radio.setDataRate(RF24_250KBPS);
+    
+}
+
+void Verzender::update()
+{
+    network.update();
+}
+
+bool Verzender::write(payload_t packet)
+{
+    RF24NetworkHeader header_tx(other_node);
+    payload_t payload_tx;
+    payload_tx = packet;
+    
+    payload_tx.messageIndex = 0;
+    payload_tx.messageAvailable = false;
+    
+    return network.write(header_tx,&payload_tx,sizeof(payload_tx));
+}
+
+void Verzender::sendMessage(char index){
+        
+        if(previous_char != index){
+        RF24NetworkHeader header_tx(other_node);
+        payload_t payload_message;
+        payload_message.messageIndex = index;
+        payload_message.messageAvailable = true;
+        bool test = network.write(header_tx,&payload_message,sizeof(payload_message));
+        }
+        previous_char = index;
+    }
+
+state_Packet Verzender::read()
+{
+    state_Packet packet;
+    network.read(header_rx,&packet,sizeof(packet));
+    
+    return packet;
+}
+
+bool Verzender::available()
+{
+    return network.available();
+}
+
+void Verzender::printDetails(){
+    radio.printDetails();
+    }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Transmitter.h	Thu Jul 12 12:02:35 2018 +0000
@@ -0,0 +1,110 @@
+#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
+
+#define INIT            '1'
+#define TARE            '2'
+#define TARE_FIRST      '3'
+#define TARE_COMPLETE   '4'
+#define TARE_ERROR      '5'
+#define POSITION        '6'
+#define POSITION_WAIT   '7'
+#define POSITION_ERROR  '8'
+#define READ            '9'
+#define RECEIVE         'a'
+#define STARTUP         'b'
+#define STARTUP_SUCCES  'c'
+#define BATTERY         'd'
+
+
+#ifdef PRINT_ENABLE
+    #define IF_PRINT_ENABLE(x) ({x;})
+    #else
+    #define IF_PRINT_ENABLE(x)
+    #endif
+
+//Identificatie verzender/ontvanger
+const uint16_t this_node = 01;
+const uint16_t other_node = 00;
+
+struct payload_t {
+    bool reedsensor;
+    float gram;
+    bool messageAvailable;
+    char messageIndex;
+};
+
+enum State {State_init,
+            State_tare,
+            State_position,
+            State_read,
+            State_receive,
+            State_calibrate
+           };
+
+struct state_Packet {
+    char setstate;
+};
+
+
+class Verzender
+{
+private:
+
+
+public:
+    Verzender();
+    
+    /**
+    Update to receive a message available for this node
+    */
+    void update();
+    
+    
+    /**
+        Send a message to the receiver
+    
+        @param message of type payload_t
+    */
+    bool write(payload_t message);
+    
+    
+    /**
+    Test whether there is a message available for this node
+    */
+    bool available();
+    
+    
+    /**
+    Read a message to the receiver
+
+    @return message of type state_packet
+    */
+    state_Packet read();
+    
+    
+    /*messageIndex:
+        0: Nothing wrong
+        1: Tare first
+        2: Error on Position
+        3: Bad Tare value (<2.5V)
+*/
+    void sendMessage(char index);
+    
+    void Verzender::printDetails();
+};
+
+#endif
+
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Jul 12 12:02:35 2018 +0000
@@ -0,0 +1,111 @@
+#include "mbed.h"
+
+Serial pc(USBTX, USBRX);
+
+// Read the voltage
+AnalogIn ain(A2);
+
+/*I2C
+I2C i2c(D4, D5);
+//Slave address
+//char SLAVE_ADDRESS = 0b11110000; // 7 bits => 0b1111000 = 0x78
+
+
+char * i2cRead(){
+     char data[2];
+     i2c.read(SLAVE_ADDRESS, data, 2);
+     pc.printf("%s\r\n",data);
+     return data;
+}
+*/
+
+
+
+float TARE_VALUE = 0 ;
+float CALIBRATION_VALUE = 0 ;
+int CALIBRATION_MASS = 1131;
+int SAMPLE_AMOUNT = 2000;
+
+
+float analogRead()
+{
+    float beginval = 0;
+     for(int i = 0; i < 100; i++) {
+        beginval += ain.read();
+
+    } 
+    float gemiddeld = beginval/100;
+    pc.printf("Gemiddelde %f\r\n", gemiddeld);
+    
+    
+    float curval = 0;
+    int del = 0 ;
+    for(int i = 0; i < SAMPLE_AMOUNT; i++) {
+        if ( ain.read()< gemiddeld +0.001012*2 && ain.read()> gemiddeld -0.001012*2){            
+            curval += ain.read();
+        }else {
+            del ++;
+            
+        }
+
+    }
+        pc.printf("Deleted %d\r\n", del);
+
+    return (curval + beginval) /(SAMPLE_AMOUNT-del + 100);
+}
+
+float calculateMass(float value)
+{
+    return ((TARE_VALUE - value)*(CALIBRATION_MASS))/(TARE_VALUE - CALIBRATION_VALUE);
+}
+
+void log(char c[])
+{
+    pc.printf("%s\r\n", c);
+}
+
+void tare()
+{
+    TARE_VALUE = analogRead();
+    pc.printf("Tare %f\r\n", TARE_VALUE);
+}
+
+void init()
+{
+    pc.printf("### INITIALISING ###\r\n");
+    tare();
+    pc.printf("### INIT COMPLETE ###\r\n");
+}
+
+
+int main()
+{
+    init();
+    while (1) {
+        if(pc.readable()) {
+            switch (pc.getc()) {
+                case 't':
+                    tare();
+                    break;
+                case 'c':
+                    CALIBRATION_VALUE = analogRead();
+                    pc.printf("Callibrate %f\r\n", CALIBRATION_VALUE);
+
+                    break;
+                case 'y':
+                    float mass = calculateMass(analogRead());
+                    pc.printf("MASS %f\r\n", mass);
+
+                    break;
+
+            };
+
+        }
+        wait_ms(500);
+
+
+    }
+    return 0;
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Jul 12 12:02:35 2018 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/a7c7b631e539
\ No newline at end of file