Basic motor code

Dependencies:   Motordriver mbed HALLFX_ENCODER Servo

Revision:
10:9066603296bd
Parent:
9:aa739588a86b
Child:
12:b107ba24faa4
--- a/main.cpp	Tue May 01 15:25:31 2018 +0000
+++ b/main.cpp	Tue May 01 16:11:08 2018 +0000
@@ -7,7 +7,7 @@
 #include <math.h>
 #include <string.h>
 
-#define SPEED 0.32
+#define SPEED 0.33
 #define TICKSPERREV 390
 #define DISTPERREV 8.25         // 8.25 inches per revolution
 #define TicksPerDeg 2.73
@@ -73,8 +73,8 @@
 void turnLeft(double degrees) {
     leftEnc.reset();
     rightEnc.reset();
-    right.speed(SPEED);
-    left.speed(-SPEED);
+    right.speed(SPEED + 0.05);
+    left.speed(-SPEED - 0.05);
     double numTicks = degrees * TicksPerDeg;
     while (leftEnc.read() < numTicks || rightEnc.read() < numTicks) {
         if (leftEnc.read() >= numTicks) {
@@ -85,19 +85,17 @@
         }
     }
     stop();
-    blue.printf("Left Turn\n Left Encoder: %d\n Right Encoder: %d\n\n", leftEnc.read(), rightEnc.read());
     wait(0.5);
 }
 
 void turnRight(double degrees) {
     leftEnc.reset();
     rightEnc.reset();
-    right.speed(-SPEED);
-    left.speed(SPEED);
+    right.speed(-SPEED - 0.05);
+    left.speed(SPEED + 0.05);
     double numTicks = degrees * TicksPerDeg;
     while (leftEnc.read() < numTicks && rightEnc.read() < numTicks) {}
     stop();
-    blue.printf("Right Turn\n Left Encoder: %d\n Right Encoder: %d\n\n", leftEnc.read(), rightEnc.read());
     wait(0.5);
 }
 
@@ -114,9 +112,10 @@
 }
 
 void makeCircle() {
+    penDown();
     leftEnc.reset();
     rightEnc.reset();
-    right.speed(SPEED);
+    right.speed(0.4);
     float numTicks = 2 * 360 * TicksPerDeg;
     while (rightEnc.read() < numTicks) {}
     stop();
@@ -124,6 +123,7 @@
 }
 
 void makeSquare(int sideLength) {
+    penDown();
     forward(sideLength);
     turn(90);
     forward(sideLength);
@@ -135,6 +135,7 @@
 }
 
 void makeTriangle(int sideLength) {
+    penDown();
     forward(sideLength);
     turn(120);
     forward(sideLength);
@@ -144,6 +145,7 @@
 }
 
 void makeHexagon(int sideLength) {
+    penDown();
     forward(sideLength);
     turn(60);
     forward(sideLength);
@@ -206,74 +208,64 @@
 int main() {
     
     penDown();
-    makeHexagon(3);
-    //makeCircle();
-//    makeTriangle();
-    //forward(5.0);
-    //turn(90);
     
-    //drawLetter(let_a);
-    /*
     blue.baud(9600);
     blue.attach(&blue_interrupt);
     
     while (1) {
         get_command();
-        blue.printf("%s, len = %d\n", command, strlen(command));
         
         const char *delimiter = " ";    // space character to separate arguments
-        char *arg1;
-        char *arg2;
         
-        arg1 = strtok(command, delimiter);
-        arg2 = strtok(NULL, delimiter);
+        char *arg1 = strtok(command, delimiter);
+        char *arg2 = strtok(NULL, delimiter);
+        char *arg3 = strtok(NULL, delimiter);
         
         if (strcmp(arg1, "draw") == 0) {
             // first argument is draw
-            blue.printf("  First argument is draw\n");
             
             if (strcmp(arg2, "square") == 0) {
                 // second argument is square
-                blue.printf("  Second argument is square\n");
+                int sideDistance = 3;
+                if (arg3 != NULL) {
+                    sideDistance = arg3[0] - '0';      
+                }
+                blue.printf("Drawing Square with side length = %d\n", sideDistance);
+                makeSquare(sideDistance);
             } else if (strcmp(arg2, "circle") == 0) {
                 // second argument is circle
-                blue.printf("  Second argument is circle\n");
+                blue.printf("Drawing Circle\n");
+                makeCircle();
+            } else if (strcmp(arg2, "triangle") == 0) {
+                // second argument is triangle
+                int sideDistance = 3;
+                if (arg3 != NULL) {
+                    sideDistance = arg3[0] - '0';      
+                }
+                blue.printf("Drawing Triangle with side length = %d\n", sideDistance);
+                makeTriangle(sideDistance);
+            } else if (strcmp(arg2, "hexagon") == 0) {
+                // second argument is hexagon
+                int sideDistance = 3;
+                if (arg3 != NULL) {
+                    sideDistance = arg3[0] - '0';      
+                }
+                blue.printf("Drawing Hexagon with side length = %d\n", sideDistance);
+                makeHexagon(sideDistance);
             } else {
                 // second argument is not recognized
-                blue.printf("  Second argument is not recognized (must be square or circle)\n");
+                blue.printf("Second argument is not recognized (must be square, circle, triangle, hexagon)\n");
             }
             
         } else if (strcmp(arg1, "write") == 0) {
             // first argument is write
-            blue.printf("  First argument is write\n");
-            blue.printf("  Second argument is %s\n", arg2);
+            blue.printf("First argument is write\n");
+            blue.printf("Second argument is %s\n", arg2);
         } else {
             // first argument is not recognized
-            blue.printf("  First argument is not recognized (must be draw or write)\n");
+            blue.printf("ERROR: First argument is not recognized (must be draw or write)\n");
         }
         
         blue.printf("\n");
     }
-    */
-    
-    /*
-    while(1) {
-        pen.calibrate(0.0005, 180.0);
-        for(float p=0; p<1.0; p += 0.1) {
-            pen.write(p);
-            wait(2);
-        }
-    }
-    */
-    
-    //forward(5.0);
-    //forward(5.0);
-    //turnLeft(90);
-    //Should make a triangle
-    //for (int i = 0; i < 3; i++){
-    //    forward(5.0);
-    //    turnLeft(120);
-    //}
-    //manualcheck();
-    //forward(10.0);
 }
\ No newline at end of file