![](/media/cache/profiles/68a15b5278e4f7c4c056df9d5f1d3b1f.jpg.50x50_q85.jpg)
Example controlling galvanomirrors (X and Y) using the spi DAC MCP4922 and the new platform FRDM_K64F
laserProjectorHardware/laserProjectorHardware.cpp@1:4be6abc4ed43, 2014-05-25 (annotated)
- Committer:
- mbedalvaro
- Date:
- Sun May 25 12:24:21 2014 +0000
- Revision:
- 1:4be6abc4ed43
- Parent:
- 0:26b228dde70c
- Child:
- 2:383b2acec6e4
this works fine, using the laserMouse_Processing controller; Note: for some reason I had to change spiDAC clock to 20MHz...
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedalvaro | 0:26b228dde70c | 1 | #include "laserProjectorHardware.h" |
mbedalvaro | 0:26b228dde70c | 2 | |
mbedalvaro | 0:26b228dde70c | 3 | HardwareIO IO; // preintantiation of cross-file global object IO |
mbedalvaro | 0:26b228dde70c | 4 | |
mbedalvaro | 0:26b228dde70c | 5 | // -------------------------------------- (0) SETUP ALL IO (call this in the setup() function in main program) |
mbedalvaro | 0:26b228dde70c | 6 | |
mbedalvaro | 0:26b228dde70c | 7 | Serial pc(USBTX, USBRX); // tx, rx |
mbedalvaro | 0:26b228dde70c | 8 | |
mbedalvaro | 0:26b228dde70c | 9 | SPI spiDAC(MOSI_PIN, MISO_PIN, SCK_PIN); // mosi, miso, sclk |
mbedalvaro | 0:26b228dde70c | 10 | DigitalOut csDAC(CS_DAC_MIRRORS); |
mbedalvaro | 0:26b228dde70c | 11 | |
mbedalvaro | 0:26b228dde70c | 12 | DigitalOut Laser_Red(LASER_RED_PIN); // NOTE: this is NOT the lock in sensing laser (actually, not used yet) |
mbedalvaro | 0:26b228dde70c | 13 | DigitalOut Laser_Green(LASER_GREEN_PIN); |
mbedalvaro | 0:26b228dde70c | 14 | DigitalOut Laser_Blue(LASER_BLUE_PIN); |
mbedalvaro | 0:26b228dde70c | 15 | |
mbedalvaro | 0:26b228dde70c | 16 | void HardwareIO::init(void) { |
mbedalvaro | 0:26b228dde70c | 17 | Laser_Red = 1; // note: this is not the lockin-laser! it is also negated |
mbedalvaro | 0:26b228dde70c | 18 | Laser_Green = 0; |
mbedalvaro | 0:26b228dde70c | 19 | Laser_Blue = 0; |
mbedalvaro | 0:26b228dde70c | 20 | |
mbedalvaro | 0:26b228dde70c | 21 | //Serial Communication setup: |
mbedalvaro | 0:26b228dde70c | 22 | pc.baud(115200);// |
mbedalvaro | 0:26b228dde70c | 23 | // pc.baud(921600);//115200);// |
mbedalvaro | 0:26b228dde70c | 24 | |
mbedalvaro | 0:26b228dde70c | 25 | // Setup the spi for 8 bit data, high steady state clock, |
mbedalvaro | 1:4be6abc4ed43 | 26 | // second edge capture, with a 20MHz clock rate (max for the MCP4922) |
mbedalvaro | 0:26b228dde70c | 27 | csDAC = 1; |
mbedalvaro | 0:26b228dde70c | 28 | spiDAC.format(16,0); |
mbedalvaro | 1:4be6abc4ed43 | 29 | spiDAC.frequency(20000000); |
mbedalvaro | 0:26b228dde70c | 30 | |
mbedalvaro | 0:26b228dde70c | 31 | // default initial mirror position: |
mbedalvaro | 0:26b228dde70c | 32 | writeOutX(CENTER_AD_MIRROR_X); |
mbedalvaro | 0:26b228dde70c | 33 | writeOutY(CENTER_AD_MIRROR_Y); |
mbedalvaro | 0:26b228dde70c | 34 | |
mbedalvaro | 0:26b228dde70c | 35 | } |
mbedalvaro | 0:26b228dde70c | 36 | |
mbedalvaro | 0:26b228dde70c | 37 | //write on the first DAC, output A (mirror X) |
mbedalvaro | 0:26b228dde70c | 38 | void HardwareIO::writeOutX(unsigned short value){ |
mbedalvaro | 0:26b228dde70c | 39 | if(value > MAX_AD_MIRRORS) value = MAX_AD_MIRRORS; |
mbedalvaro | 0:26b228dde70c | 40 | if(value < MIN_AD_MIRRORS) value = MIN_AD_MIRRORS; |
mbedalvaro | 0:26b228dde70c | 41 | |
mbedalvaro | 0:26b228dde70c | 42 | value |= 0xF000; |
mbedalvaro | 0:26b228dde70c | 43 | value &= 0xFFFF; |
mbedalvaro | 0:26b228dde70c | 44 | |
mbedalvaro | 0:26b228dde70c | 45 | csDAC = 0; |
mbedalvaro | 0:26b228dde70c | 46 | spiDAC.write(value); |
mbedalvaro | 0:26b228dde70c | 47 | csDAC = 1; |
mbedalvaro | 0:26b228dde70c | 48 | } |
mbedalvaro | 0:26b228dde70c | 49 | |
mbedalvaro | 0:26b228dde70c | 50 | //write on the first DAC, output B (mirror Y) |
mbedalvaro | 0:26b228dde70c | 51 | void HardwareIO::writeOutY(unsigned short value){ |
mbedalvaro | 0:26b228dde70c | 52 | if(value > MAX_AD_MIRRORS) value = MAX_AD_MIRRORS; |
mbedalvaro | 0:26b228dde70c | 53 | if(value < MIN_AD_MIRRORS) value = MIN_AD_MIRRORS; |
mbedalvaro | 0:26b228dde70c | 54 | |
mbedalvaro | 0:26b228dde70c | 55 | value |= 0x7000; |
mbedalvaro | 0:26b228dde70c | 56 | value &= 0x7FFF; |
mbedalvaro | 0:26b228dde70c | 57 | |
mbedalvaro | 0:26b228dde70c | 58 | csDAC = 0; |
mbedalvaro | 0:26b228dde70c | 59 | spiDAC.write(value); |
mbedalvaro | 0:26b228dde70c | 60 | csDAC = 1; |
mbedalvaro | 0:26b228dde70c | 61 | } |
mbedalvaro | 0:26b228dde70c | 62 | |
mbedalvaro | 0:26b228dde70c | 63 | void HardwareIO::setRedPower(int powerValue){ |
mbedalvaro | 0:26b228dde70c | 64 | if(powerValue > 0){ |
mbedalvaro | 1:4be6abc4ed43 | 65 | Laser_Red = 0; // att! it's negated for the laser diode module "vector" |
mbedalvaro | 0:26b228dde70c | 66 | } |
mbedalvaro | 0:26b228dde70c | 67 | else{ |
mbedalvaro | 0:26b228dde70c | 68 | Laser_Red = 1; |
mbedalvaro | 0:26b228dde70c | 69 | } |
mbedalvaro | 0:26b228dde70c | 70 | } |
mbedalvaro | 0:26b228dde70c | 71 | void HardwareIO::setGreenPower(int powerValue){ |
mbedalvaro | 0:26b228dde70c | 72 | if(powerValue > 0){ |
mbedalvaro | 0:26b228dde70c | 73 | Laser_Green = 1; |
mbedalvaro | 0:26b228dde70c | 74 | } |
mbedalvaro | 0:26b228dde70c | 75 | else{ |
mbedalvaro | 0:26b228dde70c | 76 | Laser_Green = 0; |
mbedalvaro | 0:26b228dde70c | 77 | } |
mbedalvaro | 0:26b228dde70c | 78 | } |
mbedalvaro | 0:26b228dde70c | 79 | void HardwareIO::setBluePower(int powerValue){ |
mbedalvaro | 0:26b228dde70c | 80 | if(powerValue > 0){ |
mbedalvaro | 0:26b228dde70c | 81 | Laser_Blue = 1; |
mbedalvaro | 0:26b228dde70c | 82 | } |
mbedalvaro | 0:26b228dde70c | 83 | else{ |
mbedalvaro | 0:26b228dde70c | 84 | Laser_Blue = 0; |
mbedalvaro | 0:26b228dde70c | 85 | } |
mbedalvaro | 0:26b228dde70c | 86 | } |
mbedalvaro | 0:26b228dde70c | 87 | void HardwareIO::setRGBPower(unsigned char color) { |
mbedalvaro | 0:26b228dde70c | 88 | //lockin.setLaserPower(color&0x04>0? false : true); |
mbedalvaro | 0:26b228dde70c | 89 | Laser_Red=color&0x04>>2; |
mbedalvaro | 0:26b228dde70c | 90 | Laser_Green=(color&0x02)>>1; |
mbedalvaro | 0:26b228dde70c | 91 | Laser_Blue =color&0x01; |
mbedalvaro | 0:26b228dde70c | 92 | } |
mbedalvaro | 0:26b228dde70c | 93 | |
mbedalvaro | 0:26b228dde70c | 94 | void HardwareIO::showLimitsMirrors(int times) { |
mbedalvaro | 0:26b228dde70c | 95 | unsigned short pointsPerLine=150; |
mbedalvaro | 0:26b228dde70c | 96 | int shiftX = (MAX_AD_MIRRORS - MIN_AD_MIRRORS) / pointsPerLine; |
mbedalvaro | 0:26b228dde70c | 97 | int shiftY = (MAX_AD_MIRRORS - MIN_AD_MIRRORS) / pointsPerLine; |
mbedalvaro | 0:26b228dde70c | 98 | |
mbedalvaro | 0:26b228dde70c | 99 | Laser_Green=1; |
mbedalvaro | 0:26b228dde70c | 100 | |
mbedalvaro | 0:26b228dde70c | 101 | //for (int repeat=0; repeat<times; repeat++) { |
mbedalvaro | 0:26b228dde70c | 102 | |
mbedalvaro | 0:26b228dde70c | 103 | Timer t; |
mbedalvaro | 0:26b228dde70c | 104 | t.start(); |
mbedalvaro | 0:26b228dde70c | 105 | while(t.read_ms()<times*1000) { |
mbedalvaro | 0:26b228dde70c | 106 | |
mbedalvaro | 0:26b228dde70c | 107 | writeOutX(MIN_AD_MIRRORS);writeOutY(MIN_AD_MIRRORS); |
mbedalvaro | 0:26b228dde70c | 108 | |
mbedalvaro | 0:26b228dde70c | 109 | for(int j=0; j<pointsPerLine; j++){ |
mbedalvaro | 0:26b228dde70c | 110 | wait_us(200);//delay between each points |
mbedalvaro | 0:26b228dde70c | 111 | writeOutY(j*shiftY + MIN_AD_MIRRORS); |
mbedalvaro | 0:26b228dde70c | 112 | } |
mbedalvaro | 0:26b228dde70c | 113 | |
mbedalvaro | 0:26b228dde70c | 114 | writeOutX(MIN_AD_MIRRORS);writeOutY(MAX_AD_MIRRORS); |
mbedalvaro | 0:26b228dde70c | 115 | for(int j=0; j<pointsPerLine; j++) { |
mbedalvaro | 0:26b228dde70c | 116 | wait_us(200);//delay between each points |
mbedalvaro | 0:26b228dde70c | 117 | writeOutX(j*shiftX + MIN_AD_MIRRORS); |
mbedalvaro | 0:26b228dde70c | 118 | } |
mbedalvaro | 0:26b228dde70c | 119 | |
mbedalvaro | 0:26b228dde70c | 120 | writeOutX(MAX_AD_MIRRORS);writeOutY(MAX_AD_MIRRORS); |
mbedalvaro | 0:26b228dde70c | 121 | for(int j=0; j<pointsPerLine; j++) { |
mbedalvaro | 0:26b228dde70c | 122 | wait_us(200);//delay between each points |
mbedalvaro | 0:26b228dde70c | 123 | writeOutY(-j*shiftX + MAX_AD_MIRRORS); |
mbedalvaro | 0:26b228dde70c | 124 | } |
mbedalvaro | 0:26b228dde70c | 125 | |
mbedalvaro | 0:26b228dde70c | 126 | writeOutX(MAX_AD_MIRRORS);writeOutY(MIN_AD_MIRRORS); |
mbedalvaro | 0:26b228dde70c | 127 | for(int j=0; j<pointsPerLine; j++) { |
mbedalvaro | 0:26b228dde70c | 128 | wait_us(200);//delay between each points |
mbedalvaro | 0:26b228dde70c | 129 | writeOutX(-j*shiftX + MAX_AD_MIRRORS); |
mbedalvaro | 0:26b228dde70c | 130 | } |
mbedalvaro | 0:26b228dde70c | 131 | |
mbedalvaro | 0:26b228dde70c | 132 | } |
mbedalvaro | 0:26b228dde70c | 133 | t.stop(); |
mbedalvaro | 0:26b228dde70c | 134 | Laser_Green=0; |
mbedalvaro | 0:26b228dde70c | 135 | } |