Final Project files for mBed development.

Dependencies:   m3pi mbed

Files at this revision

API Documentation at this revision

Comitter:
lsaristo
Date:
Sat Nov 15 23:03:06 2014 +0000
Parent:
9:3a0433c391cb
Child:
11:a30f30d3066e
Commit message:
Started working on control files for robot movement control.

Changed in this revision

control.c Show annotated file Show diff for this revision Revisions of this file
control.h Show annotated file Show diff for this revision Revisions of this file
main.c Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/control.c	Sat Nov 15 23:03:06 2014 +0000
@@ -0,0 +1,48 @@
+/**
+ * @file    control.c
+ * @brief   Implemention of robot control algorithms.
+ *
+ * This file should also contain the logic needed to parse
+ * any drawing file and issue motor commands through functions
+ * defined in main.h
+ *
+ * @author  John Wilkey
+ */
+#include "control.h"
+#include "main.h"
+extern m3pi pi;
+extern DigitalIn start_button;
+
+void get_ps_file(char* moves)
+{
+    char* contents;
+    FILE* fp = fopen(_CANVAS_FILE, "r");
+    fseek(fp, 0L, SEEK_END);
+    int size = ftell(fp);
+    fseek(fp, 0L, SEEK_SET);
+    contents = (char*)malloc(size);
+    fread(contents, size, 1, fp);
+    fclose(fp);
+}
+
+void robot_loop()
+{    
+    start:
+    while(1) {
+        if(start_button) {
+            pi.stop();
+            goto start;
+        }
+        
+        //
+        // Right now the robot controller is clearly a very basic
+        // 'hello world' loop that must be changed.
+        forward(10);
+        right(90);
+        forward(10);
+        right(90);
+        forward(10);
+        wait(2);
+        left(180);
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/control.h	Sat Nov 15 23:03:06 2014 +0000
@@ -0,0 +1,25 @@
+/**
+ * @file    control.h
+ * @brief   Header file specific to control algorithms.
+ * @author  John Wilkey
+ */
+ 
+#ifndef _CONTROL_H
+#define _CONTROL_H
+
+#include <vector>
+#define _CANVAS_FILE "/local/canvas.ps"
+
+/**
+ * @brief               Get the formatted file contents from local file.
+ * @param[out]  moves   A pointer to the character array of moves
+ */
+void get_ps_file(char* moves);
+ 
+/** 
+ * @brief   A main loop for robot control. Main algorithm for control
+ *          should start here
+ */
+void robot_loop(); 
+
+#endif
\ No newline at end of file
--- a/main.c	Sat Nov 15 05:07:24 2014 +0000
+++ b/main.c	Sat Nov 15 23:03:06 2014 +0000
@@ -8,6 +8,7 @@
  * @author  John Wilkey
  */
 #include "main.h"
+#include "control.h"
 
 /** 
  * These are global data Used externally in all other files 
@@ -16,7 +17,7 @@
 
 //
 // Digital inputs to the mBed
-DigitalIn   btn_b(p21);
+DigitalIn   start_button(p21);
 
 //
 // Digital outputs from the mBed. Note that by default these are
@@ -43,23 +44,27 @@
 {
     //
     // Basic setup information
-    DigitalOut olen_1(LED1);
+    start_button.mode(PullUp);
+    
     pi.locate(0,0);
     pi.printf("PiCO");
     pi.locate(0,1);
     pi.printf("%f mV", pi.battery());
+    wait(.2);
+    while(!start_button) {
+        oled_2 = 1;
+        wait(.2);
+        pi.locate(0,0);
+        pi.printf("Ready");
+        oled_2 = 0;
+    }
+    pi.locate(0,0);
+    pi.printf("GO!");
+    wait(.2);
 
     //
     // Main program loop.
-    while(1) {
-        forward(10);
-        right(90);
-        forward(10);
-        right(90);
-        forward(10);
-        wait(2);
-        left(180);
-    }
+    robot_loop();
     
     //
     // We should never reach this point!
@@ -67,36 +72,56 @@
 
 int forward(int amt)
 {
+    oled_2 = 1;
     pi.locate(0,0);
     pi.printf("Fwd %d", amt);
     pi.forward(DRIVE_SPEED);
     wait(amt*DRIVE_RATE);
+    oled_2 = 0;
     return EXIT_SUCCESS;
 }
 
 int backward(int amt)
 {
+    oled_3 = 1;
     pi.locate(0,0);
     pi.printf("Back %d", amt);
     pi.backward(DRIVE_SPEED);
     wait(amt*DRIVE_RATE);
+    oled_3 = 0;
     return EXIT_SUCCESS;
 }
 
 int right(float deg)
 {
+    oled_4 = 1;
     pi.locate(0,0);
     pi.printf("Right %f", deg);
     pi.right(TURN_SPEED);
     wait(deg/360);
+    oled_4 = 0;
     return EXIT_SUCCESS;
 }
 
 int left(float deg)
 {
+    oled_4 = 1;
+    oled_2 = 1;
     pi.locate(0,0);
     pi.printf("Left %f", deg);
     pi.left(TURN_SPEED);
     wait(deg/360);
+    oled_4 = 0;
+    oled_2 = 0;
     return EXIT_SUCCESS;
+}
+
+void pen_down()
+{
+    oled_1 = 1;
+}
+
+void pen_up()
+{
+    oled_1 = 0;
 }
\ No newline at end of file
--- a/main.h	Sat Nov 15 05:07:24 2014 +0000
+++ b/main.h	Sat Nov 15 23:03:06 2014 +0000
@@ -1,12 +1,12 @@
 /**
- * @file    project.h
+ * @file    main.h
  * @brief   Main header file for includes and whatnot 
  *          for the other project files.
  * @author  John Wilkey
  */
  
-#ifndef _PROJECT_H
-#define _PROJECT_H
+#ifndef _MAIN_H
+#define _MAIN_H
 
 #include "mbed.h"
 #include "m3pi.h"
@@ -54,6 +54,20 @@
 int left (float deg);
 
 /**
+ * @brief Enable pen motion. 
+ *
+ * Note that at the moment, for simplicity, this method just activates an LED
+ * on the expansion board to indicate that the pen should be in DOWN mode. 
+ */
+void pen_down();
+ 
+ /**
+  * @brief Raise pen
+  */
+void pen_up();
+  
+
+/**
  * @brief Controller decision logic.
  *
  * Decide what to do next based on the status of the drawing so far.