Hardware control: mirrors
hardwareIO.cpp@0:3fe2fe307968, 2012-08-01 (annotated)
- Committer:
- mbedalvaro
- Date:
- Wed Aug 01 03:01:45 2012 +0000
- Revision:
- 0:3fe2fe307968
- Child:
- 1:b1f5ed22087a
[mbed] converted /scoreLight_Advanced/hardwareIO
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedalvaro | 0:3fe2fe307968 | 1 | #include "hardwareIO.h" |
mbedalvaro | 0:3fe2fe307968 | 2 | |
mbedalvaro | 0:3fe2fe307968 | 3 | HardwareIO IO; // preintantiation of cross-file global object IO |
mbedalvaro | 0:3fe2fe307968 | 4 | |
mbedalvaro | 0:3fe2fe307968 | 5 | // -------------------------------------- (0) SETUP ALL IO (call this in the setup() function in main program) |
mbedalvaro | 0:3fe2fe307968 | 6 | |
mbedalvaro | 0:3fe2fe307968 | 7 | Serial pc(USBTX, USBRX); // tx, rx |
mbedalvaro | 0:3fe2fe307968 | 8 | LocalFileSystem local("local"); // Create the local filesystem under the name "local" |
mbedalvaro | 0:3fe2fe307968 | 9 | |
mbedalvaro | 0:3fe2fe307968 | 10 | SPI spiDAC(MOSI_PIN, MISO_PIN, SCK_PIN); // mosi, miso, sclk |
mbedalvaro | 0:3fe2fe307968 | 11 | DigitalOut csDAC(CS_DAC_MIRRORS); |
mbedalvaro | 0:3fe2fe307968 | 12 | |
mbedalvaro | 0:3fe2fe307968 | 13 | AnalogIn ain(POT_ANALOG_INPUT); |
mbedalvaro | 0:3fe2fe307968 | 14 | |
mbedalvaro | 0:3fe2fe307968 | 15 | DigitalOut Laser_Red(LASER_RED_PIN); // NOTE: this is NOT the lock in sensing laser (actually, not used yet) |
mbedalvaro | 0:3fe2fe307968 | 16 | DigitalOut Laser_Green(LASER_GREEN_PIN); |
mbedalvaro | 0:3fe2fe307968 | 17 | DigitalOut Laser_Blue(LASER_BLUE_PIN); |
mbedalvaro | 0:3fe2fe307968 | 18 | |
mbedalvaro | 0:3fe2fe307968 | 19 | void HardwareIO::init(void) { |
mbedalvaro | 0:3fe2fe307968 | 20 | Laser_Red = 0; // note: this is not the lockin-laser! |
mbedalvaro | 0:3fe2fe307968 | 21 | Laser_Green = 0; |
mbedalvaro | 0:3fe2fe307968 | 22 | Laser_Blue = 0; |
mbedalvaro | 0:3fe2fe307968 | 23 | |
mbedalvaro | 0:3fe2fe307968 | 24 | //Serial Communication setup: |
mbedalvaro | 0:3fe2fe307968 | 25 | pc.baud(115200);// |
mbedalvaro | 0:3fe2fe307968 | 26 | // pc.baud(921600);//115200);// |
mbedalvaro | 0:3fe2fe307968 | 27 | |
mbedalvaro | 0:3fe2fe307968 | 28 | // Setup for lock-in amplifier and pwm references: |
mbedalvaro | 0:3fe2fe307968 | 29 | lockin.init(); |
mbedalvaro | 0:3fe2fe307968 | 30 | |
mbedalvaro | 0:3fe2fe307968 | 31 | // Setup the spi for 8 bit data, high steady state clock, |
mbedalvaro | 0:3fe2fe307968 | 32 | // second edge capture, with a 10MHz clock rate |
mbedalvaro | 0:3fe2fe307968 | 33 | csDAC = 1; |
mbedalvaro | 0:3fe2fe307968 | 34 | spiDAC.format(16,0); |
mbedalvaro | 0:3fe2fe307968 | 35 | spiDAC.frequency(16000000); |
mbedalvaro | 0:3fe2fe307968 | 36 | |
mbedalvaro | 0:3fe2fe307968 | 37 | // default initial mirror position: |
mbedalvaro | 0:3fe2fe307968 | 38 | writeOutX(CENTER_AD_MIRROR_X); |
mbedalvaro | 0:3fe2fe307968 | 39 | writeOutY(CENTER_AD_MIRROR_Y); |
mbedalvaro | 0:3fe2fe307968 | 40 | |
mbedalvaro | 0:3fe2fe307968 | 41 | // Load LUT table: |
mbedalvaro | 0:3fe2fe307968 | 42 | setLUT(); |
mbedalvaro | 0:3fe2fe307968 | 43 | } |
mbedalvaro | 0:3fe2fe307968 | 44 | |
mbedalvaro | 0:3fe2fe307968 | 45 | //write on the first DAC, output A (mirror X) |
mbedalvaro | 0:3fe2fe307968 | 46 | void HardwareIO::writeOutX(unsigned short value){ |
mbedalvaro | 0:3fe2fe307968 | 47 | if(value > MAX_AD_MIRRORS) value = MAX_AD_MIRRORS; |
mbedalvaro | 0:3fe2fe307968 | 48 | if(value < MIN_AD_MIRRORS) value = MIN_AD_MIRRORS; |
mbedalvaro | 0:3fe2fe307968 | 49 | |
mbedalvaro | 0:3fe2fe307968 | 50 | value |= 0x7000; |
mbedalvaro | 0:3fe2fe307968 | 51 | value &= 0x7FFF; |
mbedalvaro | 0:3fe2fe307968 | 52 | |
mbedalvaro | 0:3fe2fe307968 | 53 | csDAC = 0; |
mbedalvaro | 0:3fe2fe307968 | 54 | spiDAC.write(value); |
mbedalvaro | 0:3fe2fe307968 | 55 | csDAC = 1; |
mbedalvaro | 0:3fe2fe307968 | 56 | } |
mbedalvaro | 0:3fe2fe307968 | 57 | |
mbedalvaro | 0:3fe2fe307968 | 58 | //write on the first DAC, output B (mirror Y) |
mbedalvaro | 0:3fe2fe307968 | 59 | void HardwareIO::writeOutY(unsigned short value){ |
mbedalvaro | 0:3fe2fe307968 | 60 | if(value > MAX_AD_MIRRORS) value = MAX_AD_MIRRORS; |
mbedalvaro | 0:3fe2fe307968 | 61 | if(value < MIN_AD_MIRRORS) value = MIN_AD_MIRRORS; |
mbedalvaro | 0:3fe2fe307968 | 62 | |
mbedalvaro | 0:3fe2fe307968 | 63 | value |= 0xF000; |
mbedalvaro | 0:3fe2fe307968 | 64 | value &= 0xFFFF; |
mbedalvaro | 0:3fe2fe307968 | 65 | |
mbedalvaro | 0:3fe2fe307968 | 66 | csDAC = 0; |
mbedalvaro | 0:3fe2fe307968 | 67 | spiDAC.write(value); |
mbedalvaro | 0:3fe2fe307968 | 68 | csDAC = 1; |
mbedalvaro | 0:3fe2fe307968 | 69 | } |
mbedalvaro | 0:3fe2fe307968 | 70 | |
mbedalvaro | 0:3fe2fe307968 | 71 | void HardwareIO::setRedPower(int powerValue){ |
mbedalvaro | 0:3fe2fe307968 | 72 | if(powerValue > 0){ |
mbedalvaro | 0:3fe2fe307968 | 73 | lockin.setLaserPower(true); |
mbedalvaro | 0:3fe2fe307968 | 74 | } |
mbedalvaro | 0:3fe2fe307968 | 75 | else{ |
mbedalvaro | 0:3fe2fe307968 | 76 | lockin.setLaserPower(false); |
mbedalvaro | 0:3fe2fe307968 | 77 | } |
mbedalvaro | 0:3fe2fe307968 | 78 | } |
mbedalvaro | 0:3fe2fe307968 | 79 | void HardwareIO::setGreenPower(int powerValue){ |
mbedalvaro | 0:3fe2fe307968 | 80 | if(powerValue > 0){ |
mbedalvaro | 0:3fe2fe307968 | 81 | Laser_Green = 1; |
mbedalvaro | 0:3fe2fe307968 | 82 | } |
mbedalvaro | 0:3fe2fe307968 | 83 | else{ |
mbedalvaro | 0:3fe2fe307968 | 84 | Laser_Green = 0; |
mbedalvaro | 0:3fe2fe307968 | 85 | } |
mbedalvaro | 0:3fe2fe307968 | 86 | } |
mbedalvaro | 0:3fe2fe307968 | 87 | void HardwareIO::setBluePower(int powerValue){ |
mbedalvaro | 0:3fe2fe307968 | 88 | if(powerValue > 0){ |
mbedalvaro | 0:3fe2fe307968 | 89 | Laser_Blue = 1; |
mbedalvaro | 0:3fe2fe307968 | 90 | } |
mbedalvaro | 0:3fe2fe307968 | 91 | else{ |
mbedalvaro | 0:3fe2fe307968 | 92 | Laser_Blue = 0; |
mbedalvaro | 0:3fe2fe307968 | 93 | } |
mbedalvaro | 0:3fe2fe307968 | 94 | } |
mbedalvaro | 0:3fe2fe307968 | 95 | |
mbedalvaro | 0:3fe2fe307968 | 96 | void HardwareIO::setRGBPower(unsigned char color) { |
mbedalvaro | 0:3fe2fe307968 | 97 | lockin.setLaserPower(color&0x04>0? false : true); |
mbedalvaro | 0:3fe2fe307968 | 98 | Laser_Green=(color&0x02)>>1; |
mbedalvaro | 0:3fe2fe307968 | 99 | Laser_Blue =color&0x01; |
mbedalvaro | 0:3fe2fe307968 | 100 | } |
mbedalvaro | 0:3fe2fe307968 | 101 | |
mbedalvaro | 0:3fe2fe307968 | 102 | void HardwareIO::showLimitsMirrors(int times) { |
mbedalvaro | 0:3fe2fe307968 | 103 | unsigned short pointsPerLine=150; |
mbedalvaro | 0:3fe2fe307968 | 104 | int shiftX = (MAX_AD_MIRRORS - MIN_AD_MIRRORS) / pointsPerLine; |
mbedalvaro | 0:3fe2fe307968 | 105 | int shiftY = (MAX_AD_MIRRORS - MIN_AD_MIRRORS) / pointsPerLine; |
mbedalvaro | 0:3fe2fe307968 | 106 | |
mbedalvaro | 0:3fe2fe307968 | 107 | Laser_Green=1; |
mbedalvaro | 0:3fe2fe307968 | 108 | |
mbedalvaro | 0:3fe2fe307968 | 109 | //for (int repeat=0; repeat<times; repeat++) { |
mbedalvaro | 0:3fe2fe307968 | 110 | |
mbedalvaro | 0:3fe2fe307968 | 111 | Timer t; |
mbedalvaro | 0:3fe2fe307968 | 112 | t.start(); |
mbedalvaro | 0:3fe2fe307968 | 113 | while(t.read_ms()<times*1000) { |
mbedalvaro | 0:3fe2fe307968 | 114 | |
mbedalvaro | 0:3fe2fe307968 | 115 | writeOutX(MIN_AD_MIRRORS);writeOutY(MIN_AD_MIRRORS); |
mbedalvaro | 0:3fe2fe307968 | 116 | |
mbedalvaro | 0:3fe2fe307968 | 117 | for(int j=0; j<pointsPerLine; j++){ |
mbedalvaro | 0:3fe2fe307968 | 118 | wait_us(200);//delay between each points |
mbedalvaro | 0:3fe2fe307968 | 119 | writeOutY(j*shiftY + MIN_AD_MIRRORS); |
mbedalvaro | 0:3fe2fe307968 | 120 | } |
mbedalvaro | 0:3fe2fe307968 | 121 | |
mbedalvaro | 0:3fe2fe307968 | 122 | writeOutX(MIN_AD_MIRRORS);writeOutY(MAX_AD_MIRRORS); |
mbedalvaro | 0:3fe2fe307968 | 123 | for(int j=0; j<pointsPerLine; j++) { |
mbedalvaro | 0:3fe2fe307968 | 124 | wait_us(200);//delay between each points |
mbedalvaro | 0:3fe2fe307968 | 125 | writeOutX(j*shiftX + MIN_AD_MIRRORS); |
mbedalvaro | 0:3fe2fe307968 | 126 | } |
mbedalvaro | 0:3fe2fe307968 | 127 | |
mbedalvaro | 0:3fe2fe307968 | 128 | writeOutX(MAX_AD_MIRRORS);writeOutY(MAX_AD_MIRRORS); |
mbedalvaro | 0:3fe2fe307968 | 129 | for(int j=0; j<pointsPerLine; j++) { |
mbedalvaro | 0:3fe2fe307968 | 130 | wait_us(200);//delay between each points |
mbedalvaro | 0:3fe2fe307968 | 131 | writeOutY(-j*shiftX + MAX_AD_MIRRORS); |
mbedalvaro | 0:3fe2fe307968 | 132 | } |
mbedalvaro | 0:3fe2fe307968 | 133 | |
mbedalvaro | 0:3fe2fe307968 | 134 | writeOutX(MAX_AD_MIRRORS);writeOutY(MIN_AD_MIRRORS); |
mbedalvaro | 0:3fe2fe307968 | 135 | for(int j=0; j<pointsPerLine; j++) { |
mbedalvaro | 0:3fe2fe307968 | 136 | wait_us(200);//delay between each points |
mbedalvaro | 0:3fe2fe307968 | 137 | writeOutX(-j*shiftX + MAX_AD_MIRRORS); |
mbedalvaro | 0:3fe2fe307968 | 138 | } |
mbedalvaro | 0:3fe2fe307968 | 139 | |
mbedalvaro | 0:3fe2fe307968 | 140 | } |
mbedalvaro | 0:3fe2fe307968 | 141 | t.stop(); |
mbedalvaro | 0:3fe2fe307968 | 142 | Laser_Green=0; |
mbedalvaro | 0:3fe2fe307968 | 143 | } |
mbedalvaro | 0:3fe2fe307968 | 144 | |
mbedalvaro | 0:3fe2fe307968 | 145 | void HardwareIO::scan_serial(unsigned short pointsPerLine){ |
mbedalvaro | 0:3fe2fe307968 | 146 | //scan the total surface with a custom resolution |
mbedalvaro | 0:3fe2fe307968 | 147 | //send the lockin value for each point as a byte on the serial port to the PC |
mbedalvaro | 0:3fe2fe307968 | 148 | //use "scanSLP_save" to see the data on processing |
mbedalvaro | 0:3fe2fe307968 | 149 | |
mbedalvaro | 0:3fe2fe307968 | 150 | |
mbedalvaro | 0:3fe2fe307968 | 151 | int shiftX = (MAX_AD_MIRRORS - MIN_AD_MIRRORS) / pointsPerLine; |
mbedalvaro | 0:3fe2fe307968 | 152 | int shiftY = (MAX_AD_MIRRORS - MIN_AD_MIRRORS) / pointsPerLine; |
mbedalvaro | 0:3fe2fe307968 | 153 | |
mbedalvaro | 0:3fe2fe307968 | 154 | for(int j=0; j<pointsPerLine; j++){ |
mbedalvaro | 0:3fe2fe307968 | 155 | writeOutX(MIN_AD_MIRRORS); |
mbedalvaro | 0:3fe2fe307968 | 156 | writeOutY(j*shiftY + MIN_AD_MIRRORS); |
mbedalvaro | 0:3fe2fe307968 | 157 | |
mbedalvaro | 0:3fe2fe307968 | 158 | wait_us(300);//begining of line delay |
mbedalvaro | 0:3fe2fe307968 | 159 | for(int i=0; i<pointsPerLine; i++){ |
mbedalvaro | 0:3fe2fe307968 | 160 | writeOutX(i*shiftX + MIN_AD_MIRRORS); |
mbedalvaro | 0:3fe2fe307968 | 161 | |
mbedalvaro | 0:3fe2fe307968 | 162 | wait_us(200);//delay between each points |
mbedalvaro | 0:3fe2fe307968 | 163 | |
mbedalvaro | 0:3fe2fe307968 | 164 | // SEND A VALUE BETWEEN 0 and 255: |
mbedalvaro | 0:3fe2fe307968 | 165 | pc.putc(int(255.0*lockin.getMedianValue()/4095));//printf("%dL",int(valueLockin*255));//pc.putc(int(lockin*255));// |
mbedalvaro | 0:3fe2fe307968 | 166 | } |
mbedalvaro | 0:3fe2fe307968 | 167 | } |
mbedalvaro | 0:3fe2fe307968 | 168 | } |
mbedalvaro | 0:3fe2fe307968 | 169 | |
mbedalvaro | 0:3fe2fe307968 | 170 | //load Look-up Table from LUT.TXT file |
mbedalvaro | 0:3fe2fe307968 | 171 | //or create the file with scanLUT() if not existing. |
mbedalvaro | 0:3fe2fe307968 | 172 | void HardwareIO::setLUT(){ |
mbedalvaro | 0:3fe2fe307968 | 173 | |
mbedalvaro | 0:3fe2fe307968 | 174 | FILE *fp = fopen(LUT_FILENAME, "r"); // Open file on the local file system for writing |
mbedalvaro | 0:3fe2fe307968 | 175 | if(fp){ |
mbedalvaro | 0:3fe2fe307968 | 176 | //load the file into the lut table; keep the SAME resolution! |
mbedalvaro | 0:3fe2fe307968 | 177 | fread(lut,sizeof(uint16),LUT_RESOLUTION*LUT_RESOLUTION,fp); |
mbedalvaro | 0:3fe2fe307968 | 178 | fclose(fp); |
mbedalvaro | 0:3fe2fe307968 | 179 | } |
mbedalvaro | 0:3fe2fe307968 | 180 | else{ |
mbedalvaro | 0:3fe2fe307968 | 181 | //fclose(fp); |
mbedalvaro | 0:3fe2fe307968 | 182 | //if the file "LUT.TXT" doesn't exist, create one with scanLUT() |
mbedalvaro | 0:3fe2fe307968 | 183 | lockin.setLaserPower(true); |
mbedalvaro | 0:3fe2fe307968 | 184 | scanLUT(); |
mbedalvaro | 0:3fe2fe307968 | 185 | } |
mbedalvaro | 0:3fe2fe307968 | 186 | |
mbedalvaro | 0:3fe2fe307968 | 187 | } |
mbedalvaro | 0:3fe2fe307968 | 188 | |
mbedalvaro | 0:3fe2fe307968 | 189 | //scan the total surface with a fixed 2^x resolution |
mbedalvaro | 0:3fe2fe307968 | 190 | //create the Look-Up Table used to "flatten" the scan according to the position |
mbedalvaro | 0:3fe2fe307968 | 191 | // |
mbedalvaro | 0:3fe2fe307968 | 192 | //To Do: maybe detect high frequency to be sure the area is clean and empty? |
mbedalvaro | 0:3fe2fe307968 | 193 | void HardwareIO::scanLUT(){ |
mbedalvaro | 0:3fe2fe307968 | 194 | |
mbedalvaro | 0:3fe2fe307968 | 195 | //reset lut table |
mbedalvaro | 0:3fe2fe307968 | 196 | for(int j=0; j<LUT_RESOLUTION; j++){ |
mbedalvaro | 0:3fe2fe307968 | 197 | for(int i=0; i<LUT_RESOLUTION; i++){ |
mbedalvaro | 0:3fe2fe307968 | 198 | lut[i][j] =0; |
mbedalvaro | 0:3fe2fe307968 | 199 | } |
mbedalvaro | 0:3fe2fe307968 | 200 | } |
mbedalvaro | 0:3fe2fe307968 | 201 | |
mbedalvaro | 0:3fe2fe307968 | 202 | int delayScanning = 300; //in us |
mbedalvaro | 0:3fe2fe307968 | 203 | |
mbedalvaro | 0:3fe2fe307968 | 204 | //define the distance between each points (from 0 to 4096) and the offset (here 0) |
mbedalvaro | 0:3fe2fe307968 | 205 | float shiftX = 1.0*(MAX_AD_MIRRORS - MIN_AD_MIRRORS) / (LUT_RESOLUTION-1); |
mbedalvaro | 0:3fe2fe307968 | 206 | float shiftY = 1.0*(MAX_AD_MIRRORS - MIN_AD_MIRRORS) / (LUT_RESOLUTION-1); |
mbedalvaro | 0:3fe2fe307968 | 207 | float offsetX = MIN_AD_MIRRORS; |
mbedalvaro | 0:3fe2fe307968 | 208 | float offsetY = MIN_AD_MIRRORS; |
mbedalvaro | 0:3fe2fe307968 | 209 | |
mbedalvaro | 0:3fe2fe307968 | 210 | //move the mirrors to the first position |
mbedalvaro | 0:3fe2fe307968 | 211 | writeOutX(MAX_AD_MIRRORS);writeOutY(MIN_AD_MIRRORS); |
mbedalvaro | 0:3fe2fe307968 | 212 | wait_us(500); |
mbedalvaro | 0:3fe2fe307968 | 213 | |
mbedalvaro | 0:3fe2fe307968 | 214 | float x, y; |
mbedalvaro | 0:3fe2fe307968 | 215 | |
mbedalvaro | 0:3fe2fe307968 | 216 | //scan the surface NB_SCANS times |
mbedalvaro | 0:3fe2fe307968 | 217 | //the total value in lut[i][j] shouldn't exceed uint16 !!! |
mbedalvaro | 0:3fe2fe307968 | 218 | for(int loop=0; loop<NB_SCANS; loop++){ |
mbedalvaro | 0:3fe2fe307968 | 219 | for(int j=0; j<LUT_RESOLUTION; j++){ |
mbedalvaro | 0:3fe2fe307968 | 220 | y = shiftY*j + offsetY ; |
mbedalvaro | 0:3fe2fe307968 | 221 | writeOutY(int(y)); |
mbedalvaro | 0:3fe2fe307968 | 222 | //scan from right to left |
mbedalvaro | 0:3fe2fe307968 | 223 | for(int i=LUT_RESOLUTION-1; i>=0; i--){ |
mbedalvaro | 0:3fe2fe307968 | 224 | x = shiftX*i + offsetX; |
mbedalvaro | 0:3fe2fe307968 | 225 | writeOutX(int(x)); |
mbedalvaro | 0:3fe2fe307968 | 226 | wait_us(delayScanning); |
mbedalvaro | 0:3fe2fe307968 | 227 | lut[i][j] += lockin_read(); |
mbedalvaro | 0:3fe2fe307968 | 228 | } |
mbedalvaro | 0:3fe2fe307968 | 229 | //re-scan from left to right |
mbedalvaro | 0:3fe2fe307968 | 230 | for(int i=0; i<LUT_RESOLUTION; i++){ |
mbedalvaro | 0:3fe2fe307968 | 231 | x = shiftX*i + offsetX; |
mbedalvaro | 0:3fe2fe307968 | 232 | writeOutX(int(x)); |
mbedalvaro | 0:3fe2fe307968 | 233 | wait_us(delayScanning); |
mbedalvaro | 0:3fe2fe307968 | 234 | lut[i][j] += lockin_read(); |
mbedalvaro | 0:3fe2fe307968 | 235 | } |
mbedalvaro | 0:3fe2fe307968 | 236 | } |
mbedalvaro | 0:3fe2fe307968 | 237 | } |
mbedalvaro | 0:3fe2fe307968 | 238 | |
mbedalvaro | 0:3fe2fe307968 | 239 | |
mbedalvaro | 0:3fe2fe307968 | 240 | //save tab in file |
mbedalvaro | 0:3fe2fe307968 | 241 | FILE *fp; |
mbedalvaro | 0:3fe2fe307968 | 242 | #ifdef LUT_FILENAME |
mbedalvaro | 0:3fe2fe307968 | 243 | fp = fopen(LUT_FILENAME, "w"); // Open file on the local file system for writing |
mbedalvaro | 0:3fe2fe307968 | 244 | fwrite(lut,sizeof(uint16),LUT_RESOLUTION*LUT_RESOLUTION,fp); |
mbedalvaro | 0:3fe2fe307968 | 245 | fclose(fp); //close the file (the mBed will appear connected again) |
mbedalvaro | 0:3fe2fe307968 | 246 | #endif |
mbedalvaro | 0:3fe2fe307968 | 247 | |
mbedalvaro | 0:3fe2fe307968 | 248 | #ifdef LUT_H_FILENAME |
mbedalvaro | 0:3fe2fe307968 | 249 | //save tab in Human readable file (not used by the program, this is just for checking) |
mbedalvaro | 0:3fe2fe307968 | 250 | // NOTE: we divide the content of the lut table by NB_SCANS, for easy reading (values should be between 0-4095) |
mbedalvaro | 0:3fe2fe307968 | 251 | fp = fopen(LUT_H_FILENAME, "w"); // Open file on the local file system for writing |
mbedalvaro | 0:3fe2fe307968 | 252 | fprintf(fp, "scan resolution: %d x %d\r\n",LUT_RESOLUTION, LUT_RESOLUTION); |
mbedalvaro | 0:3fe2fe307968 | 253 | for(int j=0; j<LUT_RESOLUTION; j++){ |
mbedalvaro | 0:3fe2fe307968 | 254 | for(int i=0; i<LUT_RESOLUTION; i++){ |
mbedalvaro | 0:3fe2fe307968 | 255 | fprintf(fp, "X=%d,\tY=%d,\tI=%d\t \r\n", int(shiftX*i + offsetX), int(shiftY*j + offsetY), int(1.0*lut[i][j]/NB_SCANS) ); |
mbedalvaro | 0:3fe2fe307968 | 256 | } |
mbedalvaro | 0:3fe2fe307968 | 257 | } |
mbedalvaro | 0:3fe2fe307968 | 258 | fclose(fp); //close the file (the mBed will appear connected again) |
mbedalvaro | 0:3fe2fe307968 | 259 | #endif |
mbedalvaro | 0:3fe2fe307968 | 260 | |
mbedalvaro | 0:3fe2fe307968 | 261 | } |
mbedalvaro | 0:3fe2fe307968 | 262 | |
mbedalvaro | 0:3fe2fe307968 | 263 | |
mbedalvaro | 0:3fe2fe307968 | 264 | //Return the lockin value "corrected with the Look-UpTable" - this means a RATIO between two reflectivities (and normally, this is <1). |
mbedalvaro | 0:3fe2fe307968 | 265 | float HardwareIO::lockInCorrectedValue(unsigned short x, unsigned short y){ |
mbedalvaro | 0:3fe2fe307968 | 266 | //*******Correction using DIRECT approximation |
mbedalvaro | 0:3fe2fe307968 | 267 | #ifdef LUT_DIRECT |
mbedalvaro | 0:3fe2fe307968 | 268 | return 2.0* NB_SCANS * lockin_read() / (lut[x >> LUT_BITS_SHIFT][y >> LUT_BITS_SHIFT]); // 2 * NB_SCANS is the number of recorded sample added to one position of the LUT (scan is performed twice: left-right and right-left) |
mbedalvaro | 0:3fe2fe307968 | 269 | #endif |
mbedalvaro | 0:3fe2fe307968 | 270 | |
mbedalvaro | 0:3fe2fe307968 | 271 | //*******Correction using BILINEAR approximation |
mbedalvaro | 0:3fe2fe307968 | 272 | #ifdef LUT_BILINEAR |
mbedalvaro | 0:3fe2fe307968 | 273 | unsigned short X = x >> LUT_BITS_SHIFT; //mirror "x" is 12bits, LUT "X" needs 4bits when lut is 17x17 |
mbedalvaro | 0:3fe2fe307968 | 274 | unsigned short Y = y >> LUT_BITS_SHIFT; //mirror "y" is 12bits, LUT "Y" needs 4bits when lut is 17x17 |
mbedalvaro | 0:3fe2fe307968 | 275 | float dx = 1.0*(x & LUT_BITS_MASK)/(LUT_BITS_MASK+1); //weight to apply on X (mask with 255 and norm) |
mbedalvaro | 0:3fe2fe307968 | 276 | float dy = 1.0*(y & LUT_BITS_MASK)/(LUT_BITS_MASK+1); //weight to apply on Y (mask with 255 and norm) |
mbedalvaro | 0:3fe2fe307968 | 277 | |
mbedalvaro | 0:3fe2fe307968 | 278 | //Wheighted mean approximation of the Look-Up Table at the position (x,y): |
mbedalvaro | 0:3fe2fe307968 | 279 | float wmLUT = (1-dy)*( (1-dx)*lut[X][Y] + dx*lut[X+1][Y] ) + dy*( (1-dx)*lut[X][Y+1] + dx*lut[X+1][Y+1] ); |
mbedalvaro | 0:3fe2fe307968 | 280 | |
mbedalvaro | 0:3fe2fe307968 | 281 | return 2.0* NB_SCANS * lockin_read() / wmLUT;// 2 * NB_SCANS is the number of recorded sample added to one position of the LUT (scan is performed twice: left-right and right-left) |
mbedalvaro | 0:3fe2fe307968 | 282 | #endif |
mbedalvaro | 0:3fe2fe307968 | 283 | |
mbedalvaro | 0:3fe2fe307968 | 284 | //*******Correction using LINEAR approximation |
mbedalvaro | 0:3fe2fe307968 | 285 | #ifdef LUT_LINEAR |
mbedalvaro | 0:3fe2fe307968 | 286 | unsigned short X = x >> LUT_BITS_SHIFT; //mirror "x" is 12bits, LUT "X" needs 4bits when lut is 17x17 |
mbedalvaro | 0:3fe2fe307968 | 287 | unsigned short Y = y >> LUT_BITS_SHIFT; //mirror "y" is 12bits, LUT "Y" needs 4bits when lut is 17x17 |
mbedalvaro | 0:3fe2fe307968 | 288 | float dx = 1.0*(x & LUT_BITS_MASK)/(LUT_BITS_MASK+1); //weight to apply on X (mask with 255 and norm) |
mbedalvaro | 0:3fe2fe307968 | 289 | float dy = 1.0*(y & LUT_BITS_MASK)/(LUT_BITS_MASK+1); //weight to apply on Y (mask with 255 and norm) |
mbedalvaro | 0:3fe2fe307968 | 290 | float linearLUT, dzx, dzy; |
mbedalvaro | 0:3fe2fe307968 | 291 | |
mbedalvaro | 0:3fe2fe307968 | 292 | if(dx>dy){ //if the position is on the "top-right" triangle |
mbedalvaro | 0:3fe2fe307968 | 293 | dzx = (lut[X+1][Y] - lut[X][Y]) * dx; |
mbedalvaro | 0:3fe2fe307968 | 294 | dzy = (lut[X+1][Y+1] - lut[X+1][Y]) * dy; |
mbedalvaro | 0:3fe2fe307968 | 295 | } |
mbedalvaro | 0:3fe2fe307968 | 296 | else{ //if the position is on the "bottom-left" triangle |
mbedalvaro | 0:3fe2fe307968 | 297 | dzy = (lut[X][Y+1] - lut[X][Y]) * dy; |
mbedalvaro | 0:3fe2fe307968 | 298 | dzx = (lut[X+1][Y+1] - lut[X][Y+1]) * dx; |
mbedalvaro | 0:3fe2fe307968 | 299 | } |
mbedalvaro | 0:3fe2fe307968 | 300 | |
mbedalvaro | 0:3fe2fe307968 | 301 | //linear approximation of the Look-Up Table at the position (x,y): |
mbedalvaro | 0:3fe2fe307968 | 302 | linearLUT = lut[X][Y] + dzx + dzy; |
mbedalvaro | 0:3fe2fe307968 | 303 | return 2.0* NB_SCANS * lockin_read() / linearLUT; // 2 * NB_SCANS is the number of recorded sample added to one position of the LUT (scan is performed twice: left-right and right-left) |
mbedalvaro | 0:3fe2fe307968 | 304 | |
mbedalvaro | 0:3fe2fe307968 | 305 | #endif |
mbedalvaro | 0:3fe2fe307968 | 306 | |
mbedalvaro | 0:3fe2fe307968 | 307 | //*******No corrections, just return the value divided by 4096 (this means we are assuming that the surface is everywhere perfectly reflective - we supposedly get the max value always) |
mbedalvaro | 0:3fe2fe307968 | 308 | #ifdef NO_LUT |
mbedalvaro | 0:3fe2fe307968 | 309 | return 1.0* lockin_read()/4096; |
mbedalvaro | 0:3fe2fe307968 | 310 | #endif |
mbedalvaro | 0:3fe2fe307968 | 311 | |
mbedalvaro | 0:3fe2fe307968 | 312 | } |