Skip to content

Commit 5cc780d

Browse files
Add examples
1 parent 7242814 commit 5cc780d

File tree

5 files changed

+188
-0
lines changed

5 files changed

+188
-0
lines changed

docs/ex1.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Example 1: Single Motor Test
2+
---------------------------
3+
.. literalinclude:: ../examples/ex1_qwiic_scmd.py
4+
:caption: examples/ex1_qwiic_scmd.py
5+
:linenos:

docs/ex2.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Example 2: Dual Motor Test
2+
---------------------------
3+
.. literalinclude:: ../examples/ex2_qwiic_scmd.py
4+
:caption: examples/ex2_qwiic_scmd.py
5+
:linenos:

docs/index.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ Table of Contents
2121

2222
apiref
2323

24+
.. toctree::
25+
:caption: Examples
26+
27+
ex1
28+
ex2
29+
2430
.. toctree::
2531
:caption: Other Links
2632

examples/ex1_qwiic_scmd.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env python
2+
#-----------------------------------------------------------------------------
3+
# A simple test to speed up and slow down 1 motor.
4+
#------------------------------------------------------------------------
5+
#
6+
# Written by Mark Lindemer
7+
# SparkFun Electronics, April 2020
8+
#
9+
# This python library supports the SparkFun Electroncis qwiic
10+
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
11+
# board computers.
12+
#
13+
# More information on qwiic is at https://www.sparkfun.com/qwiic
14+
#
15+
# Do you like this library? Help support SparkFun. Buy a board!
16+
#
17+
#==================================================================================
18+
# Copyright (c) 2019 SparkFun Electronics
19+
#
20+
# Permission is hereby granted, free of charge, to any person obtaining a copy
21+
# of this software and associated documentation files (the "Software"), to deal
22+
# in the Software without restriction, including without limitation the rights
23+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
24+
# copies of the Software, and to permit persons to whom the Software is
25+
# furnished to do so, subject to the following conditions:
26+
#
27+
# The above copyright notice and this permission notice shall be included in all
28+
# copies or substantial portions of the Software.
29+
#
30+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
# SOFTWARE.
37+
#==================================================================================
38+
# Example 1
39+
#
40+
41+
from __future__ import print_function
42+
import time
43+
import sys
44+
import math
45+
import qwiic_scmd
46+
47+
myMotor = qwiic_scmd.QwiicScmd()
48+
49+
def runExample():
50+
print("Motor Test.")
51+
R_MTR = 0
52+
L_MTR = 1
53+
FWD = 0
54+
BWD = 1
55+
56+
if myMotor.connected == False:
57+
print("Motor Driver not connected. Check connections.", \
58+
file=sys.stderr)
59+
return
60+
myMotor.begin()
61+
print("Motor initialized.")
62+
time.sleep(.250)
63+
myMotor.enable()
64+
print("Motor enabled")
65+
time.sleep(.250)
66+
67+
68+
while True:
69+
speed = 20
70+
for speed in range(20,255):
71+
print(speed)
72+
myMotor.set_drive(R_MTR,FWD,speed)
73+
time.sleep(.05)
74+
for speed in range(255,20, -1):
75+
print(speed)
76+
myMotor.set_drive(R_MTR,FWD,speed)
77+
time.sleep(.05)
78+
79+
if __name__ == '__main__':
80+
try:
81+
runExample()
82+
except (KeyboardInterrupt, SystemExit) as exErr:
83+
print("Ending example.")
84+
myMotor.disable()
85+
sys.exit(0)

examples/ex2_qwiic_scmd.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env python
2+
#-----------------------------------------------------------------------------
3+
# A simple test to speed up and slow down both motors in opposite directions.
4+
#------------------------------------------------------------------------
5+
#
6+
# Written by Mark Lindemer
7+
# SparkFun Electronics, April 2020
8+
#
9+
# This python library supports the SparkFun Electroncis qwiic
10+
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
11+
# board computers.
12+
#
13+
# More information on qwiic is at https://www.sparkfun.com/qwiic
14+
#
15+
# Do you like this library? Help support SparkFun. Buy a board!
16+
#
17+
#==================================================================================
18+
# Copyright (c) 2019 SparkFun Electronics
19+
#
20+
# Permission is hereby granted, free of charge, to any person obtaining a copy
21+
# of this software and associated documentation files (the "Software"), to deal
22+
# in the Software without restriction, including without limitation the rights
23+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
24+
# copies of the Software, and to permit persons to whom the Software is
25+
# furnished to do so, subject to the following conditions:
26+
#
27+
# The above copyright notice and this permission notice shall be included in all
28+
# copies or substantial portions of the Software.
29+
#
30+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
# SOFTWARE.
37+
#==================================================================================
38+
# Example 1
39+
#
40+
41+
from __future__ import print_function
42+
import time
43+
import sys
44+
import math
45+
import qwiic_scmd
46+
47+
myMotor = qwiic_scmd.QwiicScmd()
48+
49+
def runExample():
50+
print("Motor Test.")
51+
R_MTR = 0
52+
L_MTR = 1
53+
FWD = 0
54+
BWD = 1
55+
56+
if myMotor.connected == False:
57+
print("Motor Driver not connected. Check connections.", \
58+
file=sys.stderr)
59+
return
60+
myMotor.begin()
61+
print("Motor initialized.")
62+
time.sleep(.250)
63+
myMotor.enable()
64+
print("Motor enabled")
65+
time.sleep(.250)
66+
67+
68+
while True:
69+
speed = 20
70+
for speed in range(20,255):
71+
print(speed)
72+
myMotor.set_drive(R_MTR,FWD,speed)
73+
myMotor.set_drive(L_MTR,BWD,speed)
74+
time.sleep(.05)
75+
for speed in range(255,20, -1):
76+
print(speed)
77+
myMotor.set_drive(R_MTR,FWD,speed)
78+
myMotor.set_drive(L_MTR,BWD,speed)
79+
time.sleep(.05)
80+
81+
if __name__ == '__main__':
82+
try:
83+
runExample()
84+
except (KeyboardInterrupt, SystemExit) as exErr:
85+
print("Ending example.")
86+
myMotor.disable()
87+
sys.exit(0)

0 commit comments

Comments
 (0)