Motoron Motor Controller library for Raspberry Pi
Loading...
Searching...
No Matches
mpy_uart_robust_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
3# your system to just keep working, ignoring or automatically recovering from
4# errors as much as possible.
5#
6# The motors will stop but automatically recover if:
7# - Motor power (VIN) is interrupted
8# - A motor fault occurs
9# - The Motoron experiences a reset
10# - A command timeout occurs
11#
12# Errors reported by the machine.UART class are caught so they
13# do not cause the program to terminate.
14
15import sys
16import time
17import motoron
18from machine import UART, Pin
19
20port = UART(1, 115200, tx=Pin(4), rx=Pin(5), timeout=100)
21mc = motoron.MotoronSerial(port=port)
22
23def motors_init():
24 global last_time_motors_init
25 try:
26 mc.clear_reset_flag()
27
28 # Configure motor 1
29 mc.set_max_acceleration(1, 70)
30 mc.set_max_deceleration(1, 150)
31
32 # Configure motor 2
33 mc.set_max_acceleration(2, 100)
34 mc.set_max_deceleration(2, 150)
35
36 # Configure motor 3
37 mc.set_max_acceleration(3, 40)
38 mc.set_max_deceleration(3, 150)
39
40 mc.clear_motor_fault_unconditional()
41
42 except OSError as e:
43 print("Error: motors_init:", e)
44
45 last_time_motors_init = time.ticks_ms()
46
47try:
48 mc.reinitialize()
49except OSError:
50 pass
51
52motors_init()
53
54while True:
55 try:
56 if time.ticks_ms() & 2048:
57 mc.set_speed(1, 800)
58 else:
59 mc.set_speed(1, -800)
60 mc.set_speed(2, 100)
61 mc.set_speed(3, -100)
62 except OSError:
63 pass
64
65 # Every 2000 ms, run motors_init to restart the motors
66 # in case anything has caused them to shut down.
67 if time.ticks_ms() - last_time_motors_init > 2000:
68 motors_init()
Represents a serial connection to a Pololu Motoron Motor Controller.
Definition: motoron.py:1761