Hiromasa Oku
/
linearMirrorMotion1017
this version 10/17
Fork of linearMirrorMotion by
Diff: main.cpp
- Revision:
- 17:dce982e0a383
- Parent:
- 16:6dd2e60bc5bc
- Child:
- 18:6f86abfae754
diff -r 6dd2e60bc5bc -r dce982e0a383 main.cpp --- a/main.cpp Mon May 20 08:25:22 2013 +0000 +++ b/main.cpp Mon May 20 08:51:07 2013 +0000 @@ -4,11 +4,13 @@ #include "laserProjectorHardware.h" #define PI 3.141592 +render myRender; + InterruptIn clockEncoderPin(p14); DigitalIn directionPin(p15); void processSerial(); -Timer timer_v; // for shearing +Timer timer_v; // for shearing Timer timer; //for send the speed on the serial port every 30 ms: LocalFileSystem local("local"); //Serial pc(USBTX, USBRX); @@ -19,7 +21,7 @@ int startY = CENTER_AD_MIRROR_Y; float sint=0, cost=1; - + float vx = 0, vy=0; float theta=0, dt_betWords=50000, st=1800; unsigned int dt=500, ticktime=600; @@ -51,25 +53,29 @@ Ticker superEncoder; // the external interrupt routine: -void encoderClock() { - if (directionPin) angle += angleIncrement ; else angle -= angleIncrement; +void encoderClock() +{ + if (directionPin) angle += angleIncrement ; + else angle -= angleIncrement; } -void computeSpeed() { +void computeSpeed() +{ // We know exactly how much time passed since we last computed the speed, this is PERIODIC_COMPUTE in microseconds angularSpeed = 1000000.0 * (float)(angle-oldAngle) / (float)(PERIODIC_COMPUTE); // in rad/sec oldAngle=angle; } - + -int main(){ +int main() +{ //read from TextFileLibrary ------------------------------------------------------ FILE *fp = fopen("/local/text.txt", "r"); if(!fp) { IO.setGreenPower(1); exit(1); - } - + } + int letternum; fscanf(fp, "%d", &letternum); for(int i=0; i<letternum; i++) { @@ -91,155 +97,155 @@ // initialize the angle (arbitrary origin): oldAngle=angle=0; - + // Attach the external interrupt routine---------------------------------------------------------------------- superEncoder.attach_us(&encoderClock, attachSecond); - timer.reset(); timer.start(); - + timer.reset(); + timer.start(); + //using renderclass.cpp ---------------------------------------------------------- - render myRender; myRender.setRender(&myText); myRender.startRender(); - + // Attach the periodic computing function: - speedTimerCompute.attach_us(&computeSpeed, PERIODIC_COMPUTE); - + speedTimerCompute.attach_us(&computeSpeed, PERIODIC_COMPUTE); + // Set displaying laser powers-------------------------------------------------------------------------- IO.setRedPower(0); IO.setGreenPower(0); wait_ms(100); - - + + timer_v.start(); // MAIN LOOP: -------------------------------------------------------------------------------------------- while(1) { - if (pc.readable()>0) processSerial(); - - if(1/*start*/) { - timer_v.reset(); - wait_us(5000); - // send the speed on the serial port every 30 ms: - if (timer.read_ms()>30) { - pc.printf("Angular Speed = %4.2f\t Cumulative Angle = %4.2f\n" , angularSpeed, angle); - timer.reset(); + if (pc.readable()>0) processSerial(); + + if(1/*start*/) { + timer_v.reset(); + wait_us(5000); + // send the speed on the serial port every 30 ms: + if (timer.read_ms()>30) { + pc.printf("Angular Speed = %4.2f\t Cumulative Angle = %4.2f\n" , angularSpeed, angle); + timer.reset(); + } } } } - // -------------------------------------------------------------------------------------------- // String to store ALPHANUMERIC DATA (i.e., integers, floating point numbers, unsigned ints, etc represented as DEC) sent wirelessly: -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... -int indexStringData=0;//position of the byte in the string + 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... + int indexStringData=0;//position of the byte in the string -void processSerial(){ - start=true; - while(pc.readable()>0) { + void processSerial() { + start=true; + while(pc.readable()>0) { - char val =pc.getc(); + char val =pc.getc(); - // Save ASCII numeric characters (ASCII 0 - 9) on stringData: - if ((val >= '0') && (val <= '9')) { // this is 45 to 57 (included) - stringData[indexStringData] = val; - indexStringData++; - } else if ((val >= 'a') && (val <= 'z')) { // this is 45 to 57 (included) - myText.push_back(alphabet['a'-1]); - //inputletters.push_back(val); - } + // Save ASCII numeric characters (ASCII 0 - 9) on stringData: + if ((val >= '0') && (val <= '9')) { // this is 45 to 57 (included) + stringData[indexStringData] = val; + indexStringData++; + } else if ((val >= 'a') && (val <= 'z')) { // this is 45 to 57 (included) + myText.push_back(alphabet['a'-1]); + //inputletters.push_back(val); + } - /*else if (val == '/') { - makeBuffer(); - }*/ else if (val == '.') { - myText.clear(); - //inputletters.clear(); - } + /*else if (val == '/') { + makeBuffer(); + }*/ else if (val == '.') { + myText.clear(); + //inputletters.clear(); + } - else if (val == 'X') { - beforeX = X; - stringData[indexStringData] = 0; - X = atoi(stringData); - indexStringData=0; - vx = ((float)X-(float)beforeX) / (float)timer_v.read_us() *1000; - } + else if (val == 'X') { + beforeX = X; + stringData[indexStringData] = 0; + X = atoi(stringData); + indexStringData=0; + vx = ((float)X-(float)beforeX) / (float)timer_v.read_us() *1000; + } - else if (val == 'Y') { - beforeY = Y; - stringData[indexStringData] = 0; - Y = atoi(stringData); - indexStringData=0; - //timer_v.stop(); - //newSpeedReady = true; - //if( (Y-beforeY) > 5){ - vy = ((float)Y-(float)beforeY) / (float)timer_v.read_us() *1000; - //theta=atan2(vy,vx); - //sint = -sin(theta);//cos(theta); - //cost = -cos(theta);//-sin(theta); - //} - //if (cost > 0){ - //sint = -sint;//cos(thet - //cost = -cost;//-sin(theta); - //} - } + else if (val == 'Y') { + beforeY = Y; + stringData[indexStringData] = 0; + Y = atoi(stringData); + indexStringData=0; + //timer_v.stop(); + //newSpeedReady = true; + //if( (Y-beforeY) > 5){ + vy = ((float)Y-(float)beforeY) / (float)timer_v.read_us() *1000; + //theta=atan2(vy,vx); + //sint = -sin(theta);//cos(theta); + //cost = -cos(theta);//-sin(theta); + //} + //if (cost > 0){ + //sint = -sint;//cos(thet + //cost = -cost;//-sin(theta); + //} + } - else if (val == 'D') { - stringData[indexStringData] = 0; - dt = atoi(stringData); - indexStringData=0; - //makeBuffer(); - } else if (val == 'B') { - stringData[indexStringData] = 0; - dt_betWords = atoi(stringData); - indexStringData=0; - //makeBuffer(); - } else if (val == 'S') { - stringData[indexStringData] = 0; - st = atoi(stringData); - indexStringData=0; - //makeBuffer(); - }else if (val == 'R') { - stringData[indexStringData] = 0; - radious = atoi(stringData); - indexStringData=0; - //makeBuffer(); - }else if (val == 'T') { - stringData[indexStringData] = 0; - attachSecond = atoi(stringData); - indexStringData=0; - //makeBuffer(); + else if (val == 'D') { + stringData[indexStringData] = 0; + dt = atoi(stringData); + indexStringData=0; + //makeBuffer(); + } else if (val == 'B') { + stringData[indexStringData] = 0; + dt_betWords = atoi(stringData); + indexStringData=0; + //makeBuffer(); + } else if (val == 'S') { + stringData[indexStringData] = 0; + st = atoi(stringData); + indexStringData=0; + //makeBuffer(); + } else if (val == 'R') { + stringData[indexStringData] = 0; + radious = atoi(stringData); + indexStringData=0; + //makeBuffer(); + } else if (val == 'T') { + stringData[indexStringData] = 0; + attachSecond = atoi(stringData); + indexStringData=0; + //makeBuffer(); + } + // X value? + /*else if (val=='x') { + stringData[indexStringData] = 0 ; + omegaX=atoi(stringData); + indexStringData=0; + //newPositionReady=true; + } + // Y value? + else if (val=='y') { + stringData[indexStringData] = 0 ; + omegaY=atoi(stringData); + indexStringData=0; + makeBuffer(); + newPositionReady=true; + } + + else if (val=='g') { + stringData[indexStringData] = 0 ; + int power=atoi(stringData); + indexStringData=0; + IO.setGreenPower(power); + } else if (val=='r') { + stringData[indexStringData] = 0 ; + int power=atoi(stringData); + indexStringData=0; + IO.setRedPower(power); + } else if (val=='c') { + stringData[indexStringData] = 0 ; + int power=atoi(stringData); + indexStringData=0; + IO.setRGBPower(power); + } + */ + } - // X value? - /*else if (val=='x') { - stringData[indexStringData] = 0 ; - omegaX=atoi(stringData); - indexStringData=0; - //newPositionReady=true; - } - // Y value? - else if (val=='y') { - stringData[indexStringData] = 0 ; - omegaY=atoi(stringData); - indexStringData=0; - makeBuffer(); - newPositionReady=true; - } - - else if (val=='g') { - stringData[indexStringData] = 0 ; - int power=atoi(stringData); - indexStringData=0; - IO.setGreenPower(power); - } else if (val=='r') { - stringData[indexStringData] = 0 ; - int power=atoi(stringData); - indexStringData=0; - IO.setRedPower(power); - } else if (val=='c') { - stringData[indexStringData] = 0 ; - int power=atoi(stringData); - indexStringData=0; - IO.setRGBPower(power); - } - */ - - } -} \ No newline at end of file + } \ No newline at end of file