17-----------------|----------------------------------------------------------
18a [NUM] [ALTNUM] | Write all settings to EEPROM (JMP1 must be low).
21b BAUD | Use a different baud rate to communicate.
22o [OPTS] | Use different communication options.
23n | Use 115200 baud, 8-bit responses, 7-bit device number.
24j | Use 9600 baud, 8-bit responses, 7-bit device number.
25k | Use the options & baud rate we're assigning to devices.
27For more information about each command, see the comments in the
32Motoron Serial Setup Utility
43# The serial parameters below are assigned to a device when you use the "a"
44# command. You can modify these lines to assign different parameters.
45assign_baud_rate = 115200
46assign_7bit_responses = False
47assign_14bit_device_number =
False
48assign_err_is_de =
False
49assign_response_delay = 0
51if assign_baud_rate < motoron.MIN_BAUD_RATE
or \
52 assign_baud_rate > motoron.MAX_BAUD_RATE:
53 raise RuntimeError(
"Invalid baud rate.")
55assign_communication_options = \
56 (assign_7bit_responses << motoron.COMMUNICATION_OPTION_7BIT_RESPONSES) | \
57 (assign_14bit_device_number << motoron.COMMUNICATION_OPTION_14BIT_DEVICE_NUMBER) | \
58 (assign_err_is_de << motoron.COMMUNICATION_OPTION_ERR_IS_DE)
60port = serial.Serial(
"/dev/serial0", 115200, timeout=0.01, write_timeout=0.1)
64last_device_number = 16
92def assign_all_settings(line):
93 global last_device_number
94 max_device_number = 0x3FFF
if assign_14bit_device_number
else 0x7F
97 alt_device_number = -1
99 parts = line[1:].split()
102 device_number = int(parts[0])
104 print(
"Invalid device number argument.")
108 alt_device_number = int(parts[1])
110 print(
"Invalid alternative device number argument.")
113 if device_number == -1:
115 device_number = (last_device_number + 1) & max_device_number
117 if not device_number
in range(0, max_device_number + 1):
118 print(
"Invalid device number.")
121 if not alt_device_number
in range(-1, max_device_number + 1):
122 print(
"Invalid alternative device number.")
126 mc.write_eeprom_device_number(device_number)
127 if alt_device_number == -1:
128 mc.write_eeprom_disable_alternative_device_number()
130 mc.write_eeprom_alternative_device_number(alt_device_number)
131 mc.write_eeprom_baud_rate(assign_baud_rate)
132 mc.write_eeprom_communication_options(assign_communication_options)
133 mc.write_eeprom_response_delay(assign_response_delay)
135 print(
"Assigned device number", device_number, end=
'')
137 if alt_device_number != -1:
138 print(
" and", alt_device_number, end=
'')
139 print(
",", assign_baud_rate,
"baud", end=
'')
141 if assign_14bit_device_number:
142 print(
", 14-bit device number", end=
'')
144 print(
", 7-bit device number", end=
'')
146 if assign_7bit_responses:
147 print(
", 7-bit responses", end=
'')
149 print(
", 8-bit responses", end=
'')
152 print(
", ERR is DE", end=
'')
154 print(
", ERR is normal", end=
'')
158 last_device_number = device_number
160def print_communication_settings():
161 print(port.baudrate,
'baud', end=
'')
162 if mc.communication_options & (1 << motoron.COMMUNICATION_OPTION_14BIT_DEVICE_NUMBER):
163 print(
', 14-bit device number', end=
'')
165 print(
', 7-bit device number', end=
'')
166 if mc.communication_options & (1 << motoron.COMMUNICATION_OPTION_7BIT_RESPONSES):
167 print(
', 7-bit responses', end=
'')
169 print(
', 8-bit responses', end=
'')
171def print_communication_settings_line():
172 print(
"Using ", end=
'')
173 print_communication_settings();
176def print_device_info_if_possible():
177 firmware = mc.get_firmware_version()
178 product_id = firmware[
'product_id']
179 firmware_version = firmware[
'firmware_version']
181 jumper_state = mc.get_jumper_state() & 3
182 if jumper_state == 0b10: jumper_string =
'off'
183 elif jumper_state == 0b01: jumper_string =
'on'
184 else: jumper_string =
'err'
188 eeprom = mc.read_eeprom(1, 4) + mc.read_eeprom(5, 4)
190 print(
"%3d: product=0x%04X version=%x.%02x JMP1=%s EEPROM=%s" % (
191 mc.device_number, product_id,
192 firmware_version[
'major'], firmware_version[
'minor'], jumper_string,
193 ' '.join((
'%02X' % b
for b
in eeprom))))
202def identify_devices():
203 if mc.communication_options & (1 << motoron.COMMUNICATION_OPTION_14BIT_DEVICE_NUMBER):
204 max_device_number = 0x3FFF
206 max_device_number = 0x7F
208 print(
"Identifying Motoron controllers (", end=
'')
209 print_communication_settings()
211 for i
in range(max_device_number + 1):
212 port.reset_input_buffer()
216 print_device_info_if_possible()
220 mc.device_number =
None
228def command_set_baud_rate(line):
230 baud_rate = int(line[1:])
232 print(
"Invalid baud rate.")
234 if baud_rate < motoron.MIN_BAUD_RATE
or baud_rate > motoron.MAX_BAUD_RATE:
235 print(
"Invalid baud rate.")
238 port.baudrate = baud_rate
239 print_communication_settings_line()
254def command_set_communication_options(line):
256 mc.communication_options = int(line[1:])
258 mc.communication_options = (mc.communication_options + 1) & 3
260 print_communication_settings_line()
262def process_command(line):
264 elif line[0] ==
'a': assign_all_settings(line)
268 elif line[0] ==
'i': identify_devices()
269 elif line[0] ==
'b': command_set_baud_rate(line)
270 elif line[0] ==
'o': command_set_communication_options(line)
272 port.baudrate = 115200
273 mc.communication_options = 0
274 print_communication_settings_line()
277 mc.communication_options = 0
278 print_communication_settings_line()
280 port.baudrate = assign_baud_rate
281 mc.communication_options = assign_communication_options
282 print_communication_settings_line()
283 elif line[0] ==
'h' or line[0] ==
'H' or line[0] ==
'?': print(help_message)
284 elif line ==
'q' or line ==
'quit': sys.exit(0)
285 else: print(
"Error: Unreocgnized command. Type h for help.")
290 process_command(input(
"Enter command: "))
291except (KeyboardInterrupt, EOFError):
Represents a serial connection to a Pololu Motoron Motor Controller.