Motoron Motor Controller library for Raspberry Pi
Loading...
Searching...
No Matches
motoron.py
Go to the documentation of this file.
1# Copyright (C) Pololu Corporation. See LICENSE.txt for details.
2
3import math
4import os
5import struct
6import time
7
8try:
9 from enum import Enum
10 def enum_value(x): return x.value
11except ImportError:
12 Enum = object
13 def enum_value(x): return x
14
15from motoron_protocol import *
16
17
24
25class CurrentSenseType(Enum):
26 MOTORON_18V18 = 0b0001
27 MOTORON_24V14 = 0b0101
28 MOTORON_18V20 = 0b1010
29 MOTORON_24V16 = 0b1101
30 MOTORON_453 = 0b0110
31
32class VinSenseType(Enum):
33 MOTORON_256 = 0b0000 # M*256 Motorons
34 MOTORON_HP = 0b0010 # High-power Motorons
35 MOTORON_550 = 0b0011 # M*550 Motorons
36 MOTORON_453 = 0b0110 # M*453 Motorons
37
39 """
40 Represents a connection to a Pololu Motoron Motoron Controller.
41 """
42
43 __DEFAULT_PROTOCOL_OPTIONS = (
44 (1 << PROTOCOL_OPTION_I2C_GENERAL_CALL) |
45 (1 << PROTOCOL_OPTION_CRC_FOR_COMMANDS) |
46 (1 << PROTOCOL_OPTION_CRC_FOR_RESPONSES))
47
48
49 DEFAULT_ERROR_MASK = (
50 (1 << STATUS_FLAG_COMMAND_TIMEOUT) |
51 (1 << STATUS_FLAG_RESET))
52
53 def __init__(self):
54
56 self.protocol_options = MotoronBase.__DEFAULT_PROTOCOL_OPTIONS
57
59 """
60 Sends the "Get firmware version" command to get the device's firmware
61 product ID and firmware version numbers.
62
63 For more information, see the "Get firmware version"
64 command in the Motoron user's guide.
65
66 \return A dictionary in this format:
67 ```{.py}
68 {'product_id': 204, 'firmware_version': {'major': 1, 'minor': 0}}
69 ```
70 """
71 cmd = [CMD_GET_FIRMWARE_VERSION]
72 response = self._send_command_and_read_response(cmd, 4)
73 product_id, minor, major = struct.unpack('<HBB', response)
74 return {
75 'product_id': product_id,
76 'firmware_version': {'major': major, 'minor': minor}
77 }
78
79 def set_protocol_options(self, options):
80 """
81 Sends the "Set protocol options" command to the device to specify options
82 related to how the device processes commands and sends responses.
83 The options are also saved in this object and are used later
84 when sending other commands or reading responses.
85
86 When CRC for commands is enabled, this library generates the CRC
87 byte and appends it to the end of each command it sends. The Motoron
88 checks it to help ensure the command was received correctly.
89
90 When CRC for responses is enabled, this library reads the CRC byte sent
91 by the Motoron in its responses and makes sure it is correct. If the
92 response CRC byte is incorrect, get_last_error() will return a non-zero
93 error code after the command has been run.
94
95 When the I2C general call address is enabled, the Motoron receives
96 commands sent to address 0 in addition to its usual I2C address.
97 The general call address is write-only; reading bytes from it is not
98 supported.
99
100 By default (in this libary and the Motoron itself), CRC for commands and
101 responses is enabled, and the I2C general call address is enabled.
102
103 This method always sends its command with a CRC byte, so it will work
104 even if CRC was previously disabled but has been re-enabled on the device
105 (e.g. due to a reset).
106
107 The @p options argument should be 0 or combination of the following
108 expressions made using the bitwise or operator (|):
109 - (1 << motoron.PROTOCOL_OPTION_CRC_FOR_COMMANDS)
110 - (1 << motoron.PROTOCOL_OPTION_CRC_FOR_RESPONSES)
111 - (1 << motoron.PROTOCOL_OPTION_I2C_GENERAL_CALL)
112
113 For more information, see the "Set protocol optons"
114 command in the Motoron user's guide.
115
116 @sa enable_crc(), disable_crc(),
117 enable_crc_for_commands(), disable_crc_for_commands(),
118 enable_crc_for_responses(), disable_crc_for_responses(),
119 enable_i2c_general_call(), disable_i2c_general_call()
120 """
121 self.protocol_options = options
122 cmd = [
123 CMD_SET_PROTOCOL_OPTIONS,
124 options & 0x7F,
125 ~options & 0x7F
126 ]
127 self._send_command_core(cmd, True)
128
130 """
131 Sets the protocol options for this object, without sending a command to
132 the Motoron.
133
134 If the options you specify here do not match the actual configuration of
135 the Motoron, future communication could fail.
136
137 Most users should use set_protocol_options() instead of this.
138 """
139 self.protocol_options = options
140
141 def enable_crc(self):
142 """
143 Enables CRC for commands and responses. See set_protocol_options().
144 """
146 | (1 << PROTOCOL_OPTION_CRC_FOR_COMMANDS)
147 | (1 << PROTOCOL_OPTION_CRC_FOR_RESPONSES))
148
149 def disable_crc(self):
150 """
151 Disables CRC for commands and responses. See set_protocol_options().
152 """
154 & ~(1 << PROTOCOL_OPTION_CRC_FOR_COMMANDS)
155 & ~(1 << PROTOCOL_OPTION_CRC_FOR_RESPONSES))
156
158 """
159 Enables CRC for commands. See set_protocol_options().
160 """
162 | (1 << PROTOCOL_OPTION_CRC_FOR_COMMANDS))
163
165 """
166 Disables CRC for commands. See set_protocol_options().
167 """
169 & ~(1 << PROTOCOL_OPTION_CRC_FOR_COMMANDS))
170
172 """
173 Enables CRC for responses. See set_protocol_options().
174 """
176 | (1 << PROTOCOL_OPTION_CRC_FOR_RESPONSES))
177
179 """
180 Disables CRC for responses. See set_protocol_options().
181 """
183 & ~(1 << PROTOCOL_OPTION_CRC_FOR_RESPONSES))
184
186 """
187 Enables the I2C general call address. See set_protocol_options().
188 """
190 | (1 << PROTOCOL_OPTION_I2C_GENERAL_CALL))
191
193 """
194 Disables the I2C general call address. See set_protocol_options().
195 """
197 & ~(1 << PROTOCOL_OPTION_I2C_GENERAL_CALL))
198
199 def read_eeprom(self, offset, length):
200 """
201 Reads the specified bytes from the Motoron's EEPROM memory.
202
203 For more information, see the "Read EEPROM" command in the
204 Motoron user's guide.
205 """
206 cmd = [
207 CMD_READ_EEPROM,
208 offset & 0x7F,
209 length & 0x7F,
210 ]
211 return self._send_command_and_read_response(cmd, length)
212
214 """
215 Reads the EEPROM device number from the device.
216 This is the I2C address that the device uses if it detects that JMP1
217 is shorted to GND when it starts up. It is stored in non-volatile
218 EEPROM memory.
219 """
220 return self.read_eeprom(SETTING_DEVICE_NUMBER, 1)[0]
221
222 def write_eeprom(self, offset, value):
223 """
224 Writes a value to one byte in the Motoron's EEPROM memory.
225
226 **Warning: Be careful not to write to the EEPROM in a fast loop. The
227 EEPROM memory of the Motoron's microcontroller is only rated for
228 100,000 erase/write cycles.**
229
230 For more information, see the "Write EEPROM" command in the
231 Motoron user's guide.
232 """
233 cmd = [
234 CMD_WRITE_EEPROM,
235 offset & 0x7F,
236 value & 0x7F,
237 (value >> 7) & 1,
238 ]
239 cmd += [
240 cmd[1] ^ 0x7F,
241 cmd[2] ^ 0x7F,
242 cmd[3] ^ 0x7F,
243 ]
244 self._send_command(cmd)
245 time.sleep(0.006)
246
247 def write_eeprom16(self, offset, value):
248 """
249 Writes a 2-byte value in the Motoron's EEPROM memory.
250
251 This command only has an effect if JMP1 is shorted to GND.
252
253 **Warning: Be careful not to write to the EEPROM in a fast loop. The
254 EEPROM memory of the Motoron’s microcontroller is only rated for
255 100,000 erase/write cycles.
256 """
257 self.write_eeprom(offset, value & 0xFF)
258 self.write_eeprom(offset + 1, value >> 8 & 0xFF)
259
260 def write_eeprom_device_number(self, number):
261 """
262 Writes to the EEPROM device number, changing it to the specified value.
263
264 This command only has an effect if JMP1 is shorted to GND.
265
266 **Warning: Be careful not to write to the EEPROM in a fast loop. The
267 EEPROM memory of the Motoron's microcontroller is only rated for
268 100,000 erase/write cycles.**
269 """
270 self.write_eeprom(SETTING_DEVICE_NUMBER, number & 0x7F)
271 self.write_eeprom(SETTING_DEVICE_NUMBER + 1, number >> 7 & 0x7F)
272
274 """
275 Writes to the alternative device number stored in EEPROM, changing it to
276 the specified value.
277
278 This function is only useful on Motorons with a serial interface,
279 and only has an effect if JMP1 is shorted to GND.
280
281 **Warning: Be careful not to write to the EEPROM in a fast loop. The
282 EEPROM memory of the Motoron's microcontroller is only rated for
283 100,000 erase/write cycles.**
284
285 @sa write_eeprom_disable_alternative_device_number()
286 """
287 self.write_eeprom(SETTING_ALTERNATIVE_DEVICE_NUMBER, (number & 0x7F) | 0x80)
288 self.write_eeprom(SETTING_ALTERNATIVE_DEVICE_NUMBER + 1, number >> 7 & 0x7F)
289
291 """
292 Writes to EEPROM to disable the alternative device number.
293
294 This function is only useful on Motorons with a serial interface,
295 and only has an effect if JMP1 is shorted to GND.
296
297 **Warning: Be careful not to write to the EEPROM in a fast loop. The
298 EEPROM memory of the Motoron's microcontroller is only rated for
299 100,000 erase/write cycles.**
300
301 @sa write_eeprom_alternative_device_number()
302 """
303 self.write_eeprom(SETTING_ALTERNATIVE_DEVICE_NUMBER, 0)
304 self.write_eeprom(SETTING_ALTERNATIVE_DEVICE_NUMBER + 1, 0)
305
307 """
308 Writes to the serial options byte stored in EEPROM, changing it to
309 the specified value.
310
311 The bits in this byte are defined by the
312 MOTORON_COMMUNICATION_OPTION_* constants.
313
314 This function is only useful on Motorons with a serial interface,
315 and only has an effect if JMP1 is shorted to GND.
316
317 **Warning: Be careful not to write to the EEPROM in a fast loop. The
318 EEPROM memory of the Motoron's microcontroller is only rated for
319 100,000 erase/write cycles.**
320 """
321 self.write_eeprom(SETTING_COMMUNICATION_OPTIONS, options)
322
323 def write_eeprom_baud_rate(self, baud):
324 """
325 Writes to the baud rate stored in EEPROM, changing it to the
326 specified value.
327
328 This function is only useful on Motorons with a serial interface,
329 and only has an effect if JMP1 is shorted to GND.
330
331 **Warning: Be careful not to write to the EEPROM in a fast loop. The
332 EEPROM memory of the Motoron's microcontroller is only rated for
333 100,000 erase/write cycles.**
334 """
335 if (baud < MIN_BAUD_RATE): baud = MIN_BAUD_RATE
336 if (baud > MAX_BAUD_RATE): baud = MAX_BAUD_RATE
337 self.write_eeprom16(SETTING_BAUD_DIVIDER, round(16000000 / baud))
338
340 """
341 Writes to the serial response delay setting stored in EEPROM, changing
342 it to the specified value, in units of microseconds.
343
344 This function is only useful on Motorons with a serial interface,
345 and only has an effect if JMP1 is shorted to GND.
346
347 **Warning: Be careful not to write to the EEPROM in a fast loop. The
348 EEPROM memory of the Motoron's microcontroller is only rated for
349 100,000 erase/write cycles.**
350 """
351 self.write_eeprom(SETTING_RESPONSE_DELAY, delay)
352
353 def reinitialize(self):
354 """
355 Sends a "Reinitialize" command to the Motoron, which resets most of the
356 Motoron's variables to their default state.
357
358 For more information, see the "Reinitialize" command in the Motoron
359 user's guide.
360
361 @sa reset()
362 """
363 # Always send the reinitialize command with a CRC byte to make it more reliable.
364 cmd = [CMD_REINITIALIZE]
365 self._send_command_core(cmd, True)
366 self.protocol_options = MotoronBase.__DEFAULT_PROTOCOL_OPTIONS
367
368 def reset(self, ignore_nack=True):
369 """
370 Sends a "Reset" command to the Motoron, which does a full hardware reset.
371
372 This command is equivalent to briefly driving the Motoron's RST pin low.
373 The Motoron's RST is briefly driven low by the Motoron itself as a
374 result of this command.
375
376 After running this command, we recommend waiting for at least 5
377 milliseconds before you try to communicate with the Motoron.
378
379 @param ignore_nack Optional argument: if `True` (the default), this method
380 ignores a NACK error if it occurs on sending the Reset command. This is
381 useful in case the Motoron has CRC off and executes the reset before it
382 can ACK the CRC byte (which this method always sends to make it more
383 reliable).
384
385 @sa reinitialize()
386 """
387 # Always send the reset command with a CRC byte to make it more reliable.
388 cmd = [CMD_RESET]
389 try:
390 self._send_command_core(cmd, True)
391 except OSError as e:
392 # Errno 5 (Input/output error) or 121 (Remote I/O error) indicates a
393 # NACK of a data byte. Ignore it if the ignore_nack argument is True.
394 # In all other cases, re-raise the exception.
395 if not (ignore_nack and (e.args[0] == 5 or e.args[0] == 121)): raise
396 self.protocol_options = MotoronBase.__DEFAULT_PROTOCOL_OPTIONS
397
398 def get_variables(self, motor, offset, length):
399 """
400 Reads information from the Motoron using a "Get variables" command.
401
402 This library has helper methods to read every variable, but this method
403 is useful if you want to get the raw bytes, or if you want to read
404 multiple consecutive variables at the same time for efficiency.
405
406 @param motor 0 to read general variables, or a motor number to read
407 motor-specific variables.
408 @param offset The location of the first byte to read.
409 @param length How many bytes to read.
410 """
411 cmd = [
412 CMD_GET_VARIABLES,
413 motor & 0x7F,
414 offset & 0x7F,
415 length & 0x7F,
416 ]
417 return self._send_command_and_read_response(cmd, length)
418
419 def get_var_u8(self, motor, offset):
420 """
421 Reads one byte from the Motoron using a "Get variables" command
422 and returns the result as an unsigned 8-bit integer.
423
424 @param motor 0 to read a general variable, or a motor number to read
425 a motor-specific variable.
426 @param offset The location of the byte to read.
427 """
428 return self.get_variables(motor, offset, 1)[0]
429
430 def get_var_u16(self, motor, offset):
431 """
432 Reads two bytes from the Motoron using a "Get variables" command
433 and returns the result as an unsigned 16-bit integer.
434
435 @param motor 0 to read general variables, or a motor number to read
436 motor-specific variables.
437 @param offset The location of the first byte to read.
438 """
439 buffer = self.get_variables(motor, offset, 2)
440 return struct.unpack('<H', buffer)[0]
441
442 def get_var_s16(self, motor, offset):
443 """
444 Reads two bytes from the Motoron using a "Get variables" command
445 and returns the result as a signed 16-bit integer.
446
447 @param motor 0 to read general variables, or a motor number to read
448 motor-specific variables.
449 @param offset The location of the first byte to read.
450 """
451 buffer = self.get_variables(motor, offset, 2)
452 return struct.unpack('<h', buffer)[0]
453
455 """
456 Reads the "Status flags" variable from the Motoron.
457
458 The bits in this variable are defined by the STATUS_FLAGS_*
459 constants in the motoron package:
460
461 - motoron.STATUS_FLAG_PROTOCOL_ERROR
462 - motoron.STATUS_FLAG_CRC_ERROR
463 - motoron.STATUS_FLAG_COMMAND_TIMEOUT_LATCHED
464 - motoron.STATUS_FLAG_MOTOR_FAULT_LATCHED
465 - motoron.STATUS_FLAG_NO_POWER_LATCHED
466 - motoron.STATUS_FLAG_RESET
467 - motoron.STATUS_FLAG_COMMAND_TIMEOUT
468 - motoron.STATUS_FLAG_MOTOR_FAULTING
469 - motoron.STATUS_FLAG_NO_POWER
470 - motoron.STATUS_FLAG_ERROR_ACTIVE
471 - motoron.STATUS_FLAG_MOTOR_OUTPUT_ENABLED
472 - motoron.STATUS_FLAG_MOTOR_DRIVING
473
474 Here is some example code that uses bitwise operators to check
475 whether there is currently a motor fault or a lack of power:
476
477 ```{.py}
478 mask = ((1 << motoron.STATUS_FLAG_NO_POWER) |
479 (1 << motoron.STATUS_FLAG_MOTOR_FAULTING))
480 if mc.get_status_flags() & mask: # do something
481 ```
482
483 This library has helper methods that make it easier if you just want to
484 read a single bit:
485
486 - get_protocol_error_flag()
487 - get_crc_error_flag()
488 - get_command_timeout_latched_flag()
489 - get_motor_fault_latched_flag()
490 - get_no_power_latched_flag()
491 - get_reset_flag()
492 - get_motor_faulting_flag()
493 - get_no_power_flag()
494 - get_error_active_flag()
495 - get_motor_output_enabled_flag()
496 - get_motor_driving_flag()
497
498 The clear_latched_status_flags() method sets the specified set of latched
499 status flags to 0. The reinitialize() and reset() commands reset the
500 latched status flags to their default values.
501
502 For more information, see the "Status flags" variable in the Motoron
503 user's guide.
504 """
505 return self.get_var_u16(0, VAR_STATUS_FLAGS)
506
508 """
509 Returns the "Protocol error" bit from get_status_flags().
510
511 For more information, see the "Status flags" variable in the Motoron
512 user's guide.
513 """
514 return bool(self.get_status_flags() & (1 << STATUS_FLAG_PROTOCOL_ERROR))
515
517 """
518 Returns the "CRC error" bit from get_status_flags().
519
520 For more information, see the "Status flags" variable in the Motoron
521 user's guide.
522 """
523 return bool(self.get_status_flags() & (1 << STATUS_FLAG_CRC_ERROR))
524
526 """
527 Returns the "Command timeout latched" bit from get_status_flags().
528
529 For more information, see the "Status flags" variable in the Motoron
530 user's guide.
531 """
532 return bool(self.get_status_flags() & (1 << STATUS_FLAG_COMMAND_TIMEOUT_LATCHED))
533
535 """
536 Returns the "Motor fault latched" bit from get_status_flags().
537
538 For more information, see the "Status flags" variable in the Motoron
539 user's guide.
540 """
541 return bool(self.get_status_flags() & (1 << STATUS_FLAG_MOTOR_FAULT_LATCHED))
542
544 """
545 Returns the "No power latched" bit from get_status_flags().
546
547 For more information, see the "Status flags" variable in the Motoron
548 user's guide.
549 """
550 return bool(self.get_status_flags() & (1 << STATUS_FLAG_NO_POWER_LATCHED))
551
552 def get_reset_flag(self):
553 """
554 Returns the "Reset" bit from get_status_flags().
555
556 This bit is set to 1 when the Motoron powers on, its processor is
557 reset (e.g. by reset()), or it receives a reinitialize() command.
558 It can be cleared using clear_reset_flag() or clear_latched_status_flags().
559
560 By default, the Motoron is configured to treat this bit as an error,
561 so you will need to clear it before you can turn on the motors.
562
563 For more information, see the "Status flags" variable in the Motoron
564 user's guide.
565 """
566 return bool(self.get_status_flags() & (1 << STATUS_FLAG_RESET))
567
569 """
570 Returns the "Motor faulting" bit from get_status_flags().
571
572 For more information, see the "Status flags" variable in the Motoron
573 user's guide.
574 """
575 return bool(self.get_status_flags() & (1 << STATUS_FLAG_MOTOR_FAULTING))
576
578 """
579 Returns the "No power" bit from get_status_flags().
580
581 For more information, see the "Status flags" variable in the Motoron
582 user's guide.
583 """
584 return bool(self.get_status_flags() & (1 << STATUS_FLAG_NO_POWER))
585
587 """
588 Returns the "Error active" bit from get_status_flags().
589
590 For more information, see the "Status flags" variable in the Motoron
591 user's guide.
592 """
593 return bool(self.get_status_flags() & (1 << STATUS_FLAG_ERROR_ACTIVE))
594
596 """
597 Returns the "Motor output enabled" bit from get_status_flags().
598
599 For more information, see the "Status flags" variable in the Motoron
600 user's guide.
601 """
602 return bool(self.get_status_flags() & (1 << STATUS_FLAG_MOTOR_OUTPUT_ENABLED))
603
605 """
606 Returns the "Motor driving" bit from get_status_flags().
607
608 For more information, see the "Status flags" variable in the Motoron
609 user's guide.
610 """
611 return bool(self.get_status_flags() & (1 << STATUS_FLAG_MOTOR_DRIVING))
612
614 """
615 Reads voltage on the Motoron's VIN pin, in raw device units.
616
617 For more information, see the "VIN voltage" variable in the Motoron
618 user's guide.
619
620 @sa get_vin_voltage_mv()
621 """
622 return self.get_var_u16(0, VAR_VIN_VOLTAGE)
623
624 def get_vin_voltage_mv(self, reference_mv=3300, type=VinSenseType.MOTORON_256):
625 """
626 Reads the voltage on the Motoron's VIN pin and converts it to millivolts.
627
628 For more information, see the "VIN voltage" variable in the Motoron
629 user's guide.
630
631 @param reference_mv The logic voltage of the Motoron, in millivolts.
632 This is assumed to be 3300 by default.
633 @param type Specifies what type of Motoron you are using. This should be one
634 of the members of the motoron.VinSenseType enum.
635
636 @sa get_vin_voltage()
637 """
638 scale = 459 if enum_value(type) & 1 else 1047
639 return self.get_vin_voltage() * reference_mv / 1024 * scale / 47
640
642 """
643 Reads the "Command timeout" variable and converts it to milliseconds.
644
645 For more information, see the "Command timeout" variable in the Motoron
646 user's guide.
647
648 @sa set_command_timeout_milliseconds()
649 """
650 return self.get_var_u16(0, VAR_COMMAND_TIMEOUT) * 4
651
653 """
654 Reads the "Error response" variable, which defines how the Motoron will
655 stop its motors when an error is happening.
656
657 For more information, see the "Error response" variable in the Motoron
658 user's guide.
659
660 @sa set_error_response()
661 """
662 return self.get_var_u8(0, VAR_ERROR_RESPONSE)
663
664 def get_error_mask(self):
665 """
666 Reads the "Error mask" variable, which defines which status flags are
667 considered to be errors.
668
669 For more information, see the "Error mask" variable in the Motoron
670 user's guide.
671
672 @sa set_error_mask()
673 """
674 return self.get_var_u16(0, VAR_ERROR_MASK)
675
677 """
678 Reads the "Jumper state" variable.
679
680 For more information, see the "Jumper state" variable in the Motoron
681 user's guide
682 """
683 return self.get_var_u8(0, VAR_JUMPER_STATE)
684
685 def get_target_speed(self, motor):
686 """
687 Reads the target speed of the specified motor, which is the speed at
688 which the motor has been commanded to move.
689
690 For more information, see the "Target speed" variable in the Motoron
691 user's guide.
692
693 @sa set_speed(), set_all_speeds(), set_all_speeds_using_buffers()
694 """
695 return self.get_var_s16(motor, MVAR_TARGET_SPEED)
696
697 def get_target_brake_amount(self, motor):
698 """
699 Reads the target brake amount for the specified motor.
700
701 For more information, see the "Target speed" variable in the Motoron
702 user's guide.
703
704 @sa set_target_brake_amount()
705 """
706 return self.get_var_u16(motor, MVAR_TARGET_BRAKE_AMOUNT)
707
708 def get_current_speed(self, motor):
709 """
710 Reads the current speed of the specified motor, which is the speed that
711 the Motoron is currently trying to apply to the motor.
712
713 For more information, see the "Target speed" variable in the Motoron
714 user's guide.
715
716 @sa set_speed_now(), set_all_speeds_now(), set_all_speeds_now_using_buffers()
717 """
718 return self.get_var_s16(motor, MVAR_CURRENT_SPEED)
719
720 def get_buffered_speed(self, motor):
721 """
722 Reads the buffered speed of the specified motor.
723
724 For more information, see the "Buffered speed" variable in the Motoron
725 user's guide.
726
727 @sa set_buffered_speed(), set_all_buffered_speeds()
728 """
729 return self.get_var_s16(motor, MVAR_BUFFERED_SPEED)
730
731 def get_pwm_mode(self, motor):
732 """
733 Reads the PWM mode of the specified motor.
734
735 For more information, see the "PWM mode" variable in the Motoron
736 user's guide.
737
738 @sa set_pwm_mode()
739 """
740 return self.get_var_u8(motor, MVAR_PWM_MODE)
741
743 """
744 Reads the maximum acceleration of the specified motor for the forward
745 direction.
746
747 For more information, see the "Max acceleration forward" variable in the
748 Motoron user's guide.
749
750 @sa set_max_acceleration(), set_max_acceleration_forward()
751 """
752 return self.get_var_u16(motor, MVAR_MAX_ACCEL_FORWARD)
753
755 """
756 Reads the maximum acceleration of the specified motor for the reverse
757 direction.
758
759 For more information, see the "Max acceleration reverse" variable in the
760 Motoron user's guide.
761
762 @sa set_max_acceleration(), set_max_acceleration_reverse()
763 """
764 return self.get_var_u16(motor, MVAR_MAX_ACCEL_REVERSE)
765
767 """
768 Reads the maximum deceleration of the specified motor for the forward
769 direction.
770
771 For more information, see the "Max deceleration forward" variable in the
772 Motoron user's guide.
773
774 @sa set_max_deceleration(), set_max_deceleration_forward()
775 """
776 return self.get_var_u16(motor, MVAR_MAX_DECEL_FORWARD)
777
779 """
780 Reads the maximum deceleration of the specified motor for the reverse
781 direction.
782
783 For more information, see the "Max deceleration reverse" variable in the
784 Motoron user's guide.
785
786 @sa set_max_deceleration(), set_max_deceleration_reverse()
787 """
788 return self.get_var_u16(motor, MVAR_MAX_DECEL_REVERSE)
789
790
791# @cond
792
793 # This function is used by Pololu for testing.
794 def get_max_deceleration_temporary(self, motor):
795 return self.get_var_u16(motor, MVAR_MAX_DECEL_TMP)
796
797# \endcond
798
800 """
801 Reads the starting speed for the specified motor in the forward direction.
802
803 For more information, see the "Starting speed forward" variable in the
804 Motoron user's guide.
805
806 @sa set_starting_speed(), set_starting_speed_forward()
807 """
808 return self.get_var_u16(motor, MVAR_STARTING_SPEED_FORWARD)
809
811 """
812 Reads the starting speed for the specified motor in the reverse direction.
813
814 For more information, see the "Starting speed reverse" variable in the
815 Motoron user's guide.
816
817 @sa set_starting_speed(), set_starting_speed_reverse()
818 """
819 return self.get_var_u16(motor, MVAR_STARTING_SPEED_REVERSE)
820
822 """
823 Reads the direction change delay for the specified motor in the
824 forward direction.
825
826 For more information, see the "Direction change delay forward" variable
827 in the Motoron user's guide.
828
829 @sa set_direction_change_delay(), set_direction_change_delay_forward()
830 """
831 return self.get_var_u8(motor, MVAR_DIRECTION_CHANGE_DELAY_FORWARD)
832
834 """
835 Reads the direction change delay for the specified motor in the
836 reverse direction.
837
838 For more information, see the "Direction change delay reverse" variable
839 in the Motoron user's guide.
840
841 @sa set_direction_change_delay(), set_direction_change_delay_reverse()
842 """
843 return self.get_var_u8(motor, MVAR_DIRECTION_CHANGE_DELAY_REVERSE)
844
845 def get_current_limit(self, motor):
846 """
847 Reads the current limit for the specified motor.
848
849 This only works for the high-power Motorons.
850
851 For more information, see the "Current limit" variable in the Motoron user's
852 guide.
853
854 @sa set_current_limit()
855 """
856 return self.get_var_u16(motor, MVAR_CURRENT_LIMIT)
857
859 """
860 Reads all the results from the last current sense measurement for the
861 specified motor.
862
863 This function reads the "Current sense raw", "Current sense speed", and
864 "Current sense processed" variables from the Motoron using a single
865 command, so the values returned are all guaranteed to be part of the
866 same measurement.
867
868 This only works for the Motorons with current sensing.
869
870 @sa get_current_sense_raw_and_speed(), get_current_sense_processed_and_speed()
871 """
872 buffer = self.get_variables(motor, MVAR_CURRENT_SENSE_RAW, 6)
873 raw, speed, processed = struct.unpack('<HhH', buffer)
874 return { 'raw': raw, 'speed': speed, 'processed': processed }
875
877 """
878 This is like get_current_sense_reading() but it only reads the raw current
879 sense measurement and the speed.
880
881 This only works for the Motorons with current sensing.
882 """
883 buffer = self.get_variables(motor, MVAR_CURRENT_SENSE_RAW, 4)
884 raw, speed = struct.unpack('<Hh', buffer)
885 return { 'raw': raw, 'speed': speed }
886
888 """
889 This is like get_current_sense_reading() but it only reads the processed
890 current sense measurement and the speed.
891
892 This only works for the Motorons with current sensing.
893 """
894 buffer = self.get_variables(motor, MVAR_CURRENT_SENSE_SPEED, 4)
895 speed, processed = struct.unpack('<hH', buffer)
896 return { 'speed': speed, 'processed': processed }
897
898 def get_current_sense_raw(self, motor):
899 """
900 Reads the raw current sense measurement for the specified motor.
901
902 This only works for the Motorons with current sensing.
903
904 For more information, see the "Current sense raw" variable
905 in the Motoron user's guide.
906
907 @sa get_current_sense_reading()
908 """
909 return self.get_var_u16(motor, MVAR_CURRENT_SENSE_RAW)
910
911
913 """
914 Reads the processed current sense reading for the specified motor.
915
916 This only works for the Motorons with current sensing.
917
918 The units of this reading depend on the logic voltage of the Motoron
919 and on the specific model of Motoron that you have, and you can use
920 current_sense_units_milliamps() to calculate the units.
921
922 For the high-power Motorons, the accuracy of this reading can be
923 improved by measuring the current sense offset and setting it with
924 set_current_sense_offset().
925 See the "Current sense processed" variable in the Motoron user's guide for
926 or the current_sense_calibrate example for more information.
927
928 Note that this reading will be 0xFFFF if an overflow happens during the
929 calculation due to very high current.
930
931 @sa get_current_sense_processed_and_speed()
932 """
933 return self.get_var_u16(motor, MVAR_CURRENT_SENSE_PROCESSED)
934
935 def get_current_sense_offset(self, motor):
936 """
937 Reads the current sense offset setting.
938
939 This only works for the high-power Motorons.
940
941 For more information, see the "Current sense offset" variable in the
942 Motoron user's guide.
943
944 @sa set_current_sense_offset()
945 """
946 return self.get_var_u8(motor, MVAR_CURRENT_SENSE_OFFSET)
947
949 """
950 Reads the current sense minimum divisor setting and returns it as a speed
951 between 0 and 800.
952
953 This only works for the high-power Motorons.
954
955 For more information, see the "Current sense minimum divisor" variable in
956 the Motoron user's guide.
957
958 @sa set_current_sense_minimum_divisor()
959 """
960 return self.get_var_u8(motor, MVAR_CURRENT_SENSE_MINIMUM_DIVISOR) << 2
961
962
963 def set_variable(self, motor, offset, value):
964 """
965 Configures the Motoron using a "Set variable" command.
966
967 This library has helper methods to set every variable, so you should
968 not need to call this function directly.
969
970 @param motor 0 to set a general variable, or a motor number to set
971 motor-specific variables.
972 @param offset The address of the variable to set (only certain offsets
973 are allowed).
974 @param value The value to set the variable to.
975
976 @sa get_variables()
977 """
978 if value > 0x3FFF: value = 0x3FFF
979 cmd = [
980 CMD_SET_VARIABLE,
981 motor & 0x1F,
982 offset & 0x7F,
983 value & 0x7F,
984 (value >> 7) & 0x7F,
985 ]
986 self._send_command(cmd)
987
989 """
990 Sets the command timeout period, in milliseconds.
991
992 For more information, see the "Command timeout" variable
993 in the Motoron user's guide.
994
995 @sa disable_command_timeout(), get_command_timeout_milliseconds()
996 """
997 # Divide by 4, but round up.
998 timeout = math.ceil(ms / 4)
999 self.set_variable(0, VAR_COMMAND_TIMEOUT, timeout)
1000
1001 def set_error_response(self, response):
1002 """
1003 Sets the error response, which defines how the Motoron will
1004 stop its motors when an error is happening.
1005
1006 The response parameter should be one of these constants from the motoron
1007 package:
1008
1009 - motoron.ERROR_RESPONSE_COAST
1010 - motoron.ERROR_RESPONSE_BRAKE
1011 - motoron.ERROR_RESPONSE_COAST_NOW
1012 - motoron.ERROR_RESPONSE_BRAKE_NOW
1013
1014 For more information, see the "Error response" variable in the Motoron
1015 user's guide.
1016
1017 @sa get_error_response()
1018 """
1019 self.set_variable(0, VAR_ERROR_RESPONSE, response)
1020
1021 def set_error_mask(self, mask):
1022 """
1023 Sets the "Error mask" variable, which defines which status flags are
1024 considered to be errors.
1025
1026 For more information, see the "Error mask" variable in the Motoron
1027 user's guide.
1028
1029 @sa get_error_mask(), get_status_flags()
1030 """
1031 self.set_variable(0, VAR_ERROR_MASK, mask)
1032
1034 """
1035 This disables the Motoron's command timeout feature by resetting the
1036 the "Error mask" variable to its default value but with the command
1037 timeout bit cleared.
1038
1039 By default, the Motoron's command timeout will occur if no valid commands
1040 are received in 1500 milliseconds, and the command timeout is treated as
1041 an error, so the motors will shut down. You can use this function if you
1042 want to disable that feature.
1043
1044 Note that this function overrides any previous values you set in the
1045 "Error mask" variable, so if you are using set_error_mask() in your program
1046 to configure which status flags are treated as errors, you do not need to
1047 use this function and you probably should not use this function.
1048
1049 @sa set_command_timeout_milliseconds(), set_error_mask()
1050 """
1051 self.set_error_mask(MotoronBase.DEFAULT_ERROR_MASK & ~(1 << STATUS_FLAG_COMMAND_TIMEOUT))
1052
1053 def set_pwm_mode(self, motor, mode):
1054 """
1055 Sets the PWM mode for the specified motor.
1056
1057 The mode parameter should be one of the following these constants from
1058 the motoron package:
1059
1060 - motoron.PWM_MODE_DEFAULT (20 kHz)
1061 - motoron.PWM_MODE_1_KHZ 1
1062 - motoron.PWM_MODE_2_KHZ 2
1063 - motoron.PWM_MODE_4_KHZ 3
1064 - motoron.PWM_MODE_5_KHZ 4
1065 - motoron.PWM_MODE_10_KHZ 5
1066 - motoron.PWM_MODE_20_KHZ 6
1067 - motoron.PWM_MODE_40_KHZ 7
1068 - motoron.PWM_MODE_80_KHZ 8
1069
1070 For more information, see the "PWM mode" variable in the Motoron user's
1071 guide.
1072
1073 @sa get_pwm_mode(self)
1074 """
1075 self.set_variable(motor, MVAR_PWM_MODE, mode)
1076
1077 def set_max_acceleration_forward(self, motor, accel):
1078 """
1079 Sets the maximum acceleration of the specified motor for the forward
1080 direction.
1081
1082 For more information, see the "Max acceleration forward" variable in the
1083 Motoron user's guide.
1084
1085 @sa set_max_acceleration(), get_max_acceleration_forward()
1086 """
1087 self.set_variable(motor, MVAR_MAX_ACCEL_FORWARD, accel)
1088
1089 def set_max_acceleration_reverse(self, motor, accel):
1090 """
1091 Sets the maximum acceleration of the specified motor for the reverse
1092 direction.
1093
1094 For more information, see the "Max acceleration reverse" variable in the
1095 Motoron user's guide.
1096
1097 @sa set_max_acceleration(), get_max_acceleration_reverse()
1098 """
1099 self.set_variable(motor, MVAR_MAX_ACCEL_REVERSE, accel)
1100
1101 def set_max_acceleration(self, motor, accel):
1102 """
1103 Sets the maximum acceleration of the specified motor (both directions).
1104
1105 If this function succeeds, it is equivalent to calling
1106 set_max_acceleration_forward() and set_max_acceleration_reverse().
1107 """
1108 self.set_max_acceleration_forward(motor, accel)
1109 self.set_max_acceleration_reverse(motor, accel)
1110
1111 def set_max_deceleration_forward(self, motor, decel):
1112 """
1113 Sets the maximum deceleration of the specified motor for the forward
1114 direction.
1115
1116 For more information, see the "Max deceleration forward" variable in the
1117 Motoron user's guide.
1118
1119 @sa set_max_deceleration(), get_max_deceleration_forward()
1120 """
1121 self.set_variable(motor, MVAR_MAX_DECEL_FORWARD, decel)
1122
1123 def set_max_deceleration_reverse(self, motor, decel):
1124 """
1125 Sets the maximum deceleration of the specified motor for the reverse
1126 direction.
1127
1128 For more information, see the "Max deceleration reverse" variable in the
1129 Motoron user's guide.
1130
1131 @sa set_max_deceleration(), get_max_deceleration_reverse()
1132 """
1133 self.set_variable(motor, MVAR_MAX_DECEL_REVERSE, decel)
1134
1135 def set_max_deceleration(self, motor, decel):
1136 """
1137 Sets the maximum deceleration of the specified motor (both directions).
1138
1139 If this function succeeds, it is equivalent to calling
1140 set_max_deceleration_forward() and set_max_deceleration_reverse().
1141 """
1142 self.set_max_deceleration_forward(motor, decel)
1143 self.set_max_deceleration_reverse(motor, decel)
1144
1145 def set_starting_speed_forward(self, motor, speed):
1146 """
1147 Sets the starting speed of the specified motor for the forward
1148 direction.
1149
1150 For more information, see the "Starting speed forward" variable in the
1151 Motoron user's guide.
1152
1153 @sa set_starting_speed(), get_starting_speed_forward()
1154 """
1155 self.set_variable(motor, MVAR_STARTING_SPEED_FORWARD, speed)
1156
1157 def set_starting_speed_reverse(self, motor, speed):
1158 """
1159 Sets the starting speed of the specified motor for the reverse
1160 direction.
1161
1162 For more information, see the "Starting speed reverse" variable in the
1163 Motoron user's guide.
1164
1165 @sa set_starting_speed(), get_starting_speed_reverse()
1166 """
1167 self.set_variable(motor, MVAR_STARTING_SPEED_REVERSE, speed)
1168
1169 def set_starting_speed(self, motor, speed):
1170 """
1171 Sets the starting speed of the specified motor (both directions).
1172
1173 If this function succeeds, it is equivalent to calling
1174 set_starting_speed_forward() and set_starting_speed_reverse().
1175 """
1176 self.set_starting_speed_forward(motor, speed)
1177 self.set_starting_speed_reverse(motor, speed)
1178
1179 def set_direction_change_delay_forward(self, motor, duration):
1180 """
1181 Sets the direction change delay of the specified motor for the forward
1182 direction, in units of 10 ms.
1183
1184 For more information, see the "Direction change delay forward" variable
1185 in the Motoron user's guide.
1186
1187 @sa set_direction_change_delay(), get_direction_change_delay_forward()
1188 """
1189 self.set_variable(motor, MVAR_DIRECTION_CHANGE_DELAY_FORWARD, duration)
1190
1191 def set_direction_change_delay_reverse(self, motor, duration):
1192 """
1193 Sets the direction change delay of the specified motor for the reverse
1194 direction, in units of 10 ms.
1195
1196 For more information, see the "Direction change delay reverse" variable
1197 in the Motoron user's guide.
1198
1199 @sa set_direction_change_delay(), get_direction_change_delay_reverse()
1200 """
1201 self.set_variable(motor, MVAR_DIRECTION_CHANGE_DELAY_REVERSE, duration)
1202
1203 def set_direction_change_delay(self, motor, duration):
1204 """
1205 Sets the direction change delay of the specified motor (both directions),
1206 in units of 10 ms.
1207
1208 If this function succeeds, it is equivalent to calling
1209 set_direction_change_delay_forward() and set_direction_change_delay_reverse().
1210 """
1211 self.set_direction_change_delay_forward(motor, duration)
1212 self.set_direction_change_delay_reverse(motor, duration)
1213
1214 def set_current_limit(self, motor, limit):
1215 """
1216 Sets the current limit for the specified motor.
1217
1218 This only works for the high-power Motorons.
1219
1220 The units of the current limit depend on the type of Motoron you have
1221 and the logic voltage of your system. See the "Current limit" variable
1222 in the Motoron user's guide for more information, or see
1223 calculate_current_limit().
1224
1225 @sa get_current_limit()
1226 """
1227 self.set_variable(motor, MVAR_CURRENT_LIMIT, limit)
1228
1229 def set_current_sense_offset(self, motor, offset):
1230 """
1231 Sets the current sense offset setting for the specified motor.
1232
1233 This is one of the settings that determines how current sense
1234 readings are processed. It is supposed to be the value returned by
1235 get_current_sense_raw() when motor power is supplied to the Motoron and
1236 it is driving its motor outputs at speed 0.
1237
1238 The current_sense_calibrate example shows how to measure the current
1239 sense offsets and load them onto the Motoron using this function.
1240
1241 If you do not care about measuring motor current, you do not need to
1242 set this variable.
1243
1244 For more information, see the "Current sense offset" variable in the
1245 Motoron user's guide.
1246
1247 This only works for the high-power Motorons.
1248
1249 @sa get_current_sense_offset()
1250 """
1251 self.set_variable(motor, MVAR_CURRENT_SENSE_OFFSET, offset)
1252
1253 def set_current_sense_minimum_divisor(self, motor, speed):
1254 """
1255 Sets the current sense minimum divisor setting for the specified motor,
1256 given a speed between 0 and 800.
1257
1258 This is one of the settings that determines how current sense
1259 readings are processed.
1260
1261 If you do not care about measuring motor current, you do not need to
1262 set this variable.
1263
1264 For more information, see the "Current sense minimum divisor" variable in
1265 the Motoron user's guide.
1266
1267 This only works for the high-power Motorons.
1268
1269 @sa get_current_sense_minimum_divisor()
1270 """
1271 self.set_variable(motor, MVAR_CURRENT_SENSE_MINIMUM_DIVISOR, speed >> 2)
1272
1273 def coast_now(self):
1274 """
1275 Sends a "Coast now" command to the Motoron, causing all of the motors to
1276 immediately start coasting.
1277
1278 For more information, see the "Coast now" command in the Motoron
1279 user's guide.
1280 """
1281 cmd = [CMD_COAST_NOW]
1282 self._send_command(cmd)
1283
1284 def clear_motor_fault(self, flags=0):
1285 """
1286 Sends a "Clear motor fault" command to the Motoron.
1287
1288 If any of the Motoron's motors chips are currently experiencing a
1289 fault (error), or bit 0 of the flags argument is 1, this command makes
1290 the Motoron attempt to recover from the faults.
1291
1292 For more information, see the "Clear motor fault" command in the Motoron
1293 user's guide.
1294
1295 @sa clear_motor_fault_unconditional(), get_motor_faulting_flag()
1296 """
1297 cmd = [ CMD_CLEAR_MOTOR_FAULT, (flags & 0x7F) ]
1298 self._send_command(cmd)
1299
1301 """
1302 Sends a "Clear motor fault" command to the Motoron with the
1303 "unconditional" flag set, so the Motoron will attempt to recover
1304 from any motor faults even if no fault is currently occurring.
1305
1306 This is a more robust version of clear_motor_fault().
1307 """
1308 self.clear_motor_fault(1 << CLEAR_MOTOR_FAULT_UNCONDITIONAL)
1309
1311 """
1312 Clears the specified flags in get_status_flags().
1313
1314 For each bit in the flags argument that is 1, this command clears the
1315 corresponding bit in the "Status flags" variable, setting it to 0.
1316
1317 For more information, see the "Clear latched status flags" command in the
1318 Motoron user's guide.
1319
1320 @sa get_status_flags(), set_latched_status_flags()
1321 """
1322 cmd = [
1323 CMD_CLEAR_LATCHED_STATUS_FLAGS,
1324 flags & 0x7F,
1325 (flags >> 7) & 0x7F,
1326 ]
1327 self._send_command(cmd)
1328
1330 """
1331 Clears the Motoron's reset flag.
1332
1333 The reset flag is a latched status flag in get_status_flags() that is
1334 particularly important to clear: it gets set to 1 after the Motoron
1335 powers on or experiences a reset, and it is considered to be an error
1336 by default, so it prevents the motors from running. Therefore, it is
1337 necessary to call this function (or clear_latched_status_flags()) to clear
1338 the Reset flag before you can get the motors running.
1339
1340 We recommend that immediately after you clear the reset flag. you should
1341 configure the Motoron's motor settings and error response settings.
1342 That way, if the Motoron experiences an unexpected reset while your system
1343 is running, it will stop running its motors and it will not start them
1344 again until all the important settings have been configured.
1345
1346 @sa clear_latched_status_flags()
1347 """
1348 self.clear_latched_status_flags(1 << STATUS_FLAG_RESET)
1349
1351 """
1352 Sets the specified flags in get_status_flags().
1353
1354 For each bit in the flags argument that is 1, this command sets the
1355 corresponding bit in the "Status flags" variable to 1.
1356
1357 For more information, see the "Set latched status flags" command in the
1358 Motoron user's guide.
1359
1360 @sa get_status_flags(), set_latched_status_flags()
1361 """
1362 cmd = [
1363 CMD_SET_LATCHED_STATUS_FLAGS,
1364 flags & 0x7F,
1365 (flags >> 7) & 0x7F,
1366 ]
1367 self._send_command(cmd)
1368
1369 def set_speed(self, motor, speed):
1370 """
1371 Sets the target speed of the specified motor.
1372
1373 The current speed will start moving to the specified target speed,
1374 obeying any acceleration and deceleration limits.
1375
1376 The motor number should be between 1 and the number of motors supported
1377 by the Motoron.
1378
1379 The speed should be between -800 and 800. Values outside that range
1380 will be clipped to -800 or 800 by the Motoron firmware.
1381
1382 For more information, see the "Set speed" command in the Motoron
1383 user's guide.
1384
1385 @sa set_speed_now(), set_all_speeds()
1386 """
1387 cmd = [
1388 CMD_SET_SPEED,
1389 motor & 0x7F,
1390 speed & 0x7F,
1391 (speed >> 7) & 0x7F,
1392 ]
1393 self._send_command(cmd)
1394
1395 def set_speed_now(self, motor, speed):
1396 """
1397 Sets the target and current speed of the specified motor, ignoring
1398 any acceleration and deceleration limits.
1399
1400 For more information, see the "Set speed" command in the Motoron
1401 user's guide.
1402
1403 @sa set_speed(), set_all_speeds_now()
1404 """
1405 cmd = [
1406 CMD_SET_SPEED_NOW,
1407 motor & 0x7F,
1408 speed & 0x7F,
1409 (speed >> 7) & 0x7F,
1410 ]
1411 self._send_command(cmd)
1412
1413 def set_buffered_speed(self, motor, speed):
1414 """
1415 Sets the buffered speed of the specified motor.
1416
1417 This command does not immediately cause any change to the motor: it
1418 stores a speed for the specified motor in the Motoron so it can be
1419 used by later commands.
1420
1421 For more information, see the "Set speed" command in the Motoron
1422 user's guide.
1423
1424 @sa set_speed(), set_all_buffered_speeds(),
1425 set_all_speeds_using_buffers(), set_all_speeds_now_using_buffers()
1426 """
1427 cmd = [
1428 CMD_SET_BUFFERED_SPEED,
1429 motor & 0x7F,
1430 speed & 0x7F,
1431 (speed >> 7) & 0x7F,
1432 ]
1433 self._send_command(cmd)
1434
1435 def set_all_speeds(self, *speeds):
1436 """
1437 Sets the target speeds of all the motors at the same time.
1438
1439 The number of speed arguments you provide to this function must be equal
1440 to the number of motor channels your Motoron has, or else this command
1441 might not work.
1442
1443 This is equivalent to calling set_speed() once for each motor, but it is
1444 more efficient because all of the speeds are sent in the same command.
1445
1446 There are a few different ways you can call this method (and the related
1447 methods that set speeds for all the motors):
1448
1449 ```{.py}
1450 # with separate arguments
1451 mc.set_all_speeds(100, -200, 300)
1452
1453 # with arguments unpacked from a list
1454 speeds = [-400, 500, -600]
1455 mc.set_all_speeds(*speeds)
1456 ```
1457
1458 For more information, see the "Set all speeds" command in the Motoron
1459 user's guide.
1460
1461 @sa set_speed(), set_all_speeds_now(), set_all_buffered_speeds()
1462 """
1463 cmd = [CMD_SET_ALL_SPEEDS]
1464 for speed in speeds:
1465 cmd += [
1466 speed & 0x7F,
1467 (speed >> 7) & 0x7F,
1468 ]
1469 self._send_command(cmd)
1470
1471 def set_all_speeds_now(self, *speeds):
1472 """
1473 Sets the target and current speeds of all the motors at the same time.
1474
1475 The number of speed arguments you provide to this function must be equal
1476 to the number of motor channels your Motoron has, or else this command
1477 might not work.
1478
1479 This is equivalent to calling set_speed_now() once for each motor, but it is
1480 more efficient because all of the speeds are sent in the same command.
1481
1482 For more information, see the "Set all speeds" command in the Motoron
1483 user's guide.
1484
1485 @sa set_speed(), set_speed_now(), set_all_speeds()
1486 """
1487 cmd = [CMD_SET_ALL_SPEEDS_NOW]
1488 for speed in speeds:
1489 cmd += [
1490 speed & 0x7F,
1491 (speed >> 7) & 0x7F,
1492 ]
1493 self._send_command(cmd)
1494
1495 def set_all_buffered_speeds(self, *speeds):
1496 """
1497 Sets the buffered speeds of all the motors.
1498
1499 The number of speed arguments you provide to this function must be equal
1500 to the number of motor channels your Motoron has, or else this command
1501 might not work.
1502
1503 This command does not immediately cause any change to the motors: it
1504 stores speeds for each motor in the Motoron so they can be used by later
1505 commands.
1506
1507 For more information, see the "Set all speeds" command in the Motoron
1508 user's guide.
1509
1510 @sa set_speed(), set_buffered_speed(), set_all_speeds(),
1511 set_all_speeds_using_buffers(), set_all_speeds_now_using_buffers()
1512 """
1513 cmd = [CMD_SET_ALL_BUFFERED_SPEEDS]
1514 for speed in speeds:
1515 cmd += [
1516 speed & 0x7F,
1517 (speed >> 7) & 0x7F,
1518 ]
1519 self._send_command(cmd)
1520
1522 """
1523 Sets each motor's target speed equal to the buffered speed.
1524
1525 This command is the same as set_all_speeds() except that the speeds are
1526 provided ahead of time using set_buffered_speed() or set_all_buffered_speeds().
1527
1528 @sa set_all_speeds_now_using_buffers(), set_buffered_speed(),
1529 set_all_buffered_speeds()
1530 """
1531 cmd = [CMD_SET_ALL_SPEEDS_USING_BUFFERS]
1532 self._send_command(cmd)
1533
1535 """
1536 Sets each motor's target speed and current speed equal to the buffered
1537 speed.
1538
1539 This command is the same as set_all_speeds_now() except that the speeds are
1540 provided ahead of time using set_buffered_speed() or set_all_buffered_speeds().
1541
1542 @sa set_all_speeds_using_buffers(), set_buffered_speed(),
1543 set_all_buffered_speeds()
1544 """
1545 cmd = [CMD_SET_ALL_SPEEDS_NOW_USING_BUFFERS]
1546 self._send_command(cmd)
1547
1548 def set_braking(self, motor, amount):
1549 """
1550 Commands the motor to brake, coast, or something in between.
1551
1552 Sending this command causes the motor to decelerate to speed 0 obeying
1553 any relevant deceleration limits. Once the current speed reaches 0, the
1554 motor will attempt to brake or coast as specified by this command, but
1555 due to hardware limitations it might not be able to.
1556
1557 The motor number parameter should be between 1 and the number of motors
1558 supported by the Motoron.
1559
1560 The amount parameter gets stored in the "Target brake amount" variable
1561 for the motor and should be between 0 (coasting) and 800 (braking).
1562 Values above 800 will be clipped to 800 by the Motoron firmware.
1563
1564 See the "Set braking" command in the Motoron user's guide for more
1565 information.
1566
1567 @sa set_braking_now(), get_target_brake_amount()
1568 """
1569 cmd = [
1570 CMD_SET_BRAKING,
1571 motor & 0x7F,
1572 amount & 0x7F,
1573 (amount >> 7) & 0x7F,
1574 ]
1575 self._send_command(cmd)
1576
1577 def set_braking_now(self, motor, amount):
1578 """
1579 Commands the motor to brake, coast, or something in between.
1580
1581 Sending this command causes the motor's current speed to change to 0.
1582 The motor will attempt to brake or coast as specified by this command,
1583 but due to hardware limitations it might not be able to.
1584
1585 The motor number parameter should be between 1 and the number of motors
1586 supported by the Motoron.
1587
1588 The amount parameter gets stored in the "Target brake amount" variable
1589 for the motor and should be between 0 (coasting) and 800 (braking).
1590 Values above 800 will be clipped to 800 by the Motoron firmware.
1591
1592 See the "Set braking" command in the Motoron user's guide for more
1593 information.
1594
1595 @sa set_braking(), get_target_brake_amount()
1596 """
1597 cmd = [
1598 CMD_SET_BRAKING_NOW,
1599 motor & 0x7F,
1600 amount & 0x7F,
1601 (amount >> 7) & 0x7F,
1602 ]
1603 self._send_command(cmd)
1604
1606 """
1607 Resets the command timeout.
1608
1609 This prevents the command timeout status flags from getting set for some
1610 time. (The command timeout is also reset by every other Motoron command,
1611 as long as its parameters are valid.)
1612
1613 For more information, see the "Reset command timeout" command in the
1614 Motoron user's guide.
1615
1616 @sa disable_command_timeout(), set_command_timeout_milliseconds()
1617 """
1618 cmd = [CMD_RESET_COMMAND_TIMEOUT]
1619 self._send_command(cmd)
1620
1621 def _send_command(self, cmd):
1622 send_crc = bool(self.protocol_options & (1 << PROTOCOL_OPTION_CRC_FOR_COMMANDS))
1623 self._send_command_core(cmd, send_crc)
1624
1625 def _send_command_and_read_response(self, cmd, response_length):
1626 self._send_command(cmd)
1627 return self._read_response(response_length)
1628
1629def calculate_current_limit(milliamps, type, reference_mv, offset):
1630 """
1631 Calculates a current limit value that can be passed to the Motoron
1632 using set_current_limit().
1633
1634 @param milliamps The desired current limit, in units of mA.
1635 @param type Specifies what type of Motoron you are using. This should be one
1636 of the members of the motoron.CurrentSenseType enum.
1637 @param reference_mv The reference voltage (IOREF), in millivolts.
1638 For example, use 3300 for a 3.3 V system or 5000 for a 5 V system.
1639 @param offset The offset of the raw current sense signal for the Motoron
1640 channel. This is the same measurement that you would put into the
1641 Motoron's "Current sense offset" variable using set_current_sense_offset(),
1642 so see the documentation of that function for more info.
1643 The offset is typically 10 for 5 V systems and 15 for 3.3 V systems,
1644 (50*1024/reference_mv) but it can vary widely.
1645 """
1646 if milliamps > 1000000: milliamps = 1000000
1647 limit = offset * 125 / 128 + milliamps * 20 / (reference_mv * (enum_value(type) & 3))
1648 if limit > 1000: limit = 1000
1649 return math.floor(limit)
1650
1651def current_sense_units_milliamps(type, reference_mv):
1652 """
1653 Calculates the units for the Motoron's current sense reading returned by
1654 get_current_sense_processed(), in milliamps.
1655
1656 To convert a reading from get_current_sense_processed() to milliamps,
1657 multiply it by the value returned from this function.
1658
1659 @param type Specifies what type of Motoron you are using. This should be one
1660 of the members of the motoron.CurrentSenseType enum.
1661 @param reference_mv The reference voltage (IOREF), in millivolts.
1662 For example, use 3300 for a 3.3 V system or 5000 for a 5 V system.
1663 """
1664 if type == CurrentSenseType.MOTORON_453:
1665 return reference_mv * 5 / 8448
1666 else:
1667 return reference_mv * (enum_value(type) & 3) * 25 / 512
1668
1670 """
1671 Represents an I2C connection to a Pololu Motoron Motor Controller.
1672 """
1673
1674 def __init__(self, *, bus=1, address=16):
1675 """
1676 Creates a new MotoronI2C object to communicate with the Motoron over I2C.
1677
1678 @param bus Optional argument that specifies which I2C bus to use.
1679 This can be an integer, an SMBus object from the smbus2 package, or an
1680 object with an interface similar to SMBus.
1681 The default bus is 1, which corresponds to `/dev/i2c-1`.
1682 @param address Optional argument that specifies the 7-bit I2C address to
1683 use. This must match the address that the Motoron is configured to use.
1684 The default address is 16.
1685 """
1686 super().__init__()
1687
1688 self.set_bus(bus)
1689
1690
1691 self.address = address
1692
1693 """
1694 Configures this object to use the specified I2C bus object.
1695
1696 The bus argument should be one of the following:
1697 - The number of an I2C bus to open with smbus2
1698 (e.g. 2 for `/dev/i2c-2`)
1699 - An SMBus object from smbus2.
1700 - A machine.I2C object from MicroPython.
1701 """
1702 def set_bus(self, bus):
1703 if isinstance(bus, int):
1704 import smbus2
1705 bus = smbus2.SMBus(bus)
1706
1707 try:
1708 bus.i2c_rdwr
1709 type_is_smbus = True
1710 except AttributeError:
1711 type_is_smbus = False
1712
1713 if type_is_smbus:
1716 import smbus2
1717 self._msg = smbus2.i2c_msg
1718 else:
1721
1722 self.bus = bus
1723
1724 def _smbus_send_command_core(self, cmd, send_crc):
1725 if send_crc:
1726 write = self._msg.write(self.address, cmd + [calculate_crc(cmd)])
1727 else:
1728 write = self._msg.write(self.address, cmd)
1729 self.bus.i2c_rdwr(write)
1730
1731 def _smbus_read_response(self, length):
1732 # On some Raspberry Pis with buggy implementations of I2C clock stretching,
1733 # sleeping for 0.5 ms might be necessary in order to give the Motoron time
1734 # to prepare its response, so it does not need to stretch the clock.
1735 time.sleep(0.0005)
1736
1737 crc_enabled = bool(self.protocol_options & (1 << PROTOCOL_OPTION_CRC_FOR_RESPONSES))
1738 read = self._msg.read(self.address, length + crc_enabled)
1739 self.bus.i2c_rdwr(read)
1740 response = bytes(read)
1741 if crc_enabled:
1742 crc = response[-1]
1743 response = response[:-1]
1744 if crc != calculate_crc(response):
1745 raise RuntimeError('Incorrect CRC received.')
1746 return response
1747
1748 def _mpy_send_command_core(self, cmd, send_crc):
1749 if send_crc:
1750 self.bus.writeto(self.address, bytes(cmd + [calculate_crc(cmd)]))
1751 else:
1752 self.bus.writeto(self.address, bytes(cmd))
1753
1754 def _mpy_read_response(self, length):
1755 crc_enabled = bool(self.protocol_options & (1 << PROTOCOL_OPTION_CRC_FOR_RESPONSES))
1756 response = self.bus.readfrom(self.address, length + crc_enabled)
1757 if crc_enabled:
1758 crc = response[-1]
1759 response = response[:-1]
1760 if crc != calculate_crc(response):
1761 raise RuntimeError('Incorrect CRC received.')
1762 return response
1763
1764
1766 """
1767 Represents a serial connection to a Pololu Motoron Motor Controller.
1768 """
1769
1770 def __init__(self, *, port=None, device_number=None):
1771 """
1772 Creates a new MotoronSerial object.
1773
1774 The `deviceNumber` argument is optional. If it is omitted or None,
1775 the object will use the compact protocol.
1776
1777 The `port` argument specifies the serial port to use and is passed
1778 directly to set_port().
1779 """
1780 super().__init__()
1781
1782 self.set_port(port)
1783
1784
1787 self.device_number = device_number
1788
1789
1799
1800 def set_port(self, port):
1801 """
1802 Configures this object to use the specified serial port object.
1803
1804 The port argument should be one of the following:
1805 - The name of a serial port to open with pyserial
1806 (e.g. "COM6" or "/dev/ttyS0")
1807 - A Serial object from pyserial.
1808 - A machine.UART object from MicroPython.
1809 """
1810 if isinstance(port, str):
1811 import serial
1812 self.port = serial.Serial(port, 115200, timeout=0.1, write_timeout=0.1)
1813 else:
1814
1815 self.port = port
1816
1818 """
1819 Configures this object to work with Motorons that are configured to send
1820 7-bit serial responses.
1821 """
1822 self.communication_options |= (1 << COMMUNICATION_OPTION_7BIT_RESPONSES)
1823
1825 """
1826 Configures this object to work with Motorons that are configured to send
1827 responses in the normal 8-bit format.
1828 """
1829 self.communication_options &= ~(1 << COMMUNICATION_OPTION_7BIT_RESPONSES)
1830
1832 """
1833 Configures this object to send 14-bit device numbers when using the
1834 Pololu protocol, instead of the default 7-bit.
1835 """
1836 self.communication_options |= (1 << COMMUNICATION_OPTION_14BIT_DEVICE_NUMBER)
1837
1839 """
1840 Configures this object to send 7-bit device numbers, which is the default.
1841 """
1842 self.communication_options &= ~(1 << COMMUNICATION_OPTION_14BIT_DEVICE_NUMBER)
1843
1844 def multi_device_error_check_start(self, starting_device_number, device_count):
1845 """
1846 Sends a "Multi-device error check" command but does not read any
1847 responses.
1848
1849 Note: Before using this, most users should make sure the MotoronSerial
1850 object is configured to use the compact protocol: construct the object
1851 without specifying a device number, or set device_number to None.
1852 """
1853 if self.communication_options & (1 << COMMUNICATION_OPTION_14BIT_DEVICE_NUMBER):
1854 if device_count < 0 or device_count > 0x3FFF:
1855 raise RuntimeError('Invalid device count.')
1856 cmd = [
1857 CMD_MULTI_DEVICE_ERROR_CHECK,
1858 starting_device_number & 0x7F,
1859 starting_device_number >> 7 & 0x7F,
1860 device_count & 0x7F,
1861 device_count >> 7 & 0x7F,
1862 ]
1863 else:
1864 if device_count < 0 or device_count > 0x7F:
1865 raise RuntimeError('Invalid device count.')
1866 cmd = [
1867 CMD_MULTI_DEVICE_ERROR_CHECK,
1868 starting_device_number & 0x7F,
1869 device_count,
1870 ]
1871
1872 self._send_command(cmd)
1873 self.port.flush()
1874
1875 def multi_device_error_check(self, starting_device_number, device_count):
1876 """
1877 Sends a "Multi-device error check" command and reads the responses.
1878
1879 This function assumes that each addressed Motoron can see the responses
1880 sent by the other Motorons (e.g. they are in a half-duplex RS-485 network).
1881
1882 Returns the number of devices that indicated they have no errors.
1883 If the return value is less than device count, you can add the return
1884 value to the starting_device_number to get the device number of the
1885 first device where the check failed. This device either did not
1886 respond or it responded with an indication that it has an error, or an
1887 unexpected byte was received for some reason.
1888
1889 Note: Before using this, most users should make sure the MotoronSerial
1890 object is configured to use the compact protocol: construct the object
1891 without specifying a device number, or set device_number to None.
1892 """
1893 self.multi_device_error_check_start(starting_device_number, device_count)
1894 responses = self.port.read(device_count)
1895 for i, v in enumerate(responses):
1896 if v != ERROR_CHECK_CONTINUE: return i
1897 return len(responses)
1898
1899 def multi_device_write(self, starting_device_number, device_count,
1900 command_byte, data):
1901 """
1902 Sends a "Multi-device write" command.
1903
1904 Note: Before using this, most users should make sure the MotoronSerial
1905 object is configured to use the compact protocol: construct the object
1906 without specifying a device number, or call setDeviceNumber with an
1907 argument of 0xFFFF.
1908 """
1909
1910 if bool(self.communication_options & (1 << COMMUNICATION_OPTION_14BIT_DEVICE_NUMBER)):
1911 if device_count < 0 or device_count > 0x3FFF:
1912 raise RuntimeError('Invalid device count.')
1913 cmd = [
1914 CMD_MULTI_DEVICE_WRITE,
1915 starting_device_number & 0x7F,
1916 starting_device_number >> 7 & 0x7F,
1917 device_count & 0x7F,
1918 device_count >> 7 & 0x7F,
1919 ]
1920 else:
1921 if device_count < 0 or device_count > 0x7F:
1922 raise RuntimeError('Invalid device count.')
1923 cmd = [
1924 CMD_MULTI_DEVICE_WRITE,
1925 starting_device_number & 0x7F,
1926 device_count,
1927 ]
1928
1929 if data == None: data = []
1930 if len(data) % device_count:
1931 raise RuntimeError("Expected data length to be a multiple of " \
1932 f"{device_count}, got {len(data)}.")
1933 bytes_per_device = len(data) // device_count
1934 if bytes_per_device > 15: raise RuntimeError('Data too long.')
1935
1936 cmd += [bytes_per_device, command_byte & 0x7F]
1937 cmd += data
1938
1939 self._send_command(cmd)
1940
1941 def _send_command_core(self, cmd, send_crc):
1942 if self.device_number != None:
1943 if self.communication_options & (1 << COMMUNICATION_OPTION_14BIT_DEVICE_NUMBER):
1944 cmd = [
1945 0xAA,
1946 self.device_number & 0x7F,
1947 self.device_number >> 7 & 0x7F,
1948 cmd[0] & 0x7F
1949 ] + cmd[1:]
1950 else:
1951 cmd = [
1952 0xAA,
1953 self.device_number & 0x7F,
1954 cmd[0] & 0x7F
1955 ] + cmd[1:]
1956
1957 if send_crc: cmd += [calculate_crc(cmd)]
1958
1959 self.port.write(bytes(cmd))
1960
1961 def _read_response(self, length):
1962 crc_enabled = bool(self.protocol_options & (1 << PROTOCOL_OPTION_CRC_FOR_RESPONSES))
1963 response_7bit = bool(self.communication_options & (1 << COMMUNICATION_OPTION_7BIT_RESPONSES))
1964
1965 if response_7bit and length > 7:
1966 raise RuntimeError('The Motoron does not support response payloads ' \
1967 'longer than 7 bytes in 7-bit response mode.')
1968
1969 self.port.flush()
1970 read_length = length + response_7bit + crc_enabled
1971 response = self.port.read(read_length)
1972 if response is None: response = b''
1973 if len(response) != read_length:
1974 raise RuntimeError(f"Expected to read {read_length} bytes, got {len(response)}.")
1975
1976 if crc_enabled:
1977 crc = response[-1]
1978 response = response[:-1]
1979 if crc != calculate_crc(response):
1980 raise RuntimeError('Incorrect CRC received.')
1981
1982 if response_7bit:
1983 msbs = response[-1]
1984 response = bytearray(response[:-1])
1985 for i in range(length):
1986 if msbs & 1: response[i] |= 0x80
1987 msbs >>= 1
1988 response = bytes(response)
1989
1990 return response
Represents a connection to a Pololu Motoron Motoron Controller.
Definition motoron.py:38
get_vin_voltage_mv(self, reference_mv=3300, type=VinSenseType.MOTORON_256)
Reads the voltage on the Motoron's VIN pin and converts it to millivolts.
Definition motoron.py:624
get_var_u16(self, motor, offset)
Reads two bytes from the Motoron using a "Get variables" command and returns the result as an unsigne...
Definition motoron.py:430
get_pwm_mode(self, motor)
Reads the PWM mode of the specified motor.
Definition motoron.py:731
set_all_speeds_now(self, *speeds)
Sets the target and current speeds of all the motors at the same time.
Definition motoron.py:1471
get_error_response(self)
Reads the "Error response" variable, which defines how the Motoron will stop its motors when an error...
Definition motoron.py:652
get_target_speed(self, motor)
Reads the target speed of the specified motor, which is the speed at which the motor has been command...
Definition motoron.py:685
get_max_deceleration_reverse(self, motor)
Reads the maximum deceleration of the specified motor for the reverse direction.
Definition motoron.py:778
set_current_sense_offset(self, motor, offset)
Sets the current sense offset setting for the specified motor.
Definition motoron.py:1229
write_eeprom_disable_alternative_device_number(self)
Writes to EEPROM to disable the alternative device number.
Definition motoron.py:290
set_max_deceleration(self, motor, decel)
Sets the maximum deceleration of the specified motor (both directions).
Definition motoron.py:1135
get_crc_error_flag(self)
Returns the "CRC error" bit from get_status_flags().
Definition motoron.py:516
set_direction_change_delay(self, motor, duration)
Sets the direction change delay of the specified motor (both directions), in units of 10 ms.
Definition motoron.py:1203
get_current_sense_raw_and_speed(self, motor)
This is like get_current_sense_reading() but it only reads the raw current sense measurement and the ...
Definition motoron.py:876
set_variable(self, motor, offset, value)
Configures the Motoron using a "Set variable" command.
Definition motoron.py:963
get_current_sense_reading(self, motor)
Reads all the results from the last current sense measurement for the specified motor.
Definition motoron.py:858
get_command_timeout_latched_flag(self)
Returns the "Command timeout latched" bit from get_status_flags().
Definition motoron.py:525
get_vin_voltage(self)
Reads voltage on the Motoron's VIN pin, in raw device units.
Definition motoron.py:613
get_current_sense_minimum_divisor(self, motor)
Reads the current sense minimum divisor setting and returns it as a speed between 0 and 800.
Definition motoron.py:948
set_all_buffered_speeds(self, *speeds)
Sets the buffered speeds of all the motors.
Definition motoron.py:1495
disable_i2c_general_call(self)
Disables the I2C general call address.
Definition motoron.py:192
get_error_active_flag(self)
Returns the "Error active" bit from get_status_flags().
Definition motoron.py:586
set_max_deceleration_reverse(self, motor, decel)
Sets the maximum deceleration of the specified motor for the reverse direction.
Definition motoron.py:1123
get_var_s16(self, motor, offset)
Reads two bytes from the Motoron using a "Get variables" command and returns the result as a signed 1...
Definition motoron.py:442
enable_i2c_general_call(self)
Enables the I2C general call address.
Definition motoron.py:185
write_eeprom_device_number(self, number)
Writes to the EEPROM device number, changing it to the specified value.
Definition motoron.py:260
write_eeprom_alternative_device_number(self, number)
Writes to the alternative device number stored in EEPROM, changing it to the specified value.
Definition motoron.py:273
get_current_sense_processed(self, motor)
Reads the processed current sense reading for the specified motor.
Definition motoron.py:912
clear_reset_flag(self)
Clears the Motoron's reset flag.
Definition motoron.py:1329
set_buffered_speed(self, motor, speed)
Sets the buffered speed of the specified motor.
Definition motoron.py:1413
read_eeprom(self, offset, length)
Reads the specified bytes from the Motoron's EEPROM memory.
Definition motoron.py:199
get_status_flags(self)
Reads the "Status flags" variable from the Motoron.
Definition motoron.py:454
set_starting_speed(self, motor, speed)
Sets the starting speed of the specified motor (both directions).
Definition motoron.py:1169
reset(self, ignore_nack=True)
Sends a "Reset" command to the Motoron, which does a full hardware reset.
Definition motoron.py:368
get_motor_faulting_flag(self)
Returns the "Motor faulting" bit from get_status_flags().
Definition motoron.py:568
set_speed_now(self, motor, speed)
Sets the target and current speed of the specified motor, ignoring any acceleration and deceleration ...
Definition motoron.py:1395
get_jumper_state(self)
Reads the "Jumper state" variable.
Definition motoron.py:676
_send_command(self, cmd)
Definition motoron.py:1621
get_no_power_flag(self)
Returns the "No power" bit from get_status_flags().
Definition motoron.py:577
clear_motor_fault_unconditional(self)
Sends a "Clear motor fault" command to the Motoron with the "unconditional" flag set,...
Definition motoron.py:1300
get_var_u8(self, motor, offset)
Reads one byte from the Motoron using a "Get variables" command and returns the result as an unsigned...
Definition motoron.py:419
set_all_speeds_using_buffers(self)
Sets each motor's target speed equal to the buffered speed.
Definition motoron.py:1521
get_protocol_error_flag(self)
Returns the "Protocol error" bit from get_status_flags().
Definition motoron.py:507
get_direction_change_delay_forward(self, motor)
Reads the direction change delay for the specified motor in the forward direction.
Definition motoron.py:821
coast_now(self)
Sends a "Coast now" command to the Motoron, causing all of the motors to immediately start coasting.
Definition motoron.py:1273
set_current_limit(self, motor, limit)
Sets the current limit for the specified motor.
Definition motoron.py:1214
get_starting_speed_forward(self, motor)
Reads the starting speed for the specified motor in the forward direction.
Definition motoron.py:799
set_speed(self, motor, speed)
Sets the target speed of the specified motor.
Definition motoron.py:1369
clear_latched_status_flags(self, flags)
Clears the specified flags in get_status_flags().
Definition motoron.py:1310
set_braking_now(self, motor, amount)
Commands the motor to brake, coast, or something in between.
Definition motoron.py:1577
reinitialize(self)
Sends a "Reinitialize" command to the Motoron, which resets most of the Motoron's variables to their ...
Definition motoron.py:353
set_current_sense_minimum_divisor(self, motor, speed)
Sets the current sense minimum divisor setting for the specified motor, given a speed between 0 and 8...
Definition motoron.py:1253
get_direction_change_delay_reverse(self, motor)
Reads the direction change delay for the specified motor in the reverse direction.
Definition motoron.py:833
get_variables(self, motor, offset, length)
Reads information from the Motoron using a "Get variables" command.
Definition motoron.py:398
get_target_brake_amount(self, motor)
Reads the target brake amount for the specified motor.
Definition motoron.py:697
set_direction_change_delay_forward(self, motor, duration)
Sets the direction change delay of the specified motor for the forward direction, in units of 10 ms.
Definition motoron.py:1179
clear_motor_fault(self, flags=0)
Sends a "Clear motor fault" command to the Motoron.
Definition motoron.py:1284
set_max_acceleration_forward(self, motor, accel)
Sets the maximum acceleration of the specified motor for the forward direction.
Definition motoron.py:1077
read_eeprom_device_number(self)
Reads the EEPROM device number from the device.
Definition motoron.py:213
set_starting_speed_reverse(self, motor, speed)
Sets the starting speed of the specified motor for the reverse direction.
Definition motoron.py:1157
get_no_power_latched_flag(self)
Returns the "No power latched" bit from get_status_flags().
Definition motoron.py:543
get_command_timeout_milliseconds(self)
Reads the "Command timeout" variable and converts it to milliseconds.
Definition motoron.py:641
write_eeprom(self, offset, value)
Writes a value to one byte in the Motoron's EEPROM memory.
Definition motoron.py:222
get_current_sense_processed_and_speed(self, motor)
This is like get_current_sense_reading() but it only reads the processed current sense measurement an...
Definition motoron.py:887
write_eeprom_response_delay(self, delay)
Writes to the serial response delay setting stored in EEPROM, changing it to the specified value,...
Definition motoron.py:339
get_buffered_speed(self, motor)
Reads the buffered speed of the specified motor.
Definition motoron.py:720
set_protocol_options_locally(self, options)
Sets the protocol options for this object, without sending a command to the Motoron.
Definition motoron.py:129
write_eeprom_communication_options(self, options)
Writes to the serial options byte stored in EEPROM, changing it to the specified value.
Definition motoron.py:306
get_reset_flag(self)
Returns the "Reset" bit from get_status_flags().
Definition motoron.py:552
get_max_deceleration_forward(self, motor)
Reads the maximum deceleration of the specified motor for the forward direction.
Definition motoron.py:766
set_command_timeout_milliseconds(self, ms)
Sets the command timeout period, in milliseconds.
Definition motoron.py:988
write_eeprom16(self, offset, value)
Writes a 2-byte value in the Motoron's EEPROM memory.
Definition motoron.py:247
set_all_speeds_now_using_buffers(self)
Sets each motor's target speed and current speed equal to the buffered speed.
Definition motoron.py:1534
set_pwm_mode(self, motor, mode)
Sets the PWM mode for the specified motor.
Definition motoron.py:1053
set_direction_change_delay_reverse(self, motor, duration)
Sets the direction change delay of the specified motor for the reverse direction, in units of 10 ms.
Definition motoron.py:1191
get_motor_driving_flag(self)
Returns the "Motor driving" bit from get_status_flags().
Definition motoron.py:604
disable_command_timeout(self)
This disables the Motoron's command timeout feature by resetting the the "Error mask" variable to its...
Definition motoron.py:1033
disable_crc(self)
Disables CRC for commands and responses.
Definition motoron.py:149
get_motor_output_enabled_flag(self)
Returns the "Motor output enabled" bit from get_status_flags().
Definition motoron.py:595
enable_crc(self)
Enables CRC for commands and responses.
Definition motoron.py:141
get_starting_speed_reverse(self, motor)
Reads the starting speed for the specified motor in the reverse direction.
Definition motoron.py:810
set_protocol_options(self, options)
Sends the "Set protocol options" command to the device to specify options related to how the device p...
Definition motoron.py:79
set_error_response(self, response)
Sets the error response, which defines how the Motoron will stop its motors when an error is happenin...
Definition motoron.py:1001
set_all_speeds(self, *speeds)
Sets the target speeds of all the motors at the same time.
Definition motoron.py:1435
set_error_mask(self, mask)
Sets the "Error mask" variable, which defines which status flags are considered to be errors.
Definition motoron.py:1021
set_starting_speed_forward(self, motor, speed)
Sets the starting speed of the specified motor for the forward direction.
Definition motoron.py:1145
get_current_sense_offset(self, motor)
Reads the current sense offset setting.
Definition motoron.py:935
enable_crc_for_responses(self)
Enables CRC for responses.
Definition motoron.py:171
disable_crc_for_commands(self)
Disables CRC for commands.
Definition motoron.py:164
set_max_acceleration(self, motor, accel)
Sets the maximum acceleration of the specified motor (both directions).
Definition motoron.py:1101
set_max_deceleration_forward(self, motor, decel)
Sets the maximum deceleration of the specified motor for the forward direction.
Definition motoron.py:1111
get_max_acceleration_forward(self, motor)
Reads the maximum acceleration of the specified motor for the forward direction.
Definition motoron.py:742
write_eeprom_baud_rate(self, baud)
Writes to the baud rate stored in EEPROM, changing it to the specified value.
Definition motoron.py:323
set_braking(self, motor, amount)
Commands the motor to brake, coast, or something in between.
Definition motoron.py:1548
set_max_acceleration_reverse(self, motor, accel)
Sets the maximum acceleration of the specified motor for the reverse direction.
Definition motoron.py:1089
enable_crc_for_commands(self)
Enables CRC for commands.
Definition motoron.py:157
_send_command_and_read_response(self, cmd, response_length)
Definition motoron.py:1625
tuple protocol_options
The bits in this variable are defined by the motoron.PROTOCOL_OPTION_* constants.
Definition motoron.py:56
disable_crc_for_responses(self)
Disables CRC for responses.
Definition motoron.py:178
reset_command_timeout(self)
Resets the command timeout.
Definition motoron.py:1605
get_current_limit(self, motor)
Reads the current limit for the specified motor.
Definition motoron.py:845
set_latched_status_flags(self, flags)
Sets the specified flags in get_status_flags().
Definition motoron.py:1350
get_max_acceleration_reverse(self, motor)
Reads the maximum acceleration of the specified motor for the reverse direction.
Definition motoron.py:754
get_motor_fault_latched_flag(self)
Returns the "Motor fault latched" bit from get_status_flags().
Definition motoron.py:534
get_error_mask(self)
Reads the "Error mask" variable, which defines which status flags are considered to be errors.
Definition motoron.py:664
get_current_speed(self, motor)
Reads the current speed of the specified motor, which is the speed that the Motoron is currently tryi...
Definition motoron.py:708
get_firmware_version(self)
Sends the "Get firmware version" command to get the device's firmware product ID and firmware version...
Definition motoron.py:58
get_current_sense_raw(self, motor)
Reads the raw current sense measurement for the specified motor.
Definition motoron.py:898
Represents an I2C connection to a Pololu Motoron Motor Controller.
Definition motoron.py:1669
_mpy_send_command_core(self, cmd, send_crc)
Definition motoron.py:1748
_smbus_read_response(self, length)
Definition motoron.py:1731
_smbus_send_command_core(self, cmd, send_crc)
Definition motoron.py:1724
__init__(self, *, bus=1, address=16)
Creates a new MotoronI2C object to communicate with the Motoron over I2C.
Definition motoron.py:1674
address
The 7-bit I2C address used by this object.
Definition motoron.py:1691
set_bus(self, bus)
Definition motoron.py:1702
_mpy_read_response(self, length)
Definition motoron.py:1754
Represents a serial connection to a Pololu Motoron Motor Controller.
Definition motoron.py:1765
multi_device_error_check_start(self, starting_device_number, device_count)
Sends a "Multi-device error check" command but does not read any responses.
Definition motoron.py:1844
multi_device_error_check(self, starting_device_number, device_count)
Sends a "Multi-device error check" command and reads the responses.
Definition motoron.py:1875
use_7bit_device_number(self)
Configures this object to send 7-bit device numbers, which is the default.
Definition motoron.py:1838
__init__(self, *, port=None, device_number=None)
Creates a new MotoronSerial object.
Definition motoron.py:1770
use_14bit_device_number(self)
Configures this object to send 14-bit device numbers when using the Pololu protocol,...
Definition motoron.py:1831
device_number
The device number that will be included in commands sent by this object.
Definition motoron.py:1787
expect_8bit_responses(self)
Configures this object to work with Motorons that are configured to send responses in the normal 8-bi...
Definition motoron.py:1824
port
The serial port used by this object.
Definition motoron.py:1812
expect_7bit_responses(self)
Configures this object to work with Motorons that are configured to send 7-bit serial responses.
Definition motoron.py:1817
multi_device_write(self, starting_device_number, device_count, command_byte, data)
Sends a "Multi-device write" command.
Definition motoron.py:1900
int communication_options
The serial options used by this object.
Definition motoron.py:1798
set_port(self, port)
Configures this object to use the specified serial port object.
Definition motoron.py:1800