Kaynağa Gözat

lsm303: python example and js modification for lsm303 accelerometer/compass

Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
Zion Orent 9 yıl önce
ebeveyn
işleme
94fe1174c6

+ 17
- 6
examples/javascript/lsm303.js Dosyayı Görüntüle

@@ -1,6 +1,5 @@
1 1
 /*jslint node:true, vars:true, bitwise:true, unparam:true */
2 2
 /*jshint unused:true */
3
-/*global */
4 3
 /*
5 4
 * Author: Zion Orent <zorent@ics.com>
6 5
 * Copyright (c) 2014 Intel Corporation.
@@ -30,16 +29,17 @@ var accelrCompassSensor = require('jsupm_lsm303');
30 29
 // Instantiate LSM303 compass on I2C
31 30
 var myAccelrCompass = new accelrCompassSensor.LSM303(0);
32 31
 
33
-setInterval(function()
32
+var successFail, coords, outputStr, accel;
33
+var myInterval = setInterval(function()
34 34
 {
35 35
 	// Load coordinates into LSM303 object
36
-	var successFail = myAccelrCompass.getCoordinates();
36
+	successFail = myAccelrCompass.getCoordinates();
37 37
 	// in XYZ order. The sensor returns XZY,
38 38
 	// but the driver compensates and makes it XYZ
39
-	var coords = myAccelrCompass.getRawCoorData();
39
+	coords = myAccelrCompass.getRawCoorData();
40 40
 
41 41
     // Print out the X, Y, and Z coordinate data using two different methods
42
-	var outputStr = "coor: rX " + coords.getitem(0)
42
+	outputStr = "coor: rX " + coords.getitem(0)
43 43
 					+ " - rY " + coords.getitem(1)
44 44
 					+ " - rZ " + coords.getitem(2);
45 45
 	console.log(outputStr);
@@ -53,7 +53,7 @@ setInterval(function()
53 53
 
54 54
     // Get the acceleration
55 55
 	myAccelrCompass.getAcceleration();
56
-	var accel = myAccelrCompass.getRawAccelData();
56
+	accel = myAccelrCompass.getRawAccelData();
57 57
     // Print out the X, Y, and Z acceleration data using two different methods
58 58
 	outputStr = "acc: rX " + accel.getitem(0)
59 59
 				+ " - rY " + accel.getitem(1)
@@ -65,3 +65,14 @@ setInterval(function()
65 65
 	console.log(outputStr);
66 66
 	console.log(" ");
67 67
 }, 1000);
68
+
69
+// Print message when exiting
70
+process.on('SIGINT', function()
71
+{
72
+	clearInterval(myInterval);
73
+	myAccelrCompass = null;
74
+	accelrCompassSensor.cleanUp();
75
+	accelrCompassSensor = null;
76
+	console.log("Exiting");
77
+	process.exit(0);
78
+});

+ 62
- 4
examples/python/lsm303.py Dosyayı Görüntüle

@@ -1,6 +1,7 @@
1
-#!/usr/bin/env python
1
+#!/usr/bin/python
2 2
 
3 3
 # Author: Brendan Le Foll <brendan.le.foll@intel.com>
4
+# Contributions: Zion Orent <zorent@ics.com>
4 5
 # Copyright (c) 2014 Intel Corporation.
5 6
 #
6 7
 # Permission is hereby granted, free of charge, to any person obtaining
@@ -22,8 +23,65 @@
22 23
 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 24
 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
24 25
 
26
+import time, sys, signal, atexit
25 27
 import pyupm_lsm303 as lsm303
26 28
 
27
-x = lsm303.LSM303(0)
28
-x.getCoordinates()
29
-x.getHeading()
29
+# Instantiate LSM303 compass on I2C
30
+myAccelrCompass = lsm303.LSM303(0)
31
+
32
+
33
+## Exit handlers ##
34
+# This stops python from printing a stacktrace when you hit control-C
35
+def SIGINTHandler(signum, frame):
36
+	raise SystemExit
37
+
38
+# This lets you run code on exit,
39
+# including functions from myAccelrCompass
40
+def exitHandler():
41
+	print "Exiting"
42
+	sys.exit(0)
43
+
44
+# Register exit handlers
45
+atexit.register(exitHandler)
46
+signal.signal(signal.SIGINT, SIGINTHandler)
47
+
48
+
49
+while(1):
50
+	# Load coordinates into LSM303 object
51
+	successFail = myAccelrCompass.getCoordinates()
52
+	# in XYZ order. The sensor returns XZY,
53
+	# but the driver compensates and makes it XYZ
54
+	coords = myAccelrCompass.getRawCoorData()
55
+
56
+	# Print out the X, Y, and Z coordinate data
57
+	# using two different methods
58
+	outputStr = "coor: rX {0} - rY {1} - rZ {2}".format(
59
+	coords.__getitem__(0), coords.__getitem__(1),
60
+	coords.__getitem__(2))
61
+	print outputStr
62
+
63
+	outputStr = "coor: gX {0} - gY {1} - gZ {2}".format(
64
+	myAccelrCompass.getCoorX(), myAccelrCompass.getCoorY(),
65
+	myAccelrCompass.getCoorZ())
66
+	print outputStr
67
+
68
+	# Get and print out the heading
69
+	print "heading:", myAccelrCompass.getHeading()
70
+
71
+	# Get the acceleration
72
+	myAccelrCompass.getAcceleration();
73
+	accel = myAccelrCompass.getRawAccelData();
74
+
75
+	# Print out the X, Y, and Z acceleration data
76
+	# using two different methods
77
+	outputStr = "acc: rX {0} - rY {1} - Z {2}".format(
78
+	accel.__getitem__(0), accel.__getitem__(1), accel.__getitem__(2))
79
+	print outputStr
80
+
81
+	outputStr = "acc: gX {0} - gY {1} - gZ {2}".format(
82
+	myAccelrCompass.getAccelX(), myAccelrCompass.getAccelY(),
83
+	myAccelrCompass.getAccelZ())
84
+	print outputStr
85
+
86
+	print " "
87
+	time.sleep(1)

+ 7
- 0
src/lsm303/pyupm_lsm303.i Dosyayı Görüntüle

@@ -1,8 +1,15 @@
1 1
 %module pyupm_lsm303
2 2
 %include "../upm.i"
3
+%include "../carrays_int16_t.i"
3 4
 
4 5
 %feature("autodoc", "3");
5 6
 
7
+// Adding this typemap because SWIG is converting int16 into a short by default
8
+// This forces SWIG to convert it correctly
9
+%typemap(out) int16_t* {
10
+	resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_int16Array, 0 |  0 );
11
+}
12
+
6 13
 %include "lsm303.h"
7 14
 %{
8 15
     #include "lsm303.h"