Laser Sensing Display for UI interfaces in the real world

Dependencies:   mbed

Fork of skinGames_forktest by Alvaro Cassinelli

Committer:
mbedalvaro
Date:
Wed Mar 13 11:38:48 2013 +0000
Revision:
36:233b12d0b1f0
Parent:
35:35af5086ab4f
Child:
37:fa6b1f15819f
modified the way the "is touched" mode works

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedalvaro 31:5f039cbddee8 1 #include "hardwareIO.h"
mbedalvaro 31:5f039cbddee8 2
mbedalvaro 31:5f039cbddee8 3 HardwareIO IO; // preintantiation of cross-file global object IO
mbedalvaro 31:5f039cbddee8 4
mbedalvaro 31:5f039cbddee8 5 // -------------------------------------- (0) SETUP ALL IO (call this in the setup() function in main program)
mbedalvaro 31:5f039cbddee8 6
mbedalvaro 31:5f039cbddee8 7 Serial pc(USBTX, USBRX); // tx, rx
mbedalvaro 31:5f039cbddee8 8 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
mbedalvaro 31:5f039cbddee8 9
mbedalvaro 31:5f039cbddee8 10 SPI spiDAC(MOSI_PIN, MISO_PIN, SCK_PIN); // mosi, miso, sclk
mbedalvaro 31:5f039cbddee8 11 DigitalOut csDAC(CS_DAC_MIRRORS);
mbedalvaro 34:1244fa3f2559 12
mbedalvaro 31:5f039cbddee8 13 DigitalOut Laser_Red(LASER_RED_PIN); // NOTE: this is NOT the lock in sensing laser (actually, not used yet)
mbedalvaro 31:5f039cbddee8 14 DigitalOut Laser_Green(LASER_GREEN_PIN);
mbedalvaro 31:5f039cbddee8 15 DigitalOut Laser_Blue(LASER_BLUE_PIN);
mbedalvaro 34:1244fa3f2559 16
mbedalvaro 34:1244fa3f2559 17 // Some manual controls over the hardware function:
mbedalvaro 34:1244fa3f2559 18 InterruptIn switchOne(SWITCH_ONE);
mbedalvaro 34:1244fa3f2559 19 DigitalOut ledSwitchOne(LED_SWITCH_ONE);
mbedalvaro 34:1244fa3f2559 20 InterruptIn switchTwo(SWITCH_TWO);
mbedalvaro 34:1244fa3f2559 21 //AnalogIn ain(POT_ANALOG_INPUT);
mbedalvaro 31:5f039cbddee8 22
mbedalvaro 31:5f039cbddee8 23 void HardwareIO::init(void) {
mbedalvaro 34:1244fa3f2559 24
mbedalvaro 34:1244fa3f2559 25 // Set laser powers down:
mbedalvaro 34:1244fa3f2559 26 setRedPower(0);// TTL red laser (not used - actually not present)
mbedalvaro 34:1244fa3f2559 27 setGreenPower(0);
mbedalvaro 34:1244fa3f2559 28 setBluePower(0);
mbedalvaro 31:5f039cbddee8 29
mbedalvaro 31:5f039cbddee8 30 //Serial Communication setup:
mbedalvaro 31:5f039cbddee8 31 pc.baud(115200);//
mbedalvaro 31:5f039cbddee8 32 // pc.baud(921600);//115200);//
mbedalvaro 31:5f039cbddee8 33
mbedalvaro 31:5f039cbddee8 34 // Setup for lock-in amplifier and pwm references:
mbedalvaro 34:1244fa3f2559 35 setLaserLockinPower(1);// actually this is the Red laser in the hardware
mbedalvaro 31:5f039cbddee8 36 lockin.init();
mbedalvaro 31:5f039cbddee8 37
mbedalvaro 34:1244fa3f2559 38 // Setup for DAC control to move the mirrors:
mbedalvaro 34:1244fa3f2559 39 // Set spi for 8 bit data, high steady state clock,
mbedalvaro 34:1244fa3f2559 40 // second edge capture, with a 10MHz clock rate:
mbedalvaro 31:5f039cbddee8 41 csDAC = 1;
mbedalvaro 31:5f039cbddee8 42 spiDAC.format(16,0);
mbedalvaro 31:5f039cbddee8 43 spiDAC.frequency(16000000);
mbedalvaro 31:5f039cbddee8 44
mbedalvaro 31:5f039cbddee8 45 // default initial mirror position:
mbedalvaro 31:5f039cbddee8 46 writeOutX(CENTER_AD_MIRROR_X);
mbedalvaro 31:5f039cbddee8 47 writeOutY(CENTER_AD_MIRROR_Y);
mbedalvaro 31:5f039cbddee8 48
mbedalvaro 31:5f039cbddee8 49 // Load LUT table:
mbedalvaro 31:5f039cbddee8 50 setLUT();
mbedalvaro 34:1244fa3f2559 51
mbedalvaro 34:1244fa3f2559 52 // Set interrupts on RAISING edge for button-switch functions:
mbedalvaro 34:1244fa3f2559 53 // Note: The pin input will be logic '0' for any voltage on the pin below 0.8v, and '1' for any voltage above 2.0v.
mbedalvaro 34:1244fa3f2559 54 // By default, the InterruptIn is setup with an internal pull-down resistor, but for security and clarity I will do it explicitly here:
mbedalvaro 34:1244fa3f2559 55 switchOne.mode(PullUp); // pull down seems not very good
mbedalvaro 34:1244fa3f2559 56 switchTwo.mode(PullUp);
mbedalvaro 34:1244fa3f2559 57 switchOne.fall(this, &HardwareIO::switchOneInterrupt); // attach the address of the flip function to the falling edge
mbedalvaro 34:1244fa3f2559 58 switchTwo.fall(this, &HardwareIO::switchTwoInterrupt); // attach the address of the flip function to the falling edge
mbedalvaro 35:35af5086ab4f 59 switchOneState=true;
mbedalvaro 34:1244fa3f2559 60 switchTwoState=false;
mbedalvaro 34:1244fa3f2559 61 switchOneChange=false;
mbedalvaro 34:1244fa3f2559 62 switchTwoChange=false;
mbedalvaro 35:35af5086ab4f 63
mbedalvaro 36:233b12d0b1f0 64 // ATTENTION: initial state of the switch should correspond to the inital state of the threshold mode in the constructor of the laser trajectory objects (not nice):
mbedalvaro 35:35af5086ab4f 65 setSwitchOneState(true); //equal to switchOneState=true, plus set led value. False means fixed threshold, true AUTO THRESHOLD (will be the default mode)
mbedalvaro 34:1244fa3f2559 66
mbedalvaro 34:1244fa3f2559 67 // Read and update pot value:
mbedalvaro 34:1244fa3f2559 68 // updatePotValue(); // the value will be ajusted in the range 0-255
mbedalvaro 34:1244fa3f2559 69 }
mbedalvaro 34:1244fa3f2559 70
mbedalvaro 35:35af5086ab4f 71 void HardwareIO::setSwitchOneState(bool newstate) {
mbedalvaro 35:35af5086ab4f 72 switchOneState=newstate;
mbedalvaro 35:35af5086ab4f 73 ledSwitchOne=(switchOneState? 1 :0);
mbedalvaro 35:35af5086ab4f 74 }
mbedalvaro 35:35af5086ab4f 75
mbedalvaro 34:1244fa3f2559 76 // these ISR could do more (like debouncing).
mbedalvaro 34:1244fa3f2559 77 // For the time being, I will debounce electrically with a small capacitor.
mbedalvaro 34:1244fa3f2559 78 void HardwareIO::switchOneInterrupt() {
mbedalvaro 34:1244fa3f2559 79 switchOneState=!switchOneState;
mbedalvaro 34:1244fa3f2559 80 ledSwitchOne=(switchOneState? 1 :0); // this switch has a built-in led
mbedalvaro 34:1244fa3f2559 81 switchOneChange=true;
mbedalvaro 34:1244fa3f2559 82 }
mbedalvaro 34:1244fa3f2559 83 void HardwareIO::switchTwoInterrupt() {
mbedalvaro 34:1244fa3f2559 84 switchTwoState=!switchTwoState;
mbedalvaro 34:1244fa3f2559 85 switchTwoChange=true;
mbedalvaro 34:1244fa3f2559 86 }
mbedalvaro 34:1244fa3f2559 87
mbedalvaro 34:1244fa3f2559 88 bool HardwareIO::switchOneCheck(bool& state) {
mbedalvaro 34:1244fa3f2559 89 if (switchOneChange) {
mbedalvaro 34:1244fa3f2559 90 switchOneChange=false;
mbedalvaro 34:1244fa3f2559 91 state=switchOneState;
mbedalvaro 34:1244fa3f2559 92 return(true);
mbedalvaro 34:1244fa3f2559 93 } else
mbedalvaro 34:1244fa3f2559 94 return(false);
mbedalvaro 34:1244fa3f2559 95 }
mbedalvaro 34:1244fa3f2559 96
mbedalvaro 34:1244fa3f2559 97 bool HardwareIO::switchTwoCheck(bool& state) {
mbedalvaro 34:1244fa3f2559 98 if (switchTwoChange) {
mbedalvaro 34:1244fa3f2559 99 switchTwoChange=false;
mbedalvaro 34:1244fa3f2559 100 state=switchTwoState;
mbedalvaro 34:1244fa3f2559 101 return(true);
mbedalvaro 34:1244fa3f2559 102 } else return(false);
mbedalvaro 34:1244fa3f2559 103 }
mbedalvaro 34:1244fa3f2559 104
mbedalvaro 34:1244fa3f2559 105 unsigned char HardwareIO::updatePotValue() { // this will update the pot value, and return it too.
mbedalvaro 34:1244fa3f2559 106 //The value will be ajusted in the range 0-255
mbedalvaro 34:1244fa3f2559 107 //The 0.0v to 3.3v range of the AnalogIn is represented in software as a normalised floating point number from 0.0 to 1.0.
mbedalvaro 34:1244fa3f2559 108 //potValue=(unsigned char )(ain*255);
mbedalvaro 34:1244fa3f2559 109
mbedalvaro 34:1244fa3f2559 110 lockin.setADC_forLockin(0);
mbedalvaro 34:1244fa3f2559 111
mbedalvaro 34:1244fa3f2559 112 adc.setup(POT_ANALOG_INPUT,1);
mbedalvaro 34:1244fa3f2559 113 wait(1);
mbedalvaro 34:1244fa3f2559 114
mbedalvaro 34:1244fa3f2559 115 //Measure pin POT_ANALOG_INPUT
mbedalvaro 34:1244fa3f2559 116 adc.select(POT_ANALOG_INPUT);
mbedalvaro 34:1244fa3f2559 117 //Start ADC conversion
mbedalvaro 34:1244fa3f2559 118 adc.start();
mbedalvaro 34:1244fa3f2559 119 //Wait for it to complete
mbedalvaro 34:1244fa3f2559 120 while(!adc.done(POT_ANALOG_INPUT));
mbedalvaro 34:1244fa3f2559 121 potValue=adc.read(POT_ANALOG_INPUT);
mbedalvaro 34:1244fa3f2559 122
mbedalvaro 34:1244fa3f2559 123 //Unset pin POT_ANALOG_INPUT
mbedalvaro 34:1244fa3f2559 124 adc.setup(POT_ANALOG_INPUT,0);
mbedalvaro 34:1244fa3f2559 125
mbedalvaro 34:1244fa3f2559 126 lockin.setADC_forLockin(1);
mbedalvaro 34:1244fa3f2559 127
mbedalvaro 34:1244fa3f2559 128 return(potValue);
mbedalvaro 31:5f039cbddee8 129 }
mbedalvaro 31:5f039cbddee8 130
mbedalvaro 31:5f039cbddee8 131 //write on the first DAC, output A (mirror X)
mbedalvaro 31:5f039cbddee8 132 void HardwareIO::writeOutX(unsigned short value){
mbedalvaro 31:5f039cbddee8 133 if(value > MAX_AD_MIRRORS) value = MAX_AD_MIRRORS;
mbedalvaro 31:5f039cbddee8 134 if(value < MIN_AD_MIRRORS) value = MIN_AD_MIRRORS;
mbedalvaro 31:5f039cbddee8 135
mbedalvaro 31:5f039cbddee8 136 value |= 0x7000;
mbedalvaro 31:5f039cbddee8 137 value &= 0x7FFF;
mbedalvaro 31:5f039cbddee8 138
mbedalvaro 31:5f039cbddee8 139 csDAC = 0;
mbedalvaro 31:5f039cbddee8 140 spiDAC.write(value);
mbedalvaro 31:5f039cbddee8 141 csDAC = 1;
mbedalvaro 31:5f039cbddee8 142 }
mbedalvaro 31:5f039cbddee8 143
mbedalvaro 31:5f039cbddee8 144 //write on the first DAC, output B (mirror Y)
mbedalvaro 31:5f039cbddee8 145 void HardwareIO::writeOutY(unsigned short value){
mbedalvaro 31:5f039cbddee8 146 if(value > MAX_AD_MIRRORS) value = MAX_AD_MIRRORS;
mbedalvaro 31:5f039cbddee8 147 if(value < MIN_AD_MIRRORS) value = MIN_AD_MIRRORS;
mbedalvaro 31:5f039cbddee8 148
mbedalvaro 31:5f039cbddee8 149 value |= 0xF000;
mbedalvaro 31:5f039cbddee8 150 value &= 0xFFFF;
mbedalvaro 31:5f039cbddee8 151
mbedalvaro 31:5f039cbddee8 152 csDAC = 0;
mbedalvaro 31:5f039cbddee8 153 spiDAC.write(value);
mbedalvaro 31:5f039cbddee8 154 csDAC = 1;
mbedalvaro 31:5f039cbddee8 155 }
mbedalvaro 31:5f039cbddee8 156
mbedalvaro 34:1244fa3f2559 157 void HardwareIO::setLaserLockinPower(int powerValue){
mbedalvaro 31:5f039cbddee8 158 if(powerValue > 0){
mbedalvaro 31:5f039cbddee8 159 lockin.setLaserPower(true);
mbedalvaro 31:5f039cbddee8 160 }
mbedalvaro 31:5f039cbddee8 161 else{
mbedalvaro 31:5f039cbddee8 162 lockin.setLaserPower(false);
mbedalvaro 31:5f039cbddee8 163 }
mbedalvaro 31:5f039cbddee8 164 }
mbedalvaro 34:1244fa3f2559 165 // THE TTL controlled lasers:
mbedalvaro 34:1244fa3f2559 166 // Note: the red one is not used here
mbedalvaro 34:1244fa3f2559 167 void HardwareIO::setRedPower(int powerValue){
mbedalvaro 34:1244fa3f2559 168 if(powerValue > 0){
mbedalvaro 34:1244fa3f2559 169 Laser_Red = 1;
mbedalvaro 34:1244fa3f2559 170 }
mbedalvaro 34:1244fa3f2559 171 else{
mbedalvaro 34:1244fa3f2559 172 Laser_Red = 0;
mbedalvaro 34:1244fa3f2559 173 }
mbedalvaro 34:1244fa3f2559 174 }
mbedalvaro 31:5f039cbddee8 175 void HardwareIO::setGreenPower(int powerValue){
mbedalvaro 31:5f039cbddee8 176 if(powerValue > 0){
mbedalvaro 31:5f039cbddee8 177 Laser_Green = 1;
mbedalvaro 31:5f039cbddee8 178 }
mbedalvaro 31:5f039cbddee8 179 else{
mbedalvaro 31:5f039cbddee8 180 Laser_Green = 0;
mbedalvaro 31:5f039cbddee8 181 }
mbedalvaro 31:5f039cbddee8 182 }
mbedalvaro 31:5f039cbddee8 183 void HardwareIO::setBluePower(int powerValue){
mbedalvaro 31:5f039cbddee8 184 if(powerValue > 0){
mbedalvaro 31:5f039cbddee8 185 Laser_Blue = 1;
mbedalvaro 31:5f039cbddee8 186 }
mbedalvaro 31:5f039cbddee8 187 else{
mbedalvaro 31:5f039cbddee8 188 Laser_Blue = 0;
mbedalvaro 31:5f039cbddee8 189 }
mbedalvaro 31:5f039cbddee8 190 }
mbedalvaro 31:5f039cbddee8 191
mbedalvaro 31:5f039cbddee8 192 void HardwareIO::setRGBPower(unsigned char color) {
mbedalvaro 34:1244fa3f2559 193 lockin.setLaserPower((color&0x04)>0? true : false); // NOTE: here the "red" is the lockin laser, not the TTL one (not used yet)
mbedalvaro 31:5f039cbddee8 194 Laser_Green=(color&0x02)>>1;
mbedalvaro 31:5f039cbddee8 195 Laser_Blue =color&0x01;
mbedalvaro 31:5f039cbddee8 196 }
mbedalvaro 31:5f039cbddee8 197
mbedalvaro 31:5f039cbddee8 198 void HardwareIO::showLimitsMirrors(int times) {
mbedalvaro 31:5f039cbddee8 199 unsigned short pointsPerLine=150;
mbedalvaro 31:5f039cbddee8 200 int shiftX = (MAX_AD_MIRRORS - MIN_AD_MIRRORS) / pointsPerLine;
mbedalvaro 31:5f039cbddee8 201 int shiftY = (MAX_AD_MIRRORS - MIN_AD_MIRRORS) / pointsPerLine;
mbedalvaro 31:5f039cbddee8 202
mbedalvaro 31:5f039cbddee8 203 Laser_Green=1;
mbedalvaro 31:5f039cbddee8 204
mbedalvaro 31:5f039cbddee8 205 //for (int repeat=0; repeat<times; repeat++) {
mbedalvaro 31:5f039cbddee8 206
mbedalvaro 31:5f039cbddee8 207 Timer t;
mbedalvaro 31:5f039cbddee8 208 t.start();
mbedalvaro 31:5f039cbddee8 209 while(t.read_ms()<times*1000) {
mbedalvaro 31:5f039cbddee8 210
mbedalvaro 31:5f039cbddee8 211 writeOutX(MIN_AD_MIRRORS);writeOutY(MIN_AD_MIRRORS);
mbedalvaro 31:5f039cbddee8 212
mbedalvaro 31:5f039cbddee8 213 for(int j=0; j<pointsPerLine; j++){
mbedalvaro 31:5f039cbddee8 214 wait_us(200);//delay between each points
mbedalvaro 31:5f039cbddee8 215 writeOutY(j*shiftY + MIN_AD_MIRRORS);
mbedalvaro 31:5f039cbddee8 216 }
mbedalvaro 31:5f039cbddee8 217
mbedalvaro 31:5f039cbddee8 218 writeOutX(MIN_AD_MIRRORS);writeOutY(MAX_AD_MIRRORS);
mbedalvaro 31:5f039cbddee8 219 for(int j=0; j<pointsPerLine; j++) {
mbedalvaro 31:5f039cbddee8 220 wait_us(200);//delay between each points
mbedalvaro 31:5f039cbddee8 221 writeOutX(j*shiftX + MIN_AD_MIRRORS);
mbedalvaro 31:5f039cbddee8 222 }
mbedalvaro 31:5f039cbddee8 223
mbedalvaro 31:5f039cbddee8 224 writeOutX(MAX_AD_MIRRORS);writeOutY(MAX_AD_MIRRORS);
mbedalvaro 31:5f039cbddee8 225 for(int j=0; j<pointsPerLine; j++) {
mbedalvaro 31:5f039cbddee8 226 wait_us(200);//delay between each points
mbedalvaro 31:5f039cbddee8 227 writeOutY(-j*shiftX + MAX_AD_MIRRORS);
mbedalvaro 31:5f039cbddee8 228 }
mbedalvaro 31:5f039cbddee8 229
mbedalvaro 31:5f039cbddee8 230 writeOutX(MAX_AD_MIRRORS);writeOutY(MIN_AD_MIRRORS);
mbedalvaro 31:5f039cbddee8 231 for(int j=0; j<pointsPerLine; j++) {
mbedalvaro 31:5f039cbddee8 232 wait_us(200);//delay between each points
mbedalvaro 31:5f039cbddee8 233 writeOutX(-j*shiftX + MAX_AD_MIRRORS);
mbedalvaro 31:5f039cbddee8 234 }
mbedalvaro 31:5f039cbddee8 235
mbedalvaro 31:5f039cbddee8 236 }
mbedalvaro 31:5f039cbddee8 237 t.stop();
mbedalvaro 31:5f039cbddee8 238 Laser_Green=0;
mbedalvaro 31:5f039cbddee8 239 }
mbedalvaro 31:5f039cbddee8 240
mbedalvaro 31:5f039cbddee8 241 void HardwareIO::scan_serial(unsigned short pointsPerLine){
mbedalvaro 31:5f039cbddee8 242 //scan the total surface with a custom resolution
mbedalvaro 31:5f039cbddee8 243 //send the lockin value for each point as a byte on the serial port to the PC
mbedalvaro 31:5f039cbddee8 244 //use "scanSLP_save" to see the data on processing
mbedalvaro 35:35af5086ab4f 245 // First, set the red laser on, and other off:
mbedalvaro 35:35af5086ab4f 246 setRGBPower(0x4);
mbedalvaro 35:35af5086ab4f 247 wait_us(1000); // just to give time to the lockin to set
mbedalvaro 35:35af5086ab4f 248
mbedalvaro 31:5f039cbddee8 249 int shiftX = (MAX_AD_MIRRORS - MIN_AD_MIRRORS) / pointsPerLine;
mbedalvaro 31:5f039cbddee8 250 int shiftY = (MAX_AD_MIRRORS - MIN_AD_MIRRORS) / pointsPerLine;
mbedalvaro 31:5f039cbddee8 251
mbedalvaro 31:5f039cbddee8 252 for(int j=0; j<pointsPerLine; j++){
mbedalvaro 31:5f039cbddee8 253 writeOutX(MIN_AD_MIRRORS);
mbedalvaro 31:5f039cbddee8 254 writeOutY(j*shiftY + MIN_AD_MIRRORS);
mbedalvaro 31:5f039cbddee8 255
mbedalvaro 31:5f039cbddee8 256 wait_us(300);//begining of line delay
mbedalvaro 31:5f039cbddee8 257 for(int i=0; i<pointsPerLine; i++){
mbedalvaro 31:5f039cbddee8 258 writeOutX(i*shiftX + MIN_AD_MIRRORS);
mbedalvaro 31:5f039cbddee8 259
mbedalvaro 31:5f039cbddee8 260 wait_us(200);//delay between each points
mbedalvaro 31:5f039cbddee8 261
mbedalvaro 31:5f039cbddee8 262 // SEND A VALUE BETWEEN 0 and 255:
mbedalvaro 31:5f039cbddee8 263 pc.putc(int(255.0*lockin.getMedianValue()/4095));//printf("%dL",int(valueLockin*255));//pc.putc(int(lockin*255));//
mbedalvaro 31:5f039cbddee8 264 }
mbedalvaro 31:5f039cbddee8 265 }
mbedalvaro 31:5f039cbddee8 266 }
mbedalvaro 31:5f039cbddee8 267
mbedalvaro 31:5f039cbddee8 268 //load Look-up Table from LUT.TXT file
mbedalvaro 31:5f039cbddee8 269 //or create the file with scanLUT() if not existing.
mbedalvaro 31:5f039cbddee8 270 void HardwareIO::setLUT(){
mbedalvaro 31:5f039cbddee8 271
mbedalvaro 31:5f039cbddee8 272 FILE *fp = fopen(LUT_FILENAME, "r"); // Open file on the local file system for writing
mbedalvaro 31:5f039cbddee8 273 if(fp){
mbedalvaro 31:5f039cbddee8 274 //load the file into the lut table; keep the SAME resolution!
mbedalvaro 31:5f039cbddee8 275 fread(lut,sizeof(uint16),LUT_RESOLUTION*LUT_RESOLUTION,fp);
mbedalvaro 31:5f039cbddee8 276 fclose(fp);
mbedalvaro 31:5f039cbddee8 277 }
mbedalvaro 31:5f039cbddee8 278 else{
mbedalvaro 31:5f039cbddee8 279 //fclose(fp);
mbedalvaro 31:5f039cbddee8 280 //if the file "LUT.TXT" doesn't exist, create one with scanLUT()
mbedalvaro 31:5f039cbddee8 281 lockin.setLaserPower(true);
mbedalvaro 31:5f039cbddee8 282 scanLUT();
mbedalvaro 31:5f039cbddee8 283 }
mbedalvaro 31:5f039cbddee8 284
mbedalvaro 31:5f039cbddee8 285 }
mbedalvaro 31:5f039cbddee8 286
mbedalvaro 31:5f039cbddee8 287 //scan the total surface with a fixed 2^x resolution
mbedalvaro 31:5f039cbddee8 288 //create the Look-Up Table used to "flatten" the scan according to the position
mbedalvaro 31:5f039cbddee8 289 //
mbedalvaro 31:5f039cbddee8 290 //To Do: maybe detect high frequency to be sure the area is clean and empty?
mbedalvaro 31:5f039cbddee8 291 void HardwareIO::scanLUT(){
mbedalvaro 31:5f039cbddee8 292
mbedalvaro 31:5f039cbddee8 293 //reset lut table
mbedalvaro 31:5f039cbddee8 294 for(int j=0; j<LUT_RESOLUTION; j++){
mbedalvaro 31:5f039cbddee8 295 for(int i=0; i<LUT_RESOLUTION; i++){
mbedalvaro 31:5f039cbddee8 296 lut[i][j] =0;
mbedalvaro 31:5f039cbddee8 297 }
mbedalvaro 31:5f039cbddee8 298 }
mbedalvaro 31:5f039cbddee8 299
mbedalvaro 31:5f039cbddee8 300 int delayScanning = 300; //in us
mbedalvaro 31:5f039cbddee8 301
mbedalvaro 31:5f039cbddee8 302 //define the distance between each points (from 0 to 4096) and the offset (here 0)
mbedalvaro 31:5f039cbddee8 303 float shiftX = 1.0*(MAX_AD_MIRRORS - MIN_AD_MIRRORS) / (LUT_RESOLUTION-1);
mbedalvaro 31:5f039cbddee8 304 float shiftY = 1.0*(MAX_AD_MIRRORS - MIN_AD_MIRRORS) / (LUT_RESOLUTION-1);
mbedalvaro 31:5f039cbddee8 305 float offsetX = MIN_AD_MIRRORS;
mbedalvaro 31:5f039cbddee8 306 float offsetY = MIN_AD_MIRRORS;
mbedalvaro 31:5f039cbddee8 307
mbedalvaro 31:5f039cbddee8 308 //move the mirrors to the first position
mbedalvaro 31:5f039cbddee8 309 writeOutX(MAX_AD_MIRRORS);writeOutY(MIN_AD_MIRRORS);
mbedalvaro 31:5f039cbddee8 310 wait_us(500);
mbedalvaro 31:5f039cbddee8 311
mbedalvaro 31:5f039cbddee8 312 float x, y;
mbedalvaro 31:5f039cbddee8 313
mbedalvaro 31:5f039cbddee8 314 //scan the surface NB_SCANS times
mbedalvaro 31:5f039cbddee8 315 //the total value in lut[i][j] shouldn't exceed uint16 !!!
mbedalvaro 31:5f039cbddee8 316 for(int loop=0; loop<NB_SCANS; loop++){
mbedalvaro 31:5f039cbddee8 317 for(int j=0; j<LUT_RESOLUTION; j++){
mbedalvaro 31:5f039cbddee8 318 y = shiftY*j + offsetY ;
mbedalvaro 31:5f039cbddee8 319 writeOutY(int(y));
mbedalvaro 31:5f039cbddee8 320 //scan from right to left
mbedalvaro 31:5f039cbddee8 321 for(int i=LUT_RESOLUTION-1; i>=0; i--){
mbedalvaro 31:5f039cbddee8 322 x = shiftX*i + offsetX;
mbedalvaro 31:5f039cbddee8 323 writeOutX(int(x));
mbedalvaro 31:5f039cbddee8 324 wait_us(delayScanning);
mbedalvaro 31:5f039cbddee8 325 lut[i][j] += lockin_read();
mbedalvaro 31:5f039cbddee8 326 }
mbedalvaro 31:5f039cbddee8 327 //re-scan from left to right
mbedalvaro 31:5f039cbddee8 328 for(int i=0; i<LUT_RESOLUTION; i++){
mbedalvaro 31:5f039cbddee8 329 x = shiftX*i + offsetX;
mbedalvaro 31:5f039cbddee8 330 writeOutX(int(x));
mbedalvaro 31:5f039cbddee8 331 wait_us(delayScanning);
mbedalvaro 31:5f039cbddee8 332 lut[i][j] += lockin_read();
mbedalvaro 31:5f039cbddee8 333 }
mbedalvaro 31:5f039cbddee8 334 }
mbedalvaro 31:5f039cbddee8 335 }
mbedalvaro 31:5f039cbddee8 336
mbedalvaro 31:5f039cbddee8 337
mbedalvaro 31:5f039cbddee8 338 //save tab in file
mbedalvaro 31:5f039cbddee8 339 FILE *fp;
mbedalvaro 31:5f039cbddee8 340 #ifdef LUT_FILENAME
mbedalvaro 31:5f039cbddee8 341 fp = fopen(LUT_FILENAME, "w"); // Open file on the local file system for writing
mbedalvaro 31:5f039cbddee8 342 fwrite(lut,sizeof(uint16),LUT_RESOLUTION*LUT_RESOLUTION,fp);
mbedalvaro 31:5f039cbddee8 343 fclose(fp); //close the file (the mBed will appear connected again)
mbedalvaro 31:5f039cbddee8 344 #endif
mbedalvaro 31:5f039cbddee8 345
mbedalvaro 31:5f039cbddee8 346 #ifdef LUT_H_FILENAME
mbedalvaro 31:5f039cbddee8 347 //save tab in Human readable file (not used by the program, this is just for checking)
mbedalvaro 31:5f039cbddee8 348 // NOTE: we divide the content of the lut table by NB_SCANS, for easy reading (values should be between 0-4095)
mbedalvaro 31:5f039cbddee8 349 fp = fopen(LUT_H_FILENAME, "w"); // Open file on the local file system for writing
mbedalvaro 31:5f039cbddee8 350 fprintf(fp, "scan resolution: %d x %d\r\n",LUT_RESOLUTION, LUT_RESOLUTION);
mbedalvaro 31:5f039cbddee8 351 for(int j=0; j<LUT_RESOLUTION; j++){
mbedalvaro 31:5f039cbddee8 352 for(int i=0; i<LUT_RESOLUTION; i++){
mbedalvaro 31:5f039cbddee8 353 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 31:5f039cbddee8 354 }
mbedalvaro 31:5f039cbddee8 355 }
mbedalvaro 31:5f039cbddee8 356 fclose(fp); //close the file (the mBed will appear connected again)
mbedalvaro 31:5f039cbddee8 357 #endif
mbedalvaro 31:5f039cbddee8 358
mbedalvaro 31:5f039cbddee8 359 }
mbedalvaro 31:5f039cbddee8 360
mbedalvaro 31:5f039cbddee8 361
mbedalvaro 31:5f039cbddee8 362 //Return the lockin value "corrected with the Look-UpTable" - this means a RATIO between two reflectivities (and normally, this is <1).
mbedalvaro 31:5f039cbddee8 363 float HardwareIO::lockInCorrectedValue(unsigned short x, unsigned short y){
mbedalvaro 31:5f039cbddee8 364 //*******Correction using DIRECT approximation
mbedalvaro 31:5f039cbddee8 365 #ifdef LUT_DIRECT
mbedalvaro 31:5f039cbddee8 366 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 31:5f039cbddee8 367 #endif
mbedalvaro 31:5f039cbddee8 368
mbedalvaro 31:5f039cbddee8 369 //*******Correction using BILINEAR approximation
mbedalvaro 31:5f039cbddee8 370 #ifdef LUT_BILINEAR
mbedalvaro 31:5f039cbddee8 371 unsigned short X = x >> LUT_BITS_SHIFT; //mirror "x" is 12bits, LUT "X" needs 4bits when lut is 17x17
mbedalvaro 31:5f039cbddee8 372 unsigned short Y = y >> LUT_BITS_SHIFT; //mirror "y" is 12bits, LUT "Y" needs 4bits when lut is 17x17
mbedalvaro 31:5f039cbddee8 373 float dx = 1.0*(x & LUT_BITS_MASK)/(LUT_BITS_MASK+1); //weight to apply on X (mask with 255 and norm)
mbedalvaro 31:5f039cbddee8 374 float dy = 1.0*(y & LUT_BITS_MASK)/(LUT_BITS_MASK+1); //weight to apply on Y (mask with 255 and norm)
mbedalvaro 31:5f039cbddee8 375
mbedalvaro 31:5f039cbddee8 376 //Wheighted mean approximation of the Look-Up Table at the position (x,y):
mbedalvaro 31:5f039cbddee8 377 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 31:5f039cbddee8 378
mbedalvaro 31:5f039cbddee8 379 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 31:5f039cbddee8 380 #endif
mbedalvaro 31:5f039cbddee8 381
mbedalvaro 31:5f039cbddee8 382 //*******Correction using LINEAR approximation
mbedalvaro 31:5f039cbddee8 383 #ifdef LUT_LINEAR
mbedalvaro 31:5f039cbddee8 384 unsigned short X = x >> LUT_BITS_SHIFT; //mirror "x" is 12bits, LUT "X" needs 4bits when lut is 17x17
mbedalvaro 31:5f039cbddee8 385 unsigned short Y = y >> LUT_BITS_SHIFT; //mirror "y" is 12bits, LUT "Y" needs 4bits when lut is 17x17
mbedalvaro 31:5f039cbddee8 386 float dx = 1.0*(x & LUT_BITS_MASK)/(LUT_BITS_MASK+1); //weight to apply on X (mask with 255 and norm)
mbedalvaro 31:5f039cbddee8 387 float dy = 1.0*(y & LUT_BITS_MASK)/(LUT_BITS_MASK+1); //weight to apply on Y (mask with 255 and norm)
mbedalvaro 31:5f039cbddee8 388 float linearLUT, dzx, dzy;
mbedalvaro 31:5f039cbddee8 389
mbedalvaro 31:5f039cbddee8 390 if(dx>dy){ //if the position is on the "top-right" triangle
mbedalvaro 31:5f039cbddee8 391 dzx = (lut[X+1][Y] - lut[X][Y]) * dx;
mbedalvaro 31:5f039cbddee8 392 dzy = (lut[X+1][Y+1] - lut[X+1][Y]) * dy;
mbedalvaro 31:5f039cbddee8 393 }
mbedalvaro 31:5f039cbddee8 394 else{ //if the position is on the "bottom-left" triangle
mbedalvaro 31:5f039cbddee8 395 dzy = (lut[X][Y+1] - lut[X][Y]) * dy;
mbedalvaro 31:5f039cbddee8 396 dzx = (lut[X+1][Y+1] - lut[X][Y+1]) * dx;
mbedalvaro 31:5f039cbddee8 397 }
mbedalvaro 31:5f039cbddee8 398
mbedalvaro 31:5f039cbddee8 399 //linear approximation of the Look-Up Table at the position (x,y):
mbedalvaro 31:5f039cbddee8 400 linearLUT = lut[X][Y] + dzx + dzy;
mbedalvaro 31:5f039cbddee8 401 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 31:5f039cbddee8 402
mbedalvaro 31:5f039cbddee8 403 #endif
mbedalvaro 31:5f039cbddee8 404
mbedalvaro 31:5f039cbddee8 405 //*******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 31:5f039cbddee8 406 #ifdef NO_LUT
mbedalvaro 31:5f039cbddee8 407 return 1.0* lockin_read()/4096;
mbedalvaro 31:5f039cbddee8 408 #endif
mbedalvaro 31:5f039cbddee8 409
mbedalvaro 31:5f039cbddee8 410 }