-
Dependents: GPSXbee_1_1 GPSXbee_1_0
Beacon.cpp@1:d4cbfcb2be46, 2014-12-16 (annotated)
- Committer:
- Deurklink
- Date:
- Tue Dec 16 13:00:07 2014 +0000
- Revision:
- 1:d4cbfcb2be46
- Parent:
- 0:8b33ba38cc3a
Version 1.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Deurklink | 0:8b33ba38cc3a | 1 | #include "Beacon.h" |
Deurklink | 0:8b33ba38cc3a | 2 | |
Deurklink | 0:8b33ba38cc3a | 3 | Beacon::Beacon(PinName gpsTx, PinName gpsRx, PinName xbTx, PinName xbRx, int freq, int len) : |
Deurklink | 0:8b33ba38cc3a | 4 | gps(gpsTx, gpsRx), |
Deurklink | 1:d4cbfcb2be46 | 5 | sd(p5, p6, p7, p8, "sd"), |
Deurklink | 0:8b33ba38cc3a | 6 | L1(LED1), |
Deurklink | 0:8b33ba38cc3a | 7 | L2(LED2), |
Deurklink | 0:8b33ba38cc3a | 8 | L3(LED3), |
Deurklink | 0:8b33ba38cc3a | 9 | L4(LED4), |
Deurklink | 0:8b33ba38cc3a | 10 | XBee(xbTx, xbRx), |
Deurklink | 1:d4cbfcb2be46 | 11 | PPS(p30), |
Deurklink | 1:d4cbfcb2be46 | 12 | CD(p25), |
Deurklink | 0:8b33ba38cc3a | 13 | T1(p11), |
Deurklink | 0:8b33ba38cc3a | 14 | T2(p12), |
Deurklink | 0:8b33ba38cc3a | 15 | T3(p15), |
Deurklink | 0:8b33ba38cc3a | 16 | T4(p16), |
Deurklink | 0:8b33ba38cc3a | 17 | T5(p17), |
Deurklink | 0:8b33ba38cc3a | 18 | T6(p18), |
Deurklink | 0:8b33ba38cc3a | 19 | T7(p19), |
Deurklink | 0:8b33ba38cc3a | 20 | T8(p20), |
Deurklink | 0:8b33ba38cc3a | 21 | B1(p24), |
Deurklink | 0:8b33ba38cc3a | 22 | B2(p23), |
Deurklink | 0:8b33ba38cc3a | 23 | B3(p22), |
Deurklink | 0:8b33ba38cc3a | 24 | B4(p21), |
Deurklink | 0:8b33ba38cc3a | 25 | PulseFrequency(freq), |
Deurklink | 0:8b33ba38cc3a | 26 | Pulselength(len) |
Deurklink | 0:8b33ba38cc3a | 27 | { |
Deurklink | 0:8b33ba38cc3a | 28 | PWMon = false; |
Deurklink | 0:8b33ba38cc3a | 29 | RxTO = false; |
Deurklink | 0:8b33ba38cc3a | 30 | PulseFrequency = 25; //Hz |
Deurklink | 0:8b33ba38cc3a | 31 | Pulselength = 1000; //usec |
Deurklink | 1:d4cbfcb2be46 | 32 | SDEnabled = false; |
Deurklink | 0:8b33ba38cc3a | 33 | } |
Deurklink | 0:8b33ba38cc3a | 34 | |
Deurklink | 0:8b33ba38cc3a | 35 | // Beacon initialisation |
Deurklink | 0:8b33ba38cc3a | 36 | bool Beacon::init(){ |
Deurklink | 0:8b33ba38cc3a | 37 | |
Deurklink | 0:8b33ba38cc3a | 38 | // Power down ethernet module |
Deurklink | 0:8b33ba38cc3a | 39 | PHY_PowerDown(); |
Deurklink | 0:8b33ba38cc3a | 40 | // Initialize dipswitches |
Deurklink | 0:8b33ba38cc3a | 41 | initDipSwitch(); |
Deurklink | 0:8b33ba38cc3a | 42 | // Read beacon number and timing offset |
Deurklink | 0:8b33ba38cc3a | 43 | readDipSwitch(); |
Deurklink | 0:8b33ba38cc3a | 44 | |
Deurklink | 1:d4cbfcb2be46 | 45 | SDEnabled = initSD(); |
Deurklink | 0:8b33ba38cc3a | 46 | // Set baud rate |
Deurklink | 0:8b33ba38cc3a | 47 | XBee.baud(9600); |
Deurklink | 0:8b33ba38cc3a | 48 | |
Deurklink | 0:8b33ba38cc3a | 49 | // Initialize GPS unit |
Deurklink | 0:8b33ba38cc3a | 50 | gps.Init(); |
Deurklink | 0:8b33ba38cc3a | 51 | //Attach interrupt to rising edge of GPS PPS pin |
Deurklink | 0:8b33ba38cc3a | 52 | PPS.rise(this, &Beacon::ppsSync); |
Deurklink | 0:8b33ba38cc3a | 53 | |
Deurklink | 0:8b33ba38cc3a | 54 | // Initialize PWM |
Deurklink | 1:d4cbfcb2be46 | 55 | resetPWM(PulseFrequency, Pulselength); |
Deurklink | 0:8b33ba38cc3a | 56 | |
Deurklink | 0:8b33ba38cc3a | 57 | return true; |
Deurklink | 0:8b33ba38cc3a | 58 | } |
Deurklink | 0:8b33ba38cc3a | 59 | |
Deurklink | 0:8b33ba38cc3a | 60 | |
Deurklink | 0:8b33ba38cc3a | 61 | // PPS pin interrupt |
Deurklink | 0:8b33ba38cc3a | 62 | void Beacon::ppsSync(){ |
Deurklink | 0:8b33ba38cc3a | 63 | // Toggle LED1 |
Deurklink | 0:8b33ba38cc3a | 64 | L1 = !L1; |
Deurklink | 0:8b33ba38cc3a | 65 | if (PWMon == true){ |
Deurklink | 0:8b33ba38cc3a | 66 | // Reset PWM timer value so that new cycle immediately starts |
Deurklink | 0:8b33ba38cc3a | 67 | LPC_PWM1->TC = Period*24+TimingOffset - 1; |
Deurklink | 0:8b33ba38cc3a | 68 | } |
Deurklink | 0:8b33ba38cc3a | 69 | } |
Deurklink | 0:8b33ba38cc3a | 70 | |
Deurklink | 0:8b33ba38cc3a | 71 | |
Deurklink | 0:8b33ba38cc3a | 72 | // XBee Receive Timeout |
Deurklink | 0:8b33ba38cc3a | 73 | void Beacon::rxTimeOut(){ |
Deurklink | 0:8b33ba38cc3a | 74 | RxTO = true; |
Deurklink | 0:8b33ba38cc3a | 75 | } |
Deurklink | 0:8b33ba38cc3a | 76 | |
Deurklink | 0:8b33ba38cc3a | 77 | |
Deurklink | 0:8b33ba38cc3a | 78 | //------------------------------------DIP-SWITCHES--------------------------- |
Deurklink | 0:8b33ba38cc3a | 79 | void Beacon::readDipSwitch(){ |
Deurklink | 0:8b33ba38cc3a | 80 | // Transform the dip switch binary numbers into integers |
Deurklink | 0:8b33ba38cc3a | 81 | // For the timing offset, the leftmost bit is the sign bit (2's complement) |
Deurklink | 0:8b33ba38cc3a | 82 | TimingOffset = -128*T1 + 64*T2 + 32*T3 + 16*T4 + 8*T5 + 4*T6 + 2*T7 + T8; |
Deurklink | 0:8b33ba38cc3a | 83 | BeaconNumber = 8*B1 + 4*B2 + 2*B3 + B4; |
Deurklink | 0:8b33ba38cc3a | 84 | } |
Deurklink | 0:8b33ba38cc3a | 85 | |
Deurklink | 0:8b33ba38cc3a | 86 | void Beacon::initDipSwitch(){ |
Deurklink | 0:8b33ba38cc3a | 87 | // Set pull-down resistor for dip switch inputs |
Deurklink | 0:8b33ba38cc3a | 88 | T1.mode(PullDown); |
Deurklink | 0:8b33ba38cc3a | 89 | T2.mode(PullDown); |
Deurklink | 0:8b33ba38cc3a | 90 | T3.mode(PullDown); |
Deurklink | 0:8b33ba38cc3a | 91 | T4.mode(PullDown); |
Deurklink | 0:8b33ba38cc3a | 92 | T5.mode(PullDown); |
Deurklink | 0:8b33ba38cc3a | 93 | T6.mode(PullDown); |
Deurklink | 0:8b33ba38cc3a | 94 | T7.mode(PullDown); |
Deurklink | 0:8b33ba38cc3a | 95 | T8.mode(PullDown); |
Deurklink | 0:8b33ba38cc3a | 96 | |
Deurklink | 0:8b33ba38cc3a | 97 | B1.mode(PullDown); |
Deurklink | 0:8b33ba38cc3a | 98 | B2.mode(PullDown); |
Deurklink | 0:8b33ba38cc3a | 99 | B3.mode(PullDown); |
Deurklink | 0:8b33ba38cc3a | 100 | B4.mode(PullDown); |
Deurklink | 0:8b33ba38cc3a | 101 | } |
Deurklink | 0:8b33ba38cc3a | 102 | //--------------------------------------------------------------------------- |
Deurklink | 0:8b33ba38cc3a | 103 | |
Deurklink | 0:8b33ba38cc3a | 104 | |
Deurklink | 1:d4cbfcb2be46 | 105 | |
Deurklink | 1:d4cbfcb2be46 | 106 | //------------------------------------SD------------------------------------ |
Deurklink | 1:d4cbfcb2be46 | 107 | bool Beacon::initSD(){ |
Deurklink | 1:d4cbfcb2be46 | 108 | CD.mode(PullUp); |
Deurklink | 1:d4cbfcb2be46 | 109 | |
Deurklink | 1:d4cbfcb2be46 | 110 | if (CD == 1) { |
Deurklink | 1:d4cbfcb2be46 | 111 | L3 = 1; |
Deurklink | 1:d4cbfcb2be46 | 112 | FILE *fp = fopen("/sd/log.txt", "a"); |
Deurklink | 1:d4cbfcb2be46 | 113 | if(fp == NULL) { |
Deurklink | 1:d4cbfcb2be46 | 114 | fclose(fp); |
Deurklink | 1:d4cbfcb2be46 | 115 | return false; |
Deurklink | 1:d4cbfcb2be46 | 116 | } |
Deurklink | 1:d4cbfcb2be46 | 117 | else{ |
Deurklink | 1:d4cbfcb2be46 | 118 | fprintf(fp,"\r\nSTART LOG"); |
Deurklink | 1:d4cbfcb2be46 | 119 | fclose(fp); |
Deurklink | 1:d4cbfcb2be46 | 120 | L4 = 1; |
Deurklink | 1:d4cbfcb2be46 | 121 | return true; |
Deurklink | 1:d4cbfcb2be46 | 122 | } |
Deurklink | 1:d4cbfcb2be46 | 123 | } |
Deurklink | 1:d4cbfcb2be46 | 124 | else{ |
Deurklink | 1:d4cbfcb2be46 | 125 | return false; |
Deurklink | 1:d4cbfcb2be46 | 126 | } |
Deurklink | 1:d4cbfcb2be46 | 127 | } |
Deurklink | 1:d4cbfcb2be46 | 128 | |
Deurklink | 1:d4cbfcb2be46 | 129 | //--------------------------------------------------------------------------- |
Deurklink | 1:d4cbfcb2be46 | 130 | |
Deurklink | 1:d4cbfcb2be46 | 131 | |
Deurklink | 1:d4cbfcb2be46 | 132 | |
Deurklink | 0:8b33ba38cc3a | 133 | //------------------------------------PWM------------------------------------ |
Deurklink | 0:8b33ba38cc3a | 134 | void Beacon::startPWM(){ |
Deurklink | 0:8b33ba38cc3a | 135 | // enable PWM mode and counting |
Deurklink | 0:8b33ba38cc3a | 136 | LPC_PWM1->TCR = (1 << 3) | 1; |
Deurklink | 0:8b33ba38cc3a | 137 | PWMon = true; |
Deurklink | 0:8b33ba38cc3a | 138 | } |
Deurklink | 0:8b33ba38cc3a | 139 | |
Deurklink | 0:8b33ba38cc3a | 140 | void Beacon::stopPWM(){ |
Deurklink | 0:8b33ba38cc3a | 141 | // Reset PWM-counter; this also stops it |
Deurklink | 0:8b33ba38cc3a | 142 | LPC_PWM1->TCR = 2; |
Deurklink | 0:8b33ba38cc3a | 143 | PWMon = false; |
Deurklink | 0:8b33ba38cc3a | 144 | } |
Deurklink | 0:8b33ba38cc3a | 145 | |
Deurklink | 0:8b33ba38cc3a | 146 | void Beacon::resetPWM(int _PulseFrequency, int _Pulselength){ |
Deurklink | 0:8b33ba38cc3a | 147 | // Note: wherever you read pin P2.0, it is pin 2[0] of the microcontroller. |
Deurklink | 0:8b33ba38cc3a | 148 | // This is wired to p26 of the mbed board. |
Deurklink | 0:8b33ba38cc3a | 149 | stopPWM(); |
Deurklink | 0:8b33ba38cc3a | 150 | // First stop PWM if it is running |
Deurklink | 0:8b33ba38cc3a | 151 | |
Deurklink | 0:8b33ba38cc3a | 152 | PWMclock = SystemCoreClock / 96; |
Deurklink | 0:8b33ba38cc3a | 153 | Period = PWMclock / _PulseFrequency; |
Deurklink | 0:8b33ba38cc3a | 154 | |
Deurklink | 0:8b33ba38cc3a | 155 | |
Deurklink | 0:8b33ba38cc3a | 156 | LPC_SC->PCONP |= 1 << 6; |
Deurklink | 0:8b33ba38cc3a | 157 | // Reset mode PWM timer |
Deurklink | 0:8b33ba38cc3a | 158 | LPC_PWM1->TCR = 2; |
Deurklink | 0:8b33ba38cc3a | 159 | |
Deurklink | 0:8b33ba38cc3a | 160 | // configure P2.0 for PWM1.1 - O - Pulse Width Modulator 1, channel 1 output. |
Deurklink | 0:8b33ba38cc3a | 161 | LPC_PINCON->PINSEL4 = (LPC_PINCON->PINSEL4 & ~(0x3 << 0)) | (0x1 << 0); |
Deurklink | 0:8b33ba38cc3a | 162 | |
Deurklink | 0:8b33ba38cc3a | 163 | // Disable pullups for P2.0 |
Deurklink | 0:8b33ba38cc3a | 164 | LPC_PINCON->PINMODE4 = (LPC_PINCON->PINMODE4 & ~0x3) | 0x2; |
Deurklink | 0:8b33ba38cc3a | 165 | |
Deurklink | 0:8b33ba38cc3a | 166 | // Set prescaler |
Deurklink | 0:8b33ba38cc3a | 167 | //LPC_PWM1->PR = SystemCoreClock / (4 * 1000000) - 1; |
Deurklink | 0:8b33ba38cc3a | 168 | |
Deurklink | 0:8b33ba38cc3a | 169 | // Set up match registers |
Deurklink | 0:8b33ba38cc3a | 170 | LPC_PWM1->MR0 = Period*24+TimingOffset; // Set MR0 (Period) |
Deurklink | 0:8b33ba38cc3a | 171 | /* |
Deurklink | 0:8b33ba38cc3a | 172 | TimingOffset is there to compensate for crystal errors. |
Deurklink | 0:8b33ba38cc3a | 173 | It has to be calculated for every mbed, as every crystal is a little different |
Deurklink | 0:8b33ba38cc3a | 174 | It basically adds or detracts a few cycles from every period to keep the period equal for every module |
Deurklink | 0:8b33ba38cc3a | 175 | */ |
Deurklink | 0:8b33ba38cc3a | 176 | LPC_PWM1->MR1 = _Pulselength*24; // Set MR1 (Pulse length) |
Deurklink | 0:8b33ba38cc3a | 177 | LPC_PWM1->LER = 0x07; // set latch-enable register |
Deurklink | 0:8b33ba38cc3a | 178 | LPC_PWM1->MCR |= 0x02; // Reset PWM1 on match |
Deurklink | 0:8b33ba38cc3a | 179 | LPC_PWM1->PCR = 1 << 9; // enable PWM1 with single-edge operation |
Deurklink | 0:8b33ba38cc3a | 180 | } |
Deurklink | 0:8b33ba38cc3a | 181 | //------------------------------------/PWM------------------------------------ |
Deurklink | 0:8b33ba38cc3a | 182 | |
Deurklink | 0:8b33ba38cc3a | 183 | |
Deurklink | 0:8b33ba38cc3a | 184 | |
Deurklink | 0:8b33ba38cc3a | 185 | //------------------------------------XBee------------------------------------ |
Deurklink | 0:8b33ba38cc3a | 186 | bool Beacon::xbeeReadable(){ |
Deurklink | 0:8b33ba38cc3a | 187 | return XBee.readable(); |
Deurklink | 0:8b33ba38cc3a | 188 | } |
Deurklink | 0:8b33ba38cc3a | 189 | |
Deurklink | 0:8b33ba38cc3a | 190 | void Beacon::printStatus(){ |
Deurklink | 0:8b33ba38cc3a | 191 | // Print the status of a Beacon |
Deurklink | 0:8b33ba38cc3a | 192 | XBee.printf("\r\n\nBeacon (ID:%d); TimingOffset: %d",BeaconNumber, TimingOffset); |
Deurklink | 1:d4cbfcb2be46 | 193 | XBee.printf("\r\nLogging enabled: %d (0=off,1=on)",SDEnabled); |
Deurklink | 0:8b33ba38cc3a | 194 | XBee.printf("\r\nLast GPS update: %d:%d:%2.2f",gps.hours, gps.minutes, gps.seconds); |
Deurklink | 0:8b33ba38cc3a | 195 | XBee.printf("\r\nFlash frequency: %d Hz; Pulse length: %d usec", PulseFrequency, Pulselength); |
Deurklink | 0:8b33ba38cc3a | 196 | XBee.printf("\r\nPulse generation: %d (0=off,1=on)",PWMon); |
Deurklink | 0:8b33ba38cc3a | 197 | XBee.printf("\r\nGPS location: %f,%f", gps.latitude, gps.longitude); |
Deurklink | 0:8b33ba38cc3a | 198 | XBee.printf("\r\nFixtype: %d (0=no fix,1=gps,2=differential)",gps.fixtype); |
Deurklink | 0:8b33ba38cc3a | 199 | XBee.printf("\r\nNo. of satellites: %d",gps.satellites); |
Deurklink | 0:8b33ba38cc3a | 200 | } |
Deurklink | 0:8b33ba38cc3a | 201 | |
Deurklink | 0:8b33ba38cc3a | 202 | void Beacon::getXbeeData(){ |
Deurklink | 0:8b33ba38cc3a | 203 | // Function used by parse data function |
Deurklink | 0:8b33ba38cc3a | 204 | // Read data from the Xbee serial Buffer |
Deurklink | 0:8b33ba38cc3a | 205 | // Wait until $ is read, then put characters received after that in RxBuffer[] until # is read. |
Deurklink | 0:8b33ba38cc3a | 206 | |
Deurklink | 0:8b33ba38cc3a | 207 | RxTO = false; // Reset time-out |
Deurklink | 0:8b33ba38cc3a | 208 | bool dollarSignRcv = false; |
Deurklink | 0:8b33ba38cc3a | 209 | bool dashRcv = false; |
Deurklink | 0:8b33ba38cc3a | 210 | |
Deurklink | 0:8b33ba38cc3a | 211 | _RxTimeOut.attach(this,&Beacon::rxTimeOut,XBEETIMEOUT); // timeout after XBEETIMEOUT seconds |
Deurklink | 0:8b33ba38cc3a | 212 | |
Deurklink | 0:8b33ba38cc3a | 213 | while(!dollarSignRcv){ |
Deurklink | 0:8b33ba38cc3a | 214 | // Read XBee until dollarsign received or timeout |
Deurklink | 0:8b33ba38cc3a | 215 | if (XBee.readable()){ |
Deurklink | 0:8b33ba38cc3a | 216 | if (XBee.getc() == '$'){ |
Deurklink | 0:8b33ba38cc3a | 217 | dollarSignRcv = true; |
Deurklink | 0:8b33ba38cc3a | 218 | //XBee.printf("$ received"); |
Deurklink | 0:8b33ba38cc3a | 219 | } |
Deurklink | 0:8b33ba38cc3a | 220 | } |
Deurklink | 0:8b33ba38cc3a | 221 | // Timeout if no $ is received within XBEETIMEOUT seconds |
Deurklink | 0:8b33ba38cc3a | 222 | if (RxTO == true){ |
Deurklink | 0:8b33ba38cc3a | 223 | wait(BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 224 | XBee.printf("\r\nBeacon (ID:%d) rx time out",BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 225 | return; |
Deurklink | 0:8b33ba38cc3a | 226 | } |
Deurklink | 0:8b33ba38cc3a | 227 | } |
Deurklink | 0:8b33ba38cc3a | 228 | |
Deurklink | 0:8b33ba38cc3a | 229 | int i = 0; |
Deurklink | 0:8b33ba38cc3a | 230 | while(!dashRcv){ |
Deurklink | 0:8b33ba38cc3a | 231 | // Fill buffer until dash is received, buffer is full or timeout |
Deurklink | 0:8b33ba38cc3a | 232 | if (XBee.readable()){ |
Deurklink | 0:8b33ba38cc3a | 233 | RxBuffer[i] = XBee.getc(); |
Deurklink | 0:8b33ba38cc3a | 234 | if(RxBuffer[i] == '#') { |
Deurklink | 0:8b33ba38cc3a | 235 | RxBuffer[i] = 0; |
Deurklink | 0:8b33ba38cc3a | 236 | dashRcv = true; |
Deurklink | 0:8b33ba38cc3a | 237 | //XBee.printf("# received"); |
Deurklink | 0:8b33ba38cc3a | 238 | } |
Deurklink | 0:8b33ba38cc3a | 239 | i++; |
Deurklink | 0:8b33ba38cc3a | 240 | } |
Deurklink | 0:8b33ba38cc3a | 241 | // Return if buffer is full |
Deurklink | 0:8b33ba38cc3a | 242 | if (i > 255){ |
Deurklink | 0:8b33ba38cc3a | 243 | wait(BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 244 | XBee.printf("\r\nBeacon (ID:%d) rx buffer overflow",BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 245 | return; |
Deurklink | 0:8b33ba38cc3a | 246 | } |
Deurklink | 0:8b33ba38cc3a | 247 | // Timeout if no '#' is received within XBEETIMEOUT seconds |
Deurklink | 0:8b33ba38cc3a | 248 | if (RxTO == true){ |
Deurklink | 0:8b33ba38cc3a | 249 | wait(BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 250 | XBee.printf("\r\nBeacon (ID:%d) rx time out",BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 251 | XBee.printf("\r\nReceived: %s",RxBuffer); |
Deurklink | 0:8b33ba38cc3a | 252 | return; |
Deurklink | 0:8b33ba38cc3a | 253 | } |
Deurklink | 0:8b33ba38cc3a | 254 | } |
Deurklink | 0:8b33ba38cc3a | 255 | } |
Deurklink | 0:8b33ba38cc3a | 256 | |
Deurklink | 0:8b33ba38cc3a | 257 | void Beacon::parseXbee(){ |
Deurklink | 0:8b33ba38cc3a | 258 | // This function collects data from RxBuffer[] and reads it |
Deurklink | 0:8b33ba38cc3a | 259 | |
Deurklink | 0:8b33ba38cc3a | 260 | // put data into RxBuffer[] |
Deurklink | 0:8b33ba38cc3a | 261 | getXbeeData(); |
Deurklink | 0:8b33ba38cc3a | 262 | |
Deurklink | 0:8b33ba38cc3a | 263 | // $STATUS# |
Deurklink | 0:8b33ba38cc3a | 264 | // Send a status message |
Deurklink | 0:8b33ba38cc3a | 265 | if (strncmp(RxBuffer, "STATUS",6) == 0){ |
Deurklink | 0:8b33ba38cc3a | 266 | wait(BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 267 | printStatus(); |
Deurklink | 0:8b33ba38cc3a | 268 | } |
Deurklink | 0:8b33ba38cc3a | 269 | |
Deurklink | 0:8b33ba38cc3a | 270 | // $STARTPULSE# |
Deurklink | 0:8b33ba38cc3a | 271 | // Start pulse generation if there is a GPS fix |
Deurklink | 0:8b33ba38cc3a | 272 | else if (strncmp(RxBuffer, "STARTPULSE",10) == 0){ |
Deurklink | 0:8b33ba38cc3a | 273 | if (gps.fixtype > 0){ |
Deurklink | 0:8b33ba38cc3a | 274 | startPWM(); |
Deurklink | 0:8b33ba38cc3a | 275 | wait(BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 276 | XBee.printf("\r\nBeacon (ID:%d) pulses started",BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 277 | } |
Deurklink | 0:8b33ba38cc3a | 278 | else{ |
Deurklink | 0:8b33ba38cc3a | 279 | wait(BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 280 | XBee.printf("\r\nBeacon (ID:%d) has no GPS fix",BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 281 | } |
Deurklink | 0:8b33ba38cc3a | 282 | } |
Deurklink | 0:8b33ba38cc3a | 283 | |
Deurklink | 0:8b33ba38cc3a | 284 | // $FORCEPULSE# |
Deurklink | 0:8b33ba38cc3a | 285 | // Start pulse generation regardless of GPS fix |
Deurklink | 0:8b33ba38cc3a | 286 | else if (strncmp(RxBuffer, "FORCEPULSE",10) == 0){ |
Deurklink | 0:8b33ba38cc3a | 287 | startPWM(); |
Deurklink | 0:8b33ba38cc3a | 288 | if (gps.fixtype > 0){ |
Deurklink | 0:8b33ba38cc3a | 289 | wait(BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 290 | XBee.printf("\r\nBeacon (ID:%d) pulses started",BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 291 | } |
Deurklink | 0:8b33ba38cc3a | 292 | else{ |
Deurklink | 0:8b33ba38cc3a | 293 | wait(BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 294 | XBee.printf("\r\nBeacon (ID:%d) has no GPS fix; pulses started anyway",BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 295 | } |
Deurklink | 0:8b33ba38cc3a | 296 | } |
Deurklink | 0:8b33ba38cc3a | 297 | |
Deurklink | 0:8b33ba38cc3a | 298 | // $STOPPULSE# |
Deurklink | 0:8b33ba38cc3a | 299 | // Stop pulse generation |
Deurklink | 0:8b33ba38cc3a | 300 | else if (strncmp(RxBuffer, "STOPPULSE",9) == 0){ |
Deurklink | 0:8b33ba38cc3a | 301 | if (PWMon == true){ |
Deurklink | 0:8b33ba38cc3a | 302 | stopPWM(); |
Deurklink | 0:8b33ba38cc3a | 303 | wait(BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 304 | XBee.printf("\r\nBeacon (ID:%d) pulses stopped",BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 305 | } |
Deurklink | 0:8b33ba38cc3a | 306 | else{ |
Deurklink | 0:8b33ba38cc3a | 307 | wait(BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 308 | XBee.printf("\r\nBeacon (ID:%d) has not been started yet. No action",BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 309 | } |
Deurklink | 0:8b33ba38cc3a | 310 | } |
Deurklink | 0:8b33ba38cc3a | 311 | |
Deurklink | 0:8b33ba38cc3a | 312 | // $CHANGEPULSE,freq(hz),pulselength(us)# |
Deurklink | 0:8b33ba38cc3a | 313 | // Change frequency and pulse length |
Deurklink | 0:8b33ba38cc3a | 314 | else if (strncmp(RxBuffer, "CHANGEPULSE",11) == 0){ |
Deurklink | 0:8b33ba38cc3a | 315 | if(sscanf(RxBuffer, "%*s %d %d", &PulseFrequency, &Pulselength) ==2){ |
Deurklink | 0:8b33ba38cc3a | 316 | resetPWM(PulseFrequency, Pulselength); |
Deurklink | 0:8b33ba38cc3a | 317 | wait(BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 318 | XBee.printf("\r\nBeacon (ID:%d) frequency set to: %d Hz. Pulselength set to %d us",BeaconNumber,PulseFrequency,Pulselength); |
Deurklink | 0:8b33ba38cc3a | 319 | XBee.printf("\r\nWaiting for start pulse command"); |
Deurklink | 0:8b33ba38cc3a | 320 | } |
Deurklink | 0:8b33ba38cc3a | 321 | else{ |
Deurklink | 0:8b33ba38cc3a | 322 | wait(BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 323 | XBee.printf("\r\nBeacon (ID:%d) pulselength or frequency not received correctly, please try again (CHANGEPULSE,freq,pulselength)",BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 324 | } |
Deurklink | 0:8b33ba38cc3a | 325 | } |
Deurklink | 0:8b33ba38cc3a | 326 | |
Deurklink | 0:8b33ba38cc3a | 327 | // $CAMCHANGEPULSE,freq(hz),pulselength(us),delay(us)# |
Deurklink | 0:8b33ba38cc3a | 328 | // Change frequency and pulse length of camera; Ignored by beacon |
Deurklink | 0:8b33ba38cc3a | 329 | else if (strncmp(RxBuffer, "CAMCHANGEPULSE",14) == 0){ |
Deurklink | 0:8b33ba38cc3a | 330 | wait(BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 331 | XBee.printf("\r\nBeacon (ID:%d) ignored message",BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 332 | } |
Deurklink | 0:8b33ba38cc3a | 333 | |
Deurklink | 0:8b33ba38cc3a | 334 | |
Deurklink | 0:8b33ba38cc3a | 335 | // $RESETPULSE# |
Deurklink | 0:8b33ba38cc3a | 336 | // Read the dip switches, stop and reset PWM with new timing offset |
Deurklink | 0:8b33ba38cc3a | 337 | else if (strncmp(RxBuffer, "RESETPULSE",10) == 0){ |
Deurklink | 0:8b33ba38cc3a | 338 | readDipSwitch(); |
Deurklink | 0:8b33ba38cc3a | 339 | resetPWM(PulseFrequency, Pulselength); |
Deurklink | 0:8b33ba38cc3a | 340 | wait(BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 341 | XBee.printf("\r\nBeacon (ID:%d) PWM reset",BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 342 | } |
Deurklink | 0:8b33ba38cc3a | 343 | |
Deurklink | 0:8b33ba38cc3a | 344 | // Received message was not one of the above commands |
Deurklink | 0:8b33ba38cc3a | 345 | else{ |
Deurklink | 0:8b33ba38cc3a | 346 | wait(BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 347 | XBee.printf("\r\nBeacon (ID:%d) did not understand message",BeaconNumber); |
Deurklink | 0:8b33ba38cc3a | 348 | XBee.printf("\r\nReceived: %s",RxBuffer); |
Deurklink | 0:8b33ba38cc3a | 349 | } |
Deurklink | 1:d4cbfcb2be46 | 350 | |
Deurklink | 1:d4cbfcb2be46 | 351 | if (SDEnabled){ |
Deurklink | 1:d4cbfcb2be46 | 352 | FILE *fp = fopen("/sd/log.txt", "a"); |
Deurklink | 1:d4cbfcb2be46 | 353 | fprintf(fp,"\r\n%d:%d:%2.2f Received: %s",gps.hours, gps.minutes, gps.seconds, RxBuffer); |
Deurklink | 1:d4cbfcb2be46 | 354 | fclose(fp); |
Deurklink | 1:d4cbfcb2be46 | 355 | } |
Deurklink | 1:d4cbfcb2be46 | 356 | |
Deurklink | 1:d4cbfcb2be46 | 357 | |
Deurklink | 0:8b33ba38cc3a | 358 | // Flush buffer |
Deurklink | 0:8b33ba38cc3a | 359 | for (int i=0;i<255;i++){ |
Deurklink | 0:8b33ba38cc3a | 360 | RxBuffer[i] = '0'; |
Deurklink | 0:8b33ba38cc3a | 361 | } |
Deurklink | 0:8b33ba38cc3a | 362 | } |
Deurklink | 0:8b33ba38cc3a | 363 | //-----------------------------------/XBee------------------------------------ |
Deurklink | 0:8b33ba38cc3a | 364 | |
Deurklink | 0:8b33ba38cc3a | 365 | //------------------------------------GPS------------------------------------- |
Deurklink | 0:8b33ba38cc3a | 366 | bool Beacon::gpsReadable(){ |
Deurklink | 0:8b33ba38cc3a | 367 | return gps.readable(); |
Deurklink | 0:8b33ba38cc3a | 368 | } |
Deurklink | 0:8b33ba38cc3a | 369 | |
Deurklink | 0:8b33ba38cc3a | 370 | void Beacon::parseGpsData(){ |
Deurklink | 0:8b33ba38cc3a | 371 | gps.parseData(); |
Deurklink | 0:8b33ba38cc3a | 372 | } |
Deurklink | 0:8b33ba38cc3a | 373 | //-----------------------------------/GPS------------------------------------- |