-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmpy_uart_simple_example.py
56 lines (47 loc) · 1.86 KB
/
mpy_uart_simple_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
# This example shows a simple way to control the Motoron Motor Controller
# serial interface using the machine.UART class in MicroPython.
#
# The motors will stop but automatically recover if:
# - Motor power (VIN) is interrupted
# - A temporary motor fault occurs
# - A command timeout occurs
#
# This program will terminate if it does not receive an acknowledgment bit from
# the Motoron for a byte it has written or if any other exception is thrown by
# the underlying Python serial library.
#
# The motors will stop until you restart this program if the Motoron
# experiences a reset.
#
# If a latched motor fault occurs, the motors experiencing the fault will stop
# until you power cycle motor power (VIN) or cause the motors to coast.
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)
# Reset the controller to its default settings, then disable CRC. The bytes for
# each of these commands are shown here in case you want to implement them on
# your own without using the library.
mc.reinitialize() # Bytes: 0x96 0x74
mc.disable_crc() # Bytes: 0x8B 0x04 0x7B 0x43
# Clear the reset flag, which is set after the controller reinitializes and
# counts as an error.
mc.clear_reset_flag() # Bytes: 0xA9 0x00 0x04
# By default, the Motoron is configured to stop the motors if it does not get
# a motor control command for 1500 ms. You can uncomment a line below to
# adjust this time or disable the timeout feature.
# mc.set_command_timeout_milliseconds(1000)
# mc.disable_command_timeout()
# Configure motor 1
mc.set_max_acceleration(1, 140)
mc.set_max_deceleration(1, 300)
# Configure motor 2
mc.set_max_acceleration(2, 200)
mc.set_max_deceleration(2, 300)
while True:
if time.ticks_ms() & 2048:
mc.set_speed(1, 800)
else:
mc.set_speed(1, -800)
mc.set_speed(2, 100)