Final Project files for mBed development.

Dependencies:   m3pi mbed

Files at this revision

API Documentation at this revision

Comitter:
lsaristo
Date:
Sun Nov 16 09:11:34 2014 +0000
Parent:
11:a30f30d3066e
Child:
13:070846d87d4a
Commit message:
Added user-gui python file for drawing surface and file generation. Tried fixing weird wait() issue in direction functions

Changed in this revision

control.c 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
user-gui.py Show annotated file Show diff for this revision Revisions of this file
--- a/control.c	Sun Nov 16 05:26:24 2014 +0000
+++ b/control.c	Sun Nov 16 09:11:34 2014 +0000
@@ -23,6 +23,7 @@
     fseek(fp, 0L, SEEK_SET);
     contents = (char*)malloc(size);
     fread(contents, size, 1, fp);
+    memcpy(moves, contents, size);
     fclose(fp);
 }
 
@@ -48,20 +49,14 @@
     wait(.5);
 
     while(1) {
-        if(start_button) {
+        if(!start_button) {
             pi.stop();
             goto start;
         }
-        
+        forward(10);
+        right(90);    
         //
         // 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);
+        // 'hello world' loop that must be changed.      
     }
 }
\ No newline at end of file
--- a/main.c	Sun Nov 16 05:26:24 2014 +0000
+++ b/main.c	Sun Nov 16 09:11:34 2014 +0000
@@ -14,6 +14,7 @@
  * These are global data Used externally in all other files 
  */
 m3pi        pi;
+Timer       timer;
 
 //
 // Digital inputs to the mBed
@@ -45,58 +46,115 @@
     //
     // Basic setup information
     start_button.mode(PullUp);
+        
+    pi.sensor_auto_calibrate();
+    pi.backward(DRIVE_SPEED);
+    float pos;
+    
+    do {
+        pos = pi.line_position();
+        pi.cls();
+        pi.locate(0,0);
+        pi.printf("P: %f", pos);
+    } while(pos != -1 && pos != 1);
+    if(pos == 1) {
+        timer.start();
+        pi.forward(DRIVE_SPEED);
+    } else {
+        pi.stop();
+        pi.cls();
+        pi.locate(0,0);
+        pi.printf("LP: %f",pos);
+        while(1);
+    }
 
+    while(pi.line_position() == 1);
+    do {
+        pos = pi.line_position();
+        pi.cls();
+        pi.locate(0,0);
+        pi.printf("Pos: %f", pos);
+    } while(pos != -1 && pos != 1);
+    if(pos == 1) {
+        oled_1 = 1;
+        timer.stop();
+        pi.stop();           
+    } else {
+        pi.stop();
+        pi.cls();
+        pi.locate(0,0);
+        pi.printf("LP:%f", pos);
+        while(1);
+    }
+    
     //
     // Main program loop.
-    robot_loop();
+    // robot_loop();
     
     //
     // We should never reach this point!
+    while(1);
 }
 
 int forward(int amt)
 {
+    Timer t;
+    t.start();
     oled_2 = 1;
     pi.locate(0,0);
     pi.printf("Fwd %d", amt);
     pi.forward(DRIVE_SPEED);
-    wait(amt*DRIVE_RATE);
+    while(t.read_ms() < amt*DRIVE_RATE*1000);
+    t.stop();
     oled_2 = 0;
+    pi.stop();
     return EXIT_SUCCESS;
 }
 
 int backward(int amt)
 {
+    Timer t;
     oled_3 = 1;
     pi.locate(0,0);
     pi.printf("Back %d", amt);
     pi.backward(DRIVE_SPEED);
-    wait(amt*DRIVE_RATE);
+    t.start();
+    while(t.read_ms() < amt*DRIVE_RATE*1000);
+    t.stop();
     oled_3 = 0;
+    pi.stop();
     return EXIT_SUCCESS;
 }
 
 int right(float deg)
 {
+    Timer t;
     oled_4 = 1;
     pi.locate(0,0);
     pi.printf("Right %f", deg);
     pi.right(TURN_SPEED);
-    wait(deg/360);
+    t.start();
+    while(t.read_ms() < (deg/360)*1000);
+    t.stop();
     oled_4 = 0;
+    pi.stop();
     return EXIT_SUCCESS;
 }
 
 int left(float deg)
 {
+    Timer t;
     oled_4 = 1;
     oled_2 = 1;
     pi.locate(0,0);
     pi.printf("Left %f", deg);
     pi.left(TURN_SPEED);
-    wait(deg/360);
+    t.start();
+    while(t.read_ms() < (deg/360)*1000);
+    t.stop();
     oled_4 = 0;
     oled_2 = 0;
+    pi.stop();
     return EXIT_SUCCESS;
 }
 
--- a/main.h	Sun Nov 16 05:26:24 2014 +0000
+++ b/main.h	Sun Nov 16 09:11:34 2014 +0000
@@ -14,7 +14,7 @@
 #include <stdarg.h>
 #include <stdio.h>
 #define TURN_SPEED  0.25
