RF24Network Send example program.

Dependencies:   xtoff RF24Network mbed

Fork of RF24Network_Send by Akash Vibhute

Committer:
pietor
Date:
Wed Mar 21 16:22:34 2018 +0000
Revision:
11:2aa84e063c49
Parent:
10:875812a04307
Child:
12:38c5efed7950
Werkend 21/03: Added Messages

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pietor 10:875812a04307 1 //uncommend // in #define PRINT_ENABLE to see prints
pietor 10:875812a04307 2 #define PRINT_ENABLE
pietor 10:875812a04307 3
pietor 5:e6067799a414 4 #include "Verzender.h"
pietor 8:62b4607c44ca 5 #include "PowerControl/PowerControl.h"
pietor 8:62b4607c44ca 6 #include "PowerControl/EthernetPowerControl.h"
pietor 6:03ba3e18ced2 7
pietor 11:2aa84e063c49 8 #define NUM_SAMPLES 2000 // size of sample series
pietor 11:2aa84e063c49 9 #define USR_POWERDOWN (0x104)
pietor 11:2aa84e063c49 10 #define MIN_TARE_VALUE 0.7575 //2.5 / 3.3 (2.5V is zero load and adc is between 0 and 1)µ
pietor 6:03ba3e18ced2 11
pietor 5:e6067799a414 12 Verzender sent;
pietor 8:62b4607c44ca 13 Serial pc(USBTX, USBRX);
pietor 8:62b4607c44ca 14 Timer t1;
pietor 8:62b4607c44ca 15 Timer t2;
pietor 8:62b4607c44ca 16 State current_state = State_init;
pietor 8:62b4607c44ca 17 InterruptIn reedSensor(p25);
pietor 6:03ba3e18ced2 18 AnalogIn ain(p17);
pietor 6:03ba3e18ced2 19
pietor 6:03ba3e18ced2 20 float tare = 0;
pietor 6:03ba3e18ced2 21 float massa = 0;
pietor 10:875812a04307 22 float calibration = 0;
pietor 8:62b4607c44ca 23 bool reed = false;
pietor 10:875812a04307 24 bool tareDone = false;
pietor 11:2aa84e063c49 25 char nextState;
pietor 11:2aa84e063c49 26 float calibrationMass = 1003;
pietor 11:2aa84e063c49 27 float AVERAGE_TARE = 0;
pietor 11:2aa84e063c49 28 float CALIBRATION_OFFSET = 0.0199754;
pietor 6:03ba3e18ced2 29
pietor 8:62b4607c44ca 30
pietor 8:62b4607c44ca 31 /**
pietor 8:62b4607c44ca 32 Sets the reed status on rising trigger
pietor 8:62b4607c44ca 33 */
pietor 8:62b4607c44ca 34 void setReed()
pietor 8:62b4607c44ca 35 {
pietor 8:62b4607c44ca 36 reed = true;
pietor 8:62b4607c44ca 37 }
pietor 6:03ba3e18ced2 38
pietor 8:62b4607c44ca 39 /**
pietor 8:62b4607c44ca 40 resets the reed status on falling trigger
pietor 8:62b4607c44ca 41 */
pietor 8:62b4607c44ca 42 void dissableReed()
pietor 8:62b4607c44ca 43 {
pietor 8:62b4607c44ca 44 reed = false;
pietor 8:62b4607c44ca 45 }
pietor 8:62b4607c44ca 46
pietor 8:62b4607c44ca 47
pietor 8:62b4607c44ca 48 /**
pietor 8:62b4607c44ca 49 RSets the current status of the state machine
pietor 8:62b4607c44ca 50
pietor 8:62b4607c44ca 51 @param the state to be set
pietor 8:62b4607c44ca 52 */
pietor 6:03ba3e18ced2 53
pietor 6:03ba3e18ced2 54 void setCurrentState( State setState )
pietor 6:03ba3e18ced2 55 {
pietor 6:03ba3e18ced2 56 current_state = setState;
pietor 6:03ba3e18ced2 57 }
pietor 6:03ba3e18ced2 58
pietor 6:03ba3e18ced2 59
pietor 8:62b4607c44ca 60 /**
pietor 8:62b4607c44ca 61 Get the average of a given number of samples from the analog input
pietor 6:03ba3e18ced2 62
pietor 8:62b4607c44ca 63 @param amount of samples
pietor 8:62b4607c44ca 64 @return average of the analog input
pietor 8:62b4607c44ca 65 */
pietor 11:2aa84e063c49 66 float getAverageSamples(int samples, int subSamples)
pietor 6:03ba3e18ced2 67 {
pietor 6:03ba3e18ced2 68 float AVERAGE = 0;
pietor 11:2aa84e063c49 69 int num_sub_samples = 0;
pietor 6:03ba3e18ced2 70 int num_samples = 0;
pietor 11:2aa84e063c49 71
pietor 11:2aa84e063c49 72 while (num_samples <samples) {
pietor 6:03ba3e18ced2 73 float r = ain.read();
pietor 6:03ba3e18ced2 74 AVERAGE += r;
pietor 6:03ba3e18ced2 75 num_samples++;
pietor 11:2aa84e063c49 76 if(num_sub_samples >= subSamples) {
pietor 11:2aa84e063c49 77 wait_ms(10);
pietor 11:2aa84e063c49 78 num_sub_samples = 0;
pietor 11:2aa84e063c49 79 }
pietor 11:2aa84e063c49 80
pietor 11:2aa84e063c49 81 num_sub_samples++;
pietor 6:03ba3e18ced2 82 }
pietor 6:03ba3e18ced2 83 AVERAGE /= num_samples;
pietor 6:03ba3e18ced2 84 num_samples = 0;
pietor 6:03ba3e18ced2 85
pietor 6:03ba3e18ced2 86 return AVERAGE;
pietor 6:03ba3e18ced2 87 }
pietor 6:03ba3e18ced2 88
pietor 10:875812a04307 89
pietor 11:2aa84e063c49 90 float getAverageSamples2(int samples, int subSamples)
pietor 6:03ba3e18ced2 91 {
pietor 6:03ba3e18ced2 92 float AVERAGE = 0;
pietor 11:2aa84e063c49 93 float FinalAVERAGE = 0;
pietor 11:2aa84e063c49 94 float SUB_AVERAGE = 0;
pietor 11:2aa84e063c49 95 float SUB_AVERAGE_ARRAY[samples];
pietor 6:03ba3e18ced2 96 int num_samples = 0;
pietor 11:2aa84e063c49 97 int num_subSamples = 0;
pietor 11:2aa84e063c49 98 int count = 0;
pietor 11:2aa84e063c49 99
pietor 11:2aa84e063c49 100 //get average of 10 samples and store them in array
pietor 11:2aa84e063c49 101 while( num_samples < samples) {
pietor 11:2aa84e063c49 102 while (num_subSamples < subSamples) {
pietor 11:2aa84e063c49 103 float r = ain.read();
pietor 11:2aa84e063c49 104 SUB_AVERAGE += r;
pietor 11:2aa84e063c49 105 AVERAGE += r;
pietor 11:2aa84e063c49 106 num_subSamples++;
pietor 11:2aa84e063c49 107 count++;
pietor 11:2aa84e063c49 108 }
pietor 11:2aa84e063c49 109 SUB_AVERAGE /= num_subSamples;
pietor 11:2aa84e063c49 110 SUB_AVERAGE_ARRAY[num_samples] = SUB_AVERAGE;
pietor 11:2aa84e063c49 111 SUB_AVERAGE = 0;
pietor 11:2aa84e063c49 112 num_subSamples = 0;
pietor 6:03ba3e18ced2 113 num_samples++;
pietor 6:03ba3e18ced2 114 }
pietor 6:03ba3e18ced2 115 num_samples = 0;
pietor 6:03ba3e18ced2 116
pietor 11:2aa84e063c49 117 //calculate total average of 2500 samples
pietor 11:2aa84e063c49 118 AVERAGE /= count;
pietor 11:2aa84e063c49 119 AVERAGE = ((AVERAGE - tare)*(calibrationMass))/(CALIBRATION_OFFSET);
pietor 11:2aa84e063c49 120 count = 0;
pietor 11:2aa84e063c49 121
pietor 11:2aa84e063c49 122 //loop over array and check if the samples are within range of total average
pietor 11:2aa84e063c49 123 for (int i=0; i< samples; i++) {
pietor 11:2aa84e063c49 124 float massa = ((SUB_AVERAGE_ARRAY[i] - tare)*(calibrationMass))/(CALIBRATION_OFFSET);
pietor 11:2aa84e063c49 125
pietor 11:2aa84e063c49 126 if (massa < AVERAGE - 30 or massa > AVERAGE + 30) {
pietor 11:2aa84e063c49 127 massa = 0;
pietor 11:2aa84e063c49 128 count--;
pietor 11:2aa84e063c49 129 }
pietor 11:2aa84e063c49 130 FinalAVERAGE += massa;
pietor 11:2aa84e063c49 131 count++;
pietor 11:2aa84e063c49 132 }
pietor 11:2aa84e063c49 133 FinalAVERAGE /= count;
pietor 11:2aa84e063c49 134 IF_PRINT_ENABLE(pc.printf("FinalAVERAGE: %f , AVERAGE: %f, COUNT: %d\n\r", FinalAVERAGE, AVERAGE, count););
pietor 11:2aa84e063c49 135 return FinalAVERAGE;
pietor 6:03ba3e18ced2 136 }
pietor 6:03ba3e18ced2 137
pietor 8:62b4607c44ca 138 /**
pietor 8:62b4607c44ca 139 Main function:
pietor 8:62b4607c44ca 140 State machine:
pietor 8:62b4607c44ca 141 Init: initialization
pietor 8:62b4607c44ca 142 Position: Checks if the paddle is on tare position
pietor 8:62b4607c44ca 143 Tare: Set Zeroload point on average of 50000 samples
pietor 10:875812a04307 144 Read: Read the mass when the paddle passes the read position and
pietor 8:62b4607c44ca 145 send the data to the receiver.
pietor 8:62b4607c44ca 146 Receive: Check if there were messages
pietor 11:2aa84e063c49 147 Calibration: Get calibration factor
pietor 8:62b4607c44ca 148 */
akashvibhute 2:926b93a68399 149 int main()
akashvibhute 0:3982c0e9eda1 150 {
pietor 5:e6067799a414 151 while(1) {
pietor 8:62b4607c44ca 152 reedSensor.fall(&setReed);
pietor 8:62b4607c44ca 153 reedSensor.rise(&dissableReed);
pietor 5:e6067799a414 154 sent.update();
pietor 10:875812a04307 155
pietor 6:03ba3e18ced2 156 switch (current_state) {
pietor 6:03ba3e18ced2 157 case State_init:
pietor 6:03ba3e18ced2 158 pc.baud(9600);
pietor 8:62b4607c44ca 159 PHY_PowerDown(); //Power down Ethernet interface
pietor 6:03ba3e18ced2 160 wait_ms(1000);
pietor 7:cbdbaf825b4a 161 pc.printf("--Verzender--\n\r");
pietor 8:62b4607c44ca 162 reedSensor.mode(PullUp);
pietor 6:03ba3e18ced2 163 setCurrentState(State_read);
pietor 6:03ba3e18ced2 164 payload_t payload;
pietor 11:2aa84e063c49 165 sent.sendMessage(INIT);
pietor 6:03ba3e18ced2 166 break;
pietor 6:03ba3e18ced2 167
pietor 6:03ba3e18ced2 168 case State_position:
pietor 11:2aa84e063c49 169 sent.sendMessage(POSITION);
pietor 10:875812a04307 170 IF_PRINT_ENABLE(pc.printf("State: position\n\r"););
pietor 10:875812a04307 171 if (reed) {
pietor 11:2aa84e063c49 172 sent.sendMessage(POSITION_WAIT);
pietor 10:875812a04307 173 IF_PRINT_ENABLE(pc.printf("Waiting for 5 seconds\n\r"););
pietor 11:2aa84e063c49 174 wait(1);
pietor 11:2aa84e063c49 175 if (reed and nextState == 's') {
pietor 11:2aa84e063c49 176 IF_PRINT_ENABLE(pc.printf("Selecting tare state\n\r"););
pietor 10:875812a04307 177 setCurrentState(State_tare);
pietor 11:2aa84e063c49 178 break;
pietor 11:2aa84e063c49 179 } else if (reed and nextState == 'c') {
pietor 11:2aa84e063c49 180 IF_PRINT_ENABLE(pc.printf("Selecting calibrate state\n\r"););
pietor 10:875812a04307 181 setCurrentState(State_calibrate);
pietor 11:2aa84e063c49 182 break;
pietor 10:875812a04307 183 } else {
pietor 11:2aa84e063c49 184 sent.sendMessage(POSITION_ERROR);
pietor 10:875812a04307 185 IF_PRINT_ENABLE(pc.printf("Error on position\n\r"););
pietor 11:2aa84e063c49 186 setCurrentState(State_read);
pietor 10:875812a04307 187 }
pietor 10:875812a04307 188 }
pietor 6:03ba3e18ced2 189 break;
pietor 6:03ba3e18ced2 190
pietor 6:03ba3e18ced2 191
pietor 6:03ba3e18ced2 192 case State_tare:
pietor 11:2aa84e063c49 193 sent.sendMessage(TARE);
pietor 10:875812a04307 194 IF_PRINT_ENABLE(pc.printf("State: tare\n\r"););
pietor 11:2aa84e063c49 195 tare = getAverageSamples(250,100);
pietor 11:2aa84e063c49 196 if(MIN_TARE_VALUE <= tare) {
pietor 11:2aa84e063c49 197 sent.sendMessage(TARE_COMPLETE);
pietor 11:2aa84e063c49 198 IF_PRINT_ENABLE(pc.printf("tare = %f\r\n",tare*3.3););
pietor 11:2aa84e063c49 199 tareDone = true;
pietor 11:2aa84e063c49 200 } else {
pietor 11:2aa84e063c49 201 sent.sendMessage(TARE_ERROR);
pietor 11:2aa84e063c49 202 IF_PRINT_ENABLE(pc.printf("ERROR: TARE VALUE TO LOW\n\r"););
pietor 11:2aa84e063c49 203 tareDone = false;
pietor 11:2aa84e063c49 204 }
pietor 6:03ba3e18ced2 205 setCurrentState(State_read);
pietor 6:03ba3e18ced2 206 break;
akashvibhute 1:5be48a9550c3 207
pietor 6:03ba3e18ced2 208 case State_read:
pietor 8:62b4607c44ca 209 if (reed) {
pietor 11:2aa84e063c49 210 if (tareDone == true) {
pietor 11:2aa84e063c49 211 massa = getAverageSamples2(25, 100);
pietor 11:2aa84e063c49 212 payload.reedsensor = 1;
pietor 11:2aa84e063c49 213 payload.gram = massa;
pietor 11:2aa84e063c49 214 IF_PRINT_ENABLE(pc.printf("Sent packet1 -- Reed: %d --- %f g \r\n",payload.reedsensor, payload.gram););
pietor 11:2aa84e063c49 215 bool ok = sent.write(payload);
pietor 11:2aa84e063c49 216 if (ok) {
pietor 11:2aa84e063c49 217 IF_PRINT_ENABLE(pc.printf("ok.\n\r"););
pietor 11:2aa84e063c49 218 } else {
pietor 11:2aa84e063c49 219 IF_PRINT_ENABLE(pc.printf("failed.\n\r"););
pietor 11:2aa84e063c49 220 }
pietor 7:cbdbaf825b4a 221 } else {
pietor 11:2aa84e063c49 222 sent.sendMessage(TARE_FIRST);
pietor 11:2aa84e063c49 223 IF_PRINT_ENABLE(pc.printf("Tare First.\n\r"););
pietor 7:cbdbaf825b4a 224 }
pietor 7:cbdbaf825b4a 225 setCurrentState(State_receive);
pietor 6:03ba3e18ced2 226 }
pietor 6:03ba3e18ced2 227 break;
pietor 7:cbdbaf825b4a 228
pietor 6:03ba3e18ced2 229 case State_receive:
pietor 6:03ba3e18ced2 230 sent.update();
pietor 6:03ba3e18ced2 231 if (sent.available()) {
pietor 11:2aa84e063c49 232 IF_PRINT_ENABLE(pc.printf("Received something\n\r"););
pietor 6:03ba3e18ced2 233 state_Packet state;
pietor 6:03ba3e18ced2 234 state = sent.read();
pietor 11:2aa84e063c49 235
pietor 11:2aa84e063c49 236 if( state.setstate == 's') {
pietor 10:875812a04307 237 IF_PRINT_ENABLE(pc.printf("Next state: Tare\n\r"););
pietor 11:2aa84e063c49 238 nextState = 's';
pietor 6:03ba3e18ced2 239 setCurrentState(State_position);
pietor 6:03ba3e18ced2 240 break;
pietor 11:2aa84e063c49 241 }
pietor 11:2aa84e063c49 242
pietor 11:2aa84e063c49 243 if(state.setstate == 'c') {
pietor 10:875812a04307 244 IF_PRINT_ENABLE(pc.printf("Next state: Calibrate\n\r"););
pietor 11:2aa84e063c49 245 nextState = 'c';
pietor 10:875812a04307 246 setCurrentState(State_position);
pietor 11:2aa84e063c49 247 break;
pietor 6:03ba3e18ced2 248 }
pietor 6:03ba3e18ced2 249 }
pietor 11:2aa84e063c49 250
pietor 6:03ba3e18ced2 251 setCurrentState(State_read);
pietor 6:03ba3e18ced2 252 break;
pietor 10:875812a04307 253
pietor 10:875812a04307 254 case State_calibrate:
pietor 11:2aa84e063c49 255 setCurrentState(State_read);
pietor 10:875812a04307 256 if(tareDone == true) {
pietor 10:875812a04307 257 IF_PRINT_ENABLE(pc.printf("State: calibreren\n\r"););
pietor 10:875812a04307 258 IF_PRINT_ENABLE(pc.printf("Put 1kg on paddle...\n\r"););
pietor 10:875812a04307 259 IF_PRINT_ENABLE(pc.printf("Waiting: 10 seconds\n\r"););
pietor 11:2aa84e063c49 260 wait(1);
pietor 10:875812a04307 261 IF_PRINT_ENABLE(pc.printf("Starting calibration\n\r"););
pietor 11:2aa84e063c49 262 calibration = getAverageSamples(1000,10);
pietor 11:2aa84e063c49 263 IF_PRINT_ENABLE(pc.printf("Calibration= %f\n\r", calibration*3.3););
pietor 11:2aa84e063c49 264
pietor 10:875812a04307 265 } else {
pietor 10:875812a04307 266 IF_PRINT_ENABLE(pc.printf("ERROR: TARE FIRST\n\r"););
pietor 10:875812a04307 267 }
pietor 10:875812a04307 268 break;
akashvibhute 2:926b93a68399 269 }
akashvibhute 0:3982c0e9eda1 270 }
akashvibhute 0:3982c0e9eda1 271 }