this version 10/17

Dependencies:   mbed

Fork of linearMirrorMotion by Alvaro Cassinelli

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <string>
00003 #include <vector>
00004 #include "renderclass.h"
00005 #include "laserProjectorHardware.h"
00006 
00007 render myRender;
00008 
00009 InterruptIn clockEncoderPin(p14);   //blue
00010 DigitalIn directionPin(p15);    //green
00011 
00012 void processSerial();
00013 Timer timer_v;          // for shearing        //for send the speed on the serial port every 30 ms:
00014 LocalFileSystem local("local");
00015 //Serial pc(USBTX, USBRX);
00016 
00017 unsigned int X, Y, T;
00018 unsigned int beforeX , beforeY;
00019 int startX = CENTER_AD_MIRROR_X;
00020 int startY = CENTER_AD_MIRROR_Y;
00021 
00022 float sint=0, cost=1;
00023 
00024 float vx = 0, vy=0;
00025 float theta=0, dt_betWords=50000, st=1800;
00026 unsigned int dt=500, ticktime=600;
00027 
00028 bool newPositionReady=false;
00029 unsigned int counter=0;
00030 vector<char> inputletters;
00031 
00032 bool start=false;
00033 
00034 
00035 //renderclass----------------------------------------------------------
00036 
00037 vector<letter> alphabet;   // letter library
00038 vector<letter> myText;
00039 
00040 /*
00041 string textString="H";
00042 
00043 void createTextPoints(string& _str) {
00044     myText.clear();
00045     for (int i=0; i<_str.length(); i++) {
00046         myText.push_back(alphabet[(int)_str[i]-'a']);
00047     }
00048 }
00049 */
00050 
00051 ////for culculate rotary velocity --------------------------------------------------------
00052 
00053 volatile float angleIncrement = 2.0*PI / 1024; // when in Sign/Magnitude mode
00054 float radious = 700, attachSecond = 10000;
00055 
00056 // a ticker function to compute the speed periodically-------------------------------------------------------------
00057 #define PERIODIC_COMPUTE 10000 // in us
00058 volatile float angularSpeed = 0;
00059 volatile float angle=0, oldAngle=0;
00060 Ticker speedTimerCompute;
00061 Ticker superEncoder;
00062 
00063 // the external interrupt routine:
00064 void encoderClock()
00065 {
00066     if (directionPin) angle += angleIncrement ;
00067     else angle -= angleIncrement;
00068 }
00069 
00070 void computeSpeed()
00071 {
00072     // We know exactly how much time passed since we last computed the speed, this is PERIODIC_COMPUTE in microseconds
00073     float oldspeed = angularSpeed;
00074     angularSpeed = ( 1000000.0 * (float)(angle-oldAngle) / (float)(PERIODIC_COMPUTE) + oldspeed )/ 2.0; // in rad/sec
00075     oldAngle=angle;
00076     myRender.updateSpeed(angularSpeed,angle);
00077     void processSerial();
00078 }
00079 
00080 
00081 int main()
00082 {
00083     //read from TextFileLibrary ------------------------------------------------------
00084     FILE *fp = fopen("/local/text.txt", "r");
00085     if(!fp) {
00086         IO.setGreenPower(1);
00087         exit(1);
00088     }
00089 
00090     int letternum;
00091     fscanf(fp, "%d", &letternum);
00092     for(int i=0; i<letternum; i++) {
00093         letter bufl;
00094         fscanf(fp, "%d", &bufl.pointnum);
00095         for(int j=0; j<bufl.pointnum; j++) {
00096             point2dl bufp;
00097             fscanf(fp, "%d", &bufp.x);
00098             fscanf(fp, "%d", &bufp.y);
00099             fscanf(fp, "%d", &bufp.laserSwitch);
00100             bufl.letpoints.push_back(bufp);
00101         }
00102         alphabet.push_back(bufl);
00103     }
00104 
00105 
00106     // SETUP: --------------------------------------------------------------------------------------------
00107     IO.init(); // note: serial speed can be changed by checking in the hardwareIO.cpp initialization
00108 
00109     // initialize the angle (arbitrary origin):
00110     oldAngle=angle=0;
00111 
00112     // Attach the external interrupt routine----------------------------------------------------------------------
00113     superEncoder.attach_us(&encoderClock, 111);
00114     //clockEncoderPin.rise(&encoderClock);
00115     //clockEncoderPin.fall(&encoderClock);
00116 
00117     //using renderclass.cpp ----------------------------------------------------------
00118     //createTextPoints(textString);
00119     myRender.setRender(&myText);
00120     myRender.startRender();
00121 
00122     // Attach the periodic computing function:
00123     speedTimerCompute.attach_us(&computeSpeed, PERIODIC_COMPUTE);
00124 
00125     // Set displaying laser powers--------------------------------------------------------------------------
00126     IO.setRedPower(0);
00127     IO.setGreenPower(0);
00128     wait_ms(100);
00129 
00130 
00131     timer_v.start();
00132     // MAIN LOOP: --------------------------------------------------------------------------------------------
00133     while(1) {
00134         if (pc.readable()>0) processSerial();
00135     }
00136 }
00137 
00138 // --------------------------------------------------------------------------------------------
00139 // String to store ALPHANUMERIC DATA (i.e., integers, floating point numbers, unsigned ints, etc represented as DEC) sent wirelessly:
00140     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...
00141     int indexStringData=0;//position of the byte in the string
00142 
00143     void processSerial() {
00144         start=true;
00145         while(pc.readable()>0) {
00146 
00147             char val =pc.getc();
00148 
00149             // Save ASCII numeric characters (ASCII 0 - 9) on stringData:
00150             if ((val >= '0') && (val <= '9')) { // this is 45 to 57 (included)
00151                 stringData[indexStringData] = val;
00152                 indexStringData++;
00153             } else if ((val >= 'a') && (val <= 'z')) { // this is 45 to 57 (included)
00154                 myText.push_back(alphabet[val - 'a']);
00155                 //inputletters.push_back(val);
00156             }
00157 
00158             else if (val == '/') {
00159                 myRender.stopRender();
00160                 myRender.setRender(&myText);
00161                 myRender.startRender();
00162             } else if (val == '.') {
00163                 myRender.stopRender();
00164                 myText.clear();
00165                 myRender.setRender(&myText);
00166                 //inputletters.clear();
00167             }
00168 
00169             else if (val == 'X') {
00170                 beforeX = X;
00171                 stringData[indexStringData] = 0;
00172                 X = atoi(stringData);
00173                 indexStringData=0;
00174                 vx = ((float)X-(float)beforeX) / (float)timer_v.read_us() *1000;
00175             }
00176 
00177             else if (val == 'Y') {
00178                 beforeY = Y;
00179                 stringData[indexStringData] = 0;
00180                 Y = atoi(stringData);
00181                 indexStringData=0;
00182                 //timer_v.stop();
00183                 //newSpeedReady = true;
00184                 //if( (Y-beforeY) > 5){
00185                 vy = ((float)Y-(float)beforeY) / (float)timer_v.read_us() *1000;
00186                 
00187                 myRender.updateSpeed(vx, vy);
00188                 
00189                
00190             }
00191 
00192             else if (val == 'D') {
00193                 stringData[indexStringData] = 0;
00194                 dt = atoi(stringData);
00195                 indexStringData=0;
00196                 //makeBuffer();
00197             } else if (val == 'B') {
00198                 stringData[indexStringData] = 0;
00199                 dt_betWords = atoi(stringData);
00200                 indexStringData=0;
00201                 //makeBuffer();
00202             } else if (val == 'S') {
00203                 stringData[indexStringData] = 0;
00204                 myRender.shearingSwitch = 1 - myRender.shearingSwitch;
00205                 pc.printf("shearingSwitch : %d \n" , myRender.shearingSwitch);
00206                 indexStringData=0;
00207                 //makeBuffer();
00208             } else if (val == 'R') {
00209                 stringData[indexStringData] = 0;
00210                 radious = atoi(stringData);
00211                 indexStringData=0;
00212                 //makeBuffer();
00213             } else if (val == 'T') {
00214                 stringData[indexStringData] = 0;
00215                 attachSecond = atoi(stringData);
00216                 pc.printf("attachSecond : %d \n" , attachSecond);
00217                 indexStringData=0;
00218                 //makeBuffer();
00219             }
00220             // X value?
00221             /*else if (val=='x') {
00222                 stringData[indexStringData] = 0 ;
00223                 omegaX=atoi(stringData);
00224                 indexStringData=0;
00225                 //newPositionReady=true;
00226             }
00227             // Y value?
00228             else if (val=='y') {
00229                 stringData[indexStringData] = 0 ;
00230                 omegaY=atoi(stringData);
00231                 indexStringData=0;
00232                 makeBuffer();
00233                 newPositionReady=true;
00234             }
00235 
00236             else if (val=='g') {
00237                 stringData[indexStringData] = 0 ;
00238                 int power=atoi(stringData);
00239                 indexStringData=0;
00240                 IO.setGreenPower(power);
00241             } else if (val=='r') {
00242                 stringData[indexStringData] = 0 ;
00243                 int power=atoi(stringData);
00244                 indexStringData=0;
00245                 IO.setRedPower(power);
00246             } else if (val=='c') {
00247                 stringData[indexStringData] = 0 ;
00248                 int power=atoi(stringData);
00249                 indexStringData=0;
00250                 IO.setRGBPower(power);
00251             }
00252             */
00253 
00254         }
00255     }