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