Browse Source

adafruitms1438: python examples for adafruitms1438 motor shield

Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
Zion Orent 9 years ago
parent
commit
4f53c9c43a
2 changed files with 171 additions and 0 deletions
  1. 88
    0
      examples/python/adafruitms1438-stepper.py
  2. 83
    0
      examples/python/adafruitms1438.py

+ 88
- 0
examples/python/adafruitms1438-stepper.py View File

@@ -0,0 +1,88 @@
1
+#!/usr/bin/python
2
+# Author: Zion Orent <zorent@ics.com>
3
+# Copyright (c) 2015 Intel Corporation.
4
+#
5
+# Permission is hereby granted, free of charge, to any person obtaining
6
+# a copy of this software and associated documentation files (the
7
+# "Software"), to deal in the Software without restriction, including
8
+# without limitation the rights to use, copy, modify, merge, publish,
9
+# distribute, sublicense, and/or sell copies of the Software, and to
10
+# permit persons to whom the Software is furnished to do so, subject to
11
+# the following conditions:
12
+#
13
+# The above copyright notice and this permission notice shall be
14
+# included in all copies or substantial portions of the Software.
15
+#
16
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+import time, sys, signal, atexit
25
+import pyupm_adafruitms1438 as upmAdafruitms1438
26
+
27
+
28
+# Import header values
29
+I2CBus = upmAdafruitms1438.ADAFRUITMS1438_I2C_BUS
30
+I2CAddr = upmAdafruitms1438.ADAFRUITMS1438_DEFAULT_I2C_ADDR
31
+
32
+M12Motor = upmAdafruitms1438.AdafruitMS1438.STEPMOTOR_M12
33
+MotorDirCW = upmAdafruitms1438.AdafruitMS1438.DIR_CW
34
+MotorDirCCW = upmAdafruitms1438.AdafruitMS1438.DIR_CCW
35
+
36
+
37
+# Instantiate an Adafruit MS 1438 on I2C bus 0
38
+myMotorShield = upmAdafruitms1438.AdafruitMS1438(I2CBus, I2CAddr)
39
+
40
+
41
+## Exit handlers ##
42
+# This stops python from printing a stacktrace when you hit control-C
43
+def SIGINTHandler(signum, frame):
44
+	raise SystemExit
45
+
46
+# This function lets you run code on exit,
47
+# including functions from myMotorShield
48
+def exitHandler():
49
+	myMotorShield.disableStepper(M12Motor)
50
+	print "Exiting"
51
+	sys.exit(0)
52
+
53
+# Register exit handlers
54
+atexit.register(exitHandler)
55
+signal.signal(signal.SIGINT, SIGINTHandler)
56
+
57
+
58
+# Setup for use with a stepper motor connected to the M1 & M2 ports
59
+
60
+# set a PWM period of 50Hz
61
+
62
+# disable first, to be safe
63
+myMotorShield.disableStepper(M12Motor)
64
+
65
+# configure for a NEMA-17, 200 steps per revolution
66
+myMotorShield.stepConfig(M12Motor, 200)
67
+
68
+# set speed at 10 RPM's
69
+myMotorShield.setStepperSpeed(M12Motor, 10);
70
+myMotorShield.setStepperDirection(M12Motor, MotorDirCW)
71
+
72
+# enable
73
+print "Enabling..."
74
+myMotorShield.enableStepper(M12Motor)
75
+
76
+print "Rotating 1 full revolution at 10 RPM speed."
77
+myMotorShield.stepperSteps(M12Motor, 200)
78
+
79
+print "Sleeping for 2 seconds..."
80
+time.sleep(2)
81
+print "Rotating 1/2 revolution in opposite direction at 10 RPM speed." 
82
+
83
+myMotorShield.setStepperDirection(M12Motor, MotorDirCCW)
84
+myMotorShield.stepperSteps(M12Motor, 100)
85
+
86
+print "Disabling..."
87
+
88
+# exitHandler runs automatically

+ 83
- 0
examples/python/adafruitms1438.py View File

@@ -0,0 +1,83 @@
1
+#!/usr/bin/python
2
+# Author: Zion Orent <zorent@ics.com>
3
+# Copyright (c) 2015 Intel Corporation.
4
+#
5
+# Permission is hereby granted, free of charge, to any person obtaining
6
+# a copy of this software and associated documentation files (the
7
+# "Software"), to deal in the Software without restriction, including
8
+# without limitation the rights to use, copy, modify, merge, publish,
9
+# distribute, sublicense, and/or sell copies of the Software, and to
10
+# permit persons to whom the Software is furnished to do so, subject to
11
+# the following conditions:
12
+#
13
+# The above copyright notice and this permission notice shall be
14
+# included in all copies or substantial portions of the Software.
15
+#
16
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+import time, sys, signal, atexit
25
+import pyupm_adafruitms1438 as upmAdafruitms1438
26
+
27
+
28
+# Import header values
29
+I2CBus = upmAdafruitms1438.ADAFRUITMS1438_I2C_BUS
30
+I2CAddr = upmAdafruitms1438.ADAFRUITMS1438_DEFAULT_I2C_ADDR
31
+
32
+M3Motor = upmAdafruitms1438.AdafruitMS1438.MOTOR_M3
33
+MotorDirCW = upmAdafruitms1438.AdafruitMS1438.DIR_CW
34
+MotorDirCCW = upmAdafruitms1438.AdafruitMS1438.DIR_CCW
35
+
36
+
37
+# Instantiate an Adafruit MS 1438 on I2C bus 0
38
+myMotorShield = upmAdafruitms1438.AdafruitMS1438(I2CBus, I2CAddr)
39
+
40
+
41
+## Exit handlers ##
42
+# This stops python from printing a stacktrace when you hit control-C
43
+def SIGINTHandler(signum, frame):
44
+	raise SystemExit
45
+
46
+# This function lets you run code on exit,
47
+# including functions from myMotorShield
48
+def exitHandler():
49
+	myMotorShield.disableMotor(M3Motor)
50
+	print "Exiting"
51
+	sys.exit(0)
52
+
53
+# Register exit handlers
54
+atexit.register(exitHandler)
55
+signal.signal(signal.SIGINT, SIGINTHandler)
56
+
57
+
58
+# Setup for use with a DC motor connected to the M3 port
59
+
60
+# set a PWM period of 50Hz
61
+myMotorShield.setPWMPeriod(50)
62
+
63
+# disable first, to be safe
64
+myMotorShield.disableMotor(M3Motor)
65
+
66
+# set speed at 50%
67
+myMotorShield.setMotorSpeed(M3Motor, 50)
68
+myMotorShield.setMotorDirection(M3Motor, MotorDirCW)
69
+
70
+print ("Spin M3 at half speed for 3 seconds, "
71
+"then reverse for 3 seconds.")
72
+myMotorShield.enableMotor(M3Motor)
73
+
74
+time.sleep(3)
75
+
76
+print "Reversing M3"
77
+myMotorShield.setMotorDirection(M3Motor, MotorDirCCW)
78
+
79
+time.sleep(3)
80
+
81
+print "Stopping M3"
82
+
83
+# exitHandler runs automatically