SSI OpComms 3CM Board RX

Dependencies:   mbed

Fork of Optical3cmRX by Thomas Teisberg

Committer:
Aaditya Shidham
Date:
Sun Nov 15 18:51:09 2015 -0800
Revision:
8:20d405a03cb9
Parent:
7:41b22e5a1d8d
added second exp cksum

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:879aa9d0247b 1 #include "mbed.h"
bhz 6:6c996fb7a742 2 #include <sstream>
bhz 5:fc3ce1c5db88 3 #include <string>
bhz 6:6c996fb7a742 4 #include <queue>
bhz 5:fc3ce1c5db88 5 using namespace std;
tteisberg 1:b446f401a861 6
tteisberg 1:b446f401a861 7 AnalogIn rx(p20);
tteisberg 1:b446f401a861 8
mbed_official 0:879aa9d0247b 9 Serial pc(USBTX, USBRX); // tx, rx
tteisberg 1:b446f401a861 10
bhz 5:fc3ce1c5db88 11 // e.g. PPM_BITS = 2 --> 4-PPM; PPM_BITS = 3 --> 8-PPM, etc.
bhz 4:037345932888 12 static const int PPM_BITS = 2;
bhz 4:037345932888 13 static const int N_PPM = 1 << PPM_BITS;
bhz 6:6c996fb7a742 14 static const int EOM = N_PPM;
bhz 6:6c996fb7a742 15 static int PULSE_TIME; // microseconds
bhz 6:6c996fb7a742 16 static int SAMPLE_RATE; // must be a divisor of PULSE_TIME;
bhz 6:6c996fb7a742 17 static int SAMPLE_GAP;
bhz 6:6c996fb7a742 18 static int HALF_SAMPLE;
bhz 6:6c996fb7a742 19 static double THRESH; // threshold for 1 vs 0. Must adjust in the field by testing.
bhz 2:eb447fcd8284 20
bhz 6:6c996fb7a742 21 queue<int> sbuf;
bhz 2:eb447fcd8284 22 bool sample_lock = false;
bhz 2:eb447fcd8284 23 int offCount, onCount;
tteisberg 1:b446f401a861 24
bhz 5:fc3ce1c5db88 25 string getLine(string prompt)
bhz 5:fc3ce1c5db88 26 {
bhz 5:fc3ce1c5db88 27 while(pc.readable()) pc.getc();
bhz 5:fc3ce1c5db88 28 pc.printf(prompt.c_str());
bhz 5:fc3ce1c5db88 29 string out;
bhz 5:fc3ce1c5db88 30 char c;
bhz 5:fc3ce1c5db88 31 while((c = pc.getc()) != '\n')
bhz 5:fc3ce1c5db88 32 {
bhz 5:fc3ce1c5db88 33 out += c;
bhz 5:fc3ce1c5db88 34 }
bhz 5:fc3ce1c5db88 35 pc.printf((out + "\r\n").c_str());
bhz 5:fc3ce1c5db88 36 return out;
bhz 5:fc3ce1c5db88 37 }
bhz 5:fc3ce1c5db88 38
bhz 5:fc3ce1c5db88 39 double getNumber(string prompt, double def)
bhz 5:fc3ce1c5db88 40 {
bhz 5:fc3ce1c5db88 41 string s = getLine(prompt);
bhz 5:fc3ce1c5db88 42 istringstream ints(s);
bhz 5:fc3ce1c5db88 43 double out;
bhz 5:fc3ce1c5db88 44 if(!(ints >> out)) out = def;
bhz 5:fc3ce1c5db88 45 return out;
bhz 5:fc3ce1c5db88 46 }
bhz 5:fc3ce1c5db88 47
bhz 5:fc3ce1c5db88 48 void set_constants()
bhz 5:fc3ce1c5db88 49 {
bhz 5:fc3ce1c5db88 50 PULSE_TIME = (int) getNumber("Input pulse time (us; blank for 1000): ", 1000);
bhz 5:fc3ce1c5db88 51 SAMPLE_RATE = (int) getNumber("Input sample rate (blank for 10): ", 10);
bhz 5:fc3ce1c5db88 52 THRESH = getNumber("Input threshold (blank for 0.005): ", 0.005);
bhz 6:6c996fb7a742 53 pc.printf("Parameters: PULSE_TIME=%d, SAMPLE_RATE=%d, THRESH=%f\r\n", PULSE_TIME, SAMPLE_RATE, THRESH);
bhz 5:fc3ce1c5db88 54 SAMPLE_GAP = PULSE_TIME / SAMPLE_RATE;
bhz 5:fc3ce1c5db88 55 HALF_SAMPLE = SAMPLE_RATE / 2;
bhz 5:fc3ce1c5db88 56 }
bhz 5:fc3ce1c5db88 57
Aaditya Shidham 8:20d405a03cb9 58 //http://stackoverflow.com/questions/8845178/c-programming-tcp-checksum
Aaditya Shidham 8:20d405a03cb9 59 char checkSum(char *buffer, int size)
Aaditya Shidham 8:20d405a03cb9 60 {
Aaditya Shidham 8:20d405a03cb9 61 unsigned long cksum=0;
Aaditya Shidham 8:20d405a03cb9 62 while(size)
Aaditya Shidham 8:20d405a03cb9 63 {
Aaditya Shidham 8:20d405a03cb9 64 cksum+=*buffer++;
Aaditya Shidham 8:20d405a03cb9 65 size--;
Aaditya Shidham 8:20d405a03cb9 66 }
Aaditya Shidham 8:20d405a03cb9 67
Aaditya Shidham 8:20d405a03cb9 68 cksum = (cksum >> 16) + (cksum & 0xffff);
Aaditya Shidham 8:20d405a03cb9 69 cksum += (cksum >>16);
Aaditya Shidham 8:20d405a03cb9 70 return (char)(~cksum);
Aaditya Shidham 8:20d405a03cb9 71 }
Aaditya Shidham 8:20d405a03cb9 72
bhz 6:6c996fb7a742 73 void printDecodeBuf()
bhz 6:6c996fb7a742 74 {
bhz 6:6c996fb7a742 75 char cur = 0;
bhz 6:6c996fb7a742 76 int k = 0;
bhz 6:6c996fb7a742 77 string data;
bhz 7:41b22e5a1d8d 78 //pc.printf("\r\nsbuf: ");
bhz 6:6c996fb7a742 79 while(!sbuf.empty())
bhz 6:6c996fb7a742 80 {
bhz 6:6c996fb7a742 81 int c = sbuf.front();
bhz 6:6c996fb7a742 82 sbuf.pop();
bhz 6:6c996fb7a742 83 if(c == EOM) break;
bhz 7:41b22e5a1d8d 84 //pc.printf("%d ", c);
bhz 6:6c996fb7a742 85 if(sbuf.empty())
bhz 6:6c996fb7a742 86 {
bhz 6:6c996fb7a742 87 pc.printf("\r\nWarning: reached end of buffer\r\n");
bhz 6:6c996fb7a742 88 }
bhz 6:6c996fb7a742 89 for(int j = 0; j < PPM_BITS; j++)
bhz 6:6c996fb7a742 90 {
bhz 6:6c996fb7a742 91 // do some bit hacking to put the bit into the proper character in the output array
bhz 6:6c996fb7a742 92 bool bit = (c >> (PPM_BITS - j - 1)) & 1;
bhz 6:6c996fb7a742 93 cur |= bit << (7 - k%8);
bhz 6:6c996fb7a742 94 k++;
bhz 6:6c996fb7a742 95 if(k%8 == 0)
bhz 6:6c996fb7a742 96 {
bhz 6:6c996fb7a742 97 data += cur;
bhz 6:6c996fb7a742 98 cur = 0;
bhz 6:6c996fb7a742 99 }
bhz 6:6c996fb7a742 100 }
bhz 6:6c996fb7a742 101 }
Aaditya Shidham 8:20d405a03cb9 102 int rcv_checksum = data[data.size()-2];
Aaditya Shidham 8:20d405a03cb9 103 int rcv_checksum2 = data[data.size()-1];
Aaditya Shidham 8:20d405a03cb9 104 data.erase(data.size()-2, data.size());
bhz 7:41b22e5a1d8d 105 pc.printf("Received: \"");
bhz 6:6c996fb7a742 106 pc.printf(data.c_str());
bhz 6:6c996fb7a742 107 pc.printf("\"\r\n");
bhz 7:41b22e5a1d8d 108
bhz 7:41b22e5a1d8d 109 int checksum = 0;
bhz 7:41b22e5a1d8d 110 for(int i = 0; i < data.size(); i++) checksum ^= data[i];
bhz 7:41b22e5a1d8d 111
bhz 7:41b22e5a1d8d 112 pc.printf("Received: %d | Computed: %d\r\n", rcv_checksum, checksum);
Aaditya Shidham 8:20d405a03cb9 113 pc.printf("Received2: %d | Computed2: %d\r\n", rcv_checksum2, checkSum(data.c_str(),data.size()));
bhz 6:6c996fb7a742 114 }
bhz 6:6c996fb7a742 115
tteisberg 1:b446f401a861 116
bhz 5:fc3ce1c5db88 117 // Samples once, and writes to the buffer if necessary.
bhz 2:eb447fcd8284 118 void sample()
bhz 2:eb447fcd8284 119 {
bhz 2:eb447fcd8284 120 if(sample_lock) pc.printf("\r\nWarning: Sampling too fast\r\n");
bhz 2:eb447fcd8284 121 sample_lock = true;
bhz 2:eb447fcd8284 122 float v = rx.read();
bhz 3:1235cbe67ec9 123 if(v < THRESH){
bhz 2:eb447fcd8284 124 offCount++;
bhz 3:1235cbe67ec9 125 if(onCount > HALF_SAMPLE)
bhz 3:1235cbe67ec9 126 {
bhz 3:1235cbe67ec9 127 onCount = 0;
bhz 3:1235cbe67ec9 128 }
bhz 2:eb447fcd8284 129 }else{ // Pulse
bhz 3:1235cbe67ec9 130 if(offCount > HALF_SAMPLE)
bhz 2:eb447fcd8284 131 {
bhz 3:1235cbe67ec9 132 int offPulses = (offCount + HALF_SAMPLE) / SAMPLE_RATE - 1;
bhz 6:6c996fb7a742 133 if(offPulses < N_PPM)
bhz 2:eb447fcd8284 134 {
bhz 7:41b22e5a1d8d 135 pc.printf("%d ", offPulses);
bhz 6:6c996fb7a742 136 sbuf.push(offPulses);
bhz 2:eb447fcd8284 137 }
bhz 2:eb447fcd8284 138 offCount = 0;
bhz 2:eb447fcd8284 139 }
bhz 2:eb447fcd8284 140 onCount++;
tteisberg 1:b446f401a861 141 }
bhz 2:eb447fcd8284 142 sample_lock = false;
tteisberg 1:b446f401a861 143 }
tteisberg 1:b446f401a861 144
tteisberg 1:b446f401a861 145 int main()
tteisberg 1:b446f401a861 146 {
bhz 2:eb447fcd8284 147 pc.printf("3 CM Link Board - Recieve\r\n");
bhz 5:fc3ce1c5db88 148 //*
tteisberg 1:b446f401a861 149 bool calib = true;
tteisberg 1:b446f401a861 150 if(calib){
tteisberg 1:b446f401a861 151 while(true){
bhz 5:fc3ce1c5db88 152 // Just prints raw values until the user gives some input
bhz 2:eb447fcd8284 153 pc.printf("%f\r\r\n", rx.read());
tteisberg 1:b446f401a861 154 wait_ms(100);
bhz 5:fc3ce1c5db88 155 if(pc.readable()) {
bhz 5:fc3ce1c5db88 156 break;
bhz 5:fc3ce1c5db88 157 }
tteisberg 1:b446f401a861 158 }
tteisberg 1:b446f401a861 159 }
bhz 5:fc3ce1c5db88 160 set_constants();
bhz 5:fc3ce1c5db88 161 pc.printf("Ready! \r\n");
bhz 2:eb447fcd8284 162 offCount = 0;
tteisberg 1:b446f401a861 163 offCount = 0;
bhz 4:037345932888 164 //*/
bhz 2:eb447fcd8284 165 Ticker sampler;
bhz 2:eb447fcd8284 166 sampler.attach_us(&sample, SAMPLE_GAP);
bhz 2:eb447fcd8284 167
tteisberg 1:b446f401a861 168 while(true){
bhz 7:41b22e5a1d8d 169 if((offCount + HALF_SAMPLE) / SAMPLE_RATE > N_PPM && !sbuf.empty()){ // synchronize
bhz 6:6c996fb7a742 170 pc.printf("\r\n");
bhz 6:6c996fb7a742 171 sbuf.push(EOM);
bhz 6:6c996fb7a742 172 printDecodeBuf();
tteisberg 1:b446f401a861 173 }
bhz 4:037345932888 174 wait_us(PULSE_TIME);
mbed_official 0:879aa9d0247b 175 }
mbed_official 0:879aa9d0247b 176 }