pieter Berteloot / Mbed 2 deprecated Thesis_Verzender

Dependencies:   xtoff RF24Network mbed

Fork of RF24Network_Send by Akash Vibhute

main.cpp

Committer:
pietor
Date:
2018-03-08
Revision:
8:62b4607c44ca
Parent:
7:cbdbaf825b4a
Child:
10:875812a04307

File content as of revision 8:62b4607c44ca:

#include "Verzender.h"
#include "PowerControl/PowerControl.h"
#include "PowerControl/EthernetPowerControl.h"

#define NUM_SAMPLES 2000 // size of sample series
#define USR_POWERDOWN    (0x104)

Verzender sent;
Serial pc(USBTX, USBRX);
Timer t1;
Timer t2;
State current_state = State_init;
InterruptIn reedSensor(p25);
AnalogIn   ain(p17);

float tare = 0;
float massa = 0;
bool reed = false;


/**
    Sets the reed status on rising trigger
*/
void setReed()
{
    reed = true;
}

/**
    resets the reed status on falling trigger
*/
void dissableReed()
{
    reed = false;
}


/**
    RSets the current status of the state machine

    @param the state to be set
*/

void setCurrentState( State setState )
{
    current_state = setState;
}


/**
    Get the average of a given number of samples from the analog input

    @param amount of samples
    @return average of the analog input
*/
float getAverageSamples(int samples)
{
    float AVERAGE = 0;
    int num_samples = 0;
    while (num_samples < samples) {
        float r = ain.read();
        AVERAGE += r;
        num_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)
{
    t2.start();
    t2.reset();
    float AVERAGE = 0;
    int num_samples = 0;
    while (num_samples <= NUM_SAMPLES & t2.read() <= time) {
        float r = ain.read();
        AVERAGE += r;
        num_samples++;
    }
    AVERAGE /= num_samples;
    num_samples = 0;

    return AVERAGE;
}

/**
    Main function:
        State machine:
            Init: initialization
            Position: Checks if the paddle is on tare position
            Tare: Set Zeroload point on average of 50000 samples
            Read:   Read the mass when the paddle passes the read position and 
                    send the data to the receiver.
            Receive: Check if there were messages
*/
int main()
{
    while(1) {
        reedSensor.fall(&setReed);
        reedSensor.rise(&dissableReed);
        sent.update();
        
        switch (current_state) {
            case State_init:
                //sent.test();
                pc.baud(9600);
                PHY_PowerDown(); //Power down Ethernet interface
                wait_ms(1000);
                pc.printf("--Verzender--\n\r");
                reedSensor.mode(PullUp);
                setCurrentState(State_read);
                payload_t payload;
                break;

            case State_position:
                pc.printf("State: position\n\r");
                if (reed)
                    setCurrentState(State_tare);
                break;


            case State_tare:
                pc.printf("State: tare\n\r");
                tare = getAverageSamples(50000);
                pc.printf("tare = %f\r\n",tare*3.3);

                setCurrentState(State_read);
                break;

            case State_read:
                if (reed) {
                    massa = getAverageTime(1) - tare;
                    payload.reedsensor = 1;
                    payload.milligram = massa * 3.3;
                    pc.printf("Sent packet1 -- Reed: %d --- %f mg \r\n",payload.reedsensor, payload.milligram);
                    bool ok = sent.write(payload);
                    if (ok) {
                        pc.printf("ok.\n\r");
                    } else {
                        pc.printf("failed.\n\r");
                    }
                    setCurrentState(State_receive);
                }
                break;

            case State_receive:
                sent.update();
                if (sent.available()) {
                    state_Packet state;
                    state = sent.read();
                    if(state.setstate == State_position) {
                        setCurrentState(State_position);
                        break;
                    }
                }
                setCurrentState(State_read);
                break;
        }
    }
}