-
Notifications
You must be signed in to change notification settings - Fork 3
/
i2c_simple_example.py
executable file
·66 lines (53 loc) · 1.91 KB
/
i2c_simple_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
# This example shows a simple way to control the Motoron Motor Controller.
#
# The motors will stop but automatically recover if:
# - Motor power (VIN) is interrupted
# - A temporary motor fault occurs
# - A command timeout occurs
#
# This program will terminate if it does not receive an acknowledgment bit from
# the Motoron for a byte it has written or if any other exception is thrown by
# the underlying Python I2C library.
#
# The motors will stop until you restart this program if the Motoron
# experiences a reset.
#
# If a latched motor fault occurs, the motors experiencing the fault will stop
# until you power cycle motor power (VIN) or cause the motors to coast.
import time
import motoron
mc = motoron.MotoronI2C()
# Reset the controller to its default settings, then disable CRC. The bytes for
# each of these commands are shown here in case you want to implement them on
# your own without using the library.
mc.reinitialize() # Bytes: 0x96 0x74
mc.disable_crc() # Bytes: 0x8B 0x04 0x7B 0x43
# Clear the reset flag, which is set after the controller reinitializes and
# counts as an error.
mc.clear_reset_flag() # Bytes: 0xA9 0x00 0x04
# By default, the Motoron is configured to stop the motors if it does not get
# a motor control command for 1500 ms. You can uncomment a line below to
# adjust this time or disable the timeout feature.
# mc.set_command_timeout_milliseconds(1000)
# mc.disable_command_timeout()
# Configure motor 1
mc.set_max_acceleration(1, 140)
mc.set_max_deceleration(1, 300)
# Configure motor 2
mc.set_max_acceleration(2, 200)
mc.set_max_deceleration(2, 300)
# Configure motor 3
mc.set_max_acceleration(3, 80)
mc.set_max_deceleration(3, 300)
try:
while True:
if int(time.monotonic() * 1000) & 2048:
mc.set_speed(1, 800)
else:
mc.set_speed(1, -800)
mc.set_speed(2, 100)
mc.set_speed(3, -100)
time.sleep(0.005)
except KeyboardInterrupt:
pass