RF24Network Send example program.
Dependencies: xtoff RF24Network mbed
Fork of RF24Network_Send by
main.cpp@8:62b4607c44ca, 2018-03-08 (annotated)
- Committer:
- pietor
- Date:
- Thu Mar 08 09:23:05 2018 +0000
- Revision:
- 8:62b4607c44ca
- Parent:
- 7:cbdbaf825b4a
- Child:
- 10:875812a04307
8/03;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pietor | 5:e6067799a414 | 1 | #include "Verzender.h" |
pietor | 8:62b4607c44ca | 2 | #include "PowerControl/PowerControl.h" |
pietor | 8:62b4607c44ca | 3 | #include "PowerControl/EthernetPowerControl.h" |
pietor | 6:03ba3e18ced2 | 4 | |
pietor | 6:03ba3e18ced2 | 5 | #define NUM_SAMPLES 2000 // size of sample series |
pietor | 8:62b4607c44ca | 6 | #define USR_POWERDOWN (0x104) |
pietor | 6:03ba3e18ced2 | 7 | |
pietor | 5:e6067799a414 | 8 | Verzender sent; |
pietor | 8:62b4607c44ca | 9 | Serial pc(USBTX, USBRX); |
pietor | 8:62b4607c44ca | 10 | Timer t1; |
pietor | 8:62b4607c44ca | 11 | Timer t2; |
pietor | 8:62b4607c44ca | 12 | State current_state = State_init; |
pietor | 8:62b4607c44ca | 13 | InterruptIn reedSensor(p25); |
pietor | 6:03ba3e18ced2 | 14 | AnalogIn ain(p17); |
pietor | 6:03ba3e18ced2 | 15 | |
pietor | 6:03ba3e18ced2 | 16 | float tare = 0; |
pietor | 6:03ba3e18ced2 | 17 | float massa = 0; |
pietor | 8:62b4607c44ca | 18 | bool reed = false; |
pietor | 6:03ba3e18ced2 | 19 | |
pietor | 8:62b4607c44ca | 20 | |
pietor | 8:62b4607c44ca | 21 | /** |
pietor | 8:62b4607c44ca | 22 | Sets the reed status on rising trigger |
pietor | 8:62b4607c44ca | 23 | */ |
pietor | 8:62b4607c44ca | 24 | void setReed() |
pietor | 8:62b4607c44ca | 25 | { |
pietor | 8:62b4607c44ca | 26 | reed = true; |
pietor | 8:62b4607c44ca | 27 | } |
pietor | 6:03ba3e18ced2 | 28 | |
pietor | 8:62b4607c44ca | 29 | /** |
pietor | 8:62b4607c44ca | 30 | resets the reed status on falling trigger |
pietor | 8:62b4607c44ca | 31 | */ |
pietor | 8:62b4607c44ca | 32 | void dissableReed() |
pietor | 8:62b4607c44ca | 33 | { |
pietor | 8:62b4607c44ca | 34 | reed = false; |
pietor | 8:62b4607c44ca | 35 | } |
pietor | 8:62b4607c44ca | 36 | |
pietor | 8:62b4607c44ca | 37 | |
pietor | 8:62b4607c44ca | 38 | /** |
pietor | 8:62b4607c44ca | 39 | RSets the current status of the state machine |
pietor | 8:62b4607c44ca | 40 | |
pietor | 8:62b4607c44ca | 41 | @param the state to be set |
pietor | 8:62b4607c44ca | 42 | */ |
pietor | 6:03ba3e18ced2 | 43 | |
pietor | 6:03ba3e18ced2 | 44 | void setCurrentState( State setState ) |
pietor | 6:03ba3e18ced2 | 45 | { |
pietor | 6:03ba3e18ced2 | 46 | current_state = setState; |
pietor | 6:03ba3e18ced2 | 47 | } |
pietor | 6:03ba3e18ced2 | 48 | |
pietor | 6:03ba3e18ced2 | 49 | |
pietor | 8:62b4607c44ca | 50 | /** |
pietor | 8:62b4607c44ca | 51 | Get the average of a given number of samples from the analog input |
pietor | 6:03ba3e18ced2 | 52 | |
pietor | 8:62b4607c44ca | 53 | @param amount of samples |
pietor | 8:62b4607c44ca | 54 | @return average of the analog input |
pietor | 8:62b4607c44ca | 55 | */ |
pietor | 6:03ba3e18ced2 | 56 | float getAverageSamples(int samples) |
pietor | 6:03ba3e18ced2 | 57 | { |
pietor | 6:03ba3e18ced2 | 58 | float AVERAGE = 0; |
pietor | 6:03ba3e18ced2 | 59 | int num_samples = 0; |
pietor | 6:03ba3e18ced2 | 60 | while (num_samples < samples) { |
pietor | 6:03ba3e18ced2 | 61 | float r = ain.read(); |
pietor | 6:03ba3e18ced2 | 62 | AVERAGE += r; |
pietor | 6:03ba3e18ced2 | 63 | num_samples++; |
pietor | 6:03ba3e18ced2 | 64 | } |
pietor | 6:03ba3e18ced2 | 65 | |
pietor | 6:03ba3e18ced2 | 66 | AVERAGE /= num_samples; |
pietor | 6:03ba3e18ced2 | 67 | num_samples = 0; |
pietor | 6:03ba3e18ced2 | 68 | |
pietor | 6:03ba3e18ced2 | 69 | return AVERAGE; |
pietor | 6:03ba3e18ced2 | 70 | } |
pietor | 6:03ba3e18ced2 | 71 | |
pietor | 8:62b4607c44ca | 72 | /** |
pietor | 8:62b4607c44ca | 73 | Get the average from the analog input over a mount of time |
pietor | 8:62b4607c44ca | 74 | |
pietor | 8:62b4607c44ca | 75 | @param amount of time in seconds |
pietor | 8:62b4607c44ca | 76 | @return average of the analog input |
pietor | 8:62b4607c44ca | 77 | */ |
pietor | 8:62b4607c44ca | 78 | float getAverageTime(int time) |
pietor | 6:03ba3e18ced2 | 79 | { |
pietor | 6:03ba3e18ced2 | 80 | t2.start(); |
pietor | 6:03ba3e18ced2 | 81 | t2.reset(); |
pietor | 6:03ba3e18ced2 | 82 | float AVERAGE = 0; |
pietor | 6:03ba3e18ced2 | 83 | int num_samples = 0; |
pietor | 8:62b4607c44ca | 84 | while (num_samples <= NUM_SAMPLES & t2.read() <= time) { |
pietor | 6:03ba3e18ced2 | 85 | float r = ain.read(); |
pietor | 6:03ba3e18ced2 | 86 | AVERAGE += r; |
pietor | 6:03ba3e18ced2 | 87 | num_samples++; |
pietor | 6:03ba3e18ced2 | 88 | } |
pietor | 6:03ba3e18ced2 | 89 | AVERAGE /= num_samples; |
pietor | 6:03ba3e18ced2 | 90 | num_samples = 0; |
pietor | 6:03ba3e18ced2 | 91 | |
pietor | 6:03ba3e18ced2 | 92 | return AVERAGE; |
pietor | 6:03ba3e18ced2 | 93 | } |
pietor | 6:03ba3e18ced2 | 94 | |
pietor | 8:62b4607c44ca | 95 | /** |
pietor | 8:62b4607c44ca | 96 | Main function: |
pietor | 8:62b4607c44ca | 97 | State machine: |
pietor | 8:62b4607c44ca | 98 | Init: initialization |
pietor | 8:62b4607c44ca | 99 | Position: Checks if the paddle is on tare position |
pietor | 8:62b4607c44ca | 100 | Tare: Set Zeroload point on average of 50000 samples |
pietor | 8:62b4607c44ca | 101 | Read: Read the mass when the paddle passes the read position and |
pietor | 8:62b4607c44ca | 102 | send the data to the receiver. |
pietor | 8:62b4607c44ca | 103 | Receive: Check if there were messages |
pietor | 8:62b4607c44ca | 104 | */ |
akashvibhute | 2:926b93a68399 | 105 | int main() |
akashvibhute | 0:3982c0e9eda1 | 106 | { |
pietor | 5:e6067799a414 | 107 | while(1) { |
pietor | 8:62b4607c44ca | 108 | reedSensor.fall(&setReed); |
pietor | 8:62b4607c44ca | 109 | reedSensor.rise(&dissableReed); |
pietor | 5:e6067799a414 | 110 | sent.update(); |
pietor | 8:62b4607c44ca | 111 | |
pietor | 6:03ba3e18ced2 | 112 | switch (current_state) { |
pietor | 6:03ba3e18ced2 | 113 | case State_init: |
pietor | 8:62b4607c44ca | 114 | //sent.test(); |
pietor | 6:03ba3e18ced2 | 115 | pc.baud(9600); |
pietor | 8:62b4607c44ca | 116 | PHY_PowerDown(); //Power down Ethernet interface |
pietor | 6:03ba3e18ced2 | 117 | wait_ms(1000); |
pietor | 7:cbdbaf825b4a | 118 | pc.printf("--Verzender--\n\r"); |
pietor | 8:62b4607c44ca | 119 | reedSensor.mode(PullUp); |
pietor | 6:03ba3e18ced2 | 120 | setCurrentState(State_read); |
pietor | 6:03ba3e18ced2 | 121 | payload_t payload; |
pietor | 6:03ba3e18ced2 | 122 | break; |
pietor | 6:03ba3e18ced2 | 123 | |
pietor | 6:03ba3e18ced2 | 124 | case State_position: |
pietor | 6:03ba3e18ced2 | 125 | pc.printf("State: position\n\r"); |
pietor | 8:62b4607c44ca | 126 | if (reed) |
pietor | 6:03ba3e18ced2 | 127 | setCurrentState(State_tare); |
pietor | 6:03ba3e18ced2 | 128 | break; |
pietor | 6:03ba3e18ced2 | 129 | |
pietor | 6:03ba3e18ced2 | 130 | |
pietor | 6:03ba3e18ced2 | 131 | case State_tare: |
pietor | 6:03ba3e18ced2 | 132 | pc.printf("State: tare\n\r"); |
pietor | 6:03ba3e18ced2 | 133 | tare = getAverageSamples(50000); |
pietor | 6:03ba3e18ced2 | 134 | pc.printf("tare = %f\r\n",tare*3.3); |
pietor | 7:cbdbaf825b4a | 135 | |
pietor | 6:03ba3e18ced2 | 136 | setCurrentState(State_read); |
pietor | 6:03ba3e18ced2 | 137 | break; |
akashvibhute | 1:5be48a9550c3 | 138 | |
pietor | 6:03ba3e18ced2 | 139 | case State_read: |
pietor | 8:62b4607c44ca | 140 | if (reed) { |
pietor | 8:62b4607c44ca | 141 | massa = getAverageTime(1) - tare; |
pietor | 7:cbdbaf825b4a | 142 | payload.reedsensor = 1; |
pietor | 7:cbdbaf825b4a | 143 | payload.milligram = massa * 3.3; |
pietor | 8:62b4607c44ca | 144 | pc.printf("Sent packet1 -- Reed: %d --- %f mg \r\n",payload.reedsensor, payload.milligram); |
pietor | 7:cbdbaf825b4a | 145 | bool ok = sent.write(payload); |
pietor | 7:cbdbaf825b4a | 146 | if (ok) { |
pietor | 7:cbdbaf825b4a | 147 | pc.printf("ok.\n\r"); |
pietor | 7:cbdbaf825b4a | 148 | } else { |
pietor | 7:cbdbaf825b4a | 149 | pc.printf("failed.\n\r"); |
pietor | 7:cbdbaf825b4a | 150 | } |
pietor | 7:cbdbaf825b4a | 151 | setCurrentState(State_receive); |
pietor | 6:03ba3e18ced2 | 152 | } |
pietor | 6:03ba3e18ced2 | 153 | break; |
pietor | 7:cbdbaf825b4a | 154 | |
pietor | 6:03ba3e18ced2 | 155 | case State_receive: |
pietor | 6:03ba3e18ced2 | 156 | sent.update(); |
pietor | 6:03ba3e18ced2 | 157 | if (sent.available()) { |
pietor | 6:03ba3e18ced2 | 158 | state_Packet state; |
pietor | 6:03ba3e18ced2 | 159 | state = sent.read(); |
pietor | 6:03ba3e18ced2 | 160 | if(state.setstate == State_position) { |
pietor | 6:03ba3e18ced2 | 161 | setCurrentState(State_position); |
pietor | 6:03ba3e18ced2 | 162 | break; |
pietor | 6:03ba3e18ced2 | 163 | } |
pietor | 6:03ba3e18ced2 | 164 | } |
pietor | 6:03ba3e18ced2 | 165 | setCurrentState(State_read); |
pietor | 6:03ba3e18ced2 | 166 | break; |
akashvibhute | 2:926b93a68399 | 167 | } |
akashvibhute | 0:3982c0e9eda1 | 168 | } |
akashvibhute | 0:3982c0e9eda1 | 169 | } |