Motoron Motor Controller library for Raspberry Pi
Loading...
Searching...
No Matches
mpy_i2c_careful_example.py
1# This example shows how to control the Motoron Motor Controller
2# I2C interface using the machine.I2C class in MicroPython if you want to
3# shut down the motors whenever any problems are detected.
4#
5# The motors will stop and this program will terminate if:
6# - Motor power (VIN) is interrupted
7# - A motor fault occurs
8# - A protocol error or CRC error occurs
9# - The underlying Python I2C library reports an error
10# - A command timeout occurs
11# - The Motoron experiences a reset
12
13import sys
14import time
15import motoron
16from machine import I2C, Pin
17
18bus = I2C(0, scl=Pin(5), sda=Pin(4))
19mc = motoron.MotoronI2C(bus=bus)
20
21# Parameters for the VIN voltage measurement.
22reference_mv = 3300
23vin_type = motoron.VinSenseType.MOTORON_256
24
25# Minimum allowed VIN voltage. This example is aborts if the voltage drops
26# below this configurable level.
27min_vin_voltage_mv = 4500
28
29# Define which status flags the Motoron should treat as errors.
30error_mask = (
31 (1 << motoron.STATUS_FLAG_PROTOCOL_ERROR) |
32 (1 << motoron.STATUS_FLAG_CRC_ERROR) |
33 (1 << motoron.STATUS_FLAG_COMMAND_TIMEOUT_LATCHED) |
34 (1 << motoron.STATUS_FLAG_MOTOR_FAULT_LATCHED) |
35 (1 << motoron.STATUS_FLAG_NO_POWER_LATCHED) |
36 (1 << motoron.STATUS_FLAG_RESET) |
37 (1 << motoron.STATUS_FLAG_COMMAND_TIMEOUT))
38
39mc.reinitialize()
40mc.clear_reset_flag()
41
42# Configure the Motoron to coast the motors while obeying deceleration limits if
43# there is an error.
44mc.set_error_response(motoron.ERROR_RESPONSE_COAST)
45mc.set_error_mask(error_mask)
46
47# Use a short command timeout of 100 ms: the Motoron will stop the motors if it
48# does not get a command for 100 ms.
49mc.set_command_timeout_milliseconds(100)
50
51# Configure motor 1
52mc.set_max_acceleration(1, 140)
53mc.set_max_deceleration(1, 300)
54
55# Depending on what was happening before this program started, the motors will
56# either be stopped or decelerating. This loop waits for them to stop so that
57# when the rest of the code starts running, it will run from a more predictable
58# starting point. This is optional.
59while mc.get_motor_driving_flag(): pass
60
61mc.clear_motor_fault()
62
63def check_for_problems():
64 status = mc.get_status_flags()
65 if (status & error_mask):
66 # One of the error flags is set. The Motoron should already be stopping
67 # the motors. We send a reset command to be extra careful.
68 mc.reset()
69 print("Controller error: 0x%x" % status, file=sys.stderr)
70 sys.exit(1)
71
72 voltage_mv = mc.get_vin_voltage_mv(reference_mv, vin_type)
73 if voltage_mv < min_vin_voltage_mv:
74 mc.reset()
75 print("VIN voltage too low:", voltage_mv, file=sys.stderr)
76 sys.exit(1)
77
78try:
79 while True:
80 check_for_problems()
81
82 if time.ticks_ms() & 2048:
83 mc.set_speed(1, 800)
84 else:
85 mc.set_speed(1, -800)
86
87except:
88 mc.reset()
89 raise
Represents an I2C connection to a Pololu Motoron Motor Controller.
Definition: motoron.py:1665