sotsuron

Fork of linearMirrorMotion1017 by Hiromasa Oku

main.cpp

Committer:
hiromasaoku
Date:
2012-10-24
Revision:
4:e00e709d7173
Parent:
2:0548c7bf9fba
Child:
5:e02cd57242c7

File content as of revision 4:e00e709d7173:

#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");

unsigned int X, Y;
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;

bool newPositionReady=false;
unsigned int counter=0;

struct point{
    int x,y;
};

struct letter{
    int pointnum;
    vector<struct point> letpoints;
};

//int points[200][2] = {};
vector<struct letter> points;   // drawing points
    
vector<int> buf;                            //buffer vector

void recomputeBuffer()
{
    float sint = sin(theta);
    float cost = cos(theta);

    startX = CENTER_AD_MIRROR_X;
    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);
    }
}


int main()
{
    FILE *fp = fopen("/local/test.txt", "r");
    if(!fp) {IO.setGreenPower(1); exit(1);}
    int letternum;
    fscanf(fp, "%d", &letternum);
    for(int i=0;i<letternum;i++){
        struct letter bufl;
        fscanf(fp, "%d", &bufl.pointnum);
        for(int j=0;j<bufl.pointnum;j++){
            struct point bufp;
            fscanf(fp, "%d", &bufp.x);
            fscanf(fp, "%d", &bufp.y);
            bufl.letpoints.push_back(bufp);
        }
    }
    
    
    
    /*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();

    // SETUP: --------------------------------------------------------------------------------------------
    IO.init(); // note: serial speed can be changed by checking in the hardwareIO.cpp initialization

    // Set displaying laser powers:
    IO.setRedPower(0);
    IO.setGreenPower(0);//turn on the green (displaying) laser

    wait_ms(100);

    X = CENTER_AD_MIRROR_X;
    Y = CENTER_AD_MIRROR_Y;
    IO.writeOutXY(X,Y);

    // MAIN LOOP: --------------------------------------------------------------------------------------------
    while(1) {
        if (pc.readable()>0) processSerial();

        for(int j=0; j<buf.size(); j++) {
            timer.start();
            IO.writeOutXY(buf[j*2],buf[j*2+1]);
            wait_us(dt);
            IO.setRGBPower(0); //on
            timer.stop();
            //dt=timer.read();
        }

        IO.setRGBPower(1); //off
        wait(0.02);

    }
}


// --------------------------------------------------------------------------------------------
// 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

void processSerial()
{

    while(pc.readable()>0) {

        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 == 'X') {
            stringData[indexStringData] = 0;
            vx = atoi(stringData);
            indexStringData=0;
            //newSpeedReady = true;
        }

        else if (val == 'Y') {
            stringData[indexStringData] = 0;
            vy = atoi(stringData);
            indexStringData=0;
            //newSpeedReady = true;
            theta=atan2(vy,vx);
            recomputeBuffer();
        }

        else if (val == 'T') {
            stringData[indexStringData] = 0;
            dt = atoi(stringData);
            indexStringData=0;
            recomputeBuffer();
        }
        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();
        }
        // 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;
            recomputeBuffer();
            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);
        }


    }
}