this version 10/17

Dependencies:   mbed

Fork of linearMirrorMotion by Alvaro Cassinelli

Revision:
5:e02cd57242c7
Parent:
4:e00e709d7173
Child:
6:a82917b3b1aa
diff -r e00e709d7173 -r e02cd57242c7 main.cpp
--- a/main.cpp	Wed Oct 24 07:17:26 2012 +0000
+++ b/main.cpp	Thu Nov 29 08:05:10 2012 +0000
@@ -1,42 +1,49 @@
+/*
+~~~~~~present status~~~~~~
+    * X Y data from sereal.  vx and vy are culculated like -> (new X,Y) - (previous X,Y) / timer
+        --  i don't know how to send data quickly
+    
+    now I'm fighting to other problems...
+*/
+
+
 #include "mbed.h"
 #include "laserProjectorHardware.h"
 #include <vector>
-/*yarukoto
-~vx,vy,theta,scale : parameta de kaeru.
-~letters.txt tsukuru.
-~realistic na kanji ni suru  omega,dist  toka.
-*/
 
 void processSerial();
 Timer timer;
 LocalFileSystem local("local");
+//Serial pc(USBTX, USBRX);
 
-unsigned int X, Y;
+unsigned int X, Y, T;   // these datas are from other tracking system... 
+unsigned int beforeX , beforeY;
 int startX = CENTER_AD_MIRROR_X;
 int startY = CENTER_AD_MIRROR_Y;
 
-float vx = 0, vy=0, omegaX=0, omegaY=0, dist=1000; // these data may be from saccade mirror...
-float theta=0,scale=2000;
-unsigned int dt=30;
+float vx = 0, vy=0, omegaX=0, omegaY=0; 
+float theta=0,scale=700;
+unsigned int dt=500, timecounter = 0;
 
 bool newPositionReady=false;
 unsigned int counter=0;
+vector<char> inputletters;
 
