INTERFACE AND APPLICATION PROGRAMMING


This week’s assignment was to use a previous week’s circuit board and write and or modify existing code to output some reading from the micro controller.  For the week on input devices I used a Hall Effect sensor. 

 

To start out I looked at the python code that Neil provide for that week and tried to change a few small things first.  The code that was given uses serial reads to get the data.  The framing was setup in the micro controller and uses the FDTI cable to interface with the computer. The initial part of the code I was going to change is the color of the rectangles that represent the value of the data.  The line of code that says canvas.create_ rectangle, at the end of the parameter list there is a fill equals the color.  It currently is in red green blue (RGB) formate but can also use some standard colors like red of green. 

c1

Now that I found out how to change color I wanted to add a square that changed from green to red based on a certain value.  The first step was to figure out how to add another shape to the window.  To create another shape I used the canvas.create_ rectangle and moved the location and size of the shape to make a square.  I initially just tried to put the square on the canvas and make it green.  It took a few tries to get it in the right location and size. 


c2



 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#
# hello.mag.45.py
#
# receive and display magnetic field
# hello.mag.45.py serial_port
#
# Neil Gershenfeld 11/3/13
# (c) Massachusetts Institute of Technology 2013
#
# This work may be reproduced, modified, distributed,
# performed, and displayed for any purpose. Copyright is
# retained and must be preserved. The work is provided
# as is; no warranty is provided, and users accept all
# liability.
#

from Tkinter import *
import serial

WINDOW = 800 # window size
nsamples = 100.0 # number of samples accumulated

def idle(parent,canvas):
global filter, eps
#
# idle routine
#
byte2 = 0
byte3 = 0
byte4 = 0
ser.flush()
while 1:
#
# find framing
#
byte1 = byte2
byte2 = byte3
byte3 = byte4
byte4 = ord(ser.read())
if ((byte1 == 1) & (byte2 == 2) & (byte3 == 3) & (byte4 == 4)):
break
low = ord(ser.read())
med = ord(ser.read())
high = ord(ser.read())
value = (256*256*high + 256*med + low)/nsamples
x = int(.2*WINDOW + (.9-.2)*WINDOW*value/1024.0)
canvas.itemconfigure("text",text="%.1f"%value)
canvas.coords('rect1',.2*WINDOW,.05*WINDOW,x,.2*WINDOW)
canvas.coords('rect2',x,.05*WINDOW,.9*WINDOW,.2*WINDOW)
#check for value on the sensor and if it is less then 270 turn the square red else turn it green
if (value < 270):
canvas.itemconfigure("sq",fill='red')
else:
canvas.itemconfigure("sq",fill='green')
canvas.update()
parent.after_idle(idle,parent,canvas)

#
# check command line arguments
#
if (len(sys.argv) != 2):
print "command line: hello.mag.45.py serial_port"
sys.exit()
port = sys.argv[1]
#
# open serial port
#
ser = serial.Serial(port,9600)
ser.setDTR()
#
# set up GUI
#
root = Tk()
root.title('hello.mag.45.py (q to exit)')
root.bind('q','exit')
canvas = Canvas(root, width=WINDOW, height=.50*WINDOW, background='white')
canvas.create_text(.1*WINDOW,.125*WINDOW,text=".33",font=("Helvetica", 24),tags="text",fill="#0000b0")
canvas.create_rectangle(.2*WINDOW,.05*WINDOW,.3*WINDOW,.2*WINDOW, tags='rect1', fill='#b00000')
canvas.create_rectangle(.3*WINDOW,.05*WINDOW,.9*WINDOW,.2*WINDOW, tags='rect2', fill='#0000b0')

canvas.create_rectangle(.5*WINDOW,.5*WINDOW,.3*WINDOW,.3*WINDOW, tags='sq', fill='green') # create green square

canvas.pack()
#
# start idle loop
#
root.after(100,idle,root,canvas)
root.mainloop()



c3

The next step is what I really wanted to do.  I wanted to read the value form the sensor and once it reached a certain level in this case I used 270 it would change the square to red. 

c4

Creating a square that can change color is great if you would like to monitor your device and have critical level warnings with it. 

Week 16 from chris rohal on Vimeo.


Once i got the python working i tired to use Processing which is very similar to the arduino IDE but is made for easier graphical experience.  I started with the serial example to see how it communicates and then added the framing detector and had it look for the number 4 in the serial data.  Once i got the frame i had it read the next 3 bytes for the actual value from the micro.

The code below is the framing and data part

if (val == 4)
  {
     if ( myPort.available() > 0) {  // If data is available,
      // read it and store it in val
 
    byte1 = myPort.read();
     }
   if ( myPort.available() > 0) {  // If data is available,
      // read it and store it in val
 
    byte2 = myPort.read();
     }
     if ( myPort.available() > 0) {  // If data is available,
      // read it and store it in val
 
    byte3 = myPort.read();
     }
  }
  }


after i received the data i added the weights to the bytes and added them to get the total value and then divided by 100 since that is how many samples the micro took before sending the data.

mag_val = (256 * 256 * byte3 + 256 * byte2 + byte1) / 100; 

the next part of the code is creating the square and if the value is below 280 it turns black other wise it is gray.  It also displays the value from the sensor on the micro.


  background(255);             // Set background to white
  if (mag_val < 280) {              // If the serial value is 0,
    fill(0);                   // set fill to black
  }
  else {                       // If the serial value is not 0,
    fill(204);                 // set fill to light gray
  }
  rect(50, 50, 100, 100);
 
    textAlign(RIGHT);
   
   mag_val= ((256 * 256 * byte3) + (256 * byte2) + byte1)/ 100;
  drawType(width * 0.25);
 
}

void drawType(float x) {
  line(x, 0, x, 65);
  line(x, 220, x, height);
  fill(0);
  text(mag_val, x, 95);
 
}
 


w1




w16 new from chris rohal on Vimeo.



Files

Rohal Mag Python

Rohal Serial Processing File


Back