-#define DRIVE_SPEED 0.25
+#define DRIVE_SPEED 0.15
 #define ERR_SUCCESS 0
 #define ERR_FAILURE 1
 #define DRIVE_RATE  1/50
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/user-gui.py	Sun Nov 16 09:11:34 2014 +0000
@@ -0,0 +1,170 @@
+#!/usr/bin/python
+""""
+    user-gui.py
+
+    This program displays a drawing surface for users to draw images
+    for use with our robot. The program ouputs a control-compatible 
+    representation of the drawing. 
+
+    NOTE: A substantial portion of this code was taken from Dave Michell's
+    example illustrating how to draw on a Python-generated canvas. All credit
+    for those portions of the code belong to him. Below is the header from the
+    original document written by him.
+        
+    # Start origin header ########################
+    
+    Paint program by Dave Michell.
+    
+    Subject: tkinter "paint" example
+    From: Dave Mitchell <davem@magnet.com>
+    To: python-list@cwi.nl
+    Date: Fri, 23 Jan 1998 12:18:05 -0500 (EST)
+    
+      Not too long ago (last week maybe?) someone posted a request
+    for an example of a paint program using Tkinter. Try as I might
+    I can't seem to find it in the archive, so i'll just post mine
+    here and hope that the person who requested it sees this!
+    
+      All this does is put up a canvas and draw a smooth black line
+    whenever you have the mouse button down, but hopefully it will
+    be enough to start with.. It would be easy enough to add some
+    options like other shapes or colors...
+
+                                                yours,
+                                                dave mitchell
+                                                davem@magnet.com
+    
+    # End original header #########################
+    
+"""
+from Tkinter import *
+import os
+import sys
+
+TEMP_FILE = "canvas-temp.ps"
+OUTPUT_FILE = "control.out"
+b1 = "up"
+xold, yold = None, None
+height = 600
+width = 600
+
+class popupWindow(object):
+    """ Class for window resize popup """
+    def __init__(self,master):
+        top=self.top=Toplevel(master)
+        self.l=Label(top,text="Resize Window")
+        self.l.pack()
+        self.h=Label(top,text="Height");
+        self.h.pack()
+        self.e=Entry(top)
+        self.e.pack()
+        self.w=Label(top,text="Width");
+        self.w.pack()
+        self.f=Entry(top);
+        self.f.pack()
+        self.b=Button(top,text='Ok',command=self.cleanup)
+        self.b.pack()
+        self.master = master
+    def cleanup(self):
+        global height
+        global width
+        h = self.e.get()
+        w = self.f.get()
+        self.top.destroy()
+        self.master.geometry(h+"x"+w)
+        height = h
+        width = w
+
+def loop():
+    root = Tk()
+    root.geometry("%dx%d" % (height,width))
+    root.title("RoboCanvas")
+    drawing_area = Canvas(root)
+    drawing_area.pack(fill=BOTH,expand=1)
+    drawing_area.bind("<Motion>", motion)
+    drawing_area.bind("<ButtonPress-1>", b1down)
+    drawing_area.bind("<ButtonRelease-1>", b1up)
+
+    menubar = Menu(root)
+    control_menu = Menu(root);
+    control_menu.add_command(label="Deploy       ", \
+        command=lambda p=drawing_area: build(p));
+    control_menu.add_command(label="Clear Canvas",\
+        command=lambda p=drawing_area: p.delete('all'))
+    control_menu.add_command(label="Resize Canvas", command=lambda p=root:\
+        resize(root));
+    control_menu.add_separator()
+    control_menu.add_command(label="Exit", command=lambda p=root: root.quit())
+    menubar.add_cascade(label="Control Menu", menu=control_menu);
+    root.config(menu=menubar);
+    message = Label(root, text="Press and hold with the mouse to draw. Deploy to generate output file.");
+    message.pack(side=BOTTOM)
+    root.mainloop()
+
+def resize(root):
+    """ Resize the root window and expand the canvas to fill """
+    ans = popupWindow(root);
+
+def build(canvas):
+    """
+    This function is indended to be called from the drawing canvas after
+    the drawing is complete and the user wishes to push it to the robot.
+    This function must send (somehow) the appropriate information to the 
+    robot to make the drawing happen.
+    """
+    gen_postscript(canvas)
+
+def gen_postscript(canvas):
+    """ Make a postscript capture of the canvas """
+    
+    # Get lineto and moveto data from standard postscript output. 
+    l = []
+    ll = []
+    canvas.update()
+    canvas.postscript(file=TEMP_FILE);
+    f = open(TEMP_FILE, "r")
+    for line in f.readlines():
+        if 'lineto' in line or 'moveto' in line:
+            l.append(line)
+    f.close()
+
+    # Filter output.
+    ll.append(l[0])
+    for i, elem in enumerate(list(l)):
+        x = elem.split(' ')
+        ll.append(elem)
+
+    # Write filtered output to the new canvas file.
+    l = l[5:]
+    f = open(OUTPUT_FILE, "w");
+    f.write(str(height) + "/" + str(width)+"\n");
+    for line in l:
+        f.write(line);
+    f.close()
+    os.remove(TEMP_FILE);
+
+def b1down(event):
+    global b1
+    b1 = "down"           # you only want to draw when the button is down
+                          # because "Motion" events happen -all the time-
+
+def b1up(event):
+    global b1, xold, yold
+    b1 = "up"
+    xold = None           # reset the line when you let go of the button
+    yold = None
+
+def motion(event):
+    if b1 == "down":
+        global xold, yold
+        if xold is not None and yold is not None:
+            event.widget.create_line(xold,yold,event.x,event.y,smooth=TRUE)
+        xold = event.x
+        yold = event.y
+
+def main():
+    """ Main program entry point """
+    loop()
+
+if __name__ == "__main__":
+    main()