15kHz with multiplexing
Dependencies: FastAnalogIn mbed
main.cpp@1:8e7e9ef6b0bd, 2017-03-07 (annotated)
- Committer:
- keithbehrman
- Date:
- Tue Mar 07 18:43:43 2017 +0000
- Revision:
- 1:8e7e9ef6b0bd
- Parent:
- 0:d83ac315a24c
- Child:
- 2:25f679fea061
Ready for ROS
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 | 1:8e7e9ef6b0bd | 5 | |
keithbehrman | 1:8e7e9ef6b0bd | 6 | Serial pc(USBTX, USBRX); // tx, rx |
keithbehrman | 1:8e7e9ef6b0bd | 7 | |
keithbehrman | 1:8e7e9ef6b0bd | 8 | FastAnalogIn ain(p20); //Fast&Furious:Tokyo Drift Analog Input to PDmux |
keithbehrman | 1:8e7e9ef6b0bd | 9 | //AnalogIn ain(p20); //Analog Input to PDmux |
keithbehrman | 1:8e7e9ef6b0bd | 10 | |
keithbehrman | 0:d83ac315a24c | 11 | DigitalOut LEDout(p8); //5V output to LED mux |
keithbehrman | 0:d83ac315a24c | 12 | DigitalOut LEDmux0(p9); //s0 |
keithbehrman | 0:d83ac315a24c | 13 | DigitalOut LEDmux1(p10); //s1 |
keithbehrman | 0:d83ac315a24c | 14 | DigitalOut LEDmux2(p11); //s2 |
keithbehrman | 0:d83ac315a24c | 15 | DigitalOut LEDmux3(p12); //s3 |
keithbehrman | 0:d83ac315a24c | 16 | |
keithbehrman | 0:d83ac315a24c | 17 | DigitalOut PDmux0(p14); //s0 |
keithbehrman | 0:d83ac315a24c | 18 | DigitalOut PDmux1(p15); //s1 |
keithbehrman | 0:d83ac315a24c | 19 | DigitalOut PDmux2(p16); //s2 |
keithbehrman | 0:d83ac315a24c | 20 | DigitalOut PDmux3(p17); //s3 |
keithbehrman | 1:8e7e9ef6b0bd | 21 | AnalogOut aout(p18); |
keithbehrman | 0:d83ac315a24c | 22 | |
keithbehrman | 0:d83ac315a24c | 23 | |
keithbehrman | 1:8e7e9ef6b0bd | 24 | double voltageOut; |
keithbehrman | 1:8e7e9ef6b0bd | 25 | double readIn[5]; |
keithbehrman | 1:8e7e9ef6b0bd | 26 | |
keithbehrman | 0:d83ac315a24c | 27 | |
keithbehrman | 0:d83ac315a24c | 28 | double alpha; //dummy variable for ADC |
keithbehrman | 0:d83ac315a24c | 29 | |
keithbehrman | 0:d83ac315a24c | 30 | |
keithbehrman | 1:8e7e9ef6b0bd | 31 | |
keithbehrman | 0:d83ac315a24c | 32 | int main() |
keithbehrman | 0:d83ac315a24c | 33 | { |
keithbehrman | 1:8e7e9ef6b0bd | 34 | pc.baud(921600); |
keithbehrman | 1:8e7e9ef6b0bd | 35 | //pc.format(); |
keithbehrman | 1:8e7e9ef6b0bd | 36 | |
keithbehrman | 0:d83ac315a24c | 37 | // double ADCtime=.000000116; //1.6 us |
keithbehrman | 0:d83ac315a24c | 38 | double time=.000029; // ~15kHz |
keithbehrman | 0:d83ac315a24c | 39 | // double time=.0000029; //100 kHz |
keithbehrman | 1:8e7e9ef6b0bd | 40 | // double time=.0003; |
keithbehrman | 1:8e7e9ef6b0bd | 41 | |
keithbehrman | 0:d83ac315a24c | 42 | |
keithbehrman | 0:d83ac315a24c | 43 | //counters for various while loops |
keithbehrman | 1:8e7e9ef6b0bd | 44 | // int mPD=0; |
keithbehrman | 1:8e7e9ef6b0bd | 45 | // int nPD=0; |
keithbehrman | 1:8e7e9ef6b0bd | 46 | // int mLED=0; |
keithbehrman | 1:8e7e9ef6b0bd | 47 | // int nLED=0; |
keithbehrman | 1:8e7e9ef6b0bd | 48 | //int pd=0; |
keithbehrman | 1:8e7e9ef6b0bd | 49 | //int i=0; |
keithbehrman | 0:d83ac315a24c | 50 | |
keithbehrman | 0:d83ac315a24c | 51 | //boolean bits for multiplexing |
keithbehrman | 0:d83ac315a24c | 52 | LEDmux0=0; |
keithbehrman | 0:d83ac315a24c | 53 | LEDmux1=0; |
keithbehrman | 0:d83ac315a24c | 54 | LEDmux2=0; |
keithbehrman | 0:d83ac315a24c | 55 | LEDmux3=0; //MSB is always 0 |
keithbehrman | 0:d83ac315a24c | 56 | PDmux0=0; |
keithbehrman | 0:d83ac315a24c | 57 | PDmux1=0; |
keithbehrman | 0:d83ac315a24c | 58 | PDmux2=0; |
keithbehrman | 0:d83ac315a24c | 59 | PDmux3=0; //MSB is always 0 |
keithbehrman | 0:d83ac315a24c | 60 | |
keithbehrman | 0:d83ac315a24c | 61 | |
keithbehrman | 0:d83ac315a24c | 62 | //while loop that runs continously through code to constantly give measuremtn while MCU is on |
keithbehrman | 0:d83ac315a24c | 63 | while(1) { |
keithbehrman | 0:d83ac315a24c | 64 | |
keithbehrman | 0:d83ac315a24c | 65 | |
keithbehrman | 1:8e7e9ef6b0bd | 66 | //loop to mux through photodiodes |
keithbehrman | 1:8e7e9ef6b0bd | 67 | |
keithbehrman | 1:8e7e9ef6b0bd | 68 | for(int pd=0; pd<6; pd++) { |
keithbehrman | 0:d83ac315a24c | 69 | //loop will take 5 measurements for the selected LED/PD combo |
keithbehrman | 1:8e7e9ef6b0bd | 70 | for(int i=0; i<5; i++) { |
keithbehrman | 0:d83ac315a24c | 71 | LEDout = 1; |
keithbehrman | 1:8e7e9ef6b0bd | 72 | readIn[i]=ain.read(); |
keithbehrman | 0:d83ac315a24c | 73 | wait(time); |
keithbehrman | 0:d83ac315a24c | 74 | |
keithbehrman | 1:8e7e9ef6b0bd | 75 | //LEDout = 0; |
keithbehrman | 1:8e7e9ef6b0bd | 76 | //alpha=ain.read(); |
keithbehrman | 1:8e7e9ef6b0bd | 77 | //wait(time); |
keithbehrman | 0:d83ac315a24c | 78 | } |
keithbehrman | 1:8e7e9ef6b0bd | 79 | |
keithbehrman | 1:8e7e9ef6b0bd | 80 | voltageOut=(readIn[2]+readIn[3]+readIn[4])/3; |
keithbehrman | 1:8e7e9ef6b0bd | 81 | aout=voltageOut; |
keithbehrman | 0:d83ac315a24c | 82 | |
keithbehrman | 1:8e7e9ef6b0bd | 83 | //send the diode readings to Matlab |
keithbehrman | 1:8e7e9ef6b0bd | 84 | if(pd==0) { |
keithbehrman | 1:8e7e9ef6b0bd | 85 | pc.printf("1,%f \n", voltageOut); |
keithbehrman | 1:8e7e9ef6b0bd | 86 | } else if(pd==1) { |
keithbehrman | 1:8e7e9ef6b0bd | 87 | pc.printf("2,%f \n", voltageOut+10); |
keithbehrman | 1:8e7e9ef6b0bd | 88 | } else if(pd==2) { |
keithbehrman | 1:8e7e9ef6b0bd | 89 | pc.printf("3,%f \n", voltageOut+20); |
keithbehrman | 1:8e7e9ef6b0bd | 90 | } else if(pd==3) { |
keithbehrman | 1:8e7e9ef6b0bd | 91 | pc.printf("4,%f \n", voltageOut+30); |
keithbehrman | 1:8e7e9ef6b0bd | 92 | } else if(pd==4) { |
keithbehrman | 1:8e7e9ef6b0bd | 93 | pc.printf("5,%f \n", voltageOut+40); |
keithbehrman | 0:d83ac315a24c | 94 | } else { |
keithbehrman | 1:8e7e9ef6b0bd | 95 | pc.printf("6,%f \n", voltageOut+50); |
keithbehrman | 0:d83ac315a24c | 96 | } |
keithbehrman | 0:d83ac315a24c | 97 | |
keithbehrman | 1:8e7e9ef6b0bd | 98 | |
keithbehrman | 1:8e7e9ef6b0bd | 99 | |
keithbehrman | 1:8e7e9ef6b0bd | 100 | //PD multiplexing |
keithbehrman | 1:8e7e9ef6b0bd | 101 | if((PDmux0==0) && (PDmux1==0) && (PDmux2==0)) { |
keithbehrman | 1:8e7e9ef6b0bd | 102 | PDmux0=1; |
keithbehrman | 1:8e7e9ef6b0bd | 103 | } else if((PDmux0==1) && (PDmux1==0) && (PDmux2==0)) { |
keithbehrman | 1:8e7e9ef6b0bd | 104 | PDmux0=0; |
keithbehrman | 1:8e7e9ef6b0bd | 105 | PDmux1=1; |
keithbehrman | 1:8e7e9ef6b0bd | 106 | } else if((PDmux0==0) && (PDmux1==1) && (PDmux2==0)) { |
keithbehrman | 1:8e7e9ef6b0bd | 107 | PDmux0=1; |
keithbehrman | 1:8e7e9ef6b0bd | 108 | } else if((PDmux0==1) && (PDmux1==1) && (PDmux2==0)) { |
keithbehrman | 1:8e7e9ef6b0bd | 109 | PDmux0=0; |
keithbehrman | 1:8e7e9ef6b0bd | 110 | PDmux1=0; |
keithbehrman | 1:8e7e9ef6b0bd | 111 | PDmux2=1; |
keithbehrman | 1:8e7e9ef6b0bd | 112 | } else if((PDmux0==0) && (PDmux1==0) && (PDmux2==1)) { |
keithbehrman | 1:8e7e9ef6b0bd | 113 | PDmux0=1; |
keithbehrman | 0:d83ac315a24c | 114 | } else { |
keithbehrman | 1:8e7e9ef6b0bd | 115 | PDmux0=0; |
keithbehrman | 1:8e7e9ef6b0bd | 116 | PDmux2=0; |
keithbehrman | 0:d83ac315a24c | 117 | } |
keithbehrman | 0:d83ac315a24c | 118 | |
keithbehrman | 0:d83ac315a24c | 119 | } |
keithbehrman | 1:8e7e9ef6b0bd | 120 | if((LEDmux0==0) && (LEDmux1==0) && (LEDmux2==0)) { |
keithbehrman | 1:8e7e9ef6b0bd | 121 | LEDmux0=1; |
keithbehrman | 1:8e7e9ef6b0bd | 122 | } else if((LEDmux0==1) && (LEDmux1==0) && (LEDmux2==0)) { |
keithbehrman | 1:8e7e9ef6b0bd | 123 | LEDmux0=0; |
keithbehrman | 1:8e7e9ef6b0bd | 124 | LEDmux1=1; |
keithbehrman | 1:8e7e9ef6b0bd | 125 | } else if((LEDmux0==0) && (LEDmux1==1) && (LEDmux2==0)) { |
keithbehrman | 1:8e7e9ef6b0bd | 126 | LEDmux0=1; |
keithbehrman | 1:8e7e9ef6b0bd | 127 | } else if((LEDmux0==1) && (LEDmux1==1) && (LEDmux2==0)) { |
keithbehrman | 1:8e7e9ef6b0bd | 128 | LEDmux0=0; |
keithbehrman | 1:8e7e9ef6b0bd | 129 | LEDmux1=0; |
keithbehrman | 1:8e7e9ef6b0bd | 130 | LEDmux2=1; |
keithbehrman | 1:8e7e9ef6b0bd | 131 | } else if((LEDmux0==0) && (LEDmux1==0) && (LEDmux2==1)) { |
keithbehrman | 1:8e7e9ef6b0bd | 132 | LEDmux0=1; |
keithbehrman | 1:8e7e9ef6b0bd | 133 | } else { |
keithbehrman | 1:8e7e9ef6b0bd | 134 | LEDmux0=0; |
keithbehrman | 1:8e7e9ef6b0bd | 135 | LEDmux2=0; |
keithbehrman | 1:8e7e9ef6b0bd | 136 | } |
keithbehrman | 1:8e7e9ef6b0bd | 137 | |
keithbehrman | 0:d83ac315a24c | 138 | |
keithbehrman | 1:8e7e9ef6b0bd | 139 | |
keithbehrman | 0:d83ac315a24c | 140 | |
keithbehrman | 0:d83ac315a24c | 141 | } |
keithbehrman | 0:d83ac315a24c | 142 | } |
keithbehrman | 1:8e7e9ef6b0bd | 143 | |
keithbehrman | 1:8e7e9ef6b0bd | 144 |