RF24Network Send example program.

Dependencies:   xtoff RF24Network mbed

Fork of RF24Network_Send by Akash Vibhute

Committer:
pietor
Date:
Tue Jul 10 12:07:26 2018 +0000
Revision:
12:38c5efed7950
Parent:
11:2aa84e063c49
pom

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 6:03ba3e18ced2 5
pietor 11:2aa84e063c49 6 #define NUM_SAMPLES 2000 // size of sample series
pietor 12:38c5efed7950 7 #define MIN_TARE_VALUE 0.500 //2.5 / 3.3 (2.5V is zero load and adc is between 0 and 1)µ
pietor 6:03ba3e18ced2 8
pietor 5:e6067799a414 9 Verzender sent;
pietor 8:62b4607c44ca 10 Serial pc(USBTX, USBRX);
pietor 8:62b4607c44ca 11 Timer t1;
pietor 8:62b4607c44ca 12 State current_state = State_init;
pietor 12:38c5efed7950 13 State previous_state;
pietor 12:38c5efed7950 14
pietor 12:38c5efed7950 15 InterruptIn reedSensor(D9);
pietor 12:38c5efed7950 16 AnalogIn ain(A2);
pietor 12:38c5efed7950 17
pietor 6:03ba3e18ced2 18
pietor 6:03ba3e18ced2 19 float tare = 0;
pietor 10:875812a04307 20 float calibration = 0;
pietor 8:62b4607c44ca 21 bool reed = false;
pietor 10:875812a04307 22 bool tareDone = false;
pietor 11:2aa84e063c49 23 char nextState;
pietor 12:38c5efed7950 24 float calibrationMass = 1000;
pietor 11:2aa84e063c49 25 float AVERAGE_TARE = 0;
pietor 12:38c5efed7950 26 float CALIBRATION_OFFSET = 0.019992;
pietor 6:03ba3e18ced2 27
pietor 8:62b4607c44ca 28
pietor 8:62b4607c44ca 29 /**
pietor 8:62b4607c44ca 30 Sets the reed status on rising trigger
pietor 8:62b4607c44ca 31 */
pietor 8:62b4607c44ca 32 void setReed()
pietor 8:62b4607c44ca 33 {
pietor 8:62b4607c44ca 34 reed = true;
pietor 8:62b4607c44ca 35 }
pietor 6:03ba3e18ced2 36
pietor 8:62b4607c44ca 37 /**
pietor 8:62b4607c44ca 38 resets the reed status on falling trigger
pietor 8:62b4607c44ca 39 */
pietor 8:62b4607c44ca 40 void dissableReed()
pietor 8:62b4607c44ca 41 {
pietor 8:62b4607c44ca 42 reed = false;
pietor 8:62b4607c44ca 43 }
pietor 8:62b4607c44ca 44
pietor 8:62b4607c44ca 45 /**
pietor 12:38c5efed7950 46 Sets the current status of the state machine
pietor 8:62b4607c44ca 47
pietor 8:62b4607c44ca 48 @param the state to be set
pietor 8:62b4607c44ca 49 */
pietor 6:03ba3e18ced2 50
pietor 6:03ba3e18ced2 51 void setCurrentState( State setState )
pietor 6:03ba3e18ced2 52 {
pietor 12:38c5efed7950 53 previous_state = current_state;
pietor 6:03ba3e18ced2 54 current_state = setState;
pietor 6:03ba3e18ced2 55 }
pietor 6:03ba3e18ced2 56
pietor 6:03ba3e18ced2 57
pietor 12:38c5efed7950 58
pietor 12:38c5efed7950 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 12:38c5efed7950 119 AVERAGE = ((tare - AVERAGE)*(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 12:38c5efed7950 124 float massa = ((tare - SUB_AVERAGE_ARRAY[i])*(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 12:38c5efed7950 151 pc.baud(9600);
pietor 12:38c5efed7950 152 pc.printf("--Verzender2--\n\r");
pietor 12:38c5efed7950 153 sent.printDetails();
pietor 12:38c5efed7950 154 sent.sendMessage(STARTUP);
pietor 5:e6067799a414 155 while(1) {
pietor 8:62b4607c44ca 156 reedSensor.fall(&setReed);
pietor 8:62b4607c44ca 157 reedSensor.rise(&dissableReed);
pietor 5:e6067799a414 158 sent.update();
pietor 10:875812a04307 159
pietor 6:03ba3e18ced2 160 switch (current_state) {
pietor 6:03ba3e18ced2 161 case State_init:
pietor 12:38c5efed7950 162 IF_PRINT_ENABLE(pc.printf("State: Init\n\r"););
pietor 12:38c5efed7950 163 sent.sendMessage(INIT);
pietor 6:03ba3e18ced2 164 wait_ms(1000);
pietor 8:62b4607c44ca 165 reedSensor.mode(PullUp);
pietor 6:03ba3e18ced2 166 setCurrentState(State_read);
pietor 6:03ba3e18ced2 167 payload_t payload;
pietor 12:38c5efed7950 168 sent.sendMessage(STARTUP_SUCCES);
pietor 6:03ba3e18ced2 169 break;
pietor 6:03ba3e18ced2 170
pietor 6:03ba3e18ced2 171 case State_position:
pietor 11:2aa84e063c49 172 sent.sendMessage(POSITION);
pietor 10:875812a04307 173 IF_PRINT_ENABLE(pc.printf("State: position\n\r"););
pietor 10:875812a04307 174 if (reed) {
pietor 11:2aa84e063c49 175 sent.sendMessage(POSITION_WAIT);
pietor 10:875812a04307 176 IF_PRINT_ENABLE(pc.printf("Waiting for 5 seconds\n\r"););
pietor 11:2aa84e063c49 177 wait(1);
pietor 11:2aa84e063c49 178 if (reed and nextState == 's') {
pietor 11:2aa84e063c49 179 IF_PRINT_ENABLE(pc.printf("Selecting tare state\n\r"););
pietor 10:875812a04307 180 setCurrentState(State_tare);
pietor 11:2aa84e063c49 181 break;
pietor 11:2aa84e063c49 182 } else if (reed and nextState == 'c') {
pietor 11:2aa84e063c49 183 IF_PRINT_ENABLE(pc.printf("Selecting calibrate state\n\r"););
pietor 10:875812a04307 184 setCurrentState(State_calibrate);
pietor 11:2aa84e063c49 185 break;
pietor 10:875812a04307 186 } else {
pietor 11:2aa84e063c49 187 sent.sendMessage(POSITION_ERROR);
pietor 10:875812a04307 188 IF_PRINT_ENABLE(pc.printf("Error on position\n\r"););
pietor 11:2aa84e063c49 189 setCurrentState(State_read);
pietor 10:875812a04307 190 }
pietor 10:875812a04307 191 }
pietor 6:03ba3e18ced2 192 break;
pietor 6:03ba3e18ced2 193
pietor 6:03ba3e18ced2 194
pietor 6:03ba3e18ced2 195 case State_tare:
pietor 11:2aa84e063c49 196 sent.sendMessage(TARE);
pietor 10:875812a04307 197 IF_PRINT_ENABLE(pc.printf("State: tare\n\r"););
pietor 11:2aa84e063c49 198 tare = getAverageSamples(250,100);
pietor 11:2aa84e063c49 199 if(MIN_TARE_VALUE <= tare) {
pietor 11:2aa84e063c49 200 sent.sendMessage(TARE_COMPLETE);
pietor 11:2aa84e063c49 201 IF_PRINT_ENABLE(pc.printf("tare = %f\r\n",tare*3.3););
pietor 11:2aa84e063c49 202 tareDone = true;
pietor 12:38c5efed7950 203 } else if (tare <= 0.01) {
pietor 12:38c5efed7950 204 sent.sendMessage(BATTERY);
pietor 12:38c5efed7950 205 IF_PRINT_ENABLE(pc.printf("ERROR: BATTERY NOT INSERTED\n\r"););
pietor 11:2aa84e063c49 206 } else {
pietor 11:2aa84e063c49 207 sent.sendMessage(TARE_ERROR);
pietor 11:2aa84e063c49 208 IF_PRINT_ENABLE(pc.printf("ERROR: TARE VALUE TO LOW\n\r"););
pietor 11:2aa84e063c49 209 tareDone = false;
pietor 11:2aa84e063c49 210 }
pietor 6:03ba3e18ced2 211 setCurrentState(State_read);
pietor 6:03ba3e18ced2 212 break;
akashvibhute 1:5be48a9550c3 213
pietor 6:03ba3e18ced2 214 case State_read:
pietor 12:38c5efed7950 215 sent.sendMessage(READ);
pietor 8:62b4607c44ca 216 if (reed) {
pietor 12:38c5efed7950 217 if (tareDone == true){
pietor 12:38c5efed7950 218 float massa = getAverageSamples2(100, 5);
pietor 12:38c5efed7950 219 //payload.reedsensor = 1;
pietor 11:2aa84e063c49 220 payload.gram = massa;
pietor 12:38c5efed7950 221 IF_PRINT_ENABLE(pc.printf("%f\r\n", payload.gram););
pietor 11:2aa84e063c49 222 bool ok = sent.write(payload);
pietor 11:2aa84e063c49 223 if (ok) {
pietor 12:38c5efed7950 224 IF_PRINT_ENABLE(
pietor 12:38c5efed7950 225 pc.printf("ok.\n\r"););
pietor 11:2aa84e063c49 226 } else {
pietor 11:2aa84e063c49 227 IF_PRINT_ENABLE(pc.printf("failed.\n\r"););
pietor 11:2aa84e063c49 228 }
pietor 7:cbdbaf825b4a 229 } else {
pietor 11:2aa84e063c49 230 sent.sendMessage(TARE_FIRST);
pietor 11:2aa84e063c49 231 IF_PRINT_ENABLE(pc.printf("Tare First.\n\r"););
pietor 7:cbdbaf825b4a 232 }
pietor 12:38c5efed7950 233
pietor 7:cbdbaf825b4a 234 setCurrentState(State_receive);
pietor 6:03ba3e18ced2 235 }
pietor 6:03ba3e18ced2 236 break;
pietor 7:cbdbaf825b4a 237
pietor 6:03ba3e18ced2 238 case State_receive:
pietor 6:03ba3e18ced2 239 sent.update();
pietor 6:03ba3e18ced2 240 if (sent.available()) {
pietor 11:2aa84e063c49 241 IF_PRINT_ENABLE(pc.printf("Received something\n\r"););
pietor 6:03ba3e18ced2 242 state_Packet state;
pietor 6:03ba3e18ced2 243 state = sent.read();
pietor 11:2aa84e063c49 244
pietor 11:2aa84e063c49 245 if( state.setstate == 's') {
pietor 10:875812a04307 246 IF_PRINT_ENABLE(pc.printf("Next state: Tare\n\r"););
pietor 11:2aa84e063c49 247 nextState = 's';
pietor 6:03ba3e18ced2 248 setCurrentState(State_position);
pietor 6:03ba3e18ced2 249 break;
pietor 11:2aa84e063c49 250 }
pietor 11:2aa84e063c49 251 if(state.setstate == 'c') {
pietor 10:875812a04307 252 IF_PRINT_ENABLE(pc.printf("Next state: Calibrate\n\r"););
pietor 11:2aa84e063c49 253 nextState = 'c';
pietor 10:875812a04307 254 setCurrentState(State_position);
pietor 11:2aa84e063c49 255 break;
pietor 6:03ba3e18ced2 256 }
pietor 12:38c5efed7950 257 }
pietor 11:2aa84e063c49 258
pietor 6:03ba3e18ced2 259 setCurrentState(State_read);
pietor 12:38c5efed7950 260
pietor 6:03ba3e18ced2 261 break;
pietor 10:875812a04307 262
pietor 12:38c5efed7950 263
pietor 12:38c5efed7950 264 case State_calibrate:
pietor 12:38c5efed7950 265 setCurrentState(State_read);
pietor 12:38c5efed7950 266 if(tareDone == true) {
pietor 12:38c5efed7950 267 IF_PRINT_ENABLE(pc.printf("State: calibreren\n\r"););
pietor 12:38c5efed7950 268 IF_PRINT_ENABLE(pc.printf("Put 1kg on paddle...\n\r"););
pietor 12:38c5efed7950 269 IF_PRINT_ENABLE(pc.printf("Waiting: 10 seconds\n\r"););
pietor 12:38c5efed7950 270 wait(1);
pietor 12:38c5efed7950 271 IF_PRINT_ENABLE(pc.printf("Starting calibration\n\r"););
pietor 12:38c5efed7950 272 CALIBRATION_OFFSET = getAverageSamples(1000,10)-tare;
pietor 12:38c5efed7950 273 IF_PRINT_ENABLE(pc.printf("Calibration= %f\n\r", calibration*3.3););
pietor 11:2aa84e063c49 274
pietor 12:38c5efed7950 275 } else {
pietor 12:38c5efed7950 276 IF_PRINT_ENABLE(pc.printf("ERROR: TARE FIRST\n\r"););
pietor 12:38c5efed7950 277 }
pietor 12:38c5efed7950 278 break;
pietor 12:38c5efed7950 279
akashvibhute 2:926b93a68399 280 }
akashvibhute 0:3982c0e9eda1 281 }
akashvibhute 0:3982c0e9eda1 282 }