Motoron Motor Controller library for Raspberry Pi
Loading...
Searching...
No Matches
serial_simple_multi_example.py
1#!/usr/bin/env python3
2
3# This example shows a simple way to control multiple
4# Motoron Motor Controllers using serial.
5#
6# The motors will stop but automatically recover if:
7# - Motor power (VIN) is interrupted
8# - A temporary motor fault occurs
9# - A command timeout occurs
10#
11# The motors will stop until you restart this program if any Motoron
12# experiences a reset.
13#
14# If a latched motor fault occurs, the motors experiencing the fault will stop
15# until you power cycle motor power (VIN) or cause the motors to coast.
16
17import time
18import motoron
19import serial
20
21port = serial.Serial("/dev/serial0", 115200, timeout=0.1, write_timeout=0.1)
22
23# Create an object for each Motoron controller. The device numbers below must
24# match the way you have configured your Motorons.
25mc1 = motoron.MotoronSerial(device_number=17)
26mc2 = motoron.MotoronSerial(device_number=18)
27mc3 = motoron.MotoronSerial(device_number=19)
28
29def setup_motoron(mc):
30 mc.set_port(port)
31
32 mc.reinitialize()
33 mc.disable_crc()
34
35 # Clear the reset flag, which is set after the controller reinitializes
36 # and counts as an error.
37 mc.clear_reset_flag()
38
39 # By default, the Motoron is configured to stop the motors if it does not get
40 # a motor control command for 1500 ms. You can uncomment a line below to
41 # adjust this time or disable the timeout feature.
42 # mc.set_command_timeout_milliseconds(1000)
43 # mc.disable_command_timeout()
44
45setup_motoron(mc1)
46setup_motoron(mc2)
47setup_motoron(mc3)
48
49mc1.set_max_acceleration(1, 80)
50mc1.set_max_deceleration(1, 300)
51
52mc1.set_max_acceleration(2, 80)
53mc1.set_max_deceleration(2, 300)
54
55mc2.set_max_acceleration(1, 80)
56mc2.set_max_deceleration(1, 300)
57
58mc2.set_max_acceleration(2, 80)
59mc2.set_max_deceleration(2, 300)
60
61mc3.set_max_acceleration(1, 80)
62mc3.set_max_deceleration(1, 300)
63
64mc3.set_max_acceleration(2, 80)
65mc3.set_max_deceleration(2, 300)
66
67try:
68 while True:
69 if int(time.monotonic() * 1000) & 2048:
70 changing_speed = 800
71 else:
72 changing_speed = -800
73
74 mc1.set_speed(1, changing_speed)
75 mc1.set_speed(2, -changing_speed)
76
77 mc2.set_speed(1, 400)
78 mc2.set_speed(2, changing_speed)
79
80 mc3.set_speed(1, changing_speed)
81 mc3.set_speed(2, 400)
82
83 time.sleep(0.005)
84
85except KeyboardInterrupt:
86 pass
Represents a serial connection to a Pololu Motoron Motor Controller.
Definition: motoron.py:1761