SSI OpComms 3CM Board RX
Dependencies: mbed
Fork of Optical3cmRX by
main.cpp@3:1235cbe67ec9, 2015-11-08 (annotated)
- Committer:
- bhz
- Date:
- Sun Nov 08 00:45:49 2015 +0000
- Revision:
- 3:1235cbe67ec9
- Parent:
- 2:eb447fcd8284
- Child:
- 4:037345932888
Not working;
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:eb447fcd8284 | 7 | static const int N_PPM = 4; |
bhz | 3:1235cbe67ec9 | 8 | static const int PULSE_TIME = 10000; |
bhz | 3:1235cbe67ec9 | 9 | static const int SAMPLE_RATE = 10; // must be a divisor of PULSE_TIME |
bhz | 2:eb447fcd8284 | 10 | static const int SAMPLE_GAP = PULSE_TIME / SAMPLE_RATE; |
bhz | 2:eb447fcd8284 | 11 | static const int HALF_SAMPLE = SAMPLE_RATE / 2; |
bhz | 2:eb447fcd8284 | 12 | static const int BUF_SIZE = 1024; |
bhz | 2:eb447fcd8284 | 13 | static const float THRESH = 0.005; |
bhz | 2:eb447fcd8284 | 14 | |
bhz | 2:eb447fcd8284 | 15 | int buf[BUF_SIZE]; |
tteisberg | 1:b446f401a861 | 16 | int idx = 0; |
tteisberg | 1:b446f401a861 | 17 | int nzeros = 0; |
bhz | 2:eb447fcd8284 | 18 | int start = 0; |
bhz | 2:eb447fcd8284 | 19 | bool sample_lock = false; |
bhz | 2:eb447fcd8284 | 20 | int offCount, onCount; |
tteisberg | 1:b446f401a861 | 21 | |
bhz | 2:eb447fcd8284 | 22 | char* decodechar(int stopIdx){ |
bhz | 2:eb447fcd8284 | 23 | int len = (stopIdx - start + BUF_SIZE) % BUF_SIZE; |
bhz | 2:eb447fcd8284 | 24 | bool* data = new bool[len * N_PPM]; |
bhz | 2:eb447fcd8284 | 25 | for(int i = 0; i < len; i++) |
bhz | 2:eb447fcd8284 | 26 | { |
bhz | 2:eb447fcd8284 | 27 | int c = buf[start++]; |
bhz | 2:eb447fcd8284 | 28 | if(start == BUF_SIZE) start = 0; |
bhz | 2:eb447fcd8284 | 29 | for(int j = 0; j < N_PPM; j++) |
bhz | 2:eb447fcd8284 | 30 | { |
bhz | 2:eb447fcd8284 | 31 | data[i * N_PPM + j] = (c >> (N_PPM - j - 1)); |
bhz | 2:eb447fcd8284 | 32 | } |
bhz | 2:eb447fcd8284 | 33 | } |
bhz | 2:eb447fcd8284 | 34 | return (char*) data; |
bhz | 2:eb447fcd8284 | 35 | } |
tteisberg | 1:b446f401a861 | 36 | |
bhz | 2:eb447fcd8284 | 37 | void sample() |
bhz | 2:eb447fcd8284 | 38 | { |
bhz | 2:eb447fcd8284 | 39 | if(sample_lock) pc.printf("\r\nWarning: Sampling too fast\r\n"); |
bhz | 2:eb447fcd8284 | 40 | sample_lock = true; |
bhz | 2:eb447fcd8284 | 41 | float v = rx.read(); |
bhz | 3:1235cbe67ec9 | 42 | if(v < THRESH){ |
bhz | 2:eb447fcd8284 | 43 | offCount++; |
bhz | 3:1235cbe67ec9 | 44 | if(onCount > HALF_SAMPLE) |
bhz | 3:1235cbe67ec9 | 45 | { |
bhz | 3:1235cbe67ec9 | 46 | pc.printf("0:%d ", onCount); |
bhz | 3:1235cbe67ec9 | 47 | onCount = 0; |
bhz | 3:1235cbe67ec9 | 48 | } |
bhz | 2:eb447fcd8284 | 49 | }else{ // Pulse |
bhz | 3:1235cbe67ec9 | 50 | if(offCount > HALF_SAMPLE) |
bhz | 2:eb447fcd8284 | 51 | { |
bhz | 3:1235cbe67ec9 | 52 | int offPulses = (offCount + HALF_SAMPLE) / SAMPLE_RATE - 1; |
bhz | 3:1235cbe67ec9 | 53 | pc.printf("1:%d:%d ", offCount, offPulses); |
bhz | 2:eb447fcd8284 | 54 | if(offPulses <= N_PPM) |
bhz | 2:eb447fcd8284 | 55 | { |
bhz | 3:1235cbe67ec9 | 56 | buf[idx] = offPulses; |
bhz | 2:eb447fcd8284 | 57 | idx++; |
bhz | 2:eb447fcd8284 | 58 | if(idx == BUF_SIZE) idx = 0; |
bhz | 2:eb447fcd8284 | 59 | if(idx == start) pc.printf("\r\nWarning: Buffer too small\r\n"); |
bhz | 2:eb447fcd8284 | 60 | } |
bhz | 2:eb447fcd8284 | 61 | offCount = 0; |
bhz | 2:eb447fcd8284 | 62 | } |
bhz | 2:eb447fcd8284 | 63 | onCount++; |
tteisberg | 1:b446f401a861 | 64 | } |
bhz | 2:eb447fcd8284 | 65 | sample_lock = false; |
tteisberg | 1:b446f401a861 | 66 | } |
tteisberg | 1:b446f401a861 | 67 | |
tteisberg | 1:b446f401a861 | 68 | int main() |
tteisberg | 1:b446f401a861 | 69 | { |
bhz | 2:eb447fcd8284 | 70 | pc.printf("3 CM Link Board - Recieve\r\n"); |
tteisberg | 1:b446f401a861 | 71 | bool calib = true; |
tteisberg | 1:b446f401a861 | 72 | if(calib){ |
tteisberg | 1:b446f401a861 | 73 | while(true){ |
bhz | 2:eb447fcd8284 | 74 | pc.printf("%f\r\r\n", rx.read()); |
tteisberg | 1:b446f401a861 | 75 | wait_ms(100); |
tteisberg | 1:b446f401a861 | 76 | if(pc.readable()) break; |
tteisberg | 1:b446f401a861 | 77 | } |
tteisberg | 1:b446f401a861 | 78 | } |
tteisberg | 1:b446f401a861 | 79 | idx = 0; |
bhz | 2:eb447fcd8284 | 80 | offCount = 0; |
tteisberg | 1:b446f401a861 | 81 | offCount = 0; |
bhz | 2:eb447fcd8284 | 82 | pc.printf("Ready.\r\n"); |
bhz | 2:eb447fcd8284 | 83 | float v = rx.read(); |
bhz | 2:eb447fcd8284 | 84 | while(v < THRESH) |
bhz | 2:eb447fcd8284 | 85 | { |
bhz | 2:eb447fcd8284 | 86 | v = rx.read(); |
bhz | 2:eb447fcd8284 | 87 | wait_us(SAMPLE_GAP); |
bhz | 2:eb447fcd8284 | 88 | } |
bhz | 2:eb447fcd8284 | 89 | pc.printf("Starting...\r\n"); |
bhz | 2:eb447fcd8284 | 90 | Ticker sampler; |
bhz | 2:eb447fcd8284 | 91 | sampler.attach_us(&sample, SAMPLE_GAP); |
bhz | 2:eb447fcd8284 | 92 | |
tteisberg | 1:b446f401a861 | 93 | while(true){ |
bhz | 2:eb447fcd8284 | 94 | if((offCount + HALF_SAMPLE) / SAMPLE_RATE > N_PPM){ // synchronize |
bhz | 2:eb447fcd8284 | 95 | char* data = decodechar(idx); |
bhz | 2:eb447fcd8284 | 96 | pc.printf(data); |
bhz | 2:eb447fcd8284 | 97 | delete[] data; |
tteisberg | 1:b446f401a861 | 98 | offCount = 0; |
tteisberg | 1:b446f401a861 | 99 | } |
mbed_official | 0:879aa9d0247b | 100 | } |
mbed_official | 0:879aa9d0247b | 101 | } |