SSI OpComms 3CM Board RX

Dependencies:   mbed

Fork of Optical3cmRX by Thomas Teisberg

Committer:
bhz
Date:
Sun Nov 08 02:36:11 2015 +0000
Revision:
4:037345932888
Parent:
3:1235cbe67ec9
Child:
5:fc3ce1c5db88
Working on 1ms pulse length

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:879aa9d0247b 1 #include "mbed.h"
tteisberg 1:b446f401a861 2
tteisberg 1:b446f401a861 3 AnalogIn rx(p20);
tteisberg 1:b446f401a861 4
mbed_official 0:879aa9d0247b 5 Serial pc(USBTX, USBRX); // tx, rx
tteisberg 1:b446f401a861 6
bhz 4:037345932888 7 static const int PPM_BITS = 2;
bhz 4:037345932888 8 static const int N_PPM = 1 << PPM_BITS;
bhz 4:037345932888 9 static const int PULSE_TIME = 1000;
bhz 3:1235cbe67ec9 10 static const int SAMPLE_RATE = 10; // must be a divisor of PULSE_TIME
bhz 2:eb447fcd8284 11 static const int SAMPLE_GAP = PULSE_TIME / SAMPLE_RATE;
bhz 2:eb447fcd8284 12 static const int HALF_SAMPLE = SAMPLE_RATE / 2;
bhz 2:eb447fcd8284 13 static const int BUF_SIZE = 1024;
bhz 2:eb447fcd8284 14 static const float THRESH = 0.005;
bhz 2:eb447fcd8284 15
bhz 2:eb447fcd8284 16 int buf[BUF_SIZE];
tteisberg 1:b446f401a861 17 int idx = 0;
tteisberg 1:b446f401a861 18 int nzeros = 0;
bhz 2:eb447fcd8284 19 int start = 0;
bhz 2:eb447fcd8284 20 bool sample_lock = false;
bhz 2:eb447fcd8284 21 int offCount, onCount;
tteisberg 1:b446f401a861 22
bhz 4:037345932888 23 void printDecode(int stopIdx){
bhz 4:037345932888 24
bhz 2:eb447fcd8284 25 int len = (stopIdx - start + BUF_SIZE) % BUF_SIZE;
bhz 4:037345932888 26 int size = PPM_BITS * len / 8 + 1;
bhz 4:037345932888 27 char data[size];
bhz 4:037345932888 28 for(int i = 0; i < size; i++) data[i] = 0;
bhz 4:037345932888 29 //pc.printf("\r\nDecode: ");
bhz 4:037345932888 30 int k = 0;
bhz 2:eb447fcd8284 31 for(int i = 0; i < len; i++)
bhz 2:eb447fcd8284 32 {
bhz 2:eb447fcd8284 33 int c = buf[start++];
bhz 2:eb447fcd8284 34 if(start == BUF_SIZE) start = 0;
bhz 4:037345932888 35 for(int j = 0; j < PPM_BITS; j++)
bhz 2:eb447fcd8284 36 {
bhz 4:037345932888 37 bool bit = (c >> (PPM_BITS - j - 1)) & 1;
bhz 4:037345932888 38 if(k%8 == 0) data[k/8] = 0;
bhz 4:037345932888 39 data[k/8] |= bit << (7 - k%8);
bhz 4:037345932888 40 k++;
bhz 4:037345932888 41 //pc.printf("%d", bit);
bhz 2:eb447fcd8284 42 }
bhz 2:eb447fcd8284 43 }
bhz 4:037345932888 44 pc.printf("Received: \"");
bhz 4:037345932888 45 pc.printf(data);
bhz 4:037345932888 46 pc.printf("\"\r\n");
bhz 4:037345932888 47 if(start != stopIdx)
bhz 4:037345932888 48 {
bhz 4:037345932888 49 pc.printf("\r\nWarning: Incomplete byte\r\n");
bhz 4:037345932888 50 }
bhz 2:eb447fcd8284 51 }
tteisberg 1:b446f401a861 52
bhz 2:eb447fcd8284 53 void sample()
bhz 2:eb447fcd8284 54 {
bhz 2:eb447fcd8284 55 if(sample_lock) pc.printf("\r\nWarning: Sampling too fast\r\n");
bhz 2:eb447fcd8284 56 sample_lock = true;
bhz 2:eb447fcd8284 57 float v = rx.read();
bhz 3:1235cbe67ec9 58 if(v < THRESH){
bhz 2:eb447fcd8284 59 offCount++;
bhz 3:1235cbe67ec9 60 if(onCount > HALF_SAMPLE)
bhz 3:1235cbe67ec9 61 {
bhz 3:1235cbe67ec9 62 onCount = 0;
bhz 3:1235cbe67ec9 63 }
bhz 2:eb447fcd8284 64 }else{ // Pulse
bhz 3:1235cbe67ec9 65 if(offCount > HALF_SAMPLE)
bhz 2:eb447fcd8284 66 {
bhz 3:1235cbe67ec9 67 int offPulses = (offCount + HALF_SAMPLE) / SAMPLE_RATE - 1;
bhz 2:eb447fcd8284 68 if(offPulses <= N_PPM)
bhz 2:eb447fcd8284 69 {
bhz 4:037345932888 70 //pc.printf("%d ", offPulses);
bhz 3:1235cbe67ec9 71 buf[idx] = offPulses;
bhz 2:eb447fcd8284 72 idx++;
bhz 2:eb447fcd8284 73 if(idx == BUF_SIZE) idx = 0;
bhz 2:eb447fcd8284 74 if(idx == start) pc.printf("\r\nWarning: Buffer too small\r\n");
bhz 2:eb447fcd8284 75 }
bhz 2:eb447fcd8284 76 offCount = 0;
bhz 2:eb447fcd8284 77 }
bhz 2:eb447fcd8284 78 onCount++;
tteisberg 1:b446f401a861 79 }
bhz 2:eb447fcd8284 80 sample_lock = false;
tteisberg 1:b446f401a861 81 }
tteisberg 1:b446f401a861 82
tteisberg 1:b446f401a861 83 int main()
tteisberg 1:b446f401a861 84 {
bhz 2:eb447fcd8284 85 pc.printf("3 CM Link Board - Recieve\r\n");
bhz 4:037345932888 86 /*
tteisberg 1:b446f401a861 87 bool calib = true;
tteisberg 1:b446f401a861 88 if(calib){
tteisberg 1:b446f401a861 89 while(true){
bhz 2:eb447fcd8284 90 pc.printf("%f\r\r\n", rx.read());
tteisberg 1:b446f401a861 91 wait_ms(100);
tteisberg 1:b446f401a861 92 if(pc.readable()) break;
tteisberg 1:b446f401a861 93 }
tteisberg 1:b446f401a861 94 }
tteisberg 1:b446f401a861 95 idx = 0;
bhz 2:eb447fcd8284 96 offCount = 0;
tteisberg 1:b446f401a861 97 offCount = 0;
bhz 4:037345932888 98 //*/
bhz 2:eb447fcd8284 99 Ticker sampler;
bhz 2:eb447fcd8284 100 sampler.attach_us(&sample, SAMPLE_GAP);
bhz 2:eb447fcd8284 101
tteisberg 1:b446f401a861 102 while(true){
bhz 4:037345932888 103 if((offCount + HALF_SAMPLE) / SAMPLE_RATE > N_PPM && start != idx){ // synchronize
bhz 4:037345932888 104 printDecode(idx);
tteisberg 1:b446f401a861 105 }
bhz 4:037345932888 106 wait_us(PULSE_TIME);
mbed_official 0:879aa9d0247b 107 }
mbed_official 0:879aa9d0247b 108 }