![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
The transmitting code for JetX Engineering
Dependencies: MLX90614 mbed nRF24L01P
main.cpp@0:42fb8bed86d5, 2017-03-23 (annotated)
- Committer:
- FraserLaw23
- Date:
- Thu Mar 23 16:51:22 2017 +0000
- Revision:
- 0:42fb8bed86d5
The transmitting code for JetX
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
FraserLaw23 | 0:42fb8bed86d5 | 1 | #include "mbed.h" |
FraserLaw23 | 0:42fb8bed86d5 | 2 | #include <math.h> |
FraserLaw23 | 0:42fb8bed86d5 | 3 | #include "mlx90614.h" |
FraserLaw23 | 0:42fb8bed86d5 | 4 | #include "nRF24L01P.h" |
FraserLaw23 | 0:42fb8bed86d5 | 5 | |
FraserLaw23 | 0:42fb8bed86d5 | 6 | Serial pc (USBTX, USBRX); |
FraserLaw23 | 0:42fb8bed86d5 | 7 | InterruptIn rpm1 (p21); // initiate two rpm sensors 130/140 degrees was the best output for the rpm1 resistor |
FraserLaw23 | 0:42fb8bed86d5 | 8 | InterruptIn rpm2 (p22); |
FraserLaw23 | 0:42fb8bed86d5 | 9 | InterruptIn vibr (p23); // initiate vibration sensor |
FraserLaw23 | 0:42fb8bed86d5 | 10 | AnalogIn press1 (p15); // initiate four pressure sensors |
FraserLaw23 | 0:42fb8bed86d5 | 11 | AnalogIn press2 (p16); |
FraserLaw23 | 0:42fb8bed86d5 | 12 | AnalogIn press5 (p17); |
FraserLaw23 | 0:42fb8bed86d5 | 13 | AnalogIn press3 (p19); |
FraserLaw23 | 0:42fb8bed86d5 | 14 | AnalogIn press4 (p18); |
FraserLaw23 | 0:42fb8bed86d5 | 15 | AnalogIn air (p20); |
FraserLaw23 | 0:42fb8bed86d5 | 16 | |
FraserLaw23 | 0:42fb8bed86d5 | 17 | I2C i2c(p28,p27); // sda,scl |
FraserLaw23 | 0:42fb8bed86d5 | 18 | MLX90614 temp1(&i2c, 0x5A<<1); // initiate using default address |
FraserLaw23 | 0:42fb8bed86d5 | 19 | MLX90614 temp2(&i2c, 0x04<<1); // initiate using changed address |
FraserLaw23 | 0:42fb8bed86d5 | 20 | |
FraserLaw23 | 0:42fb8bed86d5 | 21 | Timer t_rpm1; // create two timers for rpm calculations |
FraserLaw23 | 0:42fb8bed86d5 | 22 | Timer t_rpm2; |
FraserLaw23 | 0:42fb8bed86d5 | 23 | Timer t_vibr; // create a timer for vibration calculations |
FraserLaw23 | 0:42fb8bed86d5 | 24 | Ticker ticker; // create ticker to send data |
FraserLaw23 | 0:42fb8bed86d5 | 25 | |
FraserLaw23 | 0:42fb8bed86d5 | 26 | nRF24L01P wireless(p5, p6, p7, p8, p9, p10); // mosi, miso, sck, csn, ce, irq |
FraserLaw23 | 0:42fb8bed86d5 | 27 | |
FraserLaw23 | 0:42fb8bed86d5 | 28 | // viarables |
FraserLaw23 | 0:42fb8bed86d5 | 29 | union rpm1_sensor{ |
FraserLaw23 | 0:42fb8bed86d5 | 30 | float rpm_value; |
FraserLaw23 | 0:42fb8bed86d5 | 31 | char bytes[4]; |
FraserLaw23 | 0:42fb8bed86d5 | 32 | }r1; |
FraserLaw23 | 0:42fb8bed86d5 | 33 | |
FraserLaw23 | 0:42fb8bed86d5 | 34 | union rpm2_sensor{ |
FraserLaw23 | 0:42fb8bed86d5 | 35 | float rpm_value; |
FraserLaw23 | 0:42fb8bed86d5 | 36 | char bytes[4]; |
FraserLaw23 | 0:42fb8bed86d5 | 37 | }r2; |
FraserLaw23 | 0:42fb8bed86d5 | 38 | |
FraserLaw23 | 0:42fb8bed86d5 | 39 | union vibr_sensor{ |
FraserLaw23 | 0:42fb8bed86d5 | 40 | float vibr_value; |
FraserLaw23 | 0:42fb8bed86d5 | 41 | char bytes[4]; |
FraserLaw23 | 0:42fb8bed86d5 | 42 | }v; |
FraserLaw23 | 0:42fb8bed86d5 | 43 | |
FraserLaw23 | 0:42fb8bed86d5 | 44 | union press1_sensor{ |
FraserLaw23 | 0:42fb8bed86d5 | 45 | float press_value; |
FraserLaw23 | 0:42fb8bed86d5 | 46 | char bytes[4]; |
FraserLaw23 | 0:42fb8bed86d5 | 47 | }p1; |
FraserLaw23 | 0:42fb8bed86d5 | 48 | |
FraserLaw23 | 0:42fb8bed86d5 | 49 | union press2_sensor{ |
FraserLaw23 | 0:42fb8bed86d5 | 50 | float press_value; |
FraserLaw23 | 0:42fb8bed86d5 | 51 | char bytes[4]; |
FraserLaw23 | 0:42fb8bed86d5 | 52 | }p2; |
FraserLaw23 | 0:42fb8bed86d5 | 53 | |
FraserLaw23 | 0:42fb8bed86d5 | 54 | union press3_sensor{ |
FraserLaw23 | 0:42fb8bed86d5 | 55 | float press_value; |
FraserLaw23 | 0:42fb8bed86d5 | 56 | char bytes[4]; |
FraserLaw23 | 0:42fb8bed86d5 | 57 | }p3; |
FraserLaw23 | 0:42fb8bed86d5 | 58 | |
FraserLaw23 | 0:42fb8bed86d5 | 59 | union press4_sensor{ |
FraserLaw23 | 0:42fb8bed86d5 | 60 | float press_value; |
FraserLaw23 | 0:42fb8bed86d5 | 61 | char bytes[4]; |
FraserLaw23 | 0:42fb8bed86d5 | 62 | }p4; |
FraserLaw23 | 0:42fb8bed86d5 | 63 | |
FraserLaw23 | 0:42fb8bed86d5 | 64 | union air_sensor{ |
FraserLaw23 | 0:42fb8bed86d5 | 65 | float air_value; |
FraserLaw23 | 0:42fb8bed86d5 | 66 | char bytes[4]; |
FraserLaw23 | 0:42fb8bed86d5 | 67 | }a; |
FraserLaw23 | 0:42fb8bed86d5 | 68 | |
FraserLaw23 | 0:42fb8bed86d5 | 69 | union temp1_sensor{ |
FraserLaw23 | 0:42fb8bed86d5 | 70 | float temp_value; |
FraserLaw23 | 0:42fb8bed86d5 | 71 | char bytes[4]; |
FraserLaw23 | 0:42fb8bed86d5 | 72 | }t1; |
FraserLaw23 | 0:42fb8bed86d5 | 73 | |
FraserLaw23 | 0:42fb8bed86d5 | 74 | union temp2_sensor{ |
FraserLaw23 | 0:42fb8bed86d5 | 75 | float temp_value; |
FraserLaw23 | 0:42fb8bed86d5 | 76 | char bytes[4]; |
FraserLaw23 | 0:42fb8bed86d5 | 77 | }t2; |
FraserLaw23 | 0:42fb8bed86d5 | 78 | |
FraserLaw23 | 0:42fb8bed86d5 | 79 | int rpm1_count = 0; |
FraserLaw23 | 0:42fb8bed86d5 | 80 | int rpm2_count = 0; |
FraserLaw23 | 0:42fb8bed86d5 | 81 | int rpm_sample_size = 6; //changed to 5 from 10 |
FraserLaw23 | 0:42fb8bed86d5 | 82 | |
FraserLaw23 | 0:42fb8bed86d5 | 83 | int vibr_count = 0; |
FraserLaw23 | 0:42fb8bed86d5 | 84 | int vibr_sample_size = 10; |
FraserLaw23 | 0:42fb8bed86d5 | 85 | |
FraserLaw23 | 0:42fb8bed86d5 | 86 | int press_sample_size = 10; |
FraserLaw23 | 0:42fb8bed86d5 | 87 | float press_sample1 = 0.0; |
FraserLaw23 | 0:42fb8bed86d5 | 88 | float press_sample2 = 0.0; |
FraserLaw23 | 0:42fb8bed86d5 | 89 | float press_sample3 = 0.0; |
FraserLaw23 | 0:42fb8bed86d5 | 90 | float press_sample4 = 0.0; |
FraserLaw23 | 0:42fb8bed86d5 | 91 | |
FraserLaw23 | 0:42fb8bed86d5 | 92 | int air_sample_size = 10; |
FraserLaw23 | 0:42fb8bed86d5 | 93 | float air_sample = 0.0; |
FraserLaw23 | 0:42fb8bed86d5 | 94 | // voltage from a voltage divider with 5v input |
FraserLaw23 | 0:42fb8bed86d5 | 95 | float air_volt_ref = 3.24; |
FraserLaw23 | 0:42fb8bed86d5 | 96 | |
FraserLaw23 | 0:42fb8bed86d5 | 97 | const int DATA_SIZE = 20; |
FraserLaw23 | 0:42fb8bed86d5 | 98 | char data[DATA_SIZE]; |
FraserLaw23 | 0:42fb8bed86d5 | 99 | char* data_pointers[DATA_SIZE] = {&r1.bytes[2], &r1.bytes[3], &r2.bytes[2], &r2.bytes[3], &v.bytes[2], &v.bytes[3], |
FraserLaw23 | 0:42fb8bed86d5 | 100 | &p1.bytes[0], &p1.bytes[1], &p2.bytes[0], &p2.bytes[1], &p3.bytes[0], &p3.bytes[1], |
FraserLaw23 | 0:42fb8bed86d5 | 101 | &p4.bytes[0], &p4.bytes[1], &a.bytes[2], &a.bytes[3], &t1.bytes[2], &t1.bytes[3], &t2.bytes[2], &t2.bytes[3]}; |
FraserLaw23 | 0:42fb8bed86d5 | 102 | |
FraserLaw23 | 0:42fb8bed86d5 | 103 | |
FraserLaw23 | 0:42fb8bed86d5 | 104 | struct |
FraserLaw23 | 0:42fb8bed86d5 | 105 | { |
FraserLaw23 | 0:42fb8bed86d5 | 106 | float pressure0, pressure1, pressure2, pressure3; |
FraserLaw23 | 0:42fb8bed86d5 | 107 | float temperature0, temperature1; |
FraserLaw23 | 0:42fb8bed86d5 | 108 | float rpm0, rpm1; |
FraserLaw23 | 0:42fb8bed86d5 | 109 | float vibration; |
FraserLaw23 | 0:42fb8bed86d5 | 110 | float airflow; |
FraserLaw23 | 0:42fb8bed86d5 | 111 | }ddata; |
FraserLaw23 | 0:42fb8bed86d5 | 112 | // functions |
FraserLaw23 | 0:42fb8bed86d5 | 113 | void setRpm1(){ |
FraserLaw23 | 0:42fb8bed86d5 | 114 | if (rpm1_count == rpm_sample_size){ |
FraserLaw23 | 0:42fb8bed86d5 | 115 | t_rpm1.stop(); |
FraserLaw23 | 0:42fb8bed86d5 | 116 | |
FraserLaw23 | 0:42fb8bed86d5 | 117 | // calculate frequency |
FraserLaw23 | 0:42fb8bed86d5 | 118 | float time = t_rpm1.read(); |
FraserLaw23 | 0:42fb8bed86d5 | 119 | float frequency = rpm1_count/time; // in Hz |
FraserLaw23 | 0:42fb8bed86d5 | 120 | r1.rpm_value = frequency; |
FraserLaw23 | 0:42fb8bed86d5 | 121 | rpm1_count = 0; |
FraserLaw23 | 0:42fb8bed86d5 | 122 | |
FraserLaw23 | 0:42fb8bed86d5 | 123 | t_rpm1.reset(); |
FraserLaw23 | 0:42fb8bed86d5 | 124 | t_rpm1.start(); |
FraserLaw23 | 0:42fb8bed86d5 | 125 | } |
FraserLaw23 | 0:42fb8bed86d5 | 126 | else{ |
FraserLaw23 | 0:42fb8bed86d5 | 127 | rpm1_count++; |
FraserLaw23 | 0:42fb8bed86d5 | 128 | } |
FraserLaw23 | 0:42fb8bed86d5 | 129 | } |
FraserLaw23 | 0:42fb8bed86d5 | 130 | |
FraserLaw23 | 0:42fb8bed86d5 | 131 | void resetRpm1(){ |
FraserLaw23 | 0:42fb8bed86d5 | 132 | if(t_rpm1.read()>rpm_sample_size/2){ |
FraserLaw23 | 0:42fb8bed86d5 | 133 | t_rpm1.reset(); |
FraserLaw23 | 0:42fb8bed86d5 | 134 | rpm1_count = 0; |
FraserLaw23 | 0:42fb8bed86d5 | 135 | r1.rpm_value = 0; |
FraserLaw23 | 0:42fb8bed86d5 | 136 | } |
FraserLaw23 | 0:42fb8bed86d5 | 137 | } |
FraserLaw23 | 0:42fb8bed86d5 | 138 | |
FraserLaw23 | 0:42fb8bed86d5 | 139 | void setRpm2(){ |
FraserLaw23 | 0:42fb8bed86d5 | 140 | if (rpm2_count == rpm_sample_size){ |
FraserLaw23 | 0:42fb8bed86d5 | 141 | t_rpm2.stop(); |
FraserLaw23 | 0:42fb8bed86d5 | 142 | |
FraserLaw23 | 0:42fb8bed86d5 | 143 | // calculate frequency |
FraserLaw23 | 0:42fb8bed86d5 | 144 | float time = t_rpm2.read(); |
FraserLaw23 | 0:42fb8bed86d5 | 145 | float frequency = rpm2_count/time; // in Hz |
FraserLaw23 | 0:42fb8bed86d5 | 146 | r2.rpm_value = frequency; |
FraserLaw23 | 0:42fb8bed86d5 | 147 | rpm2_count = 0; |
FraserLaw23 | 0:42fb8bed86d5 | 148 | |
FraserLaw23 | 0:42fb8bed86d5 | 149 | t_rpm2.reset(); |
FraserLaw23 | 0:42fb8bed86d5 | 150 | t_rpm2.start(); |
FraserLaw23 | 0:42fb8bed86d5 | 151 | } |
FraserLaw23 | 0:42fb8bed86d5 | 152 | else{ |
FraserLaw23 | 0:42fb8bed86d5 | 153 | rpm2_count++; |
FraserLaw23 | 0:42fb8bed86d5 | 154 | } |
FraserLaw23 | 0:42fb8bed86d5 | 155 | } |
FraserLaw23 | 0:42fb8bed86d5 | 156 | |
FraserLaw23 | 0:42fb8bed86d5 | 157 | void resetRpm2(){ |
FraserLaw23 | 0:42fb8bed86d5 | 158 | if(t_rpm2.read()>rpm_sample_size/2){ |
FraserLaw23 | 0:42fb8bed86d5 | 159 | t_rpm2.reset(); |
FraserLaw23 | 0:42fb8bed86d5 | 160 | rpm1_count = 0; |
FraserLaw23 | 0:42fb8bed86d5 | 161 | r2.rpm_value = 0; |
FraserLaw23 | 0:42fb8bed86d5 | 162 | } |
FraserLaw23 | 0:42fb8bed86d5 | 163 | } |
FraserLaw23 | 0:42fb8bed86d5 | 164 | |
FraserLaw23 | 0:42fb8bed86d5 | 165 | void setPressure(){ |
FraserLaw23 | 0:42fb8bed86d5 | 166 | float press_sample1 = 0; |
FraserLaw23 | 0:42fb8bed86d5 | 167 | float press_sample2 = 0; |
FraserLaw23 | 0:42fb8bed86d5 | 168 | float press_sample3 = 0; |
FraserLaw23 | 0:42fb8bed86d5 | 169 | float press_sample4 = 0; |
FraserLaw23 | 0:42fb8bed86d5 | 170 | |
FraserLaw23 | 0:42fb8bed86d5 | 171 | for (int i = 0; i < press_sample_size; i++){ |
FraserLaw23 | 0:42fb8bed86d5 | 172 | press_sample1 += press1.read(); |
FraserLaw23 | 0:42fb8bed86d5 | 173 | press_sample2 += press2.read(); |
FraserLaw23 | 0:42fb8bed86d5 | 174 | press_sample3 += press3.read(); |
FraserLaw23 | 0:42fb8bed86d5 | 175 | press_sample4 += press4.read(); |
FraserLaw23 | 0:42fb8bed86d5 | 176 | wait(0.02); |
FraserLaw23 | 0:42fb8bed86d5 | 177 | } |
FraserLaw23 | 0:42fb8bed86d5 | 178 | |
FraserLaw23 | 0:42fb8bed86d5 | 179 | float avgPr1 = press_sample1/press_sample_size; |
FraserLaw23 | 0:42fb8bed86d5 | 180 | // Transfer Function from data sheet: |
FraserLaw23 | 0:42fb8bed86d5 | 181 | //Vout = VS*(0.0018*P+0.04) ± Error |
FraserLaw23 | 0:42fb8bed86d5 | 182 | // Where : |
FraserLaw23 | 0:42fb8bed86d5 | 183 | // VS = 5.0 Vdc |
FraserLaw23 | 0:42fb8bed86d5 | 184 | // Temperature = 0 to 85°C |
FraserLaw23 | 0:42fb8bed86d5 | 185 | //pc.printf("avg press 1 = %0.2f\n", avgPr1); |
FraserLaw23 | 0:42fb8bed86d5 | 186 | //float avg = avgPr1; |
FraserLaw23 | 0:42fb8bed86d5 | 187 | avgPr1 *= 100; |
FraserLaw23 | 0:42fb8bed86d5 | 188 | //avgPr1 = (((avgPr1+0.2) / 5.13) - 0.04) / 0.0018; |
FraserLaw23 | 0:42fb8bed86d5 | 189 | //avgPr1 = ((avgPr1 / 5) - 0.04) / 0.0018; |
FraserLaw23 | 0:42fb8bed86d5 | 190 | //pc.printf("avg press 1 = %0.2f\n", avgPr1); |
FraserLaw23 | 0:42fb8bed86d5 | 191 | p1.press_value = avgPr1; // converts to integer |
FraserLaw23 | 0:42fb8bed86d5 | 192 | |
FraserLaw23 | 0:42fb8bed86d5 | 193 | float avgPr2 = press_sample2/press_sample_size; |
FraserLaw23 | 0:42fb8bed86d5 | 194 | avgPr2 *= 100; |
FraserLaw23 | 0:42fb8bed86d5 | 195 | //avgPr2 = (((avgPr2+1.0) / 4.6) - 0.04) / 0.0018; |
FraserLaw23 | 0:42fb8bed86d5 | 196 | p2.press_value = avgPr2; |
FraserLaw23 | 0:42fb8bed86d5 | 197 | |
FraserLaw23 | 0:42fb8bed86d5 | 198 | float avgPr3 = press_sample3/press_sample_size; |
FraserLaw23 | 0:42fb8bed86d5 | 199 | avgPr3 *= 100; // (((avgPr3+1.1) / 4.6) - 0.04) / 0.0018; |
FraserLaw23 | 0:42fb8bed86d5 | 200 | p3.press_value = avgPr3; |
FraserLaw23 | 0:42fb8bed86d5 | 201 | |
FraserLaw23 | 0:42fb8bed86d5 | 202 | float avgPr4 = press_sample4/press_sample_size; |
FraserLaw23 | 0:42fb8bed86d5 | 203 | avgPr4 *= 100; //(((avgPr4+1.0) / 4.6) - 0.04) / 0.0018; |
FraserLaw23 | 0:42fb8bed86d5 | 204 | p4.press_value = avgPr4; |
FraserLaw23 | 0:42fb8bed86d5 | 205 | } |
FraserLaw23 | 0:42fb8bed86d5 | 206 | |
FraserLaw23 | 0:42fb8bed86d5 | 207 | void setAirspeed(){ |
FraserLaw23 | 0:42fb8bed86d5 | 208 | float air_sample = 0; |
FraserLaw23 | 0:42fb8bed86d5 | 209 | for (int i = 0; i < air_sample_size; i++){ |
FraserLaw23 | 0:42fb8bed86d5 | 210 | air_sample += air.read(); |
FraserLaw23 | 0:42fb8bed86d5 | 211 | wait(0.02); |
FraserLaw23 | 0:42fb8bed86d5 | 212 | } |
FraserLaw23 | 0:42fb8bed86d5 | 213 | //pc.printf("analog read %f\n", air.read() ); |
FraserLaw23 | 0:42fb8bed86d5 | 214 | float avgAir = air_sample/air_sample_size; |
FraserLaw23 | 0:42fb8bed86d5 | 215 | // transfer function from datasheet |
FraserLaw23 | 0:42fb8bed86d5 | 216 | |
FraserLaw23 | 0:42fb8bed86d5 | 217 | //NOTE added comments |
FraserLaw23 | 0:42fb8bed86d5 | 218 | //avgAir = (avgAir/air_volt_ref - 0.5) * 5; |
FraserLaw23 | 0:42fb8bed86d5 | 219 | //pc.printf("pressure is %f\n", pressure); |
FraserLaw23 | 0:42fb8bed86d5 | 220 | // END NOTE |
FraserLaw23 | 0:42fb8bed86d5 | 221 | // Vout = Vs * (0.2 * P(kPa) + 0.5) +- 6.25% |
FraserLaw23 | 0:42fb8bed86d5 | 222 | avgAir = avgAir / 3.3 * 5; |
FraserLaw23 | 0:42fb8bed86d5 | 223 | float pressure = ((avgAir * air_volt_ref / 5) - 0.5f) * 1/0.2f; |
FraserLaw23 | 0:42fb8bed86d5 | 224 | pressure *= 1000; |
FraserLaw23 | 0:42fb8bed86d5 | 225 | //pc.printf("pressure is %f\r", pressure); |
FraserLaw23 | 0:42fb8bed86d5 | 226 | |
FraserLaw23 | 0:42fb8bed86d5 | 227 | |
FraserLaw23 | 0:42fb8bed86d5 | 228 | // air speed (m/s) formula: speed_of_sound(in m/s)in room temp (20 degrees) * sqrt( ( (measured_pressure/pressure_at_sea_level + 1)^(2/7) -1) *5) |
FraserLaw23 | 0:42fb8bed86d5 | 229 | float calc_power = pow((((avgAir) / 10132.5) + 1), 0.285714286); |
FraserLaw23 | 0:42fb8bed86d5 | 230 | float calc_sqrt = 5 * (calc_power - 1); |
FraserLaw23 | 0:42fb8bed86d5 | 231 | calc_sqrt = sqrt(calc_sqrt); |
FraserLaw23 | 0:42fb8bed86d5 | 232 | float calc_airSpeed = 343.21 * calc_sqrt; |
FraserLaw23 | 0:42fb8bed86d5 | 233 | //pc.printf("calc airspeed %f\n", calc_airSpeed); |
FraserLaw23 | 0:42fb8bed86d5 | 234 | a.air_value = calc_airSpeed; |
FraserLaw23 | 0:42fb8bed86d5 | 235 | } |
FraserLaw23 | 0:42fb8bed86d5 | 236 | |
FraserLaw23 | 0:42fb8bed86d5 | 237 | void setVibration(){ |
FraserLaw23 | 0:42fb8bed86d5 | 238 | if (vibr_count == vibr_sample_size){ |
FraserLaw23 | 0:42fb8bed86d5 | 239 | vibr.disable_irq(); |
FraserLaw23 | 0:42fb8bed86d5 | 240 | t_vibr.stop(); |
FraserLaw23 | 0:42fb8bed86d5 | 241 | |
FraserLaw23 | 0:42fb8bed86d5 | 242 | // calculate frequency |
FraserLaw23 | 0:42fb8bed86d5 | 243 | float time = t_vibr.read(); |
FraserLaw23 | 0:42fb8bed86d5 | 244 | float frequency = vibr_count/time; // in Hz |
FraserLaw23 | 0:42fb8bed86d5 | 245 | v.vibr_value = frequency; |
FraserLaw23 | 0:42fb8bed86d5 | 246 | vibr_count = 0; |
FraserLaw23 | 0:42fb8bed86d5 | 247 | |
FraserLaw23 | 0:42fb8bed86d5 | 248 | t_vibr.reset(); |
FraserLaw23 | 0:42fb8bed86d5 | 249 | t_vibr.start(); |
FraserLaw23 | 0:42fb8bed86d5 | 250 | wait(0.025); |
FraserLaw23 | 0:42fb8bed86d5 | 251 | vibr.enable_irq(); |
FraserLaw23 | 0:42fb8bed86d5 | 252 | } |
FraserLaw23 | 0:42fb8bed86d5 | 253 | else{ |
FraserLaw23 | 0:42fb8bed86d5 | 254 | vibr_count++; |
FraserLaw23 | 0:42fb8bed86d5 | 255 | vibr.disable_irq(); |
FraserLaw23 | 0:42fb8bed86d5 | 256 | wait(0.025); |
FraserLaw23 | 0:42fb8bed86d5 | 257 | vibr.enable_irq(); |
FraserLaw23 | 0:42fb8bed86d5 | 258 | } |
FraserLaw23 | 0:42fb8bed86d5 | 259 | } |
FraserLaw23 | 0:42fb8bed86d5 | 260 | |
FraserLaw23 | 0:42fb8bed86d5 | 261 | void resetVibration(){ |
FraserLaw23 | 0:42fb8bed86d5 | 262 | if(t_vibr.read()>vibr_sample_size/2){ |
FraserLaw23 | 0:42fb8bed86d5 | 263 | t_vibr.reset(); |
FraserLaw23 | 0:42fb8bed86d5 | 264 | vibr_count = 0; |
FraserLaw23 | 0:42fb8bed86d5 | 265 | v.vibr_value = 0; |
FraserLaw23 | 0:42fb8bed86d5 | 266 | } |
FraserLaw23 | 0:42fb8bed86d5 | 267 | } |
FraserLaw23 | 0:42fb8bed86d5 | 268 | |
FraserLaw23 | 0:42fb8bed86d5 | 269 | void synchronizeData(){ |
FraserLaw23 | 0:42fb8bed86d5 | 270 | for (int i = 0; i<DATA_SIZE; i++){ |
FraserLaw23 | 0:42fb8bed86d5 | 271 | data[i] = *data_pointers[i]; |
FraserLaw23 | 0:42fb8bed86d5 | 272 | } |
FraserLaw23 | 0:42fb8bed86d5 | 273 | } |
FraserLaw23 | 0:42fb8bed86d5 | 274 | |
FraserLaw23 | 0:42fb8bed86d5 | 275 | void pcHeader(){ |
FraserLaw23 | 0:42fb8bed86d5 | 276 | // Display the (default) setup of the nRF24L01+ chip |
FraserLaw23 | 0:42fb8bed86d5 | 277 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", wireless.getRfFrequency() ); |
FraserLaw23 | 0:42fb8bed86d5 | 278 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", wireless.getRfOutputPower() ); |
FraserLaw23 | 0:42fb8bed86d5 | 279 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", wireless.getAirDataRate() ); |
FraserLaw23 | 0:42fb8bed86d5 | 280 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", wireless.getTxAddress() ); |
FraserLaw23 | 0:42fb8bed86d5 | 281 | pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n\n", wireless.getRxAddress() ); |
FraserLaw23 | 0:42fb8bed86d5 | 282 | } |
FraserLaw23 | 0:42fb8bed86d5 | 283 | |
FraserLaw23 | 0:42fb8bed86d5 | 284 | void pcDataOut(){ |
FraserLaw23 | 0:42fb8bed86d5 | 285 | pc.printf("Temperature sensor 1 reading: %3.1f degrees celcius\n", t1.temp_value); |
FraserLaw23 | 0:42fb8bed86d5 | 286 | pc.printf("Temperature sensor 2 reading: %3.1f degrees celcius\n", t2.temp_value); |
FraserLaw23 | 0:42fb8bed86d5 | 287 | pc.printf("RPM sensor 1 reading: %.1f Hz, %d rpm\n", r1.rpm_value, (int) (r1.rpm_value*60 + 0.5)); |
FraserLaw23 | 0:42fb8bed86d5 | 288 | pc.printf("RPM sensor 2 reading: %.1f Hz, %d rpm\n", r2.rpm_value, (int) (r2.rpm_value*60 + 0.5)); |
FraserLaw23 | 0:42fb8bed86d5 | 289 | pc.printf("Vibration sensor reading: %.1f Hz, %d rpm\n", v.vibr_value, (int) (v.vibr_value*60 + 0.5)); |
FraserLaw23 | 0:42fb8bed86d5 | 290 | pc.printf("Pressure Sensor 1 reading: %.2f kPa\n", p1.press_value); |
FraserLaw23 | 0:42fb8bed86d5 | 291 | pc.printf("Pressure Sensor 2 reading: %.2f kPa\n", p2.press_value); |
FraserLaw23 | 0:42fb8bed86d5 | 292 | pc.printf("Pressure Sensor 3 reading: %.2f kPa\n", p3.press_value); |
FraserLaw23 | 0:42fb8bed86d5 | 293 | pc.printf("Pressure Sensor 4 reading: %.2f kPa\n\n", p4.press_value); |
FraserLaw23 | 0:42fb8bed86d5 | 294 | pc.printf("Air Speed Sensor reading: %.2f m/s\n\n", a.air_value); |
FraserLaw23 | 0:42fb8bed86d5 | 295 | // individual bytes to be sent in hex |
FraserLaw23 | 0:42fb8bed86d5 | 296 | //pc.printf("%x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x\n", |
FraserLaw23 | 0:42fb8bed86d5 | 297 | //data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8], data[9], |
FraserLaw23 | 0:42fb8bed86d5 | 298 | //data[10], data[11], data[12], data[13], data[14], data[15], data[16], data[17], data[18], data[19]); |
FraserLaw23 | 0:42fb8bed86d5 | 299 | |
FraserLaw23 | 0:42fb8bed86d5 | 300 | } |
FraserLaw23 | 0:42fb8bed86d5 | 301 | |
FraserLaw23 | 0:42fb8bed86d5 | 302 | int main() { |
FraserLaw23 | 0:42fb8bed86d5 | 303 | //static_assert(sizeof(data) == 40, "unexpected strucure size"); |
FraserLaw23 | 0:42fb8bed86d5 | 304 | wireless.powerUp(); |
FraserLaw23 | 0:42fb8bed86d5 | 305 | t_rpm1.start(); |
FraserLaw23 | 0:42fb8bed86d5 | 306 | t_rpm2.start(); |
FraserLaw23 | 0:42fb8bed86d5 | 307 | t_vibr.start(); |
FraserLaw23 | 0:42fb8bed86d5 | 308 | |
FraserLaw23 | 0:42fb8bed86d5 | 309 | //pcHeader(); // print info on screen |
FraserLaw23 | 0:42fb8bed86d5 | 310 | |
FraserLaw23 | 0:42fb8bed86d5 | 311 | // wireless setup |
FraserLaw23 | 0:42fb8bed86d5 | 312 | wireless.setTransferSize(DATA_SIZE); |
FraserLaw23 | 0:42fb8bed86d5 | 313 | wireless.setReceiveMode(); |
FraserLaw23 | 0:42fb8bed86d5 | 314 | wireless.enable(); |
FraserLaw23 | 0:42fb8bed86d5 | 315 | |
FraserLaw23 | 0:42fb8bed86d5 | 316 | // rpm1 sampling on rising edge |
FraserLaw23 | 0:42fb8bed86d5 | 317 | rpm1.rise(&setRpm1); |
FraserLaw23 | 0:42fb8bed86d5 | 318 | |
FraserLaw23 | 0:42fb8bed86d5 | 319 | // rpm2 sampling on rising edge |
FraserLaw23 | 0:42fb8bed86d5 | 320 | rpm2.rise(&setRpm2); |
FraserLaw23 | 0:42fb8bed86d5 | 321 | |
FraserLaw23 | 0:42fb8bed86d5 | 322 | // vibration ampling on rising edge |
FraserLaw23 | 0:42fb8bed86d5 | 323 | vibr.rise(&setVibration); |
FraserLaw23 | 0:42fb8bed86d5 | 324 | |
FraserLaw23 | 0:42fb8bed86d5 | 325 | // sample pressure and airspeed every set interval |
FraserLaw23 | 0:42fb8bed86d5 | 326 | ticker.attach(&setPressure, 0.7); |
FraserLaw23 | 0:42fb8bed86d5 | 327 | ticker.attach(&setAirspeed, 0.7); |
FraserLaw23 | 0:42fb8bed86d5 | 328 | |
FraserLaw23 | 0:42fb8bed86d5 | 329 | while(1) { |
FraserLaw23 | 0:42fb8bed86d5 | 330 | // reset to 0 if idle |
FraserLaw23 | 0:42fb8bed86d5 | 331 | resetRpm1(); |
FraserLaw23 | 0:42fb8bed86d5 | 332 | resetRpm2(); |
FraserLaw23 | 0:42fb8bed86d5 | 333 | resetVibration(); |
FraserLaw23 | 0:42fb8bed86d5 | 334 | temp1.getTemp(&t1.temp_value); |
FraserLaw23 | 0:42fb8bed86d5 | 335 | temp2.getTemp(&t2.temp_value); |
FraserLaw23 | 0:42fb8bed86d5 | 336 | |
FraserLaw23 | 0:42fb8bed86d5 | 337 | setPressure(); |
FraserLaw23 | 0:42fb8bed86d5 | 338 | |
FraserLaw23 | 0:42fb8bed86d5 | 339 | synchronizeData(); |
FraserLaw23 | 0:42fb8bed86d5 | 340 | wireless.write(NRF24L01P_PIPE_P0, data, sizeof data); |
FraserLaw23 | 0:42fb8bed86d5 | 341 | |
FraserLaw23 | 0:42fb8bed86d5 | 342 | // fill in the data and send |
FraserLaw23 | 0:42fb8bed86d5 | 343 | //ddata = {0};+ |
FraserLaw23 | 0:42fb8bed86d5 | 344 | //#define GUI |
FraserLaw23 | 0:42fb8bed86d5 | 345 | #ifdef GUI |
FraserLaw23 | 0:42fb8bed86d5 | 346 | memset(&ddata, 0, sizeof(ddata)); |
FraserLaw23 | 0:42fb8bed86d5 | 347 | ddata.temperature0 = t1.temp_value; |
FraserLaw23 | 0:42fb8bed86d5 | 348 | ddata.temperature1 = t2.temp_value; |
FraserLaw23 | 0:42fb8bed86d5 | 349 | ddata.rpm0 = r1.rpm_value; |
FraserLaw23 | 0:42fb8bed86d5 | 350 | ddata.rpm1 = r2.rpm_value; |
FraserLaw23 | 0:42fb8bed86d5 | 351 | ddata.vibration = v.vibr_value; |
FraserLaw23 | 0:42fb8bed86d5 | 352 | ddata.pressure0 = p1.press_value; |
FraserLaw23 | 0:42fb8bed86d5 | 353 | ddata.pressure1 = p2.press_value; |
FraserLaw23 | 0:42fb8bed86d5 | 354 | ddata.pressure2 = p3.press_value; |
FraserLaw23 | 0:42fb8bed86d5 | 355 | ddata.pressure3 = p4.press_value; |
FraserLaw23 | 0:42fb8bed86d5 | 356 | ddata.airflow = a.air_value; |
FraserLaw23 | 0:42fb8bed86d5 | 357 | char *ptr = (char*)&ddata; |
FraserLaw23 | 0:42fb8bed86d5 | 358 | size_t s = sizeof(ddata); |
FraserLaw23 | 0:42fb8bed86d5 | 359 | pc.putc('s'); |
FraserLaw23 | 0:42fb8bed86d5 | 360 | |
FraserLaw23 | 0:42fb8bed86d5 | 361 | for (unsigned j = 0; j < s; ++j) { |
FraserLaw23 | 0:42fb8bed86d5 | 362 | pc.putc(ptr[j]); |
FraserLaw23 | 0:42fb8bed86d5 | 363 | } |
FraserLaw23 | 0:42fb8bed86d5 | 364 | |
FraserLaw23 | 0:42fb8bed86d5 | 365 | #else |
FraserLaw23 | 0:42fb8bed86d5 | 366 | pcDataOut(); |
FraserLaw23 | 0:42fb8bed86d5 | 367 | #endif |
FraserLaw23 | 0:42fb8bed86d5 | 368 | |
FraserLaw23 | 0:42fb8bed86d5 | 369 | wait(0.2); |
FraserLaw23 | 0:42fb8bed86d5 | 370 | } |
FraserLaw23 | 0:42fb8bed86d5 | 371 | } |