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