-struct point{
+struct point2d {
     int x,y;
 };
-
-struct letter{
+struct letter {
     int pointnum;
-    vector<struct point> letpoints;
+    vector<point2d> letpoints;
 };
 
 //int points[200][2] = {};
-vector<struct letter> points;   // drawing points
-    
+vector<letter> libletter;   // letter library
+
 vector<int> buf;                            //buffer vector
+vector<int> buf2;
 
-void recomputeBuffer()
+/*void makeBuffer()
 {
     float sint = sin(theta);
     float cost = cos(theta);
@@ -45,40 +52,48 @@
     startY = CENTER_AD_MIRROR_Y;
 
     buf.clear();
-//conversion points to buf before writing;
-    for(int j=0; j<3; j++) {
-        int x = scale/(dist+tan(omegaX/360*2*3.1415)*points[0].letpoints[j].x)*points[0].letpoints[j].x - vx*dt/1000*j;
-        int y = scale/(dist+tan(omegaY/360*2*3.1415)*points[0].letpoints[j].y)*points[0].letpoints[j].y - vy*dt/1000*j;
-        buf.push_back(startX+cost*x + sint*y);
-        buf.push_back(startY-sint*x + cost*y);
+/*conversion points to buf before writing;
+    for(int i=0; i<inputletters.size(); i++) {
+        
+        buf.push_back(222);
+        buf.push_back(222);
+        
+        for(int j=0; j<libletter[inputletters[i]-'a'].letpoints.size(); j++) {
+            int x = 0.8*scale/1000*(libletter[inputletters[i]-'a'].letpoints[j].x +500*(i-inputletters.size()/2.0));
+            int y = -scale/1000*(libletter[inputletters[i]-'a'].letpoints[j].y);
+            buf.push_back(x);
+            buf.push_back(y);
+        }
     }
+
 }
-
+*/
 
 int main()
 {
+    
+    
     FILE *fp = fopen("/local/test.txt", "r");
-    if(!fp) {IO.setGreenPower(1); exit(1);}
+    if(!fp) {
+        IO.setGreenPower(1);
+        exit(1);
+    }
     int letternum;
     fscanf(fp, "%d", &letternum);
-    for(int i=0;i<letternum;i++){
-        struct letter bufl;
+    for(int i=0; i<letternum; i++) {
+        letter bufl;
         fscanf(fp, "%d", &bufl.pointnum);
-        for(int j=0;j<bufl.pointnum;j++){
-            struct point bufp;
+        for(int j=0; j<bufl.pointnum; j++) {
+            point2d bufp;
             fscanf(fp, "%d", &bufp.x);
             fscanf(fp, "%d", &bufp.y);
             bufl.letpoints.push_back(bufp);
         }
+        libletter.push_back(bufl);
     }
-    
-    
-    
-    /*for(int i=0 ; i<200 ; i++) {
-        points[i][0] = 200*cos(1.0*i/200*2*3.1415);
-        points[i][1] = 200*sin(1.0*i/200*2*3.1415);
-    }*/
-    recomputeBuffer();
+
+
+    //makeBuffer();
 
     // SETUP: --------------------------------------------------------------------------------------------
     IO.init(); // note: serial speed can be changed by checking in the hardwareIO.cpp initialization
@@ -89,23 +104,56 @@
 
     wait_ms(100);
 
-    X = CENTER_AD_MIRROR_X;
-    Y = CENTER_AD_MIRROR_Y;
-    IO.writeOutXY(X,Y);
-
+    
+    X = beforeX = CENTER_AD_MIRROR_X;
+    Y = beforeY = CENTER_AD_MIRROR_Y;
+    timer.start();
     // MAIN LOOP: --------------------------------------------------------------------------------------------
     while(1) {
         if (pc.readable()>0) processSerial();
+        
+        
+        float sint = sin(theta);
+        float cost = cos(theta);
 
-        for(int j=0; j<buf.size(); j++) {
+        for(int i=0; i<inputletters.size(); i++) {
+            if (pc.readable()>0) processSerial();
+            wait(0.1);
+            for(int j=0; j<libletter[inputletters[i]-'a'].letpoints.size(); j++) {
+                int x = 0.8*scale/1000*(libletter[inputletters[i]-'a'].letpoints[j].x - vx*dt/1000*j); //+500*(i-inputletters.size()/2.0));
+                int y = -scale/1000*(libletter[inputletters[i]-'a'].letpoints[j].y);
+                IO.writeOutXY(X+cost*x + sint*y,Y-sint*x + cost*y);
+                wait_us(dt);
+                IO.setRGBPower(0);//on
+            }
+            IO.setRGBPower(1);
+            wait(0.01);
+        }
+
+        /*for(int j=0; j<buf.size()/2; j++) {
+            if ( (buf[j*2] == 222) && (buf[j*2+1] == 222)){
+                IO.setRGBPower(1);
+                int xx=buf[j*2+2]- vx*dt/1000*j;
+                int yy=buf[j*2+3];//- vy*dt/1000*j;
+                IO.writeOutXY(startX+cost*xx + sint*yy,startY-sint*xx + cost*yy);
+                wait(0.0004);
+                IO.setRGBPower(0);
+            }
+            else{
             timer.start();
-            IO.writeOutXY(buf[j*2],buf[j*2+1]);
+            int xx=buf[j*2]- vx*dt/1000*j;
+            int yy=buf[j*2+1];//- vy*dt/1000*j;
+            IO.writeOutXY(startX+cost*xx + sint*yy,startY-sint*xx + cost*yy);
             wait_us(dt);
             IO.setRGBPower(0); //on
             timer.stop();
             //dt=timer.read();
-        }
-
+            }
+            
+            timecounter += dt;
+            if(timecounter >= T) timecounter = 0; 
+        }*/
+        buf2.clear();
         IO.setRGBPower(1); //off
         wait(0.02);
 
@@ -129,44 +177,53 @@
         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)
+            inputletters.push_back(val);
+        }
+
+        /*else if (val == '/') {
+            makeBuffer();
+        }*/ else if (val == '.') {
+            inputletters.clear();
         }
 
         else if (val == 'X') {
+            beforeX = X;
             stringData[indexStringData] = 0;
-            vx = atoi(stringData);
+            X = atoi(stringData);
             indexStringData=0;
+            timer.stop();
+            vx = ((float)X-(float)beforeX) / timer.read();
             //newSpeedReady = true;
+            theta=atan2(vx,vy);
+            timer.start();
         }
 
         else if (val == 'Y') {
+            beforeY = Y;
             stringData[indexStringData] = 0;
-            vy = atoi(stringData);
+            Y = atoi(stringData);
             indexStringData=0;
+            timer.stop();
             //newSpeedReady = true;
-            theta=atan2(vy,vx);
-            recomputeBuffer();
+            vx = ((float)Y-(float)beforeY) / timer.read();
+            theta=atan2(vx,vy);
+            timer.start();
         }
 
         else if (val == 'T') {
             stringData[indexStringData] = 0;
             dt = atoi(stringData);
             indexStringData=0;
-            recomputeBuffer();
-        }
-        else if (val == 'S') {
+            //makeBuffer();
+        } else if (val == 'S') {
             stringData[indexStringData] = 0;
             scale = atoi(stringData);
             indexStringData=0;
-            recomputeBuffer();
-        }
-        else if (val == 'D') {
-            stringData[indexStringData] = 0;
-            dist = atoi(stringData);
-            indexStringData=0;
-            recomputeBuffer();
-        }
+            //makeBuffer();
+        } 
         // X value?
-        else if (val=='x') {
+        /*else if (val=='x') {
             stringData[indexStringData] = 0 ;
             omegaX=atoi(stringData);
             indexStringData=0;
@@ -177,7 +234,7 @@
             stringData[indexStringData] = 0 ;
             omegaY=atoi(stringData);
             indexStringData=0;
-            recomputeBuffer();
+            makeBuffer();
             newPositionReady=true;
         }
 
@@ -197,7 +254,7 @@
             indexStringData=0;
             IO.setRGBPower(power);
         }
-
+        */
 
     }
 }
\ No newline at end of file