Final Project files for mBed development.

Dependencies:   m3pi mbed

Files at this revision

API Documentation at this revision

Comitter:
lsaristo
Date:
Fri Nov 28 21:08:55 2014 +0000
Parent:
13:070846d87d4a
Child:
16:f1beef7beeb9
Commit message:
Updated user-gui.py to better filter input data.

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
user-gui.py Show annotated file Show diff for this revision Revisions of this file
--- a/control.c	Sun Nov 16 09:17:20 2014 +0000
+++ b/control.c	Fri Nov 28 21:08:55 2014 +0000
@@ -16,14 +16,12 @@
 
 void get_ps_file(char* moves)
 {
-    char* contents;
+    LocalFileSystem local("local");
     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);
-    memcpy(moves, contents, size);
+    fread(moves, size, 1, fp);
     fclose(fp);
 }
 
@@ -55,6 +53,7 @@
         }
         forward(10);
         right(90);    
+        
         //
         // Right now the robot controller is clearly a very basic
         // 'hello world' loop that must be changed.      
--- a/control.h	Sun Nov 16 09:17:20 2014 +0000
+++ b/control.h	Fri Nov 28 21:08:55 2014 +0000
@@ -8,7 +8,7 @@
 #define _CONTROL_H
 
 #include <vector>
-#define _CANVAS_FILE "/local/canvas.ps"
+#define _CANVAS_FILE "/local/control.out"     // Should be on local file system. 
 
 /**
  * @brief               Get the formatted file contents from local file.
--- a/main.c	Sun Nov 16 09:17:20 2014 +0000
+++ b/main.c	Fri Nov 28 21:08:55 2014 +0000
@@ -108,6 +108,7 @@
         pi.printf("LP:%f", pos);
         while(1);
     }
+    // If we got here, calibration is complete.
     
     //
     // Main program loop.
--- a/user-gui.py	Sun Nov 16 09:17:20 2014 +0000
+++ b/user-gui.py	Fri Nov 28 21:08:55 2014 +0000
@@ -1,45 +1,41 @@
 #!/usr/bin/python
-""""
-    user-gui.py
+""""Paint program by Dave Michell.
 
-    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 ########################
+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.
     
-    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...
+# Start origin header ########################
+
+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 #########################
+# End original header #########################
     
 """
 from Tkinter import *
 import os
 import sys
+import re
+
+"""paint.py: not exactly a paint program. just a smooth line drawing demo."""
 
 TEMP_FILE = "canvas-temp.ps"
 OUTPUT_FILE = "control.out"
@@ -49,7 +45,6 @@
 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")
@@ -95,9 +90,9 @@
         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);
+    menubar.add_cascade(label="Control", 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 = Label(root, text="Press and hold with the mouse to draw");
     message.pack(side=BOTTOM)
     root.mainloop()
 
@@ -119,7 +114,6 @@
     
     # Get lineto and moveto data from standard postscript output. 
     l = []
-    ll = []
     canvas.update()
     canvas.postscript(file=TEMP_FILE);
     f = open(TEMP_FILE, "r")
@@ -128,21 +122,110 @@
             l.append(line)
     f.close()
 
-    # Filter output.
-    ll.append(l[0])
-    for i, elem in enumerate(list(l)):
-        x = elem.split(' ')
-        ll.append(elem)
+    # Filter the moves list
+    output = filter_moves(l)
 
     # 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:
+    for line in output:
         f.write(line);
     f.close()
     os.remove(TEMP_FILE);
 
+def filter_moves(moves):
+    """ Return a processed list of moves """
+
+    filtered_moves = list(moves)[5:]
+    output_list = []
+
+    prev = filtered_moves[0]
+    output_list.append(prev)    # Write the first moveto always.
+    prev = filtered_moves[1]
+    output_list.append(prev)
+
+    for move in filtered_moves[2:]:
+        x_coord = move.split()[0]
+        x_coord_prev = prev.split()[0]
+        y_coord = move.split()[1]
+        y_coord_prev = prev.split()[1]
+
+        # Remove redundant moveto's
+        if x_coord != x_coord_prev or y_coord != y_coord_prev:
+            output_list.append(move)
+            prev = move
+            
+    # Flatten on x-coordinate
+    filtered_moves = list(output_list)
+    output_list = [filtered_moves.pop(0)]
+    prev = filtered_moves.pop(0)
+    prev_s = prev.split()
+    prev_y = prev_s[1]
+    prev_x = prev_s[0]
+    while filtered_moves:
+        move = filtered_moves.pop(0)
+        move_s = move.split()
+        move_x = move_s[0]
+        move_y = move_s[1]
+        move_c = move_s[2]
+        
+        if move_c == 'moveto':
+            output_list.append(prev)
+            prev = None
+            output_list.append(move)
+            continue
+
+        if prev and move_x == prev_x:
+            prev_y = move_y
+            continue
+
+        if prev:
+            output_list.append(prev)
+        prev = move
+        prev_s = prev.split()
+        prev_y = prev_s[1]
+        prev_x = prev_s[0]
+    if prev: output_list.append(prev)
+
+    # Flatten on y-coordinate   
+    filtered_moves = list(output_list)
+    output_list = [filtered_moves.pop(0)]
+    prev = filtered_moves.pop(0)
+    prev_s = prev.split()
+    prev_y = prev_s[1]
+    prev_x = prev_s[0]
+    while filtered_moves:
+        move = filtered_moves.pop(0)
+        move_s = move.split()
+        move_x = move_s[0]
+        move_y = move_s[1]
+        move_c = move_s[2]
+        
+        if move_c == 'moveto':
+            output_list.append(prev)
+            prev = None
+            output_list.append(move)
+            continue
+        
+        if prev and move_y == prev_y:
+            prev_x = move_x
+            continue
+        if prev:
+            output_list.append(prev)
+        prev = move
+        prev_s = prev.split()
+        prev_y = prev_s[1]
+        prev_x = prev_s[0]
+    if prev: output_list.append(prev)
+
+    for move in output_list:
+        x_coord = move.split()[0]
+        y_coord = move.split()[1]
+        cmd = move.split()[2]
+
+    return output_list
+
 def b1down(event):
     global b1
     b1 = "down"           # you only want to draw when the button is down