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