Takashi Asano / Mbed 2 deprecated linearMirrorMotion1017

Fork of linearMirrorMotion1017 by Hiromasa Oku

Files at this revision

API Documentation at this revision

Comitter:
hiromasaoku
Date:
Mon May 20 08:25:22 2013 +0000
Parent:
15:1d931a305464
Child:
17:dce982e0a383
Commit message:
a safety commit

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
renderclass.cpp Show annotated file Show diff for this revision Revisions of this file
renderclass.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Apr 08 03:13:15 2013 +0000
+++ b/main.cpp	Mon May 20 08:25:22 2013 +0000
@@ -1,13 +1,15 @@
 #include "mbed.h"
+#include <vector>
+#include "renderclass.h"
 #include "laserProjectorHardware.h"
-#include <vector>
 #define PI 3.141592
 
 InterruptIn clockEncoderPin(p14);
 DigitalIn directionPin(p15);
 
 void processSerial();
-Timer timer_v;
+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);
 
@@ -28,44 +30,20 @@
 
 bool start=false;
 
-struct point2dl {
-    int x,y;
-    int laserSwitch;   // laser {1: on 0:off} until next step
-};
 
-point2dl shearing(point2dl dataBefore, float velocity, float theta){
-    point2dl dataAfter;
-    dataAfter.x = dataBefore.x + velocity * cos(theta) * timer_v.read_us()/st/1000;   //st is a parameter for ajustment
-    dataAfter.y = dataBefore.y + velocity * sin(theta) * timer_v.read_us()/st/1000; 
-    dataAfter.laserSwitch = dataBefore.laserSwitch;
-    
-    return dataAfter;
-}
+//renderclass----------------------------------------------------------
 
-point2dl rotation(point2dl dataBefore, float theta){
-    sint = sin(theta);
-    cost = cos(theta);
-    point2dl dataAfter;
-    dataAfter.x = sint*dataBefore.x + cost*dataBefore.y;
-    dataAfter.y = -cost*dataBefore.x + sint*dataBefore.y;
-    dataAfter.laserSwitch = dataBefore.laserSwitch;
+vector<letter> alphabet;   // letter library
+vector<letter> myText;
 
-    return dataAfter;
-}
+
 
-struct letter {
-    int pointnum;
-    vector<point2dl> letpoints;
-};
-
-vector<letter> libletter;   // letter library
-
-////for culculate rotary velocit800000000y --------------------------------------------------------
+////for culculate rotary velocity --------------------------------------------------------
 
 volatile float angleIncrement = 2.0*PI / 128; // when in Sign/Magnitude mode
 float radious = 700, attachSecond = 500;
 
-// a ticker function to compute the speed periodically:
+// a ticker function to compute the speed periodically-------------------------------------------------------------
 #define PERIODIC_COMPUTE 10000 // in us
 volatile float angularSpeed = 0;
 volatile float angle=0, oldAngle=0;
@@ -83,7 +61,6 @@
     oldAngle=angle;
 }
    
-Timer timer;
 
 int main(){
     //read from TextFileLibrary ------------------------------------------------------
@@ -105,7 +82,7 @@
             fscanf(fp, "%d", &bufp.laserSwitch);
             bufl.letpoints.push_back(bufp);
         }
-        libletter.push_back(bufl);
+        alphabet.push_back(bufl);
     }
 
 
@@ -115,29 +92,30 @@
     // initialize the angle (arbitrary origin):
     oldAngle=angle=0;
   
-    // Attach the external interrupt routine:
-    //clockEncoderPin.rise(&encoderClock);
-    //clockEncoderPin.fall(&encoderClock);
+    // Attach the external interrupt routine----------------------------------------------------------------------
     superEncoder.attach_us(&encoderClock, attachSecond);
     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); 
  
-    // Set displaying laser powers:
+    // Set displaying laser powers--------------------------------------------------------------------------
     IO.setRedPower(0);
     IO.setGreenPower(0);
     wait_ms(100);
-
-    X = beforeX = CENTER_AD_MIRROR_X;
-    Y = beforeY = CENTER_AD_MIRROR_Y;
+    
+    
     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:
@@ -145,44 +123,6 @@
             pc.printf("Angular Speed = %4.2f\t Cumulative Angle = %4.2f\n" , angularSpeed, angle);
             timer.reset();
         }
-        
-        
-        // drawing ///-----------------------------------------------
-        for(int i=0; i<inputletters.size(); i++) {
-        
-            for(int j=0; j<libletter[inputletters[i]-'a'].letpoints.size(); j++) {
-            
-                if (pc.readable()>0) processSerial();
-                
-                point2dl rotated = rotation(libletter[inputletters[i]-'a'].letpoints[j] ,angle);
-                point2dl sheared = shearing(rotated ,radious*angularSpeed, angle);
-                X = CENTER_AD_MIRROR_X + radious * cost;
-                Y = CENTER_AD_MIRROR_Y + radious * sint;
-                IO.writeOutXY(X + rotated.x,Y + rotated.y );
-                
-                /*
-                int x = -0.8*((libletter[inputletters[i]-'a'].letpoints[j].x ) - radious*angularSpeed*timer_v.read_us()/st/1000); //this ZURE should be modifyed not by dt but Timer.
-                int y = (libletter[inputletters[i]-'a'].letpoints[j].y);
-                IO.writeOutXY(X + radious*cost + sint*x + cost*y,Y + radious*sint - cost*x + sint*y);
-                */
-                
-                wait_us(dt);
-                IO.setRedPower(libletter[inputletters[i]-'a'].letpoints[j].laserSwitch);//on
-            }
-            timer_v.reset();
-            IO.setRedPower(0); //off
-            wait_us(dt_betWords);//sqrtf(vx*vx+vy*vy)*100000);
-
-        }
-        ////////////////
-        IO.setRGBPower(1); //off
-        wait_us(10000);
-        start=false;
-        ////////////////////*/
-        }
-        
-        
-
     }
 }
 
