Motoron Motor Controller library for Raspberry Pi
Loading...
Searching...
No Matches
serial_simple_example.py
1#!/usr/bin/env python3
2
3# This example shows a simple way to control the Motoron Motor Controller.
4#
5# The motors will stop but automatically recover if:
6# - Motor power (VIN) is interrupted
7# - A temporary motor fault occurs
8# - A command timeout occurs
9#
10# This program will terminate if it does not receive an acknowledgment bit from
11# the Motoron for a byte it has written or if any other exception is thrown by
12# the underlying Python serial library.
13#
14# The motors will stop until you restart this program if the Motoron
15# experiences a reset.
16#
17# If a latched motor fault occurs, the motors experiencing the fault will stop
18# until you power cycle motor power (VIN) or cause the motors to coast.
19
20import time
21import motoron
22
24mc.set_port("/dev/serial0")
25
26# Reset the controller to its default settings, then disable CRC. The bytes for
27# each of these commands are shown here in case you want to implement them on
28# your own without using the library.
29mc.reinitialize() # Bytes: 0x96 0x74
30mc.disable_crc() # Bytes: 0x8B 0x04 0x7B 0x43
31
32# Clear the reset flag, which is set after the controller reinitializes and
33# counts as an error.
34mc.clear_reset_flag() # Bytes: 0xA9 0x00 0x04
35
36# By default, the Motoron is configured to stop the motors if it does not get
37# a motor control command for 1500 ms. You can uncomment a line below to
38# adjust this time or disable the timeout feature.
39# mc.set_command_timeout_milliseconds(1000)
40# mc.disable_command_timeout()
41
42# Configure motor 1
43mc.set_max_acceleration(1, 140)
44mc.set_max_deceleration(1, 300)
45
46# Configure motor 2
47mc.set_max_acceleration(2, 200)
48mc.set_max_deceleration(2, 300)
49
50try:
51 while True:
52 if int(time.monotonic() * 1000) & 2048:
53 mc.set_speed(1, 800)
54 else:
55 mc.set_speed(1, -800)
56
57 mc.set_speed(2, 100)
58
59 time.sleep(0.005)
60
61except KeyboardInterrupt:
62 pass
Represents a serial connection to a Pololu Motoron Motor Controller.
Definition: motoron.py:1761