save loops

Dependencies:   mbed

Committer:
mbedalvaro
Date:
Tue Dec 02 08:29:59 2014 +0000
Revision:
1:3be7b7d050f4
Parent:
0:df6fdd9b99f0
updated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedalvaro 0:df6fdd9b99f0 1
mbedalvaro 0:df6fdd9b99f0 2 #ifndef hardwareIO_h
mbedalvaro 0:df6fdd9b99f0 3 #define hardwareIO_h
mbedalvaro 0:df6fdd9b99f0 4
mbedalvaro 0:df6fdd9b99f0 5 #include "mbed.h"
mbedalvaro 0:df6fdd9b99f0 6 #include "lockin.h"
mbedalvaro 0:df6fdd9b99f0 7 #include "CRotaryEncoder.h"
mbedalvaro 0:df6fdd9b99f0 8
mbedalvaro 0:df6fdd9b99f0 9
mbedalvaro 0:df6fdd9b99f0 10 // potentiometer to change sensitivity by hand, or other things... ACTUALLY THIS IS NOT WORKING because I cannot switch back and forth from
mbedalvaro 0:df6fdd9b99f0 11 // burst mode of analog conversion and "normal" one. Better use a rotary encoder!!
mbedalvaro 0:df6fdd9b99f0 12 #define POT_ANALOG_INPUT p15 // note: analog inputs in the mbed are from 15 to 20
mbedalvaro 0:df6fdd9b99f0 13
mbedalvaro 0:df6fdd9b99f0 14 // Momentary switches triggering INTERRUPT functions:
mbedalvaro 0:df6fdd9b99f0 15 // (Any of the numbered mbed pins can be used as an InterruptIn, except p19 and p20)
mbedalvaro 0:df6fdd9b99f0 16 // NOTE: do not use pins from p21 to p26 because they are all set as pwm output (see lockin.h)
mbedalvaro 0:df6fdd9b99f0 17 #define LED_SWITCH_ONE p9 // digital output pin
mbedalvaro 0:df6fdd9b99f0 18 #define SWITCH_ONE p10 // interrupt pin
mbedalvaro 0:df6fdd9b99f0 19 #define SWITCH_TWO p11 // interrupt pin
mbedalvaro 0:df6fdd9b99f0 20
mbedalvaro 0:df6fdd9b99f0 21 // Two state switch:
mbedalvaro 0:df6fdd9b99f0 22 #define TWO_STATE_SWITCH p12
mbedalvaro 0:df6fdd9b99f0 23
mbedalvaro 0:df6fdd9b99f0 24 // NOTE: the SPI library uses the following pins, but we don't have to set them and inputs or outputs (this is done when the library is initialized, which is done by pre-instantiation.
mbedalvaro 0:df6fdd9b99f0 25 #define SCK_PIN p7 //SPI Clock
mbedalvaro 0:df6fdd9b99f0 26 #define MISO_PIN p6 //SPI input (data comming from DAC, here not connected)
mbedalvaro 0:df6fdd9b99f0 27 #define MOSI_PIN p5 //SPI output (data going to DAC)
mbedalvaro 0:df6fdd9b99f0 28
mbedalvaro 0:df6fdd9b99f0 29 //**** CHIP SELECT pins for MP4922 DAC (mirrors and red laser)
mbedalvaro 0:df6fdd9b99f0 30 #define CS_DAC_MIRRORS p8
mbedalvaro 0:df6fdd9b99f0 31
mbedalvaro 0:df6fdd9b99f0 32 //**** LASERS pins:
mbedalvaro 0:df6fdd9b99f0 33 #define LASER_RED_PIN p28 // NOT YET USED!! (NOTE: this is NOT the locking sensing laser (also called red, but could be infrared...)
mbedalvaro 0:df6fdd9b99f0 34 #define LASER_GREEN_PIN p29 // USED (TTL control)
mbedalvaro 0:df6fdd9b99f0 35 #define LASER_BLUE_PIN p30 // USED (TTL control)
mbedalvaro 0:df6fdd9b99f0 36
mbedalvaro 0:df6fdd9b99f0 37 //**** MIRRORS:
mbedalvaro 0:df6fdd9b99f0 38 //The DAC is 12 bytes capable (Max=4096), but we will limit this a little.
mbedalvaro 0:df6fdd9b99f0 39 #define MAX_AD_MIRRORS 4000//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.
mbedalvaro 0:df6fdd9b99f0 40 #define MIN_AD_MIRRORS 155//250 // note: 0 is 0 volts for the SPI voltage.
mbedalvaro 0:df6fdd9b99f0 41 // We assume that the center of the mirror is at MAX_AD_MIRRORS/2 = 2000:
mbedalvaro 0:df6fdd9b99f0 42 #define CENTER_AD_MIRROR_X 2047 // This MUST BE the direction of the photodetector.
mbedalvaro 0:df6fdd9b99f0 43 #define CENTER_AD_MIRROR_Y 2047 // This MUST BE the direction of the photodetector.
mbedalvaro 0:df6fdd9b99f0 44
mbedalvaro 0:df6fdd9b99f0 45
mbedalvaro 0:df6fdd9b99f0 46 //**** Look-Up Table:
mbedalvaro 0:df6fdd9b99f0 47 #define uint16 unsigned short
mbedalvaro 0:df6fdd9b99f0 48 #define LUT_RESOLUTION 33 // resolution of the Look-Up Table (power of 2 +1)
mbedalvaro 0:df6fdd9b99f0 49 #define LUT_BITS_SHIFT 7 // bits shift from mirror DAC (12 bits) to LUT ( root_square(LUT_RESOLUTION - 1) )
mbedalvaro 0:df6fdd9b99f0 50 #define LUT_BITS_MASK 127 // bits mask to obtain the position remainder ( 2^LUT_BITS_SHIFT - 1 )
mbedalvaro 0:df6fdd9b99f0 51 // possible configurations:
mbedalvaro 0:df6fdd9b99f0 52 // LUT_RESOLUTION LUT_BITS_SHIFT LUT_BITS_MASK
mbedalvaro 0:df6fdd9b99f0 53 // 9 9 511
mbedalvaro 0:df6fdd9b99f0 54 // 17 8 255
mbedalvaro 0:df6fdd9b99f0 55 // 33 7 127
mbedalvaro 0:df6fdd9b99f0 56 // 65 6 63
mbedalvaro 0:df6fdd9b99f0 57 // ...
mbedalvaro 0:df6fdd9b99f0 58 #define NB_SCANS 8 // number of scans performed to generate the LUT table (actually, each site CUMULATES NB_SCANS values)
mbedalvaro 0:df6fdd9b99f0 59 // IMPORTANT: NB_SCANS*4095 should not exceed the capacity of uint16, this is 2^16-1=65535.
mbedalvaro 0:df6fdd9b99f0 60 // In other terms, NB_SCANS should be < 65535/4095=16
mbedalvaro 0:df6fdd9b99f0 61
mbedalvaro 0:df6fdd9b99f0 62 #define LUT_FILENAME "/local/LUT.txt"
mbedalvaro 0:df6fdd9b99f0 63
mbedalvaro 0:df6fdd9b99f0 64 // For checking (define if human readable file is required - note: it is not used by the program, just as output for human reading)
mbedalvaro 0:df6fdd9b99f0 65 #define LUT_H_FILENAME "/local/LUT_pos.txt"
mbedalvaro 0:df6fdd9b99f0 66
mbedalvaro 0:df6fdd9b99f0 67 // Current Look-up table approximation (only one at a time!):
mbedalvaro 0:df6fdd9b99f0 68 //#define LUT_BILINEAR
mbedalvaro 0:df6fdd9b99f0 69 //#define LUT_DIRECT
mbedalvaro 0:df6fdd9b99f0 70 //#define LUT_LINEAR
mbedalvaro 0:df6fdd9b99f0 71 #define NO_LUT
mbedalvaro 0:df6fdd9b99f0 72
mbedalvaro 0:df6fdd9b99f0 73 //Current method for lockin data acquisition and correction
mbedalvaro 0:df6fdd9b99f0 74 #define lockin_read() lockin.getMedianValue() // lockin.getSmoothValue(); //return the average of the value stored on the buffer
mbedalvaro 0:df6fdd9b99f0 75 // lockin.getLastValue(); //return the last conversion of the ADC
mbedalvaro 0:df6fdd9b99f0 76 // lockin.getMedianValue(); //return the median value of the buffer
mbedalvaro 0:df6fdd9b99f0 77
mbedalvaro 0:df6fdd9b99f0 78
mbedalvaro 0:df6fdd9b99f0 79
mbedalvaro 0:df6fdd9b99f0 80 // **** REFERENCE SIGNAL:
mbedalvaro 0:df6fdd9b99f0 81 /*
mbedalvaro 0:df6fdd9b99f0 82 #define testPin_OC1A 11 // this is, output compare pin OC1A //connected to CK2 = lockIn clock
mbedalvaro 0:df6fdd9b99f0 83 #define testPin_OC1B 12 // this is, output compare pin OC1B //connected to CK1 = laser clock
mbedalvaro 0:df6fdd9b99f0 84 #define testPin_OC1C 13
mbedalvaro 0:df6fdd9b99f0 85 */
mbedalvaro 0:df6fdd9b99f0 86
mbedalvaro 0:df6fdd9b99f0 87 // ==================================================================================================================================================================
mbedalvaro 0:df6fdd9b99f0 88
mbedalvaro 0:df6fdd9b99f0 89 class HardwareIO {
mbedalvaro 0:df6fdd9b99f0 90 public:
mbedalvaro 0:df6fdd9b99f0 91
mbedalvaro 0:df6fdd9b99f0 92
mbedalvaro 0:df6fdd9b99f0 93 void init(void);
mbedalvaro 0:df6fdd9b99f0 94
mbedalvaro 0:df6fdd9b99f0 95 //Lock-in acquisition methods:
mbedalvaro 0:df6fdd9b99f0 96 // float LockInRead_Volts();
mbedalvaro 0:df6fdd9b99f0 97 // int LockInRead_AD();
mbedalvaro 0:df6fdd9b99f0 98 // float LockInAD_to_Volts(int);
mbedalvaro 0:df6fdd9b99f0 99
mbedalvaro 0:df6fdd9b99f0 100 //Look-Up Table:
mbedalvaro 0:df6fdd9b99f0 101 unsigned short lut[LUT_RESOLUTION][LUT_RESOLUTION]; //Look-Up Table (uint16 is "unsigned short")
mbedalvaro 0:df6fdd9b99f0 102 void scanLUT(); //create and save the Look-Up Table
mbedalvaro 0:df6fdd9b99f0 103 void setLUT(); //set the Look-Up Table: either from scanning, or from the file LUT.TXT (if it is present)
mbedalvaro 0:df6fdd9b99f0 104 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)
mbedalvaro 0:df6fdd9b99f0 105
mbedalvaro 0:df6fdd9b99f0 106 void scan_serial(unsigned short pointsPerLine = 400);
mbedalvaro 0:df6fdd9b99f0 107
mbedalvaro 0:df6fdd9b99f0 108 void showLimitsMirrors( int times );
mbedalvaro 0:df6fdd9b99f0 109
mbedalvaro 0:df6fdd9b99f0 110 // SPI control for DAC for mirrors and red laser power (low level):
mbedalvaro 0:df6fdd9b99f0 111 void writeOutX(unsigned short value);
mbedalvaro 0:df6fdd9b99f0 112 void writeOutY(unsigned short value);
mbedalvaro 0:df6fdd9b99f0 113
mbedalvaro 0:df6fdd9b99f0 114 // mirror degree-to-AD units conversion factors:
mbedalvaro 0:df6fdd9b99f0 115 //float AD_to_Deg_MIRROR_X;//=1.0*(MAX_DEG_MIRROR_X-MIN_DEG_MIRROR_X)/(MAX_AD_MIRRORS-MIN_AD_MIRRORS);
mbedalvaro 0:df6fdd9b99f0 116 //float AD_to_Deg_MIRROR_Y;//=1.0*(MAX_DEG_MIRROR_Y-MIN_DEG_MIRROR_Y)/(MAX_AD_MIRRORS-MIN_AD_MIRRORS);
mbedalvaro 0:df6fdd9b99f0 117
mbedalvaro 0:df6fdd9b99f0 118 /*
mbedalvaro 0:df6fdd9b99f0 119 // Mirror position:
mbedalvaro 0:df6fdd9b99f0 120 void setMirrorX_Deg(float _Az);
mbedalvaro 0:df6fdd9b99f0 121 void setMirrorY_Deg(float _El);
mbedalvaro 0:df6fdd9b99f0 122 void setMirrorX_AD(int _AzAD);
mbedalvaro 0:df6fdd9b99f0 123 void setMirrorY_AD(int _ElAD);
mbedalvaro 0:df6fdd9b99f0 124 void setMirrorsXY_AD(int _xAD, int _yAD);
mbedalvaro 0:df6fdd9b99f0 125 void setMirrorsCenter();
mbedalvaro 0:df6fdd9b99f0 126 void getAnglesFromAD(float &Az, float &El, int _xAD, int _yAD);
mbedalvaro 0:df6fdd9b99f0 127 //void setZoneDelimiters(...) // this could be here, instead on the LivingSpot class
mbedalvaro 0:df6fdd9b99f0 128 */
mbedalvaro 0:df6fdd9b99f0 129
mbedalvaro 0:df6fdd9b99f0 130
mbedalvaro 0:df6fdd9b99f0 131 //Laser Power, for the moment laser are TTL but we can use int:
mbedalvaro 0:df6fdd9b99f0 132 //if powerValue > 0 ==> 'true'; else 'false'
mbedalvaro 0:df6fdd9b99f0 133 void setLaserLockinPower(int powerLockingLaser);
mbedalvaro 0:df6fdd9b99f0 134 // Red laser (not used)
mbedalvaro 0:df6fdd9b99f0 135 void setRedPower(int powerRed);
mbedalvaro 0:df6fdd9b99f0 136 // Green laser:
mbedalvaro 0:df6fdd9b99f0 137 void setGreenPower(int powerGreen);
mbedalvaro 0:df6fdd9b99f0 138 // Blue laser:
mbedalvaro 0:df6fdd9b99f0 139 void setBluePower(int powerBlue);
mbedalvaro 0:df6fdd9b99f0 140
mbedalvaro 0:df6fdd9b99f0 141 void setRGBPower(unsigned char color); // we will use the 3 LSB bits to set each color, note that here the "red" is the LOCKIN LASER
mbedalvaro 0:df6fdd9b99f0 142
mbedalvaro 0:df6fdd9b99f0 143 //void setupPWM();
mbedalvaro 0:df6fdd9b99f0 144 /* IN ADVANCED HARDWARE:
mbedalvaro 0:df6fdd9b99f0 145 // init PWM for reference generation:
mbedalvaro 0:df6fdd9b99f0 146 void initPWM();
mbedalvaro 0:df6fdd9b99f0 147 // reference signal:
mbedalvaro 0:df6fdd9b99f0 148 void setRefFreq(int);
mbedalvaro 0:df6fdd9b99f0 149 void incRefFreq(int inc=1);
mbedalvaro 0:df6fdd9b99f0 150 void decRefFreq(int dec=1);
mbedalvaro 0:df6fdd9b99f0 151 */
mbedalvaro 0:df6fdd9b99f0 152
mbedalvaro 0:df6fdd9b99f0 153 //float refFreq; // could be private
mbedalvaro 0:df6fdd9b99f0 154
mbedalvaro 0:df6fdd9b99f0 155 // Interrupt "momentary" switches:
mbedalvaro 0:df6fdd9b99f0 156 bool switchOneState, switchTwoState;
mbedalvaro 0:df6fdd9b99f0 157 bool switchOneChange, switchTwoChange;
mbedalvaro 0:df6fdd9b99f0 158 void switchOneInterrupt();
mbedalvaro 0:df6fdd9b99f0 159 void switchTwoInterrupt();
mbedalvaro 0:df6fdd9b99f0 160 bool switchOneCheck(bool& state);
mbedalvaro 0:df6fdd9b99f0 161 bool switchTwoCheck(bool& state);
mbedalvaro 0:df6fdd9b99f0 162 void setSwitchOneState(bool newstate);
mbedalvaro 0:df6fdd9b99f0 163
mbedalvaro 0:df6fdd9b99f0 164 // two state switch:
mbedalvaro 0:df6fdd9b99f0 165 bool twoStateSwitchState;
mbedalvaro 0:df6fdd9b99f0 166 bool twoStateSwitchChange;
mbedalvaro 0:df6fdd9b99f0 167 bool twoStateSwitchCheck(bool& stateswitch);
mbedalvaro 0:df6fdd9b99f0 168
mbedalvaro 0:df6fdd9b99f0 169 // Potentiometer (does not work!!)
mbedalvaro 0:df6fdd9b99f0 170 unsigned char updatePotValue(); // the value will be ajusted in the range 0-255
mbedalvaro 0:df6fdd9b99f0 171 unsigned char potValue;
mbedalvaro 0:df6fdd9b99f0 172
mbedalvaro 0:df6fdd9b99f0 173 private:
mbedalvaro 0:df6fdd9b99f0 174 //DigitalOut Laser_Red, Laser_Green, Laser_Blue;
mbedalvaro 0:df6fdd9b99f0 175
mbedalvaro 0:df6fdd9b99f0 176 };
mbedalvaro 0:df6fdd9b99f0 177
mbedalvaro 0:df6fdd9b99f0 178
mbedalvaro 0:df6fdd9b99f0 179 extern HardwareIO IO; // allows the object IO to be used in other .cpp files (IO is pre-instantiated in hardwareIO.cpp)
mbedalvaro 0:df6fdd9b99f0 180 // 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)
mbedalvaro 0:df6fdd9b99f0 181 extern Serial pc; // allows pc to be manipulated by other .cpp files, even if pc is defined in the hardwareIO.cpp
mbedalvaro 0:df6fdd9b99f0 182
mbedalvaro 0:df6fdd9b99f0 183
mbedalvaro 0:df6fdd9b99f0 184 #endif