Synchronous detection code with ROS communication for optic sensor
Dependencies: FastAnalogIn mbed ros_lib_indigo
Fork of Mirror_Top_Indenter_ROS by
main.cpp@0:d83ac315a24c, 2017-02-17 (annotated)
- Committer:
- keithbehrman
- Date:
- Fri Feb 17 20:37:32 2017 +0000
- Revision:
- 0:d83ac315a24c
- Child:
- 1:8e7e9ef6b0bd
15Khz code with multiplexing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
keithbehrman | 0:d83ac315a24c | 1 | #include "mbed.h" |
keithbehrman | 0:d83ac315a24c | 2 | #include "FastAnalogIn.h" |
keithbehrman | 0:d83ac315a24c | 3 | |
keithbehrman | 0:d83ac315a24c | 4 | |
keithbehrman | 0:d83ac315a24c | 5 | FastAnalogIn ain(p20); //Analog Input to PDmux |
keithbehrman | 0:d83ac315a24c | 6 | DigitalOut LEDout(p8); //5V output to LED mux |
keithbehrman | 0:d83ac315a24c | 7 | DigitalOut LEDmux0(p9); //s0 |
keithbehrman | 0:d83ac315a24c | 8 | DigitalOut LEDmux1(p10); //s1 |
keithbehrman | 0:d83ac315a24c | 9 | DigitalOut LEDmux2(p11); //s2 |
keithbehrman | 0:d83ac315a24c | 10 | DigitalOut LEDmux3(p12); //s3 |
keithbehrman | 0:d83ac315a24c | 11 | |
keithbehrman | 0:d83ac315a24c | 12 | DigitalOut PDmux0(p14); //s0 |
keithbehrman | 0:d83ac315a24c | 13 | DigitalOut PDmux1(p15); //s1 |
keithbehrman | 0:d83ac315a24c | 14 | DigitalOut PDmux2(p16); //s2 |
keithbehrman | 0:d83ac315a24c | 15 | DigitalOut PDmux3(p17); //s3 |
keithbehrman | 0:d83ac315a24c | 16 | |
keithbehrman | 0:d83ac315a24c | 17 | |
keithbehrman | 0:d83ac315a24c | 18 | |
keithbehrman | 0:d83ac315a24c | 19 | double alpha; //dummy variable for ADC |
keithbehrman | 0:d83ac315a24c | 20 | |
keithbehrman | 0:d83ac315a24c | 21 | |
keithbehrman | 0:d83ac315a24c | 22 | int main() |
keithbehrman | 0:d83ac315a24c | 23 | { |
keithbehrman | 0:d83ac315a24c | 24 | // double ADCtime=.000000116; //1.6 us |
keithbehrman | 0:d83ac315a24c | 25 | double time=.000029; // ~15kHz |
keithbehrman | 0:d83ac315a24c | 26 | // double time=.0000029; //100 kHz |
keithbehrman | 0:d83ac315a24c | 27 | // double time=.003; |
keithbehrman | 0:d83ac315a24c | 28 | |
keithbehrman | 0:d83ac315a24c | 29 | //counters for various while loops |
keithbehrman | 0:d83ac315a24c | 30 | int mPD=0; |
keithbehrman | 0:d83ac315a24c | 31 | int nPD=0; |
keithbehrman | 0:d83ac315a24c | 32 | int mLED=0; |
keithbehrman | 0:d83ac315a24c | 33 | int nLED=0; |
keithbehrman | 0:d83ac315a24c | 34 | int pd=0; |
keithbehrman | 0:d83ac315a24c | 35 | int i=0; |
keithbehrman | 0:d83ac315a24c | 36 | |
keithbehrman | 0:d83ac315a24c | 37 | //boolean bits for multiplexing |
keithbehrman | 0:d83ac315a24c | 38 | LEDmux0=0; |
keithbehrman | 0:d83ac315a24c | 39 | LEDmux1=0; |
keithbehrman | 0:d83ac315a24c | 40 | LEDmux2=0; |
keithbehrman | 0:d83ac315a24c | 41 | LEDmux3=0; //MSB is always 0 |
keithbehrman | 0:d83ac315a24c | 42 | PDmux0=0; |
keithbehrman | 0:d83ac315a24c | 43 | PDmux1=0; |
keithbehrman | 0:d83ac315a24c | 44 | PDmux2=0; |
keithbehrman | 0:d83ac315a24c | 45 | PDmux3=0; //MSB is always 0 |
keithbehrman | 0:d83ac315a24c | 46 | |
keithbehrman | 0:d83ac315a24c | 47 | |
keithbehrman | 0:d83ac315a24c | 48 | //while loop that runs continously through code to constantly give measuremtn while MCU is on |
keithbehrman | 0:d83ac315a24c | 49 | while(1) { |
keithbehrman | 0:d83ac315a24c | 50 | |
keithbehrman | 0:d83ac315a24c | 51 | |
keithbehrman | 0:d83ac315a24c | 52 | //loop to mux through the 8 photodiodes |
keithbehrman | 0:d83ac315a24c | 53 | while(pd<8) { |
keithbehrman | 0:d83ac315a24c | 54 | //loop will take 5 measurements for the selected LED/PD combo |
keithbehrman | 0:d83ac315a24c | 55 | while(i<5) { |
keithbehrman | 0:d83ac315a24c | 56 | LEDout = 1; |
keithbehrman | 0:d83ac315a24c | 57 | alpha=ain.read(); |
keithbehrman | 0:d83ac315a24c | 58 | wait(time); |
keithbehrman | 0:d83ac315a24c | 59 | |
keithbehrman | 0:d83ac315a24c | 60 | LEDout = 0; |
keithbehrman | 0:d83ac315a24c | 61 | alpha=ain.read(); |
keithbehrman | 0:d83ac315a24c | 62 | wait(time); |
keithbehrman | 0:d83ac315a24c | 63 | i++; |
keithbehrman | 0:d83ac315a24c | 64 | } |
keithbehrman | 0:d83ac315a24c | 65 | i=0; |
keithbehrman | 0:d83ac315a24c | 66 | |
keithbehrman | 0:d83ac315a24c | 67 | PDmux0=!PDmux0; //flip LSB |
keithbehrman | 0:d83ac315a24c | 68 | |
keithbehrman | 0:d83ac315a24c | 69 | //flips 2nd bit every other cycle |
keithbehrman | 0:d83ac315a24c | 70 | if (mPD==1) { |
keithbehrman | 0:d83ac315a24c | 71 | PDmux1=!PDmux1; |
keithbehrman | 0:d83ac315a24c | 72 | mPD=0; |
keithbehrman | 0:d83ac315a24c | 73 | } else { |
keithbehrman | 0:d83ac315a24c | 74 | mPD++; |
keithbehrman | 0:d83ac315a24c | 75 | } |
keithbehrman | 0:d83ac315a24c | 76 | |
keithbehrman | 0:d83ac315a24c | 77 | //flips 3rd bit after every 4th cycle |
keithbehrman | 0:d83ac315a24c | 78 | if (nPD==3) { |
keithbehrman | 0:d83ac315a24c | 79 | PDmux2=!PDmux2; |
keithbehrman | 0:d83ac315a24c | 80 | nPD=0; |
keithbehrman | 0:d83ac315a24c | 81 | } else { |
keithbehrman | 0:d83ac315a24c | 82 | nPD++; |
keithbehrman | 0:d83ac315a24c | 83 | } |
keithbehrman | 0:d83ac315a24c | 84 | pd++; |
keithbehrman | 0:d83ac315a24c | 85 | } |
keithbehrman | 0:d83ac315a24c | 86 | pd=0; |
keithbehrman | 0:d83ac315a24c | 87 | |
keithbehrman | 0:d83ac315a24c | 88 | //same multiplexing code above, but for LEDs |
keithbehrman | 0:d83ac315a24c | 89 | LEDmux0=!LEDmux0; |
keithbehrman | 0:d83ac315a24c | 90 | |
keithbehrman | 0:d83ac315a24c | 91 | if (mLED==1) { |
keithbehrman | 0:d83ac315a24c | 92 | LEDmux1=!LEDmux1; |
keithbehrman | 0:d83ac315a24c | 93 | mLED=0; |
keithbehrman | 0:d83ac315a24c | 94 | } else { |
keithbehrman | 0:d83ac315a24c | 95 | mLED++; |
keithbehrman | 0:d83ac315a24c | 96 | } |
keithbehrman | 0:d83ac315a24c | 97 | |
keithbehrman | 0:d83ac315a24c | 98 | if (nLED==3) { |
keithbehrman | 0:d83ac315a24c | 99 | LEDmux2=!LEDmux2; |
keithbehrman | 0:d83ac315a24c | 100 | nLED=0; |
keithbehrman | 0:d83ac315a24c | 101 | } else { |
keithbehrman | 0:d83ac315a24c | 102 | nLED++; |
keithbehrman | 0:d83ac315a24c | 103 | } |
keithbehrman | 0:d83ac315a24c | 104 | |
keithbehrman | 0:d83ac315a24c | 105 | } |
keithbehrman | 0:d83ac315a24c | 106 | } |