Motoron Motor Controller library for Raspberry Pi
Loading...
Searching...
No Matches
serial_careful_example.py
1#!/usr/bin/env python3
2
3# This example shows how to control the Motoron Motor Controller if you want to
4# shut down the motors whenever any problems are detected.
5#
6# The motors will stop and this program will terminate if:
7# - Motor power (VIN) is interrupted
8# - A motor fault occurs
9# - A protocol error or CRC error occurs
10# - The underlying Python serial library reports an error
11# - A command timeout occurs
12# - The Motoron experiences a reset
13
14import sys
15import time
16import motoron
17
19mc.set_port("/dev/serial0")
20
21# Parameters for the VIN voltage measurement.
22reference_mv = 3300
23vin_type = motoron.VinSenseType.MOTORON_256
24
25# Minimum allowed VIN voltage. You can change this to be closer to your power
26# supply's expected voltage.
27min_vin_voltage_mv = 4500
28
29# Define which status flags the Motoron should treat as errors.
30error_mask = (
31 (1 << motoron.STATUS_FLAG_PROTOCOL_ERROR) |
32 (1 << motoron.STATUS_FLAG_CRC_ERROR) |
33 (1 << motoron.STATUS_FLAG_COMMAND_TIMEOUT_LATCHED) |
34 (1 << motoron.STATUS_FLAG_MOTOR_FAULT_LATCHED) |
35 (1 << motoron.STATUS_FLAG_NO_POWER_LATCHED) |
36 (1 << motoron.STATUS_FLAG_UART_ERROR) |
37 (1 << motoron.STATUS_FLAG_RESET) |
38 (1 << motoron.STATUS_FLAG_COMMAND_TIMEOUT))
39
40mc.reinitialize()
41mc.clear_reset_flag()
42
43# Configure the Motoron to coast the motors while obeying deceleration limits if
44# there is an error.
45mc.set_error_response(motoron.ERROR_RESPONSE_COAST)
46mc.set_error_mask(error_mask)
47
48# Use a short command timeout of 100 ms: the Motoron will stop the motors if it
49# does not get a command for 100 ms.
50mc.set_command_timeout_milliseconds(100)
51
52# Configure motor 1
53mc.set_max_acceleration(1, 140)
54mc.set_max_deceleration(1, 300)
55
56# Depending on what was happening before this program started, the motors will
57# either be stopped or decelerating. This loop waits for them to stop so that
58# when the rest of the code starts running, it will run from a more predictable
59# starting point. This is optional.
60while mc.get_motor_driving_flag(): pass
61
62mc.clear_motor_fault()
63
64def check_for_problems():
65 status = mc.get_status_flags()
66 if (status & error_mask):
67 # One of the error flags is set. The Motoron should already be stopping
68 # the motors. We send a reset command to be extra careful.
69 mc.reset()
70 print("Controller error: 0x%x" % status, file=sys.stderr)
71 sys.exit(1)
72
73 voltage_mv = mc.get_vin_voltage_mv(reference_mv, vin_type)
74 if voltage_mv < min_vin_voltage_mv:
75 mc.reset()
76 print("VIN voltage too low:", voltage_mv, file=sys.stderr)
77 sys.exit(1)
78
79try:
80 while True:
81 check_for_problems()
82
83 if int(time.monotonic() * 1000) & 2048:
84 mc.set_speed(1, 800)
85 else:
86 mc.set_speed(1, -800)
87
88 time.sleep(0.005)
89
90except KeyboardInterrupt:
91 mc.reset()
92 pass
93except Exception:
94 mc.reset()
95 raise
Represents a serial connection to a Pololu Motoron Motor Controller.
Definition: motoron.py:1761