![](/media/cache/profiles/68a15b5278e4f7c4c056df9d5f1d3b1f.jpg.50x50_q85.jpg)
simple example program to control one mirror from serial data
main.cpp@2:0548c7bf9fba, 2012-10-04 (annotated)
- Committer:
- mbedalvaro
- Date:
- Thu Oct 04 05:14:14 2012 +0000
- Revision:
- 2:0548c7bf9fba
- Parent:
- 1:daf6b4939120
control X Y mirrors (SIMPLEST);
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedalvaro | 0:4e12dea53fbe | 1 | #include "mbed.h" |
mbedalvaro | 1:daf6b4939120 | 2 | #include "laserProjectorHardware.h" |
mbedalvaro | 0:4e12dea53fbe | 3 | |
mbedalvaro | 0:4e12dea53fbe | 4 | DigitalOut myled(LED1); |
mbedalvaro | 0:4e12dea53fbe | 5 | |
mbedalvaro | 0:4e12dea53fbe | 6 | void processSerial(); |
mbedalvaro | 0:4e12dea53fbe | 7 | |
mbedalvaro | 2:0548c7bf9fba | 8 | unsigned int X, Y; // position of the mirror (note: in fact it is an ANGLE) |
mbedalvaro | 0:4e12dea53fbe | 9 | bool newPositionReady=false; |
mbedalvaro | 0:4e12dea53fbe | 10 | |
mbedalvaro | 0:4e12dea53fbe | 11 | unsigned int counter=0; |
mbedalvaro | 0:4e12dea53fbe | 12 | |
mbedalvaro | 0:4e12dea53fbe | 13 | int main() { |
mbedalvaro | 0:4e12dea53fbe | 14 | |
mbedalvaro | 0:4e12dea53fbe | 15 | // SETUP: -------------------------------------------------------------------------------------------- |
mbedalvaro | 0:4e12dea53fbe | 16 | IO.init(); // note: serial speed can be changed by checking in the hardwareIO.cpp initialization |
mbedalvaro | 0:4e12dea53fbe | 17 | |
mbedalvaro | 0:4e12dea53fbe | 18 | // Set displaying laser powers: |
mbedalvaro | 0:4e12dea53fbe | 19 | IO.setRedPower(0); |
mbedalvaro | 0:4e12dea53fbe | 20 | IO.setGreenPower(0);//turn on the green (displaying) laser |
mbedalvaro | 0:4e12dea53fbe | 21 | |
mbedalvaro | 0:4e12dea53fbe | 22 | wait_ms(100); |
mbedalvaro | 0:4e12dea53fbe | 23 | |
mbedalvaro | 0:4e12dea53fbe | 24 | Y = CENTER_AD_MIRROR_Y; |
mbedalvaro | 0:4e12dea53fbe | 25 | IO.writeOutY(Y); |
mbedalvaro | 0:4e12dea53fbe | 26 | |
mbedalvaro | 0:4e12dea53fbe | 27 | // MAIN LOOP: -------------------------------------------------------------------------------------------- |
mbedalvaro | 0:4e12dea53fbe | 28 | while(1) { |
mbedalvaro | 0:4e12dea53fbe | 29 | processSerial(); |
mbedalvaro | 0:4e12dea53fbe | 30 | if (newPositionReady) { |
mbedalvaro | 0:4e12dea53fbe | 31 | IO.writeOutY(Y); |
mbedalvaro | 0:4e12dea53fbe | 32 | newPositionReady=false; |
mbedalvaro | 0:4e12dea53fbe | 33 | } |
mbedalvaro | 0:4e12dea53fbe | 34 | |
mbedalvaro | 0:4e12dea53fbe | 35 | Y=int(0.5*4095.0*(1.0+cos(1.0*counter/100000))); |
mbedalvaro | 0:4e12dea53fbe | 36 | IO.writeOutY(Y); |
mbedalvaro | 0:4e12dea53fbe | 37 | counter++; |
mbedalvaro | 0:4e12dea53fbe | 38 | |
mbedalvaro | 0:4e12dea53fbe | 39 | } |
mbedalvaro | 0:4e12dea53fbe | 40 | } |
mbedalvaro | 0:4e12dea53fbe | 41 | |
mbedalvaro | 0:4e12dea53fbe | 42 | // -------------------------------------------------------------------------------------------- |
mbedalvaro | 0:4e12dea53fbe | 43 | // String to store ALPHANUMERIC DATA (i.e., integers, floating point numbers, unsigned ints, etc represented as DEC) sent wirelessly: |
mbedalvaro | 0:4e12dea53fbe | 44 | 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:4e12dea53fbe | 45 | int indexStringData=0;//position of the byte in the string |
mbedalvaro | 0:4e12dea53fbe | 46 | |
mbedalvaro | 0:4e12dea53fbe | 47 | void processSerial() { |
mbedalvaro | 0:4e12dea53fbe | 48 | |
mbedalvaro | 0:4e12dea53fbe | 49 | while(pc.readable()>0){ |
mbedalvaro | 0:4e12dea53fbe | 50 | |
mbedalvaro | 0:4e12dea53fbe | 51 | char val =pc.getc(); |
mbedalvaro | 0:4e12dea53fbe | 52 | |
mbedalvaro | 0:4e12dea53fbe | 53 | // Save ASCII numeric characters (ASCII 0 - 9) on stringData: |
mbedalvaro | 0:4e12dea53fbe | 54 | if ((val >= '0') && (val <= '9')){ // this is 45 to 57 (included) |
mbedalvaro | 0:4e12dea53fbe | 55 | stringData[indexStringData] = val; |
mbedalvaro | 0:4e12dea53fbe | 56 | indexStringData++; |
mbedalvaro | 0:4e12dea53fbe | 57 | } |
mbedalvaro | 2:0548c7bf9fba | 58 | |
mbedalvaro | 0:4e12dea53fbe | 59 | // X value? |
mbedalvaro | 0:4e12dea53fbe | 60 | else if (val=='X') { |
mbedalvaro | 0:4e12dea53fbe | 61 | stringData[indexStringData] = 0 ; |
mbedalvaro | 2:0548c7bf9fba | 62 | X=atoi(stringData); |
mbedalvaro | 2:0548c7bf9fba | 63 | indexStringData=0; |
mbedalvaro | 2:0548c7bf9fba | 64 | //newPositionReady=true; |
mbedalvaro | 2:0548c7bf9fba | 65 | } |
mbedalvaro | 2:0548c7bf9fba | 66 | // Y value? |
mbedalvaro | 2:0548c7bf9fba | 67 | else if (val=='Y') { |
mbedalvaro | 2:0548c7bf9fba | 68 | stringData[indexStringData] = 0 ; |
mbedalvaro | 2:0548c7bf9fba | 69 | Y=atoi(stringData); |
mbedalvaro | 0:4e12dea53fbe | 70 | indexStringData=0; |
mbedalvaro | 0:4e12dea53fbe | 71 | newPositionReady=true; |
mbedalvaro | 0:4e12dea53fbe | 72 | } |
mbedalvaro | 0:4e12dea53fbe | 73 | |
mbedalvaro | 0:4e12dea53fbe | 74 | } |
mbedalvaro | 0:4e12dea53fbe | 75 | } |