Motoron Motor Controller library for Raspberry Pi
Loading...
Searching...
No Matches
i2c_simple_multi_example.py
1#!/usr/bin/env python3
2
3# This example shows a simple way to control multiple
4# Motoron Motor Controllers.
5#
6# The motors will stop but automatically recover if:
7# - Motor power (VIN) is interrupted
8# - A temporary motor fault occurs
9# - A command timeout occurs
10#
11# This program will terminate if it does not receive an acknowledgment bit from
12# a Motoron for a byte it has written or if any other exception is thrown by
13# the underlying Python I2C library.
14#
15# The motors will stop until you restart this program if any Motoron
16# experiences a reset.
17#
18# If a latched motor fault occurs, the motors experiencing the fault will stop
19# until you power cycle motor power (VIN) or cause the motors to coast.
20
21import time
22import motoron
23
24# Creates an object for each Motoron controller. Each address argument below
25# should be the 7-bit I2C address of the controller.
26#
27# You should use the i2c_set_addresses_example.py utility to assign a unique
28# address to each Motoron, and then modify the list below to match your setup.
29mc1 = motoron.MotoronI2C(address=17)
30mc2 = motoron.MotoronI2C(address=18)
31mc3 = motoron.MotoronI2C(address=19)
32mc4 = motoron.MotoronI2C(address=20)
33
34def setup_motoron(mc):
35 mc.reinitialize()
36 mc.disable_crc()
37
38 # Clear the reset flag, which is set after the controller reinitializes
39 # and counts as an error.
40 mc.clear_reset_flag()
41
42 # By default, the Motoron is configured to stop the motors if it does not get
43 # a motor control command for 1500 ms. You can uncomment a line below to
44 # adjust this time or disable the timeout feature.
45 # mc.set_command_timeout_milliseconds(1000)
46 # mc.disable_command_timeout()
47
48setup_motoron(mc1)
49setup_motoron(mc2)
50setup_motoron(mc3)
51setup_motoron(mc4)
52
53mc1.set_max_acceleration(1, 80)
54mc1.set_max_deceleration(1, 300)
55
56mc1.set_max_acceleration(2, 80)
57mc1.set_max_deceleration(2, 300)
58
59mc1.set_max_acceleration(3, 80)
60mc1.set_max_deceleration(3, 300)
61
62mc2.set_max_acceleration(1, 80)
63mc2.set_max_deceleration(1, 300)
64
65mc2.set_max_acceleration(2, 80)
66mc2.set_max_deceleration(2, 300)
67
68mc2.set_max_acceleration(3, 80)
69mc2.set_max_deceleration(3, 300)
70
71mc3.set_max_acceleration(1, 80)
72mc3.set_max_deceleration(1, 300)
73
74mc3.set_max_acceleration(2, 80)
75mc3.set_max_deceleration(2, 300)
76
77mc3.set_max_acceleration(3, 80)
78mc3.set_max_deceleration(3, 300)
79
80mc4.set_max_acceleration(1, 80)
81mc4.set_max_deceleration(1, 300)
82
83mc4.set_max_acceleration(2, 80)
84mc4.set_max_deceleration(2, 300)
85
86mc4.set_max_acceleration(3, 80)
87mc4.set_max_deceleration(3, 300)
88
89try:
90 while True:
91 if int(time.monotonic() * 1000) & 2048:
92 changing_speed = 800
93 else:
94 changing_speed = -800
95
96 mc1.set_speed(1, changing_speed)
97 mc1.set_speed(2, 100)
98 mc1.set_speed(3, -100)
99
100 mc2.set_speed(1, 100)
101 mc2.set_speed(2, changing_speed)
102 mc2.set_speed(3, -100)
103
104 mc3.set_speed(1, 100)
105 mc3.set_speed(2, -100)
106 mc3.set_speed(3, changing_speed)
107
108 mc4.set_speed(1, changing_speed)
109 mc4.set_speed(2, -changing_speed)
110 mc4.set_speed(3, changing_speed)
111
112 time.sleep(0.005)
113
114except KeyboardInterrupt:
115 pass
Represents an I2C connection to a Pololu Motoron Motor Controller.
Definition: motoron.py:1665