Skip to content

Commit e7ece51

Browse files
committed
simple graphical plotter
1 parent 1667cc3 commit e7ece51

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed

contrib/plot_accel.py

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright (C) 2010 Marc Poulhiès
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
#
18+
# Author: Marc Poulhiès
19+
20+
import time
21+
import sys
22+
23+
import pygame
24+
import os
25+
import optparse
26+
from datetime import datetime
27+
import math
28+
29+
30+
import serial
31+
import array
32+
33+
def startAccessPoint():
34+
return array.array('B', [0xFF, 0x07, 0x03]).tostring()
35+
36+
def accDataRequest():
37+
return array.array('B', [0xFF, 0x08, 0x07, 0x00, 0x00, 0x00, 0x00]).tostring()
38+
39+
40+
class Graph:
41+
fontheight = 20
42+
# These four values sould go into the configuration file
43+
winheight = 800 # Window height at startup
44+
width = 700 # Window width at startup
45+
46+
bgcolor = (255,255,255) # Background color for the window
47+
48+
color = [(255,0,0), (0,255,0), (0,0,255)]
49+
50+
txtcolor = (255,0,0)
51+
52+
t = 0
53+
54+
prev_display_debug = None
55+
56+
def __init__(self, size):
57+
pygame.init()
58+
59+
self.previous_points = [0]*size
60+
61+
self.font = pygame.font.Font(None, self.fontheight)
62+
63+
self.screen = pygame.display.set_mode( (self.width, self.winheight), pygame.RESIZABLE )
64+
65+
pygame.display.set_caption( "Plotter" )
66+
self.screen.fill( self.bgcolor )
67+
68+
pygame.display.flip()
69+
70+
def display_debug(self, dtext):
71+
text = self.font.render(dtext, 1, self.txtcolor)
72+
pos = text.get_rect(left=0, top=self.winheight -self.fontheight - 5)
73+
74+
if self.prev_display_debug:
75+
self.screen.fill(self.bgcolor, self.prev_display_debug)
76+
77+
self.screen.blit(text, pos)
78+
79+
if self.prev_display_debug:
80+
pygame.display.update(self.prev_display_debug)
81+
else:
82+
pygame.display.update(pos)
83+
84+
self.prev_display_debug = pos
85+
86+
def plot_values(self, values):
87+
self.t += 1
88+
yoff = 0
89+
90+
each_y = self.winheight/len(values)
91+
92+
norms = [a * (each_y-5)/each_y for a in values]
93+
94+
95+
for i in xrange(len(values)):
96+
pygame.draw.aaline(self.screen,
97+
self.color[i%3],
98+
(self.t-1,
99+
self.previous_points[i]+yoff),
100+
(self.t,
101+
norms[i]+yoff),
102+
1)
103+
yoff += each_y
104+
105+
rect = pygame.Rect(self.t-1, 0, 40, self.winheight)
106+
pygame.display.update(rect)
107+
108+
if self.t >= 700:
109+
self.t = 0
110+
self.screen.fill( self.bgcolor )
111+
112+
self.previous_points = norms
113+
114+
def conv(c):
115+
if c > 127:
116+
return c - 256
117+
else:
118+
return c
119+
120+
def main():
121+
parser = optparse.OptionParser(
122+
usage='Usage: %prog [options]',
123+
description="Plotter !")
124+
125+
parser.add_option("-f", "--freq",
126+
help="Sampling frequency in ms (defaults: 10ms)",
127+
default=10, type="int",
128+
metavar="sampling_freq")
129+
130+
parser.add_option("-v", "--verbose",
131+
help="Be verbose and display lots of garbage",
132+
action="store_true", default=False,
133+
metavar="verbose")
134+
135+
(options, args) = parser.parse_args(sys.argv[1:])
136+
sampling_freq = options.freq
137+
138+
139+
ser = serial.Serial('/dev/ttyACM0',115200,timeout=1)
140+
ser.write(startAccessPoint())
141+
142+
g = None
143+
while True:
144+
ser.write(accDataRequest())
145+
accel = ser.read(7)
146+
if len(accel) < 3:
147+
continue
148+
149+
if ord(accel[0]) != 0 and ord(accel[1]) != 0 and ord(accel[2]) != 0:
150+
l = [conv(ord(accel[0]))+128, conv(ord(accel[1]))+128,conv(ord(accel[2]))+128]
151+
print l
152+
print "***"
153+
else:
154+
continue
155+
if g == None:
156+
g = Graph(len(l))
157+
g.plot_values(l)
158+
pygame.time.wait(sampling_freq)
159+
160+
161+
162+
if __name__ == '__main__':
163+
sys.exit(main())

0 commit comments

Comments
 (0)