| 1 | #!/usr/bin/python |
|---|
| 2 | """ |
|---|
| 3 | Test a lis3lv02d or similar accelerometer to see if it is |
|---|
| 4 | installed and returning data |
|---|
| 5 | |
|---|
| 6 | Copyright (c) 2011 Saadia Baloch <saadia@laptop.org> |
|---|
| 7 | Copyright (c) 2011 Martin Langhoff <martin@laptop.org> |
|---|
| 8 | Copyright (c) 2011 One Laptop per Child Association |
|---|
| 9 | |
|---|
| 10 | This program is free software; you can redistribute it and/or modify it |
|---|
| 11 | under the terms of the GNU General Public License as published by the |
|---|
| 12 | Free Software Foundation; either version 2, or (at your option) any |
|---|
| 13 | later version. |
|---|
| 14 | |
|---|
| 15 | This program is distributed in the hope that it will be useful, |
|---|
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 18 | GNU General Public License for more details. |
|---|
| 19 | |
|---|
| 20 | You should have received a copy of the GNU General Public License |
|---|
| 21 | along with this program; if not, write to the Free Software |
|---|
| 22 | Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 23 | |
|---|
| 24 | """ |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | ACCURACY = 18 |
|---|
| 28 | WIDTH = 1200 |
|---|
| 29 | HEIGHT = 800 |
|---|
| 30 | VARIATION = 7 |
|---|
| 31 | """ The expectation is that values will vary, |
|---|
| 32 | but be in a range of 7 points each way. |
|---|
| 33 | """ |
|---|
| 34 | TREMBLE = ACCURACY * 2 * VARIATION |
|---|
| 35 | |
|---|
| 36 | import sys |
|---|
| 37 | import re |
|---|
| 38 | from optparse import OptionParser |
|---|
| 39 | |
|---|
| 40 | posextract = re.compile("\(([-+]?\d+),([-+]?\d+),([-+]?\d+)\)") |
|---|
| 41 | def accelread(posfile): |
|---|
| 42 | try: |
|---|
| 43 | f = open(posfile, 'r') |
|---|
| 44 | pos = f.read() |
|---|
| 45 | f.close() |
|---|
| 46 | except: |
|---|
| 47 | print >> sys.stderr, "Cannot open or read position file %s . Perhaps the accelerometer driver is not installed" % posfile |
|---|
| 48 | sys.exit(1) |
|---|
| 49 | |
|---|
| 50 | m = posextract.match(pos) |
|---|
| 51 | if m: |
|---|
| 52 | px = int(m.group(1)) |
|---|
| 53 | py = int(m.group(2)) |
|---|
| 54 | pz = int(m.group(3)) |
|---|
| 55 | else: |
|---|
| 56 | print >> sys.stderr, "Accelerometer position file unreadable" |
|---|
| 57 | sys.exit(1) |
|---|
| 58 | |
|---|
| 59 | return (px, py, pz) |
|---|
| 60 | |
|---|
| 61 | def acceltest(rep, op): |
|---|
| 62 | print >> sys.stderr, "Testing accelerometer %d times. # means movement, _ means stationary\n" % rep |
|---|
| 63 | |
|---|
| 64 | for h in range(0,rep): |
|---|
| 65 | minx = miny = minz = maxx = maxy = maxz = None |
|---|
| 66 | |
|---|
| 67 | for i in range(0, 100): |
|---|
| 68 | (px, py, pz) = accelread(op.posfile) |
|---|
| 69 | |
|---|
| 70 | if (minx == None or px < minx): |
|---|
| 71 | minx = px |
|---|
| 72 | if (miny == None or py < miny): |
|---|
| 73 | miny = py |
|---|
| 74 | if (minz == None or pz < minz): |
|---|
| 75 | minz = pz |
|---|
| 76 | |
|---|
| 77 | if (maxx == None or px > maxx): |
|---|
| 78 | maxx = px |
|---|
| 79 | if (maxy == None or py > maxy): |
|---|
| 80 | maxy = py |
|---|
| 81 | if (maxz == None or pz > maxz): |
|---|
| 82 | maxz = pz |
|---|
| 83 | |
|---|
| 84 | px = maxx-minx |
|---|
| 85 | py = maxy-miny |
|---|
| 86 | pz = maxz-minz |
|---|
| 87 | |
|---|
| 88 | if px == 0 and py == 0 and pz == 0: |
|---|
| 89 | print >> sys.stderr, "\nAccelerometer failed @ %d" % h |
|---|
| 90 | #print "+" |
|---|
| 91 | #print >> sys.stderr, '=' |
|---|
| 92 | |
|---|
| 93 | if px > TREMBLE or py > TREMBLE or pz > TREMBLE: |
|---|
| 94 | sys.stderr.write('#') |
|---|
| 95 | else: |
|---|
| 96 | sys.stderr.write('_') |
|---|
| 97 | sys.stderr.flush() |
|---|
| 98 | |
|---|
| 99 | # print sys.stderr, "X range is %d\nY range is %d\nZ range is %d" % ( px, py, pz) |
|---|
| 100 | |
|---|
| 101 | print >> sys.stderr, "\nAccelerometer test succeeded" |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | op = OptionParser(usage="%prog [options] repetitions") |
|---|
| 106 | op.add_option('--positionfile', dest='posfile', |
|---|
| 107 | default='/sys/devices/platform/lis3lv02d/position', |
|---|
| 108 | help='Override default position file') |
|---|
| 109 | (options, args) = op.parse_args() |
|---|
| 110 | rep = 100 |
|---|
| 111 | if len(args) == 1: |
|---|
| 112 | rep = int(args[0]) |
|---|
| 113 | |
|---|
| 114 | acceltest(rep, options) |
|---|