Motoron Motor Controller library for Raspberry Pi
Loading...
Searching...
No Matches
mpy_uart_careful_example.py
1# This example shows how to control the Motoron Motor Controller serial
2# interface using the machine.UART 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 serial 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 UART, Pin
17
18port = UART(1, 115200, tx=Pin(4), rx=Pin(5), timeout=100)
19mc = motoron.MotoronSerial(port=port)
20
21# Parameters for the VIN voltage measurement.
22reference_mv = 3300
23vin_type = motoron.VinSenseType.MOTORON_256
24
25# Minimum allowed VIN voltage. You can change this to be closer to your power
26# supply's expected voltage.
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_UART_ERROR) |
37 (1 << motoron.STATUS_FLAG_RESET) |
38 (1 << motoron.STATUS_FLAG_COMMAND_TIMEOUT))
39
40mc.reinitialize()
41mc.clear_reset_flag()
42
43# Configure the Motoron to coast the motors while obeying deceleration limits if
44# there is an error.
45mc.set_error_response(motoron.ERROR_RESPONSE_COAST)
46mc.set_error_mask(error_mask)
47
48# Use a short command timeout of 100 ms: the Motoron will stop the motors if it
49# does not get a command for 100 ms.
50mc.set_command_timeout_milliseconds(100)
51
52# Configure motor 1
53mc.set_max_acceleration(1, 140)
54mc.set_max_deceleration(1, 300)
55
56# Depending on what was happening before this program started, the motors will
57# either be stopped or decelerating. This loop waits for them to stop so that
58# when the rest of the code starts running, it will run from a more predictable
59# starting point. This is optional.
60while mc.get_motor_driving_flag(): pass
61
62mc.clear_motor_fault()
63
64def check_for_problems():
65 status = mc.get_status_flags()
66 if (status & error_mask):
67 # One of the error flags is set. The Motoron should already be stopping
68 # the motors. We send a reset command to be extra careful.
69 mc.reset()
70 print("Controller error: 0x%x" % status, file=sys.stderr)
71 sys.exit(1)
72
73 voltage_mv = mc.get_vin_voltage_mv(reference_mv, vin_type)
74 if voltage_mv < min_vin_voltage_mv:
75 mc.reset()
76 print("VIN voltage too low:", voltage_mv, file=sys.stderr)
77 sys.exit(1)
78
79try:
80 while True:
81 check_for_problems()
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 a serial connection to a Pololu Motoron Motor Controller.
Definition: motoron.py:1761