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