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