Alvaro Cassinelli / Mbed 2 deprecated laserUI

Dependencies:   mbed

Fork of skinGames_forktest by Alvaro Cassinelli

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hardwareIO.h Source File

hardwareIO.h

00001 
00002 #ifndef hardwareIO_h
00003 #define hardwareIO_h
00004 
00005 #include "mbed.h"
00006 #include "lockin.h"
00007 
00008 //#define USING_EXTENAL_DAC // ... or we can do both at the same time: external plus the output for the mbed DAC pins
00009 // ARG: MBED has only one "true" analog output (p18) - the others are pwm.. but perhaps we can set a fast PWM cycle and have enough resolution?
00010 // 10 bit resolution (0-1023) should be ok (a display 1024x1024...). What is the max pwm cycle then? I want to be able to write a point every 100us or so (see
00011 // laserSensingDisplay.h), which means 10kHz. Assuming I have a low pass filter, and I filter several cycles - at least 10 - this means I need a freq of about
00012 // 100kHz (presumably it will work with less than that). Possible? I found this: http://mbed.org/questions/192/what-is-the-maximum-frequency-of-the-PWM/
00013 // It seems that the minimum step is 1us... so, at 100kHz (period of 10us) we will only have 10 levels! (about 3 bits). More precisely, if I want 10 bits resolution, the max
00014 // frequency will be 2^10x1us = 1024us, or about 1kHz... too slow!! 10kHz? => about two bits less, ie, 8 bits (256 levels). Mmm... Perhaps good for testing 
00015 // on an osciloscope, but not good for mirror control.
00016 // #define mbedAnalogX p18 
00017 // #define mbedAnalogY
00018 
00019 
00020 // Serial communication speed:
00021 #define SERIAL_SPEED 115200//230400//115200 //230400
00022 
00023 // potentiometer to change sensitivity by hand, or other things. 
00024 #define POT_ANALOG_INPUT p15 // note: analog inputs in the mbed are from 15 to 20
00025 
00026 // switches triggering interrupt functions:
00027 // (Any of the numbered mbed pins can be used as an InterruptIn, except p19 and p20)
00028 // NOTE: do not use pins from p21 to p26 because they are all set as pwm output (see lockin.h)
00029 #define LED_SWITCH_ONE p9 // digital output pin
00030 #define SWITCH_ONE p10 // interrupt pin
00031 #define SWITCH_TWO p11 // interrupt pin
00032 
00033 //SPI library (for DAC chip) uses the following pins, but we don't have to set them and inputs or outputs 
00034 // (this is done when the library is initialized, which is done by pre-instantiation:
00035 #define SCK_PIN   p7 //SPI Clock
00036 #define MISO_PIN  p6 //SPI input (data comming from DAC, here not connected) 
00037 #define MOSI_PIN  p5 //SPI output (data going to DAC)
00038 
00039 //**** CHIP SELECT pins for MP4922 DAC (mirrors and red laser)
00040 // VERY IMPORTANT: the chip select for the DACs should be different from the default SPI "SS" pin (Slave Select), which is 53 by default (and will be used by the Ethernet Shield). 
00041 #define CS_DAC_MIRRORS   p8 //Chip Select of the first DAC (mirrors)
00042 
00043 //**** LASERS pins:
00044 #define LASER_RED_PIN   p28 // NOT YET USED (TTL control). NOTE: this is NOT the locking sensing laser!
00045 #define LASER_GREEN_PIN p29 // USED (TTL control)
00046 #define LASER_BLUE_PIN  p30 // USED (TTL control)
00047 
00048 //**** MIRRORS: 
00049 //The DAC is 12 bytes capable (Max=4096), but we will limit this a little. 
00050 #define MAX_AD_MIRRORS 3845 // note: 4095 is the absolute maximum for the SPI voltage (5V). This is for checking hardware compliance, but max and min angles can be defined for X and Y in each LivingSpot instance.
00051 #define MIN_AD_MIRRORS 250  // note: 0 is 0 volts for the SPI voltage. 
00052 // We assume that the center of the mirror is at MAX_AD_MIRRORS/2 = 2000:
00053 #define CENTER_AD_MIRROR_X 2047 // This MUST BE the direction of the photodetector. 
00054 #define CENTER_AD_MIRROR_Y 2047 // This MUST BE the direction of the photodetector.
00055 
00056 
00057 //**** Look-Up Table:
00058 #define uint16 unsigned short
00059 #define LUT_RESOLUTION  33   // resolution of the Look-Up Table (power of 2 +1)
00060 #define LUT_BITS_SHIFT  7    // bits shift from mirror DAC (12 bits) to LUT ( root_square(LUT_RESOLUTION - 1) )
00061 #define LUT_BITS_MASK   127   // bits mask to obtain the position remainder ( 2^LUT_BITS_SHIFT - 1 )
00062 //  possible configurations:
00063 //  LUT_RESOLUTION  LUT_BITS_SHIFT  LUT_BITS_MASK
00064 //      9               9               511
00065 //      17              8               255
00066 //      33              7               127
00067 //      65              6               63
00068 //  ...
00069 #define NB_SCANS 8 // number of scans performed to generate the LUT table (actually, each site CUMULATES NB_SCANS values)
00070                    // IMPORTANT: NB_SCANS*4095 should not exceed the capacity of uint16, this is 2^16-1=65535.
00071                    // In other terms, NB_SCANS should be < 65535/4095=16
00072 
00073 #define LUT_FILENAME "/local/LUT.txt"
00074 
00075 // For checking (define if human readable file is required - note: it is not used by the program, just as output for human reading)
00076 #define LUT_H_FILENAME "/local/LUT_pos.txt"
00077 
00078 // Current Look-up table approximation (only one at a time!): 
00079 //#define LUT_BILINEAR 
00080 //#define LUT_DIRECT
00081 //#define LUT_LINEAR
00082 #define NO_LUT
00083 
00084 //Current method for lockin data acquisition and correction
00085 #define lockin_read() lockin.getMedianValue() // lockin.getSmoothValue(); //return the average of the value stored on the buffer
00086                                               // lockin.getLastValue(); //return the last conversion of the ADC
00087                                               // lockin.getMedianValue(); //return the median value of the buffer
00088 
00089 
00090 extern  DigitalOut Laser_Red, Laser_Green, Laser_Blue;
00091 
00092 // LEDS for debugging:
00093 extern DigitalOut myLed1, myLed2, myLed3, myLed4;
00094 
00095 // **** REFERENCE SIGNAL: 
00096 /*
00097 #define testPin_OC1A 11 // this is, output compare pin OC1A //connected to CK2 = lockIn clock
00098 #define testPin_OC1B 12 // this is, output compare pin OC1B //connected to CK1 = laser clock
00099 #define testPin_OC1C 13
00100 */
00101 
00102 // ==================================================================================================================================================================
00103 
00104 class HardwareIO {
00105 public:
00106 
00107 
00108     void init(void);
00109 
00110     //Lock-in acquisition methods:
00111    // float LockInRead_Volts(); 
00112    // int LockInRead_AD(); 
00113   //  float LockInAD_to_Volts(int); 
00114     
00115     //Look-Up Table:
00116     unsigned short lut[LUT_RESOLUTION][LUT_RESOLUTION]; //Look-Up Table (uint16 is "unsigned short")
00117     void scanLUT(); //create and save the Look-Up Table
00118     void setLUT(); //set the Look-Up Table: either from scanning, or from the file LUT.TXT (if it is present)
00119     float lockInCorrectedValue(unsigned short x, unsigned short y); //return the lockin value corrected with the Look-UpTable (this is, a RATIO of reflectivities, and <1)
00120     
00121     void scan_serial(unsigned short pointsPerLine = 400);
00122 
00123     void showLimitsMirrors( unsigned short pointsPerLine, unsigned short timeInSeconds );
00124 //    void showLimitsConcentric(int pointsPerSide, int times);
00125 //    void showCircle(int cv, int cy, float radius, int nmpoints, int times);
00126 //    void showGrid(float cx, float cy, int nx, int ny, float pasx, float pasy, int repeat);
00127 //    void gridCircles(float cx, float cy, int nx, int ny, float pasx, float pasy, float radius, int repeat, boolean sym);
00128 //    void showContinuousGrid(int nx, int ny, float cx, float cy, int width, int height, float pasx, float pasy, int repeat);
00129 
00130 
00131     // SPI control for DAC for mirrors and red laser power (low level): 
00132     void writeOutX(unsigned short value);
00133     void writeOutY(unsigned short value);
00134     void writeOutXY(unsigned short valueX, unsigned short valueY) {writeOutX(valueX); writeOutY(valueY);};
00135     
00136     // mirror degree-to-AD units conversion factors: 
00137     //float AD_to_Deg_MIRROR_X;//=1.0*(MAX_DEG_MIRROR_X-MIN_DEG_MIRROR_X)/(MAX_AD_MIRRORS-MIN_AD_MIRRORS);
00138     //float AD_to_Deg_MIRROR_Y;//=1.0*(MAX_DEG_MIRROR_Y-MIN_DEG_MIRROR_Y)/(MAX_AD_MIRRORS-MIN_AD_MIRRORS);
00139 
00140     /*
00141     // Mirror position:
00142     void setMirrorX_Deg(float _Az); 
00143     void setMirrorY_Deg(float _El);
00144     void setMirrorX_AD(int _AzAD); 
00145     void setMirrorY_AD(int _ElAD);
00146     void setMirrorsXY_AD(int _xAD, int _yAD); 
00147     void setMirrorsCenter();
00148     void getAnglesFromAD(float &Az, float &El, int _xAD, int _yAD); 
00149     //void setZoneDelimiters(...) // this could be here, instead on the LivingSpot class
00150     */
00151     
00152     // Setting the laser power of the SENSING LASER (note: for the moment this is TTL, but could be analog): 
00153     void setLaserLockinPower(int powerLockingLaser);       //if powerValue > 0 ==> 'true'; else 'false'
00154     // Setting the power of the DISPLAYING  lasers: 
00155     // Again: for the moment laser are TTL but these could be analog. Now, it is just: powerValue > 0 ==> 'true'; else 'false'
00156     // Red laser:
00157     void setRedPower(int powerRed);
00158     // Green laser: 
00159     void setGreenPower(int powerGreen);
00160     // Blue laser: 
00161     void setBluePower(int powerBlue);
00162     // Setting all colors at once: 
00163     void setRGBPower(unsigned char color); // we will use the 3 LSB bits to set each color
00164     void switchOffDisplayLasers() {setRGBPower(0x00);}
00165     
00166     //void setupPWM();
00167     /* IN ADVANCED HARDWARE: 
00168     // init PWM for reference generation:
00169     void initPWM();
00170     // reference signal: 
00171     void setRefFreq(int);
00172     void incRefFreq(int inc=1);
00173     void decRefFreq(int dec=1);
00174     */
00175 
00176     //float refFreq; // could be private
00177     
00178     bool switchOneChange, switchTwoChange;
00179     void switchOneInterrupt();
00180     void switchTwoInterrupt();
00181     bool switchOneCheck(bool& state);
00182     bool switchTwoCheck(bool& state);
00183     void setSwitchOneState(bool newstate);
00184 
00185     unsigned char updatePotValue(); // the value will be ajusted in the range 0-255
00186     unsigned char potValue;
00187     
00188     bool switchOneState, switchTwoState;
00189     
00190 private:
00191      //DigitalOut Laser_Red, Laser_Green, Laser_Blue;
00192 };
00193 
00194 
00195 extern HardwareIO IO; // allows the object IO to be used in other .cpp files (IO is pre-instantiated in hardwareIO.cpp)
00196 // NOTE: IO encapsulates many IO functions, but perhaps it is better not to have an IO object - just use each IO function separatedly (as with pc object for instance)
00197 extern Serial pc; // allows pc to be manipulated by other .cpp files, even if pc is defined in the hardwareIO.cpp
00198 
00199 
00200 #endif
00201