just a test

Dependencies:   mbed

Fork of scoreLight_Advanced by Alvaro Cassinelli

Committer:
mbedalvaro
Date:
Tue Dec 02 04:28:42 2014 +0000
Revision:
48:7633d8e7b0d3
Parent:
47:2312a8dc9658
this is the working version of the skin games sowtware (aka, scorelight but with pre-determined "games")

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