@@ -192,9 +132,7 @@
 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()
-{
-
+void processSerial(){
     start=true;
     while(pc.readable()>0) {
 
@@ -205,13 +143,15 @@
             stringData[indexStringData] = val;
             indexStringData++;
         } else if ((val >= 'a') && (val <= 'z')) { // this is 45 to 57 (included)
-            inputletters.push_back(val);
+            myText.push_back(alphabet['a'-1]);
+            //inputletters.push_back(val);
         }
 
         /*else if (val == '/') {
             makeBuffer();
         }*/ else if (val == '.') {
-            inputletters.clear();
+            myText.clear();
+            //inputletters.clear();
         }
 
         else if (val == 'X') {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/renderclass.cpp	Mon May 20 08:25:22 2013 +0000
@@ -0,0 +1,85 @@
+#include "renderclass.h"
+#include "mbed.h"
+#include "laserProjectorHardware.h"
+#include <vector>
+
+Ticker renderTimer;
+Ticker translationTimer;
+
+//for translating -----------------------------------------------------------------
+
+point2dl shearing(point2dl dataBefore, float velocity, float theta){
+    point2dl dataAfter;
+    dataAfter.x = dataBefore.x ;//+ velocity * cos(theta) * timer_v.read_us()/st/1000;   
+    dataAfter.y = dataBefore.y ;//+ velocity * sin(theta) * timer_v.read_us()/st/1000; 
+    dataAfter.laserSwitch = dataBefore.laserSwitch;
+    
+    return dataAfter;
+}
+
+point2dl rotation(point2dl dataBefore, float theta){
+    point2dl dataAfter;
+    dataAfter.x = cos(theta)*dataBefore.x - sin(theta)*dataBefore.y;
+    dataAfter.y = sin(theta)*dataBefore.x + cos(theta)*dataBefore.y;
+    dataAfter.laserSwitch = dataBefore.laserSwitch;
+
+    return dataAfter;
+}
+//----------------------------------------------------------------------------------
+
+void render::startRender(){
+    //void (render::*fpdraw)() = draw;                  // I can't solve the error.......I tried a few days.......... 
+    //renderTimer.attach_us((this->*fpdraw)(), 200);   //  ???????how to set menber function???????
+}
+
+void render::stopRender(){
+    renderTimer.detach();
+}
+
+void render::setRender(vector<letter> *p){
+    ptext = p;
+    
+    //for(int i=0; i!=(*ptext).size(); i++){
+    for(std::vector<letter>::iterator itr = (*ptext).begin(); itr != (*ptext).end(); ++itr){
+        translated.push_back(*itr);
+    }                                            //copy the liblary to the buffer.
+}
+
+void render::draw(){
+    drawPoint.x = translated[currentLetters].letpoints[currentPoints].x;
+    drawPoint.y = translated[currentLetters].letpoints[currentPoints].y;
+    drawPoint.laserSwitch = translated[currentLetters].letpoints[currentPoints].laserSwitch;
+    IO.writeOutXY(drawPoint.x , drawPoint.y);
+    IO.setRedPower(translated[currentLetters].letpoints[currentPoints].laserSwitch);
+    
+    
+    currentPoints++;
+    if(currentPoints > translated[currentLetters].letpoints.size() ){
+        currentLetters++;
+        currentPoints = 0;
+    }
+    if(currentLetters > translated.size()){
+        currentLetters = 0;
+        currentPoints = 0;
+    }
+
+}
+
+void render::translating(float speed, float angle){ //DO when get the speed and angle data.
+    for (int i = 0; i > 10;  i++){
+        int j=0;
+        if(currentPoints + j > (*ptext)[currentLetters].letpoints.size()){
+            currentPoints = 0;
+            currentLetters++;
+            j=0;
+        }
+        if(currentLetters > (*ptext).size()) break; 
+        
+        point2dl bufferRotate = rotation((*ptext)[currentLetters].letpoints[currentPoints],angle);
+        point2dl bufferShear = shearing(bufferRotate, speed, angle);
+    
+        translated[currentLetters].letpoints[currentPoints] = bufferShear;
+    
+        j++;
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/renderclass.h	Mon May 20 08:25:22 2013 +0000
@@ -0,0 +1,27 @@
+#include "mbed.h"
+#include <vector>
+
+struct point2dl {
+    int x,y;
+    int laserSwitch;   // laser {1: on 0:off} until next step
+};
+
+struct letter {
+    int pointnum;
+    vector<point2dl> letpoints;
+};
+
+class render{
+    public:
+    void startRender();
+    void setRender(vector<letter> *p);
+    void translating(float speed, float angle);
+    void stopRender();
+    void draw();
+    private:
+    vector<letter> *ptext;
+    vector<letter> translated;
+    int currentLetters;
+    int currentPoints;
+    point2dl drawPoint;
+};
\ No newline at end of file