RF24Network Send example program.

Dependencies:   xtoff RF24Network mbed

Fork of RF24Network_Send by Akash Vibhute

main.cpp

Committer:
pietor
Date:
2018-03-21
Revision:
11:2aa84e063c49
Parent:
10:875812a04307
Child:
12:38c5efed7950

File content as of revision 11:2aa84e063c49:

//uncommend // in #define PRINT_ENABLE to see prints
#define PRINT_ENABLE

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

#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);
Timer t1;
Timer t2;
State current_state = State_init;
InterruptIn reedSensor(p25);
AnalogIn   ain(p17);

float tare = 0;
float massa = 0;
float calibration = 0;
bool reed = false;
bool tareDone = false;
char nextState;
float calibrationMass = 1003;
float AVERAGE_TARE = 0;
float CALIBRATION_OFFSET = 0.0199754;


/**
    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, int subSamples)
{
    float AVERAGE = 0;
    int num_sub_samples = 0;
    int num_samples = 0;

    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;
}


float getAverageSamples2(int samples, int subSamples)
{
    float AVERAGE = 0;
    float FinalAVERAGE = 0;
    float SUB_AVERAGE = 0;
    float SUB_AVERAGE_ARRAY[samples];
    int num_samples = 0;
    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++;
    }
    num_samples = 0;

    //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;
}

/**
    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
            Calibration: Get calibration factor
*/
int main()
{
    while(1) {
        reedSensor.fall(&setReed);
        reedSensor.rise(&dissableReed);
        sent.update();

        switch (current_state) {
            case State_init:
                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;
                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(1);
                    if (reed and nextState == 's') {
                        IF_PRINT_ENABLE(pc.printf("Selecting tare state\n\r"););
                        setCurrentState(State_tare);
                        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(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) {
                    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 {
                        sent.sendMessage(TARE_FIRST);
                        IF_PRINT_ENABLE(pc.printf("Tare First.\n\r"););
                    }
                    setCurrentState(State_receive);
                }
                break;

            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 == 's') {
                        IF_PRINT_ENABLE(pc.printf("Next state: Tare\n\r"););
                        nextState = 's';
                        setCurrentState(State_position);
                        break;
                    }

                    if(state.setstate == 'c') {
                        IF_PRINT_ENABLE(pc.printf("Next state: Calibrate\n\r"););
                        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(1);
                    IF_PRINT_ENABLE(pc.printf("Starting calibration\n\r"););
                    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"););
                }
                break;
        }
    }
}