![](/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@3:89f592efbe84, 2014-06-04 (annotated)
- Committer:
- mbedalvaro
- Date:
- Wed Jun 04 09:47:04 2014 +0000
- Revision:
- 3:89f592efbe84
- Parent:
- 2:383b2acec6e4
- Child:
- 4:1428775752f7
seems working. I had to change the modulating pin D8 to D5, because as PWM output the pin D8 seems not to reach 0...
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 | 3:89f592efbe84 | 11 | |
mbedalvaro | 3:89f592efbe84 | 12 | PwmOut Laser_Lockin(LASER_LOCKIN_PIN); |
mbedalvaro | 3:89f592efbe84 | 13 | DigitalOut Laser_Red(LASER_RED_PIN); |
mbedalvaro | 0:26b228dde70c | 14 | DigitalOut Laser_Green(LASER_GREEN_PIN); |
mbedalvaro | 0:26b228dde70c | 15 | DigitalOut Laser_Blue(LASER_BLUE_PIN); |
mbedalvaro | 0:26b228dde70c | 16 | |
mbedalvaro | 0:26b228dde70c | 17 | void HardwareIO::init(void) { |
mbedalvaro | 3:89f592efbe84 | 18 | |
mbedalvaro | 3:89f592efbe84 | 19 | setLockinFreq(LOCKIN_FREQUENCY); |
mbedalvaro | 3:89f592efbe84 | 20 | setLockinPower(1); |
mbedalvaro | 3:89f592efbe84 | 21 | |
mbedalvaro | 3:89f592efbe84 | 22 | setRedPower(0); |
mbedalvaro | 3:89f592efbe84 | 23 | setGreenPower(0); |
mbedalvaro | 3:89f592efbe84 | 24 | setBluePower(0); |
mbedalvaro | 0:26b228dde70c | 25 | |
mbedalvaro | 0:26b228dde70c | 26 | //Serial Communication setup: |
mbedalvaro | 0:26b228dde70c | 27 | pc.baud(115200);// |
mbedalvaro | 0:26b228dde70c | 28 | // pc.baud(921600);//115200);// |
mbedalvaro | 0:26b228dde70c | 29 | |
mbedalvaro | 0:26b228dde70c | 30 | // Setup the spi for 8 bit data, high steady state clock, |
mbedalvaro | 1:4be6abc4ed43 | 31 | // second edge capture, with a 20MHz clock rate (max for the MCP4922) |
mbedalvaro | 0:26b228dde70c | 32 | csDAC = 1; |
mbedalvaro | 0:26b228dde70c | 33 | spiDAC.format(16,0); |
mbedalvaro | 1:4be6abc4ed43 | 34 | spiDAC.frequency(20000000); |
mbedalvaro | 0:26b228dde70c | 35 | |
mbedalvaro | 0:26b228dde70c | 36 | // default initial mirror position: |
mbedalvaro | 0:26b228dde70c | 37 | writeOutX(CENTER_AD_MIRROR_X); |
mbedalvaro | 0:26b228dde70c | 38 | writeOutY(CENTER_AD_MIRROR_Y); |
mbedalvaro | 0:26b228dde70c | 39 | |
mbedalvaro | 0:26b228dde70c | 40 | } |
mbedalvaro | 0:26b228dde70c | 41 | |
mbedalvaro | 0:26b228dde70c | 42 | //write on the first DAC, output A (mirror X) |
mbedalvaro | 0:26b228dde70c | 43 | void HardwareIO::writeOutX(unsigned short value){ |
mbedalvaro | 0:26b228dde70c | 44 | if(value > MAX_AD_MIRRORS) value = MAX_AD_MIRRORS; |
mbedalvaro | 0:26b228dde70c | 45 | if(value < MIN_AD_MIRRORS) value = MIN_AD_MIRRORS; |
mbedalvaro | 0:26b228dde70c | 46 | |
mbedalvaro | 0:26b228dde70c | 47 | value |= 0xF000; |
mbedalvaro | 0:26b228dde70c | 48 | value &= 0xFFFF; |
mbedalvaro | 0:26b228dde70c | 49 | |
mbedalvaro | 0:26b228dde70c | 50 | csDAC = 0; |
mbedalvaro | 0:26b228dde70c | 51 | spiDAC.write(value); |
mbedalvaro | 0:26b228dde70c | 52 | csDAC = 1; |
mbedalvaro | 0:26b228dde70c | 53 | } |
mbedalvaro | 0:26b228dde70c | 54 | |
mbedalvaro | 0:26b228dde70c | 55 | //write on the first DAC, output B (mirror Y) |
mbedalvaro | 0:26b228dde70c | 56 | void HardwareIO::writeOutY(unsigned short value){ |
mbedalvaro | 0:26b228dde70c | 57 | if(value > MAX_AD_MIRRORS) value = MAX_AD_MIRRORS; |
mbedalvaro | 0:26b228dde70c | 58 | if(value < MIN_AD_MIRRORS) value = MIN_AD_MIRRORS; |
mbedalvaro | 0:26b228dde70c | 59 | |
mbedalvaro | 0:26b228dde70c | 60 | value |= 0x7000; |
mbedalvaro | 0:26b228dde70c | 61 | value &= 0x7FFF; |
mbedalvaro | 0:26b228dde70c | 62 | |
mbedalvaro | 0:26b228dde70c | 63 | csDAC = 0; |
mbedalvaro | 0:26b228dde70c | 64 | spiDAC.write(value); |
mbedalvaro | 0:26b228dde70c | 65 | csDAC = 1; |
mbedalvaro | 0:26b228dde70c | 66 | } |
mbedalvaro | 0:26b228dde70c | 67 | |
mbedalvaro | 3:89f592efbe84 | 68 | void HardwareIO::setLockinFreq(int valueHz) { |
mbedalvaro | 3:89f592efbe84 | 69 | Laser_Lockin.period_ms(int(1000.0/valueHz)); |
mbedalvaro | 3:89f592efbe84 | 70 | } |
mbedalvaro | 3:89f592efbe84 | 71 | |
mbedalvaro | 3:89f592efbe84 | 72 | void HardwareIO::setLockinPower(int value) { //note: 0 means off, and 1 or >0 means 50% |
mbedalvaro | 3:89f592efbe84 | 73 | if (value>0) Laser_Lockin.write(0.5); |
mbedalvaro | 3:89f592efbe84 | 74 | else Laser_Lockin.write(0); |
mbedalvaro | 3:89f592efbe84 | 75 | } |
mbedalvaro | 3:89f592efbe84 | 76 | |
mbedalvaro | 0:26b228dde70c | 77 | void HardwareIO::setRedPower(int powerValue){ |
mbedalvaro | 0:26b228dde70c | 78 | if(powerValue > 0){ |
mbedalvaro | 3:89f592efbe84 | 79 | Laser_Red = 1; |
mbedalvaro | 0:26b228dde70c | 80 | } |
mbedalvaro | 0:26b228dde70c | 81 | else{ |
mbedalvaro | 3:89f592efbe84 | 82 | Laser_Red = 0; |
mbedalvaro | 0:26b228dde70c | 83 | } |
mbedalvaro | 0:26b228dde70c | 84 | } |
mbedalvaro | 0:26b228dde70c | 85 | void HardwareIO::setGreenPower(int powerValue){ |
mbedalvaro | 0:26b228dde70c | 86 | if(powerValue > 0){ |
mbedalvaro | 0:26b228dde70c | 87 | Laser_Green = 1; |
mbedalvaro | 0:26b228dde70c | 88 | } |
mbedalvaro | 0:26b228dde70c | 89 | else{ |
mbedalvaro | 0:26b228dde70c | 90 | Laser_Green = 0; |
mbedalvaro | 0:26b228dde70c | 91 | } |
mbedalvaro | 0:26b228dde70c | 92 | } |
mbedalvaro | 0:26b228dde70c | 93 | void HardwareIO::setBluePower(int powerValue){ |
mbedalvaro | 0:26b228dde70c | 94 | if(powerValue > 0){ |
mbedalvaro | 0:26b228dde70c | 95 | Laser_Blue = 1; |
mbedalvaro | 0:26b228dde70c | 96 | } |
mbedalvaro | 0:26b228dde70c | 97 | else{ |
mbedalvaro | 0:26b228dde70c | 98 | Laser_Blue = 0; |
mbedalvaro | 0:26b228dde70c | 99 | } |
mbedalvaro | 0:26b228dde70c | 100 | } |
mbedalvaro | 0:26b228dde70c | 101 | void HardwareIO::setRGBPower(unsigned char color) { |
mbedalvaro | 0:26b228dde70c | 102 | //lockin.setLaserPower(color&0x04>0? false : true); |
mbedalvaro | 3:89f592efbe84 | 103 | Laser_Red=(color&0x04)>>2; |
mbedalvaro | 0:26b228dde70c | 104 | Laser_Green=(color&0x02)>>1; |
mbedalvaro | 0:26b228dde70c | 105 | Laser_Blue =color&0x01; |
mbedalvaro | 0:26b228dde70c | 106 | } |
mbedalvaro | 0:26b228dde70c | 107 | |
mbedalvaro | 2:383b2acec6e4 | 108 | void HardwareIO::drawCircle(int X, int Y, int R, int numPoints) { |
mbedalvaro | 2:383b2acec6e4 | 109 | float alpha=2*3.141592/numPoints; |
mbedalvaro | 2:383b2acec6e4 | 110 | float theta=0; |
mbedalvaro | 2:383b2acec6e4 | 111 | for (int i=0; i<numPoints; i++) { |
mbedalvaro | 2:383b2acec6e4 | 112 | writeOutX(X+1.0*R*cos(theta)); |
mbedalvaro | 2:383b2acec6e4 | 113 | writeOutY(Y+1.0*R*sin(theta)); |
mbedalvaro | 2:383b2acec6e4 | 114 | theta+=alpha; |
mbedalvaro | 2:383b2acec6e4 | 115 | } |
mbedalvaro | 2:383b2acec6e4 | 116 | } |
mbedalvaro | 2:383b2acec6e4 | 117 | |
mbedalvaro | 2:383b2acec6e4 | 118 | void HardwareIO::showLimitsMirrors(int seconds) { |
mbedalvaro | 0:26b228dde70c | 119 | unsigned short pointsPerLine=150; |
mbedalvaro | 0:26b228dde70c | 120 | int shiftX = (MAX_AD_MIRRORS - MIN_AD_MIRRORS) / pointsPerLine; |
mbedalvaro | 0:26b228dde70c | 121 | int shiftY = (MAX_AD_MIRRORS - MIN_AD_MIRRORS) / pointsPerLine; |
mbedalvaro | 0:26b228dde70c | 122 | |
mbedalvaro | 0:26b228dde70c | 123 | Laser_Green=1; |
mbedalvaro | 2:383b2acec6e4 | 124 | Laser_Red=1; |
mbedalvaro | 0:26b228dde70c | 125 | |
mbedalvaro | 0:26b228dde70c | 126 | //for (int repeat=0; repeat<times; repeat++) { |
mbedalvaro | 0:26b228dde70c | 127 | |
mbedalvaro | 0:26b228dde70c | 128 | Timer t; |
mbedalvaro | 0:26b228dde70c | 129 | t.start(); |
mbedalvaro | 2:383b2acec6e4 | 130 | while(t.read_ms()<seconds*1000) { |
mbedalvaro | 0:26b228dde70c | 131 | |
mbedalvaro | 0:26b228dde70c | 132 | writeOutX(MIN_AD_MIRRORS);writeOutY(MIN_AD_MIRRORS); |
mbedalvaro | 0:26b228dde70c | 133 | |
mbedalvaro | 0:26b228dde70c | 134 | for(int j=0; j<pointsPerLine; j++){ |
mbedalvaro | 2:383b2acec6e4 | 135 | wait_us(50);//delay between each points |
mbedalvaro | 0:26b228dde70c | 136 | writeOutY(j*shiftY + MIN_AD_MIRRORS); |
mbedalvaro | 0:26b228dde70c | 137 | } |
mbedalvaro | 0:26b228dde70c | 138 | |
mbedalvaro | 0:26b228dde70c | 139 | writeOutX(MIN_AD_MIRRORS);writeOutY(MAX_AD_MIRRORS); |
mbedalvaro | 0:26b228dde70c | 140 | for(int j=0; j<pointsPerLine; j++) { |
mbedalvaro | 2:383b2acec6e4 | 141 | wait_us(50);//delay between each points |
mbedalvaro | 0:26b228dde70c | 142 | writeOutX(j*shiftX + MIN_AD_MIRRORS); |
mbedalvaro | 0:26b228dde70c | 143 | } |
mbedalvaro | 0:26b228dde70c | 144 | |
mbedalvaro | 0:26b228dde70c | 145 | writeOutX(MAX_AD_MIRRORS);writeOutY(MAX_AD_MIRRORS); |
mbedalvaro | 0:26b228dde70c | 146 | for(int j=0; j<pointsPerLine; j++) { |
mbedalvaro | 2:383b2acec6e4 | 147 | wait_us(50);//delay between each points |
mbedalvaro | 0:26b228dde70c | 148 | writeOutY(-j*shiftX + MAX_AD_MIRRORS); |
mbedalvaro | 0:26b228dde70c | 149 | } |
mbedalvaro | 0:26b228dde70c | 150 | |
mbedalvaro | 0:26b228dde70c | 151 | writeOutX(MAX_AD_MIRRORS);writeOutY(MIN_AD_MIRRORS); |
mbedalvaro | 0:26b228dde70c | 152 | for(int j=0; j<pointsPerLine; j++) { |
mbedalvaro | 2:383b2acec6e4 | 153 | wait_us(50);//delay between each points |
mbedalvaro | 0:26b228dde70c | 154 | writeOutX(-j*shiftX + MAX_AD_MIRRORS); |
mbedalvaro | 0:26b228dde70c | 155 | } |
mbedalvaro | 0:26b228dde70c | 156 | |
mbedalvaro | 0:26b228dde70c | 157 | } |
mbedalvaro | 0:26b228dde70c | 158 | t.stop(); |
mbedalvaro | 0:26b228dde70c | 159 | Laser_Green=0; |
mbedalvaro | 0:26b228dde70c | 160 | } |