Example controlling galvanomirrors (X and Y) using the spi DAC MCP4922 and the new platform FRDM_K64F

Dependencies:   mbed

Committer:
mbedalvaro
Date:
Wed Dec 10 07:22:41 2014 +0000
Revision:
4:1428775752f7
Parent:
3:89f592efbe84
Child:
5:cea6da0fe2f0
added circle with timer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedalvaro 0:26b228dde70c 1 #include "mbed.h"
mbedalvaro 0:26b228dde70c 2 #include "laserProjectorHardware.h"
mbedalvaro 0:26b228dde70c 3
mbedalvaro 0:26b228dde70c 4 DigitalOut myled(LED3);
mbedalvaro 0:26b228dde70c 5
mbedalvaro 0:26b228dde70c 6 void processSerial();
mbedalvaro 0:26b228dde70c 7
mbedalvaro 0:26b228dde70c 8 unsigned int X, Y; // position of the mirrors (note: in fact, azimuth and elevation)
mbedalvaro 0:26b228dde70c 9 bool newPositionReady=false;
mbedalvaro 4:1428775752f7 10 bool handshakeMode=true;
mbedalvaro 2:383b2acec6e4 11 unsigned long counter=0;
mbedalvaro 2:383b2acec6e4 12
mbedalvaro 0:26b228dde70c 13 int main() {
mbedalvaro 0:26b228dde70c 14
mbedalvaro 0:26b228dde70c 15 // SETUP: --------------------------------------------------------------------------------------------
mbedalvaro 0:26b228dde70c 16 IO.init(); // note: serial speed can be changed by checking in the hardwareIO.cpp initialization
mbedalvaro 0:26b228dde70c 17
mbedalvaro 0:26b228dde70c 18 wait_ms(100);
mbedalvaro 0:26b228dde70c 19
mbedalvaro 0:26b228dde70c 20 Y = CENTER_AD_MIRROR_X;
mbedalvaro 0:26b228dde70c 21 X = CENTER_AD_MIRROR_Y;
mbedalvaro 0:26b228dde70c 22 IO.writeOutXY(X,Y);
mbedalvaro 0:26b228dde70c 23
mbedalvaro 0:26b228dde70c 24 // MAIN LOOP: --------------------------------------------------------------------------------------------
mbedalvaro 1:4be6abc4ed43 25 while(1) {
mbedalvaro 1:4be6abc4ed43 26
mbedalvaro 1:4be6abc4ed43 27 // myled=0;IO.setRedPower(1);
mbedalvaro 1:4be6abc4ed43 28 // wait_ms(1000);
mbedalvaro 1:4be6abc4ed43 29 // myled=1;IO.setRedPower(0);
mbedalvaro 1:4be6abc4ed43 30 // wait_ms(1000);
mbedalvaro 1:4be6abc4ed43 31
mbedalvaro 2:383b2acec6e4 32 // Y = CENTER_AD_MIRROR_X+1000*cos(1.0*counter/10);
mbedalvaro 2:383b2acec6e4 33 // X = CENTER_AD_MIRROR_Y+800*sin(1.0*counter/15);
mbedalvaro 2:383b2acec6e4 34 // IO.writeOutXY(X,Y);
mbedalvaro 2:383b2acec6e4 35 // counter++;
mbedalvaro 2:383b2acec6e4 36
mbedalvaro 2:383b2acec6e4 37
mbedalvaro 2:383b2acec6e4 38 //IO.drawCircle(X, Y, 1000, 200);
mbedalvaro 2:383b2acec6e4 39
mbedalvaro 0:26b228dde70c 40 processSerial();
mbedalvaro 0:26b228dde70c 41 if (newPositionReady) {
mbedalvaro 2:383b2acec6e4 42 IO.writeOutXY(X,Y);
mbedalvaro 2:383b2acec6e4 43 //IO.drawCircle(X, Y, 1000, 20);
mbedalvaro 0:26b228dde70c 44 newPositionReady=false;
mbedalvaro 0:26b228dde70c 45 }
mbedalvaro 0:26b228dde70c 46 }
mbedalvaro 0:26b228dde70c 47 }
mbedalvaro 0:26b228dde70c 48
mbedalvaro 0:26b228dde70c 49 // --------------------------------------------------------------------------------------------
mbedalvaro 0:26b228dde70c 50 // String to store ALPHANUMERIC DATA (i.e., integers, floating point numbers, unsigned ints, etc represented as DEC) sent wirelessly:
mbedalvaro 0:26b228dde70c 51 char stringData[24]; // note: an integer is two bytes long, represented with a maximum of 5 digits, but we may send floats or unsigned int...
mbedalvaro 0:26b228dde70c 52 int indexStringData=0;//position of the byte in the string
mbedalvaro 0:26b228dde70c 53
mbedalvaro 0:26b228dde70c 54 void processSerial() {
mbedalvaro 1:4be6abc4ed43 55
mbedalvaro 1:4be6abc4ed43 56 while(pc.readable()>0) {
mbedalvaro 1:4be6abc4ed43 57
mbedalvaro 0:26b228dde70c 58 char val =pc.getc();
mbedalvaro 0:26b228dde70c 59
mbedalvaro 0:26b228dde70c 60 // Save ASCII numeric characters (ASCII 0 - 9) on stringData:
mbedalvaro 0:26b228dde70c 61 if ((val >= '0') && (val <= '9')){ // this is 45 to 57 (included)
mbedalvaro 0:26b228dde70c 62 stringData[indexStringData] = val;
mbedalvaro 0:26b228dde70c 63 indexStringData++;
mbedalvaro 1:4be6abc4ed43 64
mbedalvaro 0:26b228dde70c 65 }
mbedalvaro 0:26b228dde70c 66
mbedalvaro 0:26b228dde70c 67 // X value?
mbedalvaro 0:26b228dde70c 68 else if (val=='X') {
mbedalvaro 0:26b228dde70c 69 stringData[indexStringData] = 0 ;
mbedalvaro 0:26b228dde70c 70 X=atoi(stringData);
mbedalvaro 0:26b228dde70c 71 indexStringData=0;
mbedalvaro 0:26b228dde70c 72 }
mbedalvaro 0:26b228dde70c 73
mbedalvaro 0:26b228dde70c 74 // Y value?
mbedalvaro 0:26b228dde70c 75 else if (val=='Y') {
mbedalvaro 0:26b228dde70c 76 stringData[indexStringData] = 0 ;
mbedalvaro 1:4be6abc4ed43 77 Y=atoi(stringData);
mbedalvaro 0:26b228dde70c 78 indexStringData=0;
mbedalvaro 0:26b228dde70c 79 newPositionReady=true;
mbedalvaro 4:1428775752f7 80 if (handshakeMode) pc.putc('A');// send acknowledgement of correctly received position:
mbedalvaro 4:1428775752f7 81 }
mbedalvaro 4:1428775752f7 82
mbedalvaro 4:1428775752f7 83 else if (val=='H') { // this sets handshake mode to ON or OFF:
mbedalvaro 4:1428775752f7 84 stringData[indexStringData] = 0 ;
mbedalvaro 4:1428775752f7 85 if(atoi(stringData)>0) handshakeMode=true; else handshakeMode=false;
mbedalvaro 4:1428775752f7 86 indexStringData=0;
mbedalvaro 0:26b228dde70c 87 }
mbedalvaro 1:4be6abc4ed43 88
mbedalvaro 1:4be6abc4ed43 89 else if (val=='L') {
mbedalvaro 1:4be6abc4ed43 90 stringData[indexStringData] = 0 ;
mbedalvaro 3:89f592efbe84 91 //if(atoi(stringData)>0) IO.setRedPower(1); else IO.setRedPower(0);
mbedalvaro 3:89f592efbe84 92 if(atoi(stringData)>0) IO.setLockinPower(1); else IO.setLockinPower(0); // for the time being, we only use the lockin laser
mbedalvaro 1:4be6abc4ed43 93 indexStringData=0;
mbedalvaro 1:4be6abc4ed43 94 }
mbedalvaro 0:26b228dde70c 95
mbedalvaro 2:383b2acec6e4 96 else if (val=='S') {
mbedalvaro 2:383b2acec6e4 97 stringData[indexStringData] = 0 ;
mbedalvaro 2:383b2acec6e4 98 IO.showLimitsMirrors(int(atoi(stringData)));
mbedalvaro 2:383b2acec6e4 99 indexStringData=0;
mbedalvaro 2:383b2acec6e4 100 }
mbedalvaro 4:1428775752f7 101
mbedalvaro 4:1428775752f7 102 else if (val=='C') {
mbedalvaro 4:1428775752f7 103 stringData[indexStringData] = 0 ;
mbedalvaro 4:1428775752f7 104 if(atoi(stringData)>0) {
mbedalvaro 4:1428775752f7 105 IO.drawCircle(2048, 2048, 1500, 1000, int(atoi(stringData)));
mbedalvaro 4:1428775752f7 106 }
mbedalvaro 4:1428775752f7 107 indexStringData=0;
mbedalvaro 4:1428775752f7 108 }
mbedalvaro 2:383b2acec6e4 109
mbedalvaro 0:26b228dde70c 110 }
mbedalvaro 0:26b228dde70c 111 }