Alvaro Cassinelli
/
linearMirrorMotion
simple example program to control one mirror from serial data
Revision 3:3fe7d6b5cf24, committed 2012-10-04
- Comitter:
- mbedalvaro
- Date:
- Thu Oct 04 05:16:25 2012 +0000
- Parent:
- 2:0548c7bf9fba
- Commit message:
- simplest control X Y mirrors;
Changed in this revision
diff -r 0548c7bf9fba -r 3fe7d6b5cf24 laserProjectorHardware.lib --- a/laserProjectorHardware.lib Thu Oct 04 05:14:14 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbedalvaro/code/laserProjectorHardware/#a77af54f9acd
diff -r 0548c7bf9fba -r 3fe7d6b5cf24 laserProjectorHardware/laserProjectorHardware.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/laserProjectorHardware/laserProjectorHardware.cpp Thu Oct 04 05:16:25 2012 +0000 @@ -0,0 +1,135 @@ +#include "laserProjectorHardware.h" + +HardwareIO IO; // preintantiation of cross-file global object IO + +// -------------------------------------- (0) SETUP ALL IO (call this in the setup() function in main program) + +Serial pc(USBTX, USBRX); // tx, rx + + SPI spiDAC(MOSI_PIN, MISO_PIN, SCK_PIN); // mosi, miso, sclk + DigitalOut csDAC(CS_DAC_MIRRORS); + + DigitalOut Laser_Red(LASER_RED_PIN); // NOTE: this is NOT the lock in sensing laser (actually, not used yet) + DigitalOut Laser_Green(LASER_GREEN_PIN); + DigitalOut Laser_Blue(LASER_BLUE_PIN); + +void HardwareIO::init(void) { + Laser_Red = 0; // note: this is not the lockin-laser! + Laser_Green = 0; + Laser_Blue = 0; + + //Serial Communication setup: + pc.baud(115200);// + // pc.baud(921600);//115200);// + + // Setup the spi for 8 bit data, high steady state clock, + // second edge capture, with a 10MHz clock rate + csDAC = 1; + spiDAC.format(16,0); + spiDAC.frequency(16000000); + + // default initial mirror position: + writeOutX(CENTER_AD_MIRROR_X); + writeOutY(CENTER_AD_MIRROR_Y); + +} + +//write on the first DAC, output A (mirror X) +void HardwareIO::writeOutX(unsigned short value){ + if(value > MAX_AD_MIRRORS) value = MAX_AD_MIRRORS; + if(value < MIN_AD_MIRRORS) value = MIN_AD_MIRRORS; + + value |= 0x7000; + value &= 0x7FFF; + + csDAC = 0; + spiDAC.write(value); + csDAC = 1; +} + +//write on the first DAC, output B (mirror Y) +void HardwareIO::writeOutY(unsigned short value){ + if(value > MAX_AD_MIRRORS) value = MAX_AD_MIRRORS; + if(value < MIN_AD_MIRRORS) value = MIN_AD_MIRRORS; + + value |= 0xF000; + value &= 0xFFFF; + + csDAC = 0; + spiDAC.write(value); + csDAC = 1; +} + +void HardwareIO::setRedPower(int powerValue){ + if(powerValue > 0){ + Laser_Red = 1; + } + else{ + Laser_Red = 0; + } +} +void HardwareIO::setGreenPower(int powerValue){ + if(powerValue > 0){ + Laser_Green = 1; + } + else{ + Laser_Green = 0; + } +} +void HardwareIO::setBluePower(int powerValue){ + if(powerValue > 0){ + Laser_Blue = 1; + } + else{ + Laser_Blue = 0; + } +} +void HardwareIO::setRGBPower(unsigned char color) { + //lockin.setLaserPower(color&0x04>0? false : true); + Laser_Red=color&0x04>>2; + Laser_Green=(color&0x02)>>1; + Laser_Blue =color&0x01; +} + +void HardwareIO::showLimitsMirrors(int times) { + unsigned short pointsPerLine=150; + int shiftX = (MAX_AD_MIRRORS - MIN_AD_MIRRORS) / pointsPerLine; + int shiftY = (MAX_AD_MIRRORS - MIN_AD_MIRRORS) / pointsPerLine; + + Laser_Green=1; + + //for (int repeat=0; repeat<times; repeat++) { + + Timer t; + t.start(); + while(t.read_ms()<times*1000) { + + writeOutX(MIN_AD_MIRRORS);writeOutY(MIN_AD_MIRRORS); + + for(int j=0; j<pointsPerLine; j++){ + wait_us(200);//delay between each points + writeOutY(j*shiftY + MIN_AD_MIRRORS); + } + + writeOutX(MIN_AD_MIRRORS);writeOutY(MAX_AD_MIRRORS); + for(int j=0; j<pointsPerLine; j++) { + wait_us(200);//delay between each points + writeOutX(j*shiftX + MIN_AD_MIRRORS); + } + + writeOutX(MAX_AD_MIRRORS);writeOutY(MAX_AD_MIRRORS); + for(int j=0; j<pointsPerLine; j++) { + wait_us(200);//delay between each points + writeOutY(-j*shiftX + MAX_AD_MIRRORS); + } + + writeOutX(MAX_AD_MIRRORS);writeOutY(MIN_AD_MIRRORS); + for(int j=0; j<pointsPerLine; j++) { + wait_us(200);//delay between each points + writeOutX(-j*shiftX + MAX_AD_MIRRORS); + } + + } + t.stop(); + Laser_Green=0; +}
diff -r 0548c7bf9fba -r 3fe7d6b5cf24 laserProjectorHardware/laserProjectorHardware.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/laserProjectorHardware/laserProjectorHardware.h Thu Oct 04 05:16:25 2012 +0000 @@ -0,0 +1,73 @@ + +#ifndef hardwareIO_h +#define hardwareIO_h + +#include "mbed.h" + + +//SPI library (for ADC chip) 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: +#define SCK_PIN p7 //SPI Clock +#define MISO_PIN p6 //SPI input (data comming from DAC, here not connected) +#define MOSI_PIN p5 //SPI output (data going to DAC) + +//**** CHIP SELECT pins for MP4922 DAC (mirrors and red laser) +// 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). +#define CS_DAC_MIRRORS p8 //Chip Select of the first DAC (mirrors) + +//**** LASERS pins: +#define LASER_RED_PIN p28 // NOT YET USED (TTL control). NOTE: this is NOT the locking sensing laser! +#define LASER_GREEN_PIN p29 // USED (TTL control) +#define LASER_BLUE_PIN p30 // USED (TTL control) + +//**** MIRRORS: +//The DAC is 12 bytes capable (Max=4096), but we will limit this a little. +#define MAX_AD_MIRRORS 4095 // 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. +#define MIN_AD_MIRRORS 0 // note: 0 is 0 volts for the SPI voltage. +// We assume that the center of the mirror is at MAX_AD_MIRRORS/2 = 2000: +#define CENTER_AD_MIRROR_X 2047 // This MUST BE the direction of the photodetector. +#define CENTER_AD_MIRROR_Y 2047 // This MUST BE the direction of the photodetector. + + +extern DigitalOut Laser_Red, Laser_Green, Laser_Blue; + +// ================================================================================================================================================================== + +class HardwareIO { +public: + + + void init(void); + + void showLimitsMirrors( int times ); + + // SPI control for DAC for mirrors and red laser power (low level): + void writeOutX(unsigned short value); + void writeOutY(unsigned short value); + void writeOutXY(unsigned short valueX, unsigned short valueY) {writeOutX(valueX); writeOutY(valueY);}; + + + //Displaying lasers: + // Again: for the moment laser are TTL but these could be analog. Now, it is just: powerValue > 0 ==> 'true'; else 'false' + // Red laser: + void setRedPower(int powerRed); + // Green laser: + void setGreenPower(int powerGreen); + // Blue laser: + void setBluePower(int powerBlue); + // Setting all colors at once: + void setRGBPower(unsigned char color); // we will use the 3 LSB bits to set each color + + + +private: + +}; + + +extern HardwareIO IO; // allows the object IO to be used in other .cpp files (IO is pre-instantiated in hardwareIO.cpp) +// 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) +extern Serial pc; // allows pc to be manipulated by other .cpp files, even if pc is defined in the hardwareIO.cpp + + +#endif