Final Project files for mBed development.

Dependencies:   m3pi mbed

Files at this revision

API Documentation at this revision

Comitter:
alecguertin
Date:
Thu Dec 18 03:53:42 2014 +0000
Parent:
41:1b6d8664d7a7
Child:
43:a0c9549a6f8a
Commit message:
added finished state and fix for reading instructions

Changed in this revision

main.c Show annotated file Show diff for this revision Revisions of this file
--- a/main.c	Thu Dec 18 03:31:58 2014 +0000
+++ b/main.c	Thu Dec 18 03:53:42 2014 +0000
@@ -29,6 +29,7 @@
     TURN,
     MOVE,
     DRAW,
+    FINISHED,
 };
 
 /** @brief Entry point. Main loop. */
@@ -40,7 +41,7 @@
     double      dist;
     float       delta_a, angle, cal_time;
     size_t      bytes_read;
-    int         corner, err, init, not_done, x, y, last_x, last_y, dim_x, dim_y, offset;
+    int         corner, err, init, x, y, last_x, last_y, dim_x, dim_y, offset;
     char        instbuf[INST_BUF_SIZE];
     char        *cur, *next;
     angle       =   0;
@@ -51,7 +52,6 @@
     bytes_read  =   0;
     offset      =   0;
     init        =   0;
-    not_done    =   1;
     caltimer = Timer();
 
     /* First calibrate robot and figure out space dimensions. */
@@ -116,10 +116,10 @@
      */
     state = PARSE;
     last_state = PARSE;
-    while (not_done) {
+    while (1) {
         switch (state) {
             case PARSE:
-                if (!init) {
+                if (init) {
                     next = strchr(cur, '\n');
                     if (next == NULL) {
                         last_state = state;
@@ -129,14 +129,15 @@
                     cur = next+1;
                     offset = instbuf+INST_BUF_SIZE-cur-1;
                     memcpy(instbuf, cur, offset);
-                    init = 1;
                 }
+                init = 1;
     
                 memset(instbuf+offset, 0, INST_BUF_SIZE-offset);
                 bytes_read = fread(instbuf+offset, sizeof(char), INST_BUF_SIZE-1-offset, ps_file);
                 if (instbuf[0] == '\0') {
                     pen_up();
-                    not_done = 0;
+                    last_state = state;
+                    state = FINISHED;
                     break;
                 }
 
@@ -172,23 +173,28 @@
                 }
             case DRAW:
                 dist = fabs(distance(last_x, last_y, x, y));
-                dist = CAL_FACTOR*cal_time*(dist/(double)dim_x);
                 pen_down();
-                forward(dist);
+                forward(CAL_FACTOR*cal_time*(dist/(double)dim_x));
                 pen_up();
                 last_state = state;
                 state = PARSE;
                 break;
             case MOVE:
                 dist = fabs(distance(last_x, last_y, x, y));
-                dist = CAL_FACTOR*cal_time*(dist/(double)dim_x);
-                forward(dist);
+                forward(CAL_FACTOR*cal_time*(dist/(double)dim_x));
                 last_state = state;
                 state = PARSE;
                 break;
+            case FINISHED:
+                robot_printf(0, "done\n");
+                while(1);
             case ERROR:
-                robot_printf(0, "E:%d", last_state);
+                robot_printf(0, "E:%d\n", last_state);
                 while(1);
+            default:
+                last_state = state;
+                state = ERROR;
+                break;
         }
     }
     /*