this version 10/17

Dependencies:   mbed

Fork of linearMirrorMotion by Alvaro Cassinelli

Committer:
hiromasaoku
Date:
Fri May 24 06:37:58 2013 +0000
Revision:
19:8e9fe7d25b9c
Parent:
18:6f86abfae754
Child:
20:8475768fc2f7
moves almost successfully  \(^u^)/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedalvaro 0:4e12dea53fbe 1 #include "mbed.h"
hiromasaoku 19:8e9fe7d25b9c 2 #include <string>
hiromasaoku 16:6dd2e60bc5bc 3 #include <vector>
hiromasaoku 16:6dd2e60bc5bc 4 #include "renderclass.h"
mbedalvaro 1:daf6b4939120 5 #include "laserProjectorHardware.h"
hiromasaoku 7:64b313c58420 6
hiromasaoku 17:dce982e0a383 7 render myRender;
hiromasaoku 17:dce982e0a383 8
hiromasaoku 19:8e9fe7d25b9c 9 InterruptIn clockEncoderPin(p14); //blue
hiromasaoku 19:8e9fe7d25b9c 10 DigitalIn directionPin(p15); //green
mbedalvaro 0:4e12dea53fbe 11
mbedalvaro 0:4e12dea53fbe 12 void processSerial();
hiromasaoku 17:dce982e0a383 13 Timer timer_v; // for shearing
hiromasaoku 16:6dd2e60bc5bc 14 Timer timer; //for send the speed on the serial port every 30 ms:
hiromasaoku 4:e00e709d7173 15 LocalFileSystem local("local");
hiromasaoku 5:e02cd57242c7 16 //Serial pc(USBTX, USBRX);
mbedalvaro 0:4e12dea53fbe 17
hiromasaoku 10:a3dd8ec4be60 18 unsigned int X, Y, T;
hiromasaoku 5:e02cd57242c7 19 unsigned int beforeX , beforeY;
hiromasaoku 4:e00e709d7173 20 int startX = CENTER_AD_MIRROR_X;
hiromasaoku 4:e00e709d7173 21 int startY = CENTER_AD_MIRROR_Y;
hiromasaoku 4:e00e709d7173 22
hiromasaoku 6:a82917b3b1aa 23 float sint=0, cost=1;
hiromasaoku 17:dce982e0a383 24
hiromasaoku 10:a3dd8ec4be60 25 float vx = 0, vy=0;
hiromasaoku 14:314b86828ed2 26 float theta=0, dt_betWords=50000, st=1800;
hiromasaoku 10:a3dd8ec4be60 27 unsigned int dt=500, ticktime=600;
hiromasaoku 4:e00e709d7173 28
mbedalvaro 0:4e12dea53fbe 29 bool newPositionReady=false;
mbedalvaro 0:4e12dea53fbe 30 unsigned int counter=0;
hiromasaoku 5:e02cd57242c7 31 vector<char> inputletters;
mbedalvaro 0:4e12dea53fbe 32
hiromasaoku 6:a82917b3b1aa 33 bool start=false;
hiromasaoku 6:a82917b3b1aa 34
hiromasaoku 12:d945fb6d4988 35
hiromasaoku 16:6dd2e60bc5bc 36 //renderclass----------------------------------------------------------
hiromasaoku 12:d945fb6d4988 37
hiromasaoku 16:6dd2e60bc5bc 38 vector<letter> alphabet; // letter library
hiromasaoku 16:6dd2e60bc5bc 39 vector<letter> myText;
hiromasaoku 12:d945fb6d4988 40
hiromasaoku 19:8e9fe7d25b9c 41 /*
hiromasaoku 19:8e9fe7d25b9c 42 string textString="H";
hiromasaoku 16:6dd2e60bc5bc 43
hiromasaoku 19:8e9fe7d25b9c 44 void createTextPoints(string& _str) {
hiromasaoku 19:8e9fe7d25b9c 45 myText.clear();
hiromasaoku 19:8e9fe7d25b9c 46 for (int i=0; i<_str.length(); i++) {
hiromasaoku 19:8e9fe7d25b9c 47 myText.push_back(alphabet[(int)_str[i]-'a']);
hiromasaoku 19:8e9fe7d25b9c 48 }
hiromasaoku 19:8e9fe7d25b9c 49 }
hiromasaoku 19:8e9fe7d25b9c 50 */
hiromasaoku 12:d945fb6d4988 51
hiromasaoku 16:6dd2e60bc5bc 52 ////for culculate rotary velocity --------------------------------------------------------
hiromasaoku 4:e00e709d7173 53
hiromasaoku 19:8e9fe7d25b9c 54 volatile float angleIncrement = 2.0*PI / 360; // when in Sign/Magnitude mode
hiromasaoku 19:8e9fe7d25b9c 55 float radious = 700, attachSecond = 10000;
hiromasaoku 4:e00e709d7173 56
hiromasaoku 16:6dd2e60bc5bc 57 // a ticker function to compute the speed periodically-------------------------------------------------------------
hiromasaoku 10:a3dd8ec4be60 58 #define PERIODIC_COMPUTE 10000 // in us
hiromasaoku 10:a3dd8ec4be60 59 volatile float angularSpeed = 0;
hiromasaoku 10:a3dd8ec4be60 60 volatile float angle=0, oldAngle=0;
hiromasaoku 10:a3dd8ec4be60 61 Ticker speedTimerCompute;
hiromasaoku 11:8a0fecc86705 62 Ticker superEncoder;
hiromasaoku 5:e02cd57242c7 63
hiromasaoku 10:a3dd8ec4be60 64 // the external interrupt routine:
hiromasaoku 17:dce982e0a383 65 void encoderClock()
hiromasaoku 17:dce982e0a383 66 {
hiromasaoku 17:dce982e0a383 67 if (directionPin) angle += angleIncrement ;
hiromasaoku 17:dce982e0a383 68 else angle -= angleIncrement;
hiromasaoku 7:64b313c58420 69 }
hiromasaoku 7:64b313c58420 70
hiromasaoku 17:dce982e0a383 71 void computeSpeed()
hiromasaoku 17:dce982e0a383 72 {
hiromasaoku 10:a3dd8ec4be60 73 // We know exactly how much time passed since we last computed the speed, this is PERIODIC_COMPUTE in microseconds
hiromasaoku 14:314b86828ed2 74 angularSpeed = 1000000.0 * (float)(angle-oldAngle) / (float)(PERIODIC_COMPUTE); // in rad/sec
hiromasaoku 10:a3dd8ec4be60 75 oldAngle=angle;
hiromasaoku 19:8e9fe7d25b9c 76 myRender.updateSpeed(angularSpeed,angle);
hiromasaoku 10:a3dd8ec4be60 77 }
hiromasaoku 17:dce982e0a383 78
hiromasaoku 10:a3dd8ec4be60 79
hiromasaoku 17:dce982e0a383 80 int main()
hiromasaoku 17:dce982e0a383 81 {
hiromasaoku 10:a3dd8ec4be60 82 //read from TextFileLibrary ------------------------------------------------------
hiromasaoku 9:4db47b8eb750 83 FILE *fp = fopen("/local/text.txt", "r");
hiromasaoku 5:e02cd57242c7 84 if(!fp) {
hiromasaoku 5:e02cd57242c7 85 IO.setGreenPower(1);
hiromasaoku 5:e02cd57242c7 86 exit(1);
hiromasaoku 17:dce982e0a383 87 }
hiromasaoku 17:dce982e0a383 88
hiromasaoku 4:e00e709d7173 89 int letternum;
hiromasaoku 4:e00e709d7173 90 fscanf(fp, "%d", &letternum);
hiromasaoku 5:e02cd57242c7 91 for(int i=0; i<letternum; i++) {
hiromasaoku 5:e02cd57242c7 92 letter bufl;
hiromasaoku 4:e00e709d7173 93 fscanf(fp, "%d", &bufl.pointnum);
hiromasaoku 5:e02cd57242c7 94 for(int j=0; j<bufl.pointnum; j++) {
hiromasaoku 12:d945fb6d4988 95 point2dl bufp;
hiromasaoku 4:e00e709d7173 96 fscanf(fp, "%d", &bufp.x);
hiromasaoku 4:e00e709d7173 97 fscanf(fp, "%d", &bufp.y);
hiromasaoku 9:4db47b8eb750 98 fscanf(fp, "%d", &bufp.laserSwitch);
hiromasaoku 4:e00e709d7173 99 bufl.letpoints.push_back(bufp);
hiromasaoku 4:e00e709d7173 100 }
hiromasaoku 16:6dd2e60bc5bc 101 alphabet.push_back(bufl);
hiromasaoku 4:e00e709d7173 102 }
hiromasaoku 5:e02cd57242c7 103
hiromasaoku 5:e02cd57242c7 104
mbedalvaro 0:4e12dea53fbe 105 // SETUP: --------------------------------------------------------------------------------------------
mbedalvaro 0:4e12dea53fbe 106 IO.init(); // note: serial speed can be changed by checking in the hardwareIO.cpp initialization
hiromasaoku 4:e00e709d7173 107
hiromasaoku 10:a3dd8ec4be60 108 // initialize the angle (arbitrary origin):
hiromasaoku 10:a3dd8ec4be60 109 oldAngle=angle=0;
hiromasaoku 17:dce982e0a383 110
hiromasaoku 16:6dd2e60bc5bc 111 // Attach the external interrupt routine----------------------------------------------------------------------
hiromasaoku 13:26263959903b 112 superEncoder.attach_us(&encoderClock, attachSecond);
hiromasaoku 17:dce982e0a383 113 timer.reset();
hiromasaoku 17:dce982e0a383 114 timer.start();
hiromasaoku 17:dce982e0a383 115
hiromasaoku 16:6dd2e60bc5bc 116 //using renderclass.cpp ----------------------------------------------------------
hiromasaoku 19:8e9fe7d25b9c 117 //createTextPoints(textString);
hiromasaoku 16:6dd2e60bc5bc 118 myRender.setRender(&myText);
hiromasaoku 16:6dd2e60bc5bc 119 myRender.startRender();
hiromasaoku 17:dce982e0a383 120
hiromasaoku 10:a3dd8ec4be60 121 // Attach the periodic computing function:
hiromasaoku 17:dce982e0a383 122 speedTimerCompute.attach_us(&computeSpeed, PERIODIC_COMPUTE);
hiromasaoku 17:dce982e0a383 123
hiromasaoku 16:6dd2e60bc5bc 124 // Set displaying laser powers--------------------------------------------------------------------------
mbedalvaro 0:4e12dea53fbe 125 IO.setRedPower(0);
hiromasaoku 9:4db47b8eb750 126 IO.setGreenPower(0);
mbedalvaro 0:4e12dea53fbe 127 wait_ms(100);
hiromasaoku 17:dce982e0a383 128
hiromasaoku 17:dce982e0a383 129
hiromasaoku 10:a3dd8ec4be60 130 timer_v.start();
mbedalvaro 0:4e12dea53fbe 131 // MAIN LOOP: --------------------------------------------------------------------------------------------
mbedalvaro 0:4e12dea53fbe 132 while(1) {
hiromasaoku 17:dce982e0a383 133 if (pc.readable()>0) processSerial();
hiromasaoku 17:dce982e0a383 134
hiromasaoku 17:dce982e0a383 135 if(1/*start*/) {
hiromasaoku 17:dce982e0a383 136 timer_v.reset();
hiromasaoku 17:dce982e0a383 137 wait_us(5000);
hiromasaoku 19:8e9fe7d25b9c 138 /* send the speed on the serial port every 30 ms:
hiromasaoku 19:8e9fe7d25b9c 139 if (timer.read_ms()>300000) {
hiromasaoku 17:dce982e0a383 140 pc.printf("Angular Speed = %4.2f\t Cumulative Angle = %4.2f\n" , angularSpeed, angle);
hiromasaoku 17:dce982e0a383 141 timer.reset();
hiromasaoku 19:8e9fe7d25b9c 142 }*/
hiromasaoku 11:8a0fecc86705 143 }
mbedalvaro 0:4e12dea53fbe 144 }
mbedalvaro 0:4e12dea53fbe 145 }
mbedalvaro 0:4e12dea53fbe 146
mbedalvaro 0:4e12dea53fbe 147 // --------------------------------------------------------------------------------------------
hiromasaoku 4:e00e709d7173 148 // String to store ALPHANUMERIC DATA (i.e., integers, floating point numbers, unsigned ints, etc represented as DEC) sent wirelessly:
hiromasaoku 17:dce982e0a383 149 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...
hiromasaoku 17:dce982e0a383 150 int indexStringData=0;//position of the byte in the string
mbedalvaro 0:4e12dea53fbe 151
hiromasaoku 17:dce982e0a383 152 void processSerial() {
hiromasaoku 17:dce982e0a383 153 start=true;
hiromasaoku 17:dce982e0a383 154 while(pc.readable()>0) {
hiromasaoku 4:e00e709d7173 155
hiromasaoku 17:dce982e0a383 156 char val =pc.getc();
hiromasaoku 4:e00e709d7173 157
hiromasaoku 17:dce982e0a383 158 // Save ASCII numeric characters (ASCII 0 - 9) on stringData:
hiromasaoku 17:dce982e0a383 159 if ((val >= '0') && (val <= '9')) { // this is 45 to 57 (included)
hiromasaoku 17:dce982e0a383 160 stringData[indexStringData] = val;
hiromasaoku 17:dce982e0a383 161 indexStringData++;
hiromasaoku 17:dce982e0a383 162 } else if ((val >= 'a') && (val <= 'z')) { // this is 45 to 57 (included)
hiromasaoku 19:8e9fe7d25b9c 163 myText.push_back(alphabet[val - 'a']);
hiromasaoku 17:dce982e0a383 164 //inputletters.push_back(val);
hiromasaoku 17:dce982e0a383 165 }
hiromasaoku 5:e02cd57242c7 166
hiromasaoku 19:8e9fe7d25b9c 167 else if (val == '/') {
hiromasaoku 19:8e9fe7d25b9c 168 myRender.stopRender();
hiromasaoku 19:8e9fe7d25b9c 169 myRender.setRender(&myText);
hiromasaoku 19:8e9fe7d25b9c 170 myRender.startRender();
hiromasaoku 19:8e9fe7d25b9c 171 } else if (val == '.') {
hiromasaoku 19:8e9fe7d25b9c 172 myRender.stopRender();
hiromasaoku 17:dce982e0a383 173 myText.clear();
hiromasaoku 19:8e9fe7d25b9c 174 myRender.setRender(&myText);
hiromasaoku 17:dce982e0a383 175 //inputletters.clear();
hiromasaoku 17:dce982e0a383 176 }
mbedalvaro 0:4e12dea53fbe 177
hiromasaoku 17:dce982e0a383 178 else if (val == 'X') {
hiromasaoku 17:dce982e0a383 179 beforeX = X;
hiromasaoku 17:dce982e0a383 180 stringData[indexStringData] = 0;
hiromasaoku 17:dce982e0a383 181 X = atoi(stringData);
hiromasaoku 17:dce982e0a383 182 indexStringData=0;
hiromasaoku 17:dce982e0a383 183 vx = ((float)X-(float)beforeX) / (float)timer_v.read_us() *1000;
hiromasaoku 17:dce982e0a383 184 }
hiromasaoku 4:e00e709d7173 185
hiromasaoku 17:dce982e0a383 186 else if (val == 'Y') {
hiromasaoku 17:dce982e0a383 187 beforeY = Y;
hiromasaoku 17:dce982e0a383 188 stringData[indexStringData] = 0;
hiromasaoku 17:dce982e0a383 189 Y = atoi(stringData);
hiromasaoku 17:dce982e0a383 190 indexStringData=0;
hiromasaoku 17:dce982e0a383 191 //timer_v.stop();
hiromasaoku 17:dce982e0a383 192 //newSpeedReady = true;
hiromasaoku 17:dce982e0a383 193 //if( (Y-beforeY) > 5){
hiromasaoku 17:dce982e0a383 194 vy = ((float)Y-(float)beforeY) / (float)timer_v.read_us() *1000;
hiromasaoku 18:6f86abfae754 195
hiromasaoku 18:6f86abfae754 196 myRender.updateSpeed(vx, vy);
hiromasaoku 18:6f86abfae754 197
hiromasaoku 18:6f86abfae754 198
hiromasaoku 17:dce982e0a383 199 }
mbedalvaro 2:0548c7bf9fba 200
hiromasaoku 17:dce982e0a383 201 else if (val == 'D') {
hiromasaoku 17:dce982e0a383 202 stringData[indexStringData] = 0;
hiromasaoku 17:dce982e0a383 203 dt = atoi(stringData);
hiromasaoku 17:dce982e0a383 204 indexStringData=0;
hiromasaoku 17:dce982e0a383 205 //makeBuffer();
hiromasaoku 17:dce982e0a383 206 } else if (val == 'B') {
hiromasaoku 17:dce982e0a383 207 stringData[indexStringData] = 0;
hiromasaoku 17:dce982e0a383 208 dt_betWords = atoi(stringData);
hiromasaoku 17:dce982e0a383 209 indexStringData=0;
hiromasaoku 17:dce982e0a383 210 //makeBuffer();
hiromasaoku 17:dce982e0a383 211 } else if (val == 'S') {
hiromasaoku 17:dce982e0a383 212 stringData[indexStringData] = 0;
hiromasaoku 17:dce982e0a383 213 st = atoi(stringData);
hiromasaoku 17:dce982e0a383 214 indexStringData=0;
hiromasaoku 17:dce982e0a383 215 //makeBuffer();
hiromasaoku 17:dce982e0a383 216 } else if (val == 'R') {
hiromasaoku 17:dce982e0a383 217 stringData[indexStringData] = 0;
hiromasaoku 17:dce982e0a383 218 radious = atoi(stringData);
hiromasaoku 17:dce982e0a383 219 indexStringData=0;
hiromasaoku 17:dce982e0a383 220 //makeBuffer();
hiromasaoku 17:dce982e0a383 221 } else if (val == 'T') {
hiromasaoku 17:dce982e0a383 222 stringData[indexStringData] = 0;
hiromasaoku 17:dce982e0a383 223 attachSecond = atoi(stringData);
hiromasaoku 17:dce982e0a383 224 indexStringData=0;
hiromasaoku 17:dce982e0a383 225 //makeBuffer();
hiromasaoku 17:dce982e0a383 226 }
hiromasaoku 17:dce982e0a383 227 // X value?
hiromasaoku 17:dce982e0a383 228 /*else if (val=='x') {
hiromasaoku 17:dce982e0a383 229 stringData[indexStringData] = 0 ;
hiromasaoku 17:dce982e0a383 230 omegaX=atoi(stringData);
hiromasaoku 17:dce982e0a383 231 indexStringData=0;
hiromasaoku 17:dce982e0a383 232 //newPositionReady=true;
hiromasaoku 17:dce982e0a383 233 }
hiromasaoku 17:dce982e0a383 234 // Y value?
hiromasaoku 17:dce982e0a383 235 else if (val=='y') {
hiromasaoku 17:dce982e0a383 236 stringData[indexStringData] = 0 ;
hiromasaoku 17:dce982e0a383 237 omegaY=atoi(stringData);
hiromasaoku 17:dce982e0a383 238 indexStringData=0;
hiromasaoku 17:dce982e0a383 239 makeBuffer();
hiromasaoku 17:dce982e0a383 240 newPositionReady=true;
hiromasaoku 17:dce982e0a383 241 }
hiromasaoku 17:dce982e0a383 242
hiromasaoku 17:dce982e0a383 243 else if (val=='g') {
hiromasaoku 17:dce982e0a383 244 stringData[indexStringData] = 0 ;
hiromasaoku 17:dce982e0a383 245 int power=atoi(stringData);
hiromasaoku 17:dce982e0a383 246 indexStringData=0;
hiromasaoku 17:dce982e0a383 247 IO.setGreenPower(power);
hiromasaoku 17:dce982e0a383 248 } else if (val=='r') {
hiromasaoku 17:dce982e0a383 249 stringData[indexStringData] = 0 ;
hiromasaoku 17:dce982e0a383 250 int power=atoi(stringData);
hiromasaoku 17:dce982e0a383 251 indexStringData=0;
hiromasaoku 17:dce982e0a383 252 IO.setRedPower(power);
hiromasaoku 17:dce982e0a383 253 } else if (val=='c') {
hiromasaoku 17:dce982e0a383 254 stringData[indexStringData] = 0 ;
hiromasaoku 17:dce982e0a383 255 int power=atoi(stringData);
hiromasaoku 17:dce982e0a383 256 indexStringData=0;
hiromasaoku 17:dce982e0a383 257 IO.setRGBPower(power);
hiromasaoku 17:dce982e0a383 258 }
hiromasaoku 17:dce982e0a383 259 */
hiromasaoku 17:dce982e0a383 260
hiromasaoku 10:a3dd8ec4be60 261 }
hiromasaoku 17:dce982e0a383 262 }