Cast tube scanner

Dependencies:   EALib mbed

Committer:
BPPearson
Date:
Tue Jan 05 16:47:27 2016 +0000
Revision:
1:196b5b827e29
Parent:
0:ceb7120ddb13
Cast tube scanner

Who changed what in which revision?

UserRevisionLine numberNew contents of line
BPPearson 0:ceb7120ddb13 1 /******************************************************************************
BPPearson 0:ceb7120ddb13 2 * Includes
BPPearson 0:ceb7120ddb13 3 *****************************************************************************/
BPPearson 0:ceb7120ddb13 4 #include "mbed.h"
BPPearson 0:ceb7120ddb13 5 #include "XBee.h"
BPPearson 0:ceb7120ddb13 6
BPPearson 0:ceb7120ddb13 7 /******************************************************************************
BPPearson 0:ceb7120ddb13 8 * Typedefs and defines
BPPearson 0:ceb7120ddb13 9 *****************************************************************************/
BPPearson 0:ceb7120ddb13 10 //#define CMD_BTN_MSG (0)
BPPearson 0:ceb7120ddb13 11 //#define XBEE_REMOTE_ADDR_HI (0X0013A200)
BPPearson 0:ceb7120ddb13 12 //#define XBEE_REMOTE_ADDR_LO (0x40B3EB68)
BPPearson 0:ceb7120ddb13 13
BPPearson 0:ceb7120ddb13 14 #define RS422_Underflow 0x01
BPPearson 0:ceb7120ddb13 15 #define RS422_Overflow 0x02
BPPearson 0:ceb7120ddb13 16 #define Data_Overflow 0x04
BPPearson 0:ceb7120ddb13 17 #define No_Peak_Available 0x08
BPPearson 0:ceb7120ddb13 18 #define Peak_Before_Measuring_Range 0x10
BPPearson 0:ceb7120ddb13 19 #define Peak_After_Measuring_Range 0x20
BPPearson 0:ceb7120ddb13 20 #define Measurement_Incalcuable 0x40
BPPearson 0:ceb7120ddb13 21 #define Measurement_Not_Evaluable 0x80
BPPearson 0:ceb7120ddb13 22 #define Peak_Too_Wide 0x100
BPPearson 0:ceb7120ddb13 23 #define Laser_Off 0x200
BPPearson 0:ceb7120ddb13 24 #define ThicknessSensorRangeError 0x400
BPPearson 0:ceb7120ddb13 25
BPPearson 0:ceb7120ddb13 26
BPPearson 0:ceb7120ddb13 27 /******************************************************************************
BPPearson 0:ceb7120ddb13 28 * Local variables
BPPearson 0:ceb7120ddb13 29 *****************************************************************************/
BPPearson 0:ceb7120ddb13 30
BPPearson 0:ceb7120ddb13 31 static XBee xbee(P4_22, P4_23, P4_17, P4_19);
BPPearson 0:ceb7120ddb13 32 static DigitalOut led1(LED1); // led1 + led 2 -> active low
BPPearson 0:ceb7120ddb13 33 static DigitalOut led2(LED2);
BPPearson 0:ceb7120ddb13 34 static DigitalOut led3(LED3); // led3 + led 4 -> active high
BPPearson 0:ceb7120ddb13 35 static DigitalOut led4(LED4);
BPPearson 0:ceb7120ddb13 36 Ticker readInputsTic;
BPPearson 0:ceb7120ddb13 37 Ticker XBeeCommsTic;
BPPearson 0:ceb7120ddb13 38 Ticker ledPulseCheck;
BPPearson 0:ceb7120ddb13 39 Serial temperatureSensor(p9, p10);
BPPearson 0:ceb7120ddb13 40 Serial thicknessSensor(p37,p31);
BPPearson 0:ceb7120ddb13 41 Serial pc(USBTX, USBRX); // tx, rx
BPPearson 0:ceb7120ddb13 42
BPPearson 0:ceb7120ddb13 43 static bool xbeeIsUp = false;
BPPearson 0:ceb7120ddb13 44 static uint32_t endpointAddrHi = 0x0013A200;
BPPearson 1:196b5b827e29 45 static uint32_t endpointAddrLo = 0x40F92FFA;
BPPearson 0:ceb7120ddb13 46 float rawTemperature = 0.0;
BPPearson 0:ceb7120ddb13 47 float rawThickness = 0.0;
BPPearson 0:ceb7120ddb13 48 uint16_t errorFlags = 0;
BPPearson 0:ceb7120ddb13 49 uint32_t packetId = 0;
BPPearson 0:ceb7120ddb13 50 uint8_t tChr0, tChr1, tChr2;
BPPearson 0:ceb7120ddb13 51 uint16_t binaryThickness;
BPPearson 0:ceb7120ddb13 52 bool thicknessReadingToProcess = false;
BPPearson 0:ceb7120ddb13 53 int led1Duration = 0;
BPPearson 0:ceb7120ddb13 54 int led2Duration = 0;
BPPearson 0:ceb7120ddb13 55 int led3Duration = 0;
BPPearson 0:ceb7120ddb13 56 int led4Duration = 0;
BPPearson 0:ceb7120ddb13 57
BPPearson 0:ceb7120ddb13 58
BPPearson 0:ceb7120ddb13 59 void pulseLed(int led, int duration)
BPPearson 0:ceb7120ddb13 60 {
BPPearson 0:ceb7120ddb13 61
BPPearson 0:ceb7120ddb13 62 switch (led)
BPPearson 0:ceb7120ddb13 63 {
BPPearson 0:ceb7120ddb13 64 case LED1:
BPPearson 0:ceb7120ddb13 65 led1 = 0; // turn on led
BPPearson 0:ceb7120ddb13 66
BPPearson 0:ceb7120ddb13 67 led1Duration = duration; // set duration
BPPearson 0:ceb7120ddb13 68 break;
BPPearson 0:ceb7120ddb13 69
BPPearson 0:ceb7120ddb13 70 case LED2:
BPPearson 0:ceb7120ddb13 71 led2 = 0; // turn on led
BPPearson 0:ceb7120ddb13 72
BPPearson 0:ceb7120ddb13 73 led2Duration = duration; // set duration
BPPearson 0:ceb7120ddb13 74 break;
BPPearson 0:ceb7120ddb13 75
BPPearson 0:ceb7120ddb13 76 case LED3:
BPPearson 0:ceb7120ddb13 77 led3 = 1; // turn on led
BPPearson 0:ceb7120ddb13 78
BPPearson 0:ceb7120ddb13 79 led3Duration = duration; // set duration
BPPearson 0:ceb7120ddb13 80 break;
BPPearson 0:ceb7120ddb13 81
BPPearson 0:ceb7120ddb13 82 case LED4:
BPPearson 0:ceb7120ddb13 83 led4 = 1; // turn on led
BPPearson 0:ceb7120ddb13 84
BPPearson 0:ceb7120ddb13 85 led4Duration = duration; // set duration
BPPearson 0:ceb7120ddb13 86 break;
BPPearson 0:ceb7120ddb13 87 default: ;
BPPearson 0:ceb7120ddb13 88 }
BPPearson 0:ceb7120ddb13 89 }
BPPearson 0:ceb7120ddb13 90
BPPearson 0:ceb7120ddb13 91
BPPearson 0:ceb7120ddb13 92
BPPearson 0:ceb7120ddb13 93 void ledPulser()
BPPearson 0:ceb7120ddb13 94 {
BPPearson 0:ceb7120ddb13 95
BPPearson 0:ceb7120ddb13 96 if (led1Duration > 0)
BPPearson 0:ceb7120ddb13 97 {
BPPearson 0:ceb7120ddb13 98 led1Duration--;
BPPearson 0:ceb7120ddb13 99
BPPearson 0:ceb7120ddb13 100 if (led1Duration == 0)
BPPearson 0:ceb7120ddb13 101 led1 = 1; // turn led off
BPPearson 0:ceb7120ddb13 102 }
BPPearson 0:ceb7120ddb13 103
BPPearson 0:ceb7120ddb13 104 if (led2Duration > 0)
BPPearson 0:ceb7120ddb13 105 {
BPPearson 0:ceb7120ddb13 106 led2Duration--;
BPPearson 0:ceb7120ddb13 107
BPPearson 0:ceb7120ddb13 108 if (led2Duration == 0)
BPPearson 0:ceb7120ddb13 109 led2 = 1; // turn led off
BPPearson 0:ceb7120ddb13 110 }
BPPearson 0:ceb7120ddb13 111
BPPearson 0:ceb7120ddb13 112 if (led3Duration > 0)
BPPearson 0:ceb7120ddb13 113 {
BPPearson 0:ceb7120ddb13 114 led3Duration--;
BPPearson 0:ceb7120ddb13 115
BPPearson 0:ceb7120ddb13 116 if (led3Duration == 0)
BPPearson 0:ceb7120ddb13 117 led3 = 0; // turn led off
BPPearson 0:ceb7120ddb13 118 }
BPPearson 0:ceb7120ddb13 119
BPPearson 0:ceb7120ddb13 120 if (led4Duration > 0)
BPPearson 0:ceb7120ddb13 121 {
BPPearson 0:ceb7120ddb13 122 led4Duration--;
BPPearson 0:ceb7120ddb13 123
BPPearson 0:ceb7120ddb13 124 if (led4Duration == 0)
BPPearson 0:ceb7120ddb13 125 led4 = 0; // turn led off
BPPearson 0:ceb7120ddb13 126 }
BPPearson 0:ceb7120ddb13 127 }
BPPearson 0:ceb7120ddb13 128
BPPearson 0:ceb7120ddb13 129
BPPearson 0:ceb7120ddb13 130 static void xbeeDeviceUp(void)
BPPearson 0:ceb7120ddb13 131 {
BPPearson 0:ceb7120ddb13 132 xbeeIsUp = true;
BPPearson 0:ceb7120ddb13 133 }
BPPearson 0:ceb7120ddb13 134
BPPearson 0:ceb7120ddb13 135
BPPearson 0:ceb7120ddb13 136 static void xbeeDeviceDown(void)
BPPearson 0:ceb7120ddb13 137 {
BPPearson 0:ceb7120ddb13 138 xbeeIsUp = false;
BPPearson 0:ceb7120ddb13 139 }
BPPearson 0:ceb7120ddb13 140
BPPearson 0:ceb7120ddb13 141
BPPearson 0:ceb7120ddb13 142 static void xbeeNodeFound(void)
BPPearson 0:ceb7120ddb13 143 {
BPPearson 0:ceb7120ddb13 144 uint32_t addrHi = 0;
BPPearson 0:ceb7120ddb13 145 uint32_t addrLo = 0;
BPPearson 0:ceb7120ddb13 146 uint8_t rssi = 0;
BPPearson 0:ceb7120ddb13 147
BPPearson 0:ceb7120ddb13 148 xbee.getRemoteAddress(&addrHi, &addrLo);
BPPearson 0:ceb7120ddb13 149 xbee.getRssi(&rssi);
BPPearson 0:ceb7120ddb13 150 }
BPPearson 0:ceb7120ddb13 151
BPPearson 0:ceb7120ddb13 152
BPPearson 0:ceb7120ddb13 153 static void xbeeTxStat(void)
BPPearson 0:ceb7120ddb13 154 {
BPPearson 0:ceb7120ddb13 155 uint8_t frameId = 0;
BPPearson 0:ceb7120ddb13 156
BPPearson 0:ceb7120ddb13 157 XBee::XBeeTxStatus status = XBee::TxStatusOk;
BPPearson 0:ceb7120ddb13 158
BPPearson 0:ceb7120ddb13 159 xbee.getTxStatus(&frameId, &status);
BPPearson 0:ceb7120ddb13 160 }
BPPearson 0:ceb7120ddb13 161
BPPearson 0:ceb7120ddb13 162
BPPearson 0:ceb7120ddb13 163
BPPearson 0:ceb7120ddb13 164 static void xbeeDataAvailable(void)
BPPearson 0:ceb7120ddb13 165 {
BPPearson 0:ceb7120ddb13 166 char* data = NULL;
BPPearson 0:ceb7120ddb13 167 uint8_t len = 0;
BPPearson 0:ceb7120ddb13 168 uint32_t addrHi = 0;
BPPearson 0:ceb7120ddb13 169 uint32_t addrLo = 0;
BPPearson 0:ceb7120ddb13 170 uint8_t rssi = 0;
BPPearson 0:ceb7120ddb13 171
BPPearson 0:ceb7120ddb13 172 xbee.getRemoteAddress(&addrHi, &addrLo);
BPPearson 0:ceb7120ddb13 173 xbee.getData(&data, &len);
BPPearson 0:ceb7120ddb13 174 xbee.getRssi(&rssi);
BPPearson 0:ceb7120ddb13 175
BPPearson 0:ceb7120ddb13 176 if (len > 0)
BPPearson 0:ceb7120ddb13 177 {
BPPearson 0:ceb7120ddb13 178 switch(data[0])
BPPearson 0:ceb7120ddb13 179 {
BPPearson 0:ceb7120ddb13 180 }
BPPearson 0:ceb7120ddb13 181
BPPearson 0:ceb7120ddb13 182 }
BPPearson 0:ceb7120ddb13 183
BPPearson 0:ceb7120ddb13 184 }
BPPearson 0:ceb7120ddb13 185
BPPearson 0:ceb7120ddb13 186
BPPearson 0:ceb7120ddb13 187
BPPearson 0:ceb7120ddb13 188 static bool xbeeInit()
BPPearson 0:ceb7120ddb13 189 {
BPPearson 0:ceb7120ddb13 190
BPPearson 0:ceb7120ddb13 191 xbee.registerCallback(xbeeDeviceUp, XBee::CbDeviceUp);
BPPearson 0:ceb7120ddb13 192 xbee.registerCallback(xbeeDeviceDown, XBee::CbDeviceDown);
BPPearson 0:ceb7120ddb13 193 xbee.registerCallback(xbeeNodeFound, XBee::CbNodeFound);
BPPearson 0:ceb7120ddb13 194 xbee.registerCallback(xbeeTxStat, XBee::CbTxStat);
BPPearson 0:ceb7120ddb13 195 xbee.registerCallback(xbeeDataAvailable, XBee::CbDataAvailable);
BPPearson 0:ceb7120ddb13 196 XBee::XBeeError err = xbee.init(XBee::Coordinator, "EAEA");
BPPearson 0:ceb7120ddb13 197
BPPearson 0:ceb7120ddb13 198 if (err != XBee::Ok)
BPPearson 0:ceb7120ddb13 199 {
BPPearson 0:ceb7120ddb13 200 return false;
BPPearson 0:ceb7120ddb13 201 }
BPPearson 0:ceb7120ddb13 202
BPPearson 0:ceb7120ddb13 203 return true;
BPPearson 0:ceb7120ddb13 204 }
BPPearson 0:ceb7120ddb13 205
BPPearson 0:ceb7120ddb13 206
BPPearson 0:ceb7120ddb13 207 static void reportInitFailed()
BPPearson 0:ceb7120ddb13 208 {
BPPearson 0:ceb7120ddb13 209
BPPearson 0:ceb7120ddb13 210 while (true)
BPPearson 0:ceb7120ddb13 211 {
BPPearson 0:ceb7120ddb13 212 led4 = !led4;
BPPearson 0:ceb7120ddb13 213 wait_ms(200);
BPPearson 0:ceb7120ddb13 214 }
BPPearson 0:ceb7120ddb13 215 }
BPPearson 0:ceb7120ddb13 216
BPPearson 0:ceb7120ddb13 217
BPPearson 0:ceb7120ddb13 218 static void ledInit()
BPPearson 0:ceb7120ddb13 219 {
BPPearson 0:ceb7120ddb13 220
BPPearson 0:ceb7120ddb13 221 led1 = 1; // turn off
BPPearson 0:ceb7120ddb13 222 led2 = 1; // turn off
BPPearson 0:ceb7120ddb13 223 led3 = 0; // turn off
BPPearson 0:ceb7120ddb13 224 led4 = 0; // turn off
BPPearson 0:ceb7120ddb13 225 }
BPPearson 0:ceb7120ddb13 226
BPPearson 0:ceb7120ddb13 227
BPPearson 0:ceb7120ddb13 228
BPPearson 0:ceb7120ddb13 229 void readTemperature()
BPPearson 0:ceb7120ddb13 230 {
BPPearson 0:ceb7120ddb13 231
BPPearson 0:ceb7120ddb13 232 char buff[10];
BPPearson 0:ceb7120ddb13 233 int idx = 0;
BPPearson 0:ceb7120ddb13 234
BPPearson 0:ceb7120ddb13 235 temperatureSensor.putc('?'); // send request to Raytek temperature probe
BPPearson 0:ceb7120ddb13 236 temperatureSensor.putc('T');
BPPearson 0:ceb7120ddb13 237 temperatureSensor.putc('\r');
BPPearson 0:ceb7120ddb13 238
BPPearson 0:ceb7120ddb13 239 wait_ms(20); // wait while sensor replies
BPPearson 0:ceb7120ddb13 240
BPPearson 0:ceb7120ddb13 241 while (temperatureSensor.readable() && idx < 10) // get response
BPPearson 0:ceb7120ddb13 242 {
BPPearson 0:ceb7120ddb13 243 buff[idx] = temperatureSensor.getc();
BPPearson 0:ceb7120ddb13 244
BPPearson 0:ceb7120ddb13 245 if (buff[idx] == '\n') //if (buff[idx] == 0x0d)
BPPearson 0:ceb7120ddb13 246 {
BPPearson 0:ceb7120ddb13 247 buff[idx + 1] = NULL;
BPPearson 0:ceb7120ddb13 248
BPPearson 0:ceb7120ddb13 249 break;
BPPearson 0:ceb7120ddb13 250 }
BPPearson 0:ceb7120ddb13 251
BPPearson 0:ceb7120ddb13 252 idx++;
BPPearson 0:ceb7120ddb13 253 }
BPPearson 0:ceb7120ddb13 254
BPPearson 0:ceb7120ddb13 255 if (idx > 0)
BPPearson 0:ceb7120ddb13 256 {
BPPearson 0:ceb7120ddb13 257 sscanf( buff, "!T%f\n", &rawTemperature); // read temperature from data packet
BPPearson 0:ceb7120ddb13 258 }
BPPearson 0:ceb7120ddb13 259 else
BPPearson 0:ceb7120ddb13 260 {
BPPearson 0:ceb7120ddb13 261 pulseLed( LED2, 10); // pulse led2 to error reading from temperature probe
BPPearson 0:ceb7120ddb13 262
BPPearson 0:ceb7120ddb13 263 rawTemperature = -1.0;
BPPearson 0:ceb7120ddb13 264 }
BPPearson 0:ceb7120ddb13 265 }
BPPearson 0:ceb7120ddb13 266
BPPearson 0:ceb7120ddb13 267
BPPearson 0:ceb7120ddb13 268
BPPearson 0:ceb7120ddb13 269 void readThickness(){
BPPearson 0:ceb7120ddb13 270
BPPearson 0:ceb7120ddb13 271
BPPearson 0:ceb7120ddb13 272 if (!thicknessReadingToProcess) // set in serial interrput handler once it has received a valid packet
BPPearson 0:ceb7120ddb13 273 {
BPPearson 0:ceb7120ddb13 274 return;
BPPearson 0:ceb7120ddb13 275 }
BPPearson 0:ceb7120ddb13 276
BPPearson 0:ceb7120ddb13 277 if (binaryThickness <= 642)
BPPearson 0:ceb7120ddb13 278 {
BPPearson 0:ceb7120ddb13 279 errorFlags |= ThicknessSensorRangeError; // set range error bit in error flags
BPPearson 0:ceb7120ddb13 280
BPPearson 0:ceb7120ddb13 281 rawThickness = 0.2; // set a dummy value for the raw thickness
BPPearson 0:ceb7120ddb13 282 }
BPPearson 0:ceb7120ddb13 283 else
BPPearson 0:ceb7120ddb13 284 if (binaryThickness > 642 && binaryThickness < 64877) // check signal within valid range
BPPearson 0:ceb7120ddb13 285 {
BPPearson 0:ceb7120ddb13 286 rawThickness = (((binaryThickness * 1.02 / 65520.0) - 0.01) * 32.0); // calculate the raw thickness from the binary value (calculation in the ILD2300 manual
BPPearson 0:ceb7120ddb13 287
BPPearson 0:ceb7120ddb13 288 if (rawThickness < 0.5F || rawThickness > 3.0F) // if raw thickness outside range
BPPearson 0:ceb7120ddb13 289 {
BPPearson 0:ceb7120ddb13 290 errorFlags |= ThicknessSensorRangeError; // set error bit in error flags
BPPearson 0:ceb7120ddb13 291
BPPearson 0:ceb7120ddb13 292 rawThickness = 0.1; // set default value
BPPearson 0:ceb7120ddb13 293 }
BPPearson 0:ceb7120ddb13 294 }
BPPearson 0:ceb7120ddb13 295 else
BPPearson 0:ceb7120ddb13 296 {
BPPearson 0:ceb7120ddb13 297 pulseLed( LED1, 10); // pulse error led
BPPearson 0:ceb7120ddb13 298
BPPearson 0:ceb7120ddb13 299 switch (binaryThickness - 262073) // subtract error offset to get correct switch case
BPPearson 0:ceb7120ddb13 300 {
BPPearson 0:ceb7120ddb13 301 case 0:
BPPearson 0:ceb7120ddb13 302 errorFlags |= RS422_Underflow;
BPPearson 0:ceb7120ddb13 303 break;
BPPearson 0:ceb7120ddb13 304
BPPearson 0:ceb7120ddb13 305 case 1:
BPPearson 0:ceb7120ddb13 306 errorFlags |= RS422_Overflow;
BPPearson 0:ceb7120ddb13 307 break;
BPPearson 0:ceb7120ddb13 308
BPPearson 0:ceb7120ddb13 309 case 2:
BPPearson 0:ceb7120ddb13 310 errorFlags |= Data_Overflow;
BPPearson 0:ceb7120ddb13 311 break;
BPPearson 0:ceb7120ddb13 312
BPPearson 0:ceb7120ddb13 313 case 3:
BPPearson 0:ceb7120ddb13 314 errorFlags |= No_Peak_Available;
BPPearson 0:ceb7120ddb13 315 break;
BPPearson 0:ceb7120ddb13 316
BPPearson 0:ceb7120ddb13 317 case 4:
BPPearson 0:ceb7120ddb13 318 errorFlags |= Peak_Before_Measuring_Range;
BPPearson 0:ceb7120ddb13 319 break;
BPPearson 0:ceb7120ddb13 320
BPPearson 0:ceb7120ddb13 321 case 5:
BPPearson 0:ceb7120ddb13 322 errorFlags |= Peak_After_Measuring_Range;
BPPearson 0:ceb7120ddb13 323 break;
BPPearson 0:ceb7120ddb13 324
BPPearson 0:ceb7120ddb13 325 case 6:
BPPearson 0:ceb7120ddb13 326 errorFlags |= Measurement_Incalcuable;
BPPearson 0:ceb7120ddb13 327 break;
BPPearson 0:ceb7120ddb13 328
BPPearson 0:ceb7120ddb13 329 case 8:
BPPearson 0:ceb7120ddb13 330 errorFlags |= Measurement_Not_Evaluable;
BPPearson 0:ceb7120ddb13 331 break;
BPPearson 0:ceb7120ddb13 332
BPPearson 0:ceb7120ddb13 333 case 9:
BPPearson 0:ceb7120ddb13 334 errorFlags |= Peak_Too_Wide;
BPPearson 0:ceb7120ddb13 335 break;
BPPearson 0:ceb7120ddb13 336
BPPearson 0:ceb7120ddb13 337 case 10:
BPPearson 0:ceb7120ddb13 338 errorFlags |= Laser_Off;
BPPearson 0:ceb7120ddb13 339 break;
BPPearson 0:ceb7120ddb13 340
BPPearson 0:ceb7120ddb13 341 default:
BPPearson 0:ceb7120ddb13 342 errorFlags |= ThicknessSensorRangeError;
BPPearson 0:ceb7120ddb13 343 rawThickness = 0.0;
BPPearson 0:ceb7120ddb13 344 }
BPPearson 0:ceb7120ddb13 345 }
BPPearson 0:ceb7120ddb13 346
BPPearson 0:ceb7120ddb13 347 thicknessReadingToProcess = false; // clear flag so we know when another packet has been received
BPPearson 0:ceb7120ddb13 348 }
BPPearson 0:ceb7120ddb13 349
BPPearson 0:ceb7120ddb13 350
BPPearson 0:ceb7120ddb13 351
BPPearson 0:ceb7120ddb13 352 void serialIntHandler(){
BPPearson 0:ceb7120ddb13 353
BPPearson 0:ceb7120ddb13 354 uint8_t ch;
BPPearson 0:ceb7120ddb13 355
BPPearson 0:ceb7120ddb13 356 ch = thicknessSensor.getc(); // get char that caused this interrupt
BPPearson 0:ceb7120ddb13 357
BPPearson 0:ceb7120ddb13 358 if (ch < 0x40) // if first byte of packet
BPPearson 0:ceb7120ddb13 359 {
BPPearson 0:ceb7120ddb13 360 tChr0 = ch;
BPPearson 0:ceb7120ddb13 361 return;
BPPearson 0:ceb7120ddb13 362 }
BPPearson 0:ceb7120ddb13 363
BPPearson 0:ceb7120ddb13 364 if ((ch & 0x40) == 0x40) // if second byte of packet
BPPearson 0:ceb7120ddb13 365 {
BPPearson 0:ceb7120ddb13 366 tChr1 = ch & 0x3f;
BPPearson 0:ceb7120ddb13 367 return;
BPPearson 0:ceb7120ddb13 368 }
BPPearson 0:ceb7120ddb13 369
BPPearson 0:ceb7120ddb13 370 if ((ch & 0x80) == 0x80) // if third byte of packet
BPPearson 0:ceb7120ddb13 371 {
BPPearson 0:ceb7120ddb13 372 tChr2 = ch & 0x3f;
BPPearson 0:ceb7120ddb13 373
BPPearson 0:ceb7120ddb13 374 binaryThickness = tChr0 + (tChr1 << 6) + (tChr2 << 12); // build binary thickness value from the 3 characters received
BPPearson 0:ceb7120ddb13 375
BPPearson 0:ceb7120ddb13 376 thicknessReadingToProcess = true; // set the flag to indicate a thickness reading to process
BPPearson 0:ceb7120ddb13 377 }
BPPearson 0:ceb7120ddb13 378 }
BPPearson 0:ceb7120ddb13 379
BPPearson 0:ceb7120ddb13 380
BPPearson 0:ceb7120ddb13 381
BPPearson 0:ceb7120ddb13 382 void readInputs()
BPPearson 0:ceb7120ddb13 383 {
BPPearson 0:ceb7120ddb13 384
BPPearson 0:ceb7120ddb13 385 pulseLed( LED3, 5); // pulse led3 to show periodic read of temperature and thickness
BPPearson 0:ceb7120ddb13 386
BPPearson 0:ceb7120ddb13 387 readTemperature();
BPPearson 0:ceb7120ddb13 388
BPPearson 0:ceb7120ddb13 389 readThickness();
BPPearson 0:ceb7120ddb13 390 }
BPPearson 0:ceb7120ddb13 391
BPPearson 0:ceb7120ddb13 392
BPPearson 0:ceb7120ddb13 393
BPPearson 0:ceb7120ddb13 394
BPPearson 0:ceb7120ddb13 395
BPPearson 0:ceb7120ddb13 396 void xbeeComms()
BPPearson 0:ceb7120ddb13 397 {
BPPearson 0:ceb7120ddb13 398 char data[40];
BPPearson 0:ceb7120ddb13 399 uint8_t frameId = 0;
BPPearson 0:ceb7120ddb13 400
BPPearson 0:ceb7120ddb13 401 if (xbeeIsUp)
BPPearson 0:ceb7120ddb13 402 {
BPPearson 0:ceb7120ddb13 403 pulseLed( LED4, 5); // pulse led4 to show XBee transmission
BPPearson 0:ceb7120ddb13 404
BPPearson 0:ceb7120ddb13 405 sprintf(data, "1%5.3f, %5.3f, %4x\n", rawTemperature, rawThickness, errorFlags); // build data packet
BPPearson 0:ceb7120ddb13 406
BPPearson 0:ceb7120ddb13 407 xbee.send(endpointAddrHi, endpointAddrLo, data, strlen(data), &frameId); // send the temperature and thickness packet to the client
BPPearson 0:ceb7120ddb13 408
BPPearson 0:ceb7120ddb13 409 errorFlags = 0; // clear error flags
BPPearson 0:ceb7120ddb13 410 }
BPPearson 0:ceb7120ddb13 411 }
BPPearson 0:ceb7120ddb13 412
BPPearson 0:ceb7120ddb13 413
BPPearson 0:ceb7120ddb13 414
BPPearson 0:ceb7120ddb13 415
BPPearson 0:ceb7120ddb13 416 int main()
BPPearson 0:ceb7120ddb13 417 {
BPPearson 0:ceb7120ddb13 418
BPPearson 0:ceb7120ddb13 419 ledInit(); // clear all leds
BPPearson 0:ceb7120ddb13 420
BPPearson 0:ceb7120ddb13 421 led1 = 0; // start of initialisation sequence
BPPearson 0:ceb7120ddb13 422 wait_ms(500);
BPPearson 0:ceb7120ddb13 423
BPPearson 0:ceb7120ddb13 424 temperatureSensor.baud(9600); // initialise the serial port for the temperature sensor
BPPearson 0:ceb7120ddb13 425 temperatureSensor.format(8,SerialBase::None,1);
BPPearson 0:ceb7120ddb13 426
BPPearson 0:ceb7120ddb13 427 thicknessSensor.baud(9600); // initialise the serial port for the thickness sensor
BPPearson 0:ceb7120ddb13 428 thicknessSensor.format(8,SerialBase::None,1);
BPPearson 0:ceb7120ddb13 429
BPPearson 0:ceb7120ddb13 430 led2 = 0; // next step of initialisation sequence
BPPearson 0:ceb7120ddb13 431 wait_ms(500);
BPPearson 0:ceb7120ddb13 432
BPPearson 0:ceb7120ddb13 433 if (!xbeeInit())
BPPearson 0:ceb7120ddb13 434 {
BPPearson 0:ceb7120ddb13 435 reportInitFailed();
BPPearson 0:ceb7120ddb13 436 }
BPPearson 0:ceb7120ddb13 437
BPPearson 0:ceb7120ddb13 438 led3 = 1; // next step of initialisation sequence
BPPearson 0:ceb7120ddb13 439 wait_ms(500);
BPPearson 0:ceb7120ddb13 440
BPPearson 0:ceb7120ddb13 441 // Wait until XBee node is reported to be up.
BPPearson 0:ceb7120ddb13 442 // - For End-device this means that the node is associated with a
BPPearson 0:ceb7120ddb13 443 // coordinator
BPPearson 0:ceb7120ddb13 444 // - For a coordinator this means that the node is initialized and ready
BPPearson 0:ceb7120ddb13 445 while(!xbeeIsUp)
BPPearson 0:ceb7120ddb13 446 {
BPPearson 0:ceb7120ddb13 447 xbee.process();
BPPearson 0:ceb7120ddb13 448 }
BPPearson 0:ceb7120ddb13 449
BPPearson 0:ceb7120ddb13 450 led4 = 1; // final step of initialisation sequence
BPPearson 0:ceb7120ddb13 451 wait_ms(500);
BPPearson 0:ceb7120ddb13 452
BPPearson 0:ceb7120ddb13 453 thicknessSensor.attach(&serialIntHandler);
BPPearson 0:ceb7120ddb13 454
BPPearson 0:ceb7120ddb13 455 // start periodic read of sensors
BPPearson 0:ceb7120ddb13 456 readInputsTic.attach(&readInputs, 0.2); // read inputs every 200 mS
BPPearson 0:ceb7120ddb13 457 XBeeCommsTic.attach(&xbeeComms, 0.2); // send to base station every 200 mS
BPPearson 0:ceb7120ddb13 458 ledPulseCheck.attach(ledPulser, 0.01);
BPPearson 0:ceb7120ddb13 459
BPPearson 0:ceb7120ddb13 460 ledInit(); // clear all leds
BPPearson 0:ceb7120ddb13 461
BPPearson 0:ceb7120ddb13 462 while (1)
BPPearson 0:ceb7120ddb13 463 {
BPPearson 0:ceb7120ddb13 464
BPPearson 0:ceb7120ddb13 465 if (xbeeIsUp)
BPPearson 0:ceb7120ddb13 466 {
BPPearson 0:ceb7120ddb13 467
BPPearson 0:ceb7120ddb13 468 }
BPPearson 0:ceb7120ddb13 469
BPPearson 0:ceb7120ddb13 470 xbee.process();
BPPearson 0:ceb7120ddb13 471
BPPearson 0:ceb7120ddb13 472 wait_ms(10);
BPPearson 0:ceb7120ddb13 473 }
BPPearson 0:ceb7120ddb13 474 }