10 def enum_value(x):
return x.value
13 def enum_value(x):
return x
15from motoron_protocol
import *
26 MOTORON_18V18 = 0b0001
27 MOTORON_24V14 = 0b0101
28 MOTORON_18V20 = 0b1010
29 MOTORON_24V16 = 0b1101
40 Represents a connection to a Pololu Motoron Motoron Controller.
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))
49 DEFAULT_ERROR_MASK = (
50 (1 << STATUS_FLAG_COMMAND_TIMEOUT) |
51 (1 << STATUS_FLAG_RESET))
60 Sends the "Get firmware version" command to get the device's firmware
61 product ID and firmware version numbers.
63 For more information, see the "Get firmware version"
64 command in the Motoron user's guide.
66 \return A dictionary in this format:
68 {'product_id': 204, 'firmware_version': {'major': 1, 'minor': 0}}
71 cmd = [CMD_GET_FIRMWARE_VERSION]
73 product_id, minor, major = struct.unpack(
'<HBB', response)
75 'product_id': product_id,
76 'firmware_version': {
'major': major,
'minor': minor}
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.
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.
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.
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
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.
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).
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)
113 For more information, see the "Set protocol optons"
114 command in the Motoron user's guide.
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()
123 CMD_SET_PROTOCOL_OPTIONS,
127 self._send_command_core(cmd,
True)
131 Sets the protocol options for this object, without sending a command to
134 If the options you specify here do not match the actual configuration of
135 the Motoron, future communication could fail.
137 Most users should use set_protocol_options() instead of this.
143 Enables CRC for commands and responses. See set_protocol_options().
146 | (1 << PROTOCOL_OPTION_CRC_FOR_COMMANDS)
147 | (1 << PROTOCOL_OPTION_CRC_FOR_RESPONSES))
151 Disables CRC for commands and responses. See set_protocol_options().
154 & ~(1 << PROTOCOL_OPTION_CRC_FOR_COMMANDS)
155 & ~(1 << PROTOCOL_OPTION_CRC_FOR_RESPONSES))
159 Enables CRC for commands. See set_protocol_options().
162 | (1 << PROTOCOL_OPTION_CRC_FOR_COMMANDS))
166 Disables CRC for commands. See set_protocol_options().
169 & ~(1 << PROTOCOL_OPTION_CRC_FOR_COMMANDS))
173 Enables CRC for responses. See set_protocol_options().
176 | (1 << PROTOCOL_OPTION_CRC_FOR_RESPONSES))
180 Disables CRC for responses. See set_protocol_options().
183 & ~(1 << PROTOCOL_OPTION_CRC_FOR_RESPONSES))
187 Enables the I2C general call address. See set_protocol_options().
190 | (1 << PROTOCOL_OPTION_I2C_GENERAL_CALL))
194 Disables the I2C general call address. See set_protocol_options().
197 & ~(1 << PROTOCOL_OPTION_I2C_GENERAL_CALL))
201 Reads the specified bytes from the Motoron's EEPROM memory.
203 For more information, see the "Read EEPROM" command in the
204 Motoron user's guide.
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
220 return self.
read_eeprom(SETTING_DEVICE_NUMBER, 1)[0]
224 Writes a value to one byte in the Motoron's EEPROM memory.
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.**
230 For more information, see the "Write EEPROM" command in the
231 Motoron user's guide.
249 Writes a 2-byte value in the Motoron's EEPROM memory.
251 This command only has an effect if JMP1 is shorted to GND.
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.
262 Writes to the EEPROM device number, changing it to the specified value.
264 This command only has an effect if JMP1 is shorted to GND.
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.**
271 self.
write_eeprom(SETTING_DEVICE_NUMBER + 1, number >> 7 & 0x7F)
275 Writes to the alternative device number stored in EEPROM, changing it to
278 This function is only useful on Motorons with a serial interface,
279 and only has an effect if JMP1 is shorted to GND.
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.**
285 @sa write_eeprom_disable_alternative_device_number()
287 self.
write_eeprom(SETTING_ALTERNATIVE_DEVICE_NUMBER, (number & 0x7F) | 0x80)
288 self.
write_eeprom(SETTING_ALTERNATIVE_DEVICE_NUMBER + 1, number >> 7 & 0x7F)
292 Writes to EEPROM to disable the alternative device number.
294 This function is only useful on Motorons with a serial interface,
295 and only has an effect if JMP1 is shorted to GND.
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.**
301 @sa write_eeprom_alternative_device_number()
304 self.
write_eeprom(SETTING_ALTERNATIVE_DEVICE_NUMBER + 1, 0)
308 Writes to the serial options byte stored in EEPROM, changing it to
311 The bits in this byte are defined by the
312 MOTORON_COMMUNICATION_OPTION_* constants.
314 This function is only useful on Motorons with a serial interface,
315 and only has an effect if JMP1 is shorted to GND.
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.**
321 self.
write_eeprom(SETTING_COMMUNICATION_OPTIONS, options)
325 Writes to the baud rate stored in EEPROM, changing it to the
328 This function is only useful on Motorons with a serial interface,
329 and only has an effect if JMP1 is shorted to GND.
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.**
335 if (baud < MIN_BAUD_RATE): baud = MIN_BAUD_RATE
336 if (baud > MAX_BAUD_RATE): baud = MAX_BAUD_RATE
341 Writes to the serial response delay setting stored in EEPROM, changing
342 it to the specified value, in units of microseconds.
344 This function is only useful on Motorons with a serial interface,
345 and only has an effect if JMP1 is shorted to GND.
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.**
355 Sends a "Reinitialize" command to the Motoron, which resets most of the
356 Motoron's variables to their default state.
358 For more information, see the "Reinitialize" command in the Motoron
364 cmd = [CMD_REINITIALIZE]
365 self._send_command_core(cmd,
True)
370 Sends a "Reset" command to the Motoron, which does a full hardware reset.
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.
376 After running this command, we recommend waiting for at least 5
377 milliseconds before you try to communicate with the Motoron.
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
390 self._send_command_core(cmd,
True)
395 if not (ignore_nack
and (e.args[0] == 5
or e.args[0] == 121)):
raise
400 Reads information from the Motoron using a "Get variables" command.
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.
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.
421 Reads one byte from the Motoron using a "Get variables" command
422 and returns the result as an unsigned 8-bit integer.
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.
432 Reads two bytes from the Motoron using a "Get variables" command
433 and returns the result as an unsigned 16-bit integer.
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.
440 return struct.unpack(
'<H', buffer)[0]
444 Reads two bytes from the Motoron using a "Get variables" command
445 and returns the result as a signed 16-bit integer.
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.
452 return struct.unpack(
'<h', buffer)[0]
456 Reads the "Status flags" variable from the Motoron.
458 The bits in this variable are defined by the STATUS_FLAGS_*
459 constants in the motoron package:
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
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:
478 mask = ((1 << motoron.STATUS_FLAG_NO_POWER) |
479 (1 << motoron.STATUS_FLAG_MOTOR_FAULTING))
480 if mc.get_status_flags() & mask: # do something
483 This library has helper methods that make it easier if you just want to
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()
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()
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.
502 For more information, see the "Status flags" variable in the Motoron
509 Returns the "Protocol error" bit from get_status_flags().
511 For more information, see the "Status flags" variable in the Motoron
518 Returns the "CRC error" bit from get_status_flags().
520 For more information, see the "Status flags" variable in the Motoron
527 Returns the "Command timeout latched" bit from get_status_flags().
529 For more information, see the "Status flags" variable in the Motoron
532 return bool(self.
get_status_flags() & (1 << STATUS_FLAG_COMMAND_TIMEOUT_LATCHED))
536 Returns the "Motor fault latched" bit from get_status_flags().
538 For more information, see the "Status flags" variable in the Motoron
541 return bool(self.
get_status_flags() & (1 << STATUS_FLAG_MOTOR_FAULT_LATCHED))
545 Returns the "No power latched" bit from get_status_flags().
547 For more information, see the "Status flags" variable in the Motoron
554 Returns the "Reset" bit from get_status_flags().
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().
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.
563 For more information, see the "Status flags" variable in the Motoron
570 Returns the "Motor faulting" bit from get_status_flags().
572 For more information, see the "Status flags" variable in the Motoron
579 Returns the "No power" bit from get_status_flags().
581 For more information, see the "Status flags" variable in the Motoron
588 Returns the "Error active" bit from get_status_flags().
590 For more information, see the "Status flags" variable in the Motoron
597 Returns the "Motor output enabled" bit from get_status_flags().
599 For more information, see the "Status flags" variable in the Motoron
602 return bool(self.
get_status_flags() & (1 << STATUS_FLAG_MOTOR_OUTPUT_ENABLED))
606 Returns the "Motor driving" bit from get_status_flags().
608 For more information, see the "Status flags" variable in the Motoron
615 Reads voltage on the Motoron's VIN pin, in raw device units.
617 For more information, see the "VIN voltage" variable in the Motoron
620 @sa get_vin_voltage_mv()
626 Reads the voltage on the Motoron's VIN pin and converts it to millivolts.
628 For more information, see the "VIN voltage" variable in the Motoron
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.
636 @sa get_vin_voltage()
638 scale = 459
if enum_value(type) & 1
else 1047
643 Reads the "Command timeout" variable and converts it to milliseconds.
645 For more information, see the "Command timeout" variable in the Motoron
648 @sa set_command_timeout_milliseconds()
650 return self.
get_var_u16(0, VAR_COMMAND_TIMEOUT) * 4
654 Reads the "Error response" variable, which defines how the Motoron will
655 stop its motors when an error is happening.
657 For more information, see the "Error response" variable in the Motoron
660 @sa set_error_response()
666 Reads the "Error mask" variable, which defines which status flags are
667 considered to be errors.
669 For more information, see the "Error mask" variable in the Motoron
678 Reads the "Jumper state" variable.
680 For more information, see the "Jumper state" variable in the Motoron
687 Reads the target speed of the specified motor, which is the speed at
688 which the motor has been commanded to move.
690 For more information, see the "Target speed" variable in the Motoron
693 @sa set_speed(), set_all_speeds(), set_all_speeds_using_buffers()
699 Reads the target brake amount for the specified motor.
701 For more information, see the "Target speed" variable in the Motoron
704 @sa set_target_brake_amount()
706 return self.
get_var_u16(motor, MVAR_TARGET_BRAKE_AMOUNT)
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.
713 For more information, see the "Target speed" variable in the Motoron
716 @sa set_speed_now(), set_all_speeds_now(), set_all_speeds_now_using_buffers()
722 Reads the buffered speed of the specified motor.
724 For more information, see the "Buffered speed" variable in the Motoron
727 @sa set_buffered_speed(), set_all_buffered_speeds()
729 return self.
get_var_s16(motor, MVAR_BUFFERED_SPEED)
733 Reads the PWM mode of the specified motor.
735 For more information, see the "PWM mode" variable in the Motoron
744 Reads the maximum acceleration of the specified motor for the forward
747 For more information, see the "Max acceleration forward" variable in the
748 Motoron user's guide.
750 @sa set_max_acceleration(), set_max_acceleration_forward()
752 return self.
get_var_u16(motor, MVAR_MAX_ACCEL_FORWARD)
756 Reads the maximum acceleration of the specified motor for the reverse
759 For more information, see the "Max acceleration reverse" variable in the
760 Motoron user's guide.
762 @sa set_max_acceleration(), set_max_acceleration_reverse()
764 return self.
get_var_u16(motor, MVAR_MAX_ACCEL_REVERSE)
768 Reads the maximum deceleration of the specified motor for the forward
771 For more information, see the "Max deceleration forward" variable in the
772 Motoron user's guide.
774 @sa set_max_deceleration(), set_max_deceleration_forward()
776 return self.
get_var_u16(motor, MVAR_MAX_DECEL_FORWARD)
780 Reads the maximum deceleration of the specified motor for the reverse
783 For more information, see the "Max deceleration reverse" variable in the
784 Motoron user's guide.
786 @sa set_max_deceleration(), set_max_deceleration_reverse()
788 return self.
get_var_u16(motor, MVAR_MAX_DECEL_REVERSE)
794 def get_max_deceleration_temporary(self, motor):
801 Reads the starting speed for the specified motor in the forward direction.
803 For more information, see the "Starting speed forward" variable in the
804 Motoron user's guide.
806 @sa set_starting_speed(), set_starting_speed_forward()
808 return self.
get_var_u16(motor, MVAR_STARTING_SPEED_FORWARD)
812 Reads the starting speed for the specified motor in the reverse direction.
814 For more information, see the "Starting speed reverse" variable in the
815 Motoron user's guide.
817 @sa set_starting_speed(), set_starting_speed_reverse()
819 return self.
get_var_u16(motor, MVAR_STARTING_SPEED_REVERSE)
823 Reads the direction change delay for the specified motor in the
826 For more information, see the "Direction change delay forward" variable
827 in the Motoron user's guide.
829 @sa set_direction_change_delay(), set_direction_change_delay_forward()
831 return self.
get_var_u8(motor, MVAR_DIRECTION_CHANGE_DELAY_FORWARD)
835 Reads the direction change delay for the specified motor in the
838 For more information, see the "Direction change delay reverse" variable
839 in the Motoron user's guide.
841 @sa set_direction_change_delay(), set_direction_change_delay_reverse()
843 return self.
get_var_u8(motor, MVAR_DIRECTION_CHANGE_DELAY_REVERSE)
847 Reads the current limit for the specified motor.
849 This only works for the high-power Motorons.
851 For more information, see the "Current limit" variable in the Motoron user's
854 @sa set_current_limit()
860 Reads all the results from the last current sense measurement for the
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
868 This only works for the Motorons with current sensing.
870 @sa get_current_sense_raw_and_speed(), get_current_sense_processed_and_speed()
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 }
878 This is like get_current_sense_reading() but it only reads the raw current
879 sense measurement and the speed.
881 This only works for the Motorons with current sensing.
883 buffer = self.
get_variables(motor, MVAR_CURRENT_SENSE_RAW, 4)
884 raw, speed = struct.unpack(
'<Hh', buffer)
885 return {
'raw': raw,
'speed': speed }
889 This is like get_current_sense_reading() but it only reads the processed
890 current sense measurement and the speed.
892 This only works for the Motorons with current sensing.
894 buffer = self.
get_variables(motor, MVAR_CURRENT_SENSE_SPEED, 4)
895 speed, processed = struct.unpack(
'<hH', buffer)
896 return {
'speed': speed,
'processed': processed }
900 Reads the raw current sense measurement for the specified motor.
902 This only works for the Motorons with current sensing.
904 For more information, see the "Current sense raw" variable
905 in the Motoron user's guide.
907 @sa get_current_sense_reading()
909 return self.
get_var_u16(motor, MVAR_CURRENT_SENSE_RAW)
914 Reads the processed current sense reading for the specified motor.
916 This only works for the Motorons with current sensing.
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.
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.
928 Note that this reading will be 0xFFFF if an overflow happens during the
929 calculation due to very high current.
931 @sa get_current_sense_processed_and_speed()
933 return self.
get_var_u16(motor, MVAR_CURRENT_SENSE_PROCESSED)
937 Reads the current sense offset setting.
939 This only works for the high-power Motorons.
941 For more information, see the "Current sense offset" variable in the
942 Motoron user's guide.
944 @sa set_current_sense_offset()
946 return self.
get_var_u8(motor, MVAR_CURRENT_SENSE_OFFSET)
950 Reads the current sense minimum divisor setting and returns it as a speed
953 This only works for the high-power Motorons.
955 For more information, see the "Current sense minimum divisor" variable in
956 the Motoron user's guide.
958 @sa set_current_sense_minimum_divisor()
960 return self.
get_var_u8(motor, MVAR_CURRENT_SENSE_MINIMUM_DIVISOR) << 2
965 Configures the Motoron using a "Set variable" command.
967 This library has helper methods to set every variable, so you should
968 not need to call this function directly.
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
974 @param value The value to set the variable to.
978 if value > 0x3FFF: value = 0x3FFF
990 Sets the command timeout period, in milliseconds.
992 For more information, see the "Command timeout" variable
993 in the Motoron user's guide.
995 @sa disable_command_timeout(), get_command_timeout_milliseconds()
998 timeout = math.ceil(ms / 4)
1003 Sets the error response, which defines how the Motoron will
1004 stop its motors when an error is happening.
1006 The response parameter should be one of these constants from the motoron
1009 - motoron.ERROR_RESPONSE_COAST
1010 - motoron.ERROR_RESPONSE_BRAKE
1011 - motoron.ERROR_RESPONSE_COAST_NOW
1012 - motoron.ERROR_RESPONSE_BRAKE_NOW
1014 For more information, see the "Error response" variable in the Motoron
1017 @sa get_error_response()
1023 Sets the "Error mask" variable, which defines which status flags are
1024 considered to be errors.
1026 For more information, see the "Error mask" variable in the Motoron
1029 @sa get_error_mask(), get_status_flags()
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.
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.
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.
1049 @sa set_command_timeout_milliseconds(), set_error_mask()
1051 self.
set_error_mask(MotoronBase.DEFAULT_ERROR_MASK & ~(1 << STATUS_FLAG_COMMAND_TIMEOUT))
1055 Sets the PWM mode for the specified motor.
1057 The mode parameter should be one of the following these constants from
1058 the motoron package:
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
1070 For more information, see the "PWM mode" variable in the Motoron user's
1073 @sa get_pwm_mode(self)
1079 Sets the maximum acceleration of the specified motor for the forward
1082 For more information, see the "Max acceleration forward" variable in the
1083 Motoron user's guide.
1085 @sa set_max_acceleration(), get_max_acceleration_forward()
1087 self.
set_variable(motor, MVAR_MAX_ACCEL_FORWARD, accel)
1091 Sets the maximum acceleration of the specified motor for the reverse
1094 For more information, see the "Max acceleration reverse" variable in the
1095 Motoron user's guide.
1097 @sa set_max_acceleration(), get_max_acceleration_reverse()
1099 self.
set_variable(motor, MVAR_MAX_ACCEL_REVERSE, accel)
1103 Sets the maximum acceleration of the specified motor (both directions).
1105 If this function succeeds, it is equivalent to calling
1106 set_max_acceleration_forward() and set_max_acceleration_reverse().
1113 Sets the maximum deceleration of the specified motor for the forward
1116 For more information, see the "Max deceleration forward" variable in the
1117 Motoron user's guide.
1119 @sa set_max_deceleration(), get_max_deceleration_forward()
1121 self.
set_variable(motor, MVAR_MAX_DECEL_FORWARD, decel)
1125 Sets the maximum deceleration of the specified motor for the reverse
1128 For more information, see the "Max deceleration reverse" variable in the
1129 Motoron user's guide.
1131 @sa set_max_deceleration(), get_max_deceleration_reverse()
1133 self.
set_variable(motor, MVAR_MAX_DECEL_REVERSE, decel)
1137 Sets the maximum deceleration of the specified motor (both directions).
1139 If this function succeeds, it is equivalent to calling
1140 set_max_deceleration_forward() and set_max_deceleration_reverse().
1147 Sets the starting speed of the specified motor for the forward
1150 For more information, see the "Starting speed forward" variable in the
1151 Motoron user's guide.
1153 @sa set_starting_speed(), get_starting_speed_forward()
1155 self.
set_variable(motor, MVAR_STARTING_SPEED_FORWARD, speed)
1159 Sets the starting speed of the specified motor for the reverse
1162 For more information, see the "Starting speed reverse" variable in the
1163 Motoron user's guide.
1165 @sa set_starting_speed(), get_starting_speed_reverse()
1167 self.
set_variable(motor, MVAR_STARTING_SPEED_REVERSE, speed)
1171 Sets the starting speed of the specified motor (both directions).
1173 If this function succeeds, it is equivalent to calling
1174 set_starting_speed_forward() and set_starting_speed_reverse().
1181 Sets the direction change delay of the specified motor for the forward
1182 direction, in units of 10 ms.
1184 For more information, see the "Direction change delay forward" variable
1185 in the Motoron user's guide.
1187 @sa set_direction_change_delay(), get_direction_change_delay_forward()
1189 self.
set_variable(motor, MVAR_DIRECTION_CHANGE_DELAY_FORWARD, duration)
1193 Sets the direction change delay of the specified motor for the reverse
1194 direction, in units of 10 ms.
1196 For more information, see the "Direction change delay reverse" variable
1197 in the Motoron user's guide.
1199 @sa set_direction_change_delay(), get_direction_change_delay_reverse()
1201 self.
set_variable(motor, MVAR_DIRECTION_CHANGE_DELAY_REVERSE, duration)
1205 Sets the direction change delay of the specified motor (both directions),
1208 If this function succeeds, it is equivalent to calling
1209 set_direction_change_delay_forward() and set_direction_change_delay_reverse().
1216 Sets the current limit for the specified motor.
1218 This only works for the high-power Motorons.
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().
1225 @sa get_current_limit()
1231 Sets the current sense offset setting for the specified motor.
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.
1238 The current_sense_calibrate example shows how to measure the current
1239 sense offsets and load them onto the Motoron using this function.
1241 If you do not care about measuring motor current, you do not need to
1244 For more information, see the "Current sense offset" variable in the
1245 Motoron user's guide.
1247 This only works for the high-power Motorons.
1249 @sa get_current_sense_offset()
1251 self.
set_variable(motor, MVAR_CURRENT_SENSE_OFFSET, offset)
1255 Sets the current sense minimum divisor setting for the specified motor,
1256 given a speed between 0 and 800.
1258 This is one of the settings that determines how current sense
1259 readings are processed.
1261 If you do not care about measuring motor current, you do not need to
1264 For more information, see the "Current sense minimum divisor" variable in
1265 the Motoron user's guide.
1267 This only works for the high-power Motorons.
1269 @sa get_current_sense_minimum_divisor()
1271 self.
set_variable(motor, MVAR_CURRENT_SENSE_MINIMUM_DIVISOR, speed >> 2)
1275 Sends a "Coast now" command to the Motoron, causing all of the motors to
1276 immediately start coasting.
1278 For more information, see the "Coast now" command in the Motoron
1281 cmd = [CMD_COAST_NOW]
1286 Sends a "Clear motor fault" command to the Motoron.
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.
1292 For more information, see the "Clear motor fault" command in the Motoron
1295 @sa clear_motor_fault_unconditional(), get_motor_faulting_flag()
1297 cmd = [ CMD_CLEAR_MOTOR_FAULT, (flags & 0x7F) ]
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.
1306 This is a more robust version of clear_motor_fault().
1312 Clears the specified flags in get_status_flags().
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.
1317 For more information, see the "Clear latched status flags" command in the
1318 Motoron user's guide.
1320 @sa get_status_flags(), set_latched_status_flags()
1323 CMD_CLEAR_LATCHED_STATUS_FLAGS,
1325 (flags >> 7) & 0x7F,
1331 Clears the Motoron's reset flag.
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.
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.
1346 @sa clear_latched_status_flags()
1352 Sets the specified flags in get_status_flags().
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.
1357 For more information, see the "Set latched status flags" command in the
1358 Motoron user's guide.
1360 @sa get_status_flags(), set_latched_status_flags()
1363 CMD_SET_LATCHED_STATUS_FLAGS,
1365 (flags >> 7) & 0x7F,
1371 Sets the target speed of the specified motor.
1373 The current speed will start moving to the specified target speed,
1374 obeying any acceleration and deceleration limits.
1376 The motor number should be between 1 and the number of motors supported
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.
1382 For more information, see the "Set speed" command in the Motoron
1385 @sa set_speed_now(), set_all_speeds()
1391 (speed >> 7) & 0x7F,
1397 Sets the target and current speed of the specified motor, ignoring
1398 any acceleration and deceleration limits.
1400 For more information, see the "Set speed" command in the Motoron
1403 @sa set_speed(), set_all_speeds_now()
1409 (speed >> 7) & 0x7F,
1415 Sets the buffered speed of the specified motor.
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.
1421 For more information, see the "Set speed" command in the Motoron
1424 @sa set_speed(), set_all_buffered_speeds(),
1425 set_all_speeds_using_buffers(), set_all_speeds_now_using_buffers()
1428 CMD_SET_BUFFERED_SPEED,
1431 (speed >> 7) & 0x7F,
1437 Sets the target speeds of all the motors at the same time.
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
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.
1446 There are a few different ways you can call this method (and the related
1447 methods that set speeds for all the motors):
1450 # with separate arguments
1451 mc.set_all_speeds(100, -200, 300)
1453 # with arguments unpacked from a list
1454 speeds = [-400, 500, -600]
1455 mc.set_all_speeds(*speeds)
1458 For more information, see the "Set all speeds" command in the Motoron
1461 @sa set_speed(), set_all_speeds_now(), set_all_buffered_speeds()
1463 cmd = [CMD_SET_ALL_SPEEDS]
1464 for speed
in speeds:
1467 (speed >> 7) & 0x7F,
1473 Sets the target and current speeds of all the motors at the same time.
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
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.
1482 For more information, see the "Set all speeds" command in the Motoron
1485 @sa set_speed(), set_speed_now(), set_all_speeds()
1487 cmd = [CMD_SET_ALL_SPEEDS_NOW]
1488 for speed
in speeds:
1491 (speed >> 7) & 0x7F,
1497 Sets the buffered speeds of all the motors.
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
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
1507 For more information, see the "Set all speeds" command in the Motoron
1510 @sa set_speed(), set_buffered_speed(), set_all_speeds(),
1511 set_all_speeds_using_buffers(), set_all_speeds_now_using_buffers()
1513 cmd = [CMD_SET_ALL_BUFFERED_SPEEDS]
1514 for speed
in speeds:
1517 (speed >> 7) & 0x7F,
1523 Sets each motor's target speed equal to the buffered speed.
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().
1528 @sa set_all_speeds_now_using_buffers(), set_buffered_speed(),
1529 set_all_buffered_speeds()
1531 cmd = [CMD_SET_ALL_SPEEDS_USING_BUFFERS]
1536 Sets each motor's target speed and current speed equal to the buffered
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().
1542 @sa set_all_speeds_using_buffers(), set_buffered_speed(),
1543 set_all_buffered_speeds()
1545 cmd = [CMD_SET_ALL_SPEEDS_NOW_USING_BUFFERS]
1550 Commands the motor to brake, coast, or something in between.
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.
1557 The motor number parameter should be between 1 and the number of motors
1558 supported by the Motoron.
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.
1564 See the "Set braking" command in the Motoron user's guide for more
1567 @sa set_braking_now(), get_target_brake_amount()
1573 (amount >> 7) & 0x7F,
1579 Commands the motor to brake, coast, or something in between.
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.
1585 The motor number parameter should be between 1 and the number of motors
1586 supported by the Motoron.
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.
1592 See the "Set braking" command in the Motoron user's guide for more
1595 @sa set_braking(), get_target_brake_amount()
1598 CMD_SET_BRAKING_NOW,
1601 (amount >> 7) & 0x7F,
1607 Resets the command timeout.
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.)
1613 For more information, see the "Reset command timeout" command in the
1614 Motoron user's guide.
1616 @sa disable_command_timeout(), set_command_timeout_milliseconds()
1618 cmd = [CMD_RESET_COMMAND_TIMEOUT]
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)
1625 def _send_command_and_read_response(self, cmd, response_length):
1627 return self._read_response(response_length)
1629def calculate_current_limit(milliamps, type, reference_mv, offset):
1631 Calculates a current limit value that can be passed to the Motoron
1632 using set_current_limit().
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.
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)
1651def current_sense_units_milliamps(type, reference_mv):
1653 Calculates the units for the Motoron's current sense reading returned by
1654 get_current_sense_processed(), in milliamps.
1656 To convert a reading from get_current_sense_processed() to milliamps,
1657 multiply it by the value returned from this function.
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.
1664 if type == CurrentSenseType.MOTORON_453:
1665 return reference_mv * 5 / 8448
1667 return reference_mv * (enum_value(type) & 3) * 25 / 512
1671 Represents an I2C connection to a Pololu Motoron Motor Controller.
1676 Creates a new MotoronI2C object to communicate with the Motoron over I2C.
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.
1694 Configures this object to use the specified I2C bus object.
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.
1702 def set_bus(self, bus):
1703 if isinstance(bus, int):
1705 bus = smbus2.SMBus(bus)
1709 type_is_smbus =
True
1710 except AttributeError:
1711 type_is_smbus =
False
1717 self.
_msg = smbus2.i2c_msg
1724 def _smbus_send_command_core(self, cmd, send_crc):
1726 write = self.
_msg.write(self.
address, cmd + [calculate_crc(cmd)])
1729 self.
bus.i2c_rdwr(write)
1731 def _smbus_read_response(self, length):
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)
1743 response = response[:-1]
1744 if crc != calculate_crc(response):
1745 raise RuntimeError(
'Incorrect CRC received.')
1748 def _mpy_send_command_core(self, cmd, send_crc):
1750 self.
bus.writeto(self.
address, bytes(cmd + [calculate_crc(cmd)]))
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)
1759 response = response[:-1]
1760 if crc != calculate_crc(response):
1761 raise RuntimeError(
'Incorrect CRC received.')
1767 Represents a serial connection to a Pololu Motoron Motor Controller.
1772 Creates a new MotoronSerial object.
1774 The `deviceNumber` argument is optional. If it is omitted or None,
1775 the object will use the compact protocol.
1777 The `port` argument specifies the serial port to use and is passed
1778 directly to set_port().
1802 Configures this object to use the specified serial port object.
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.
1810 if isinstance(port, str):
1812 self.
port = serial.Serial(port, 115200, timeout=0.1, write_timeout=0.1)
1819 Configures this object to work with Motorons that are configured to send
1820 7-bit serial responses.
1826 Configures this object to work with Motorons that are configured to send
1827 responses in the normal 8-bit format.
1833 Configures this object to send 14-bit device numbers when using the
1834 Pololu protocol, instead of the default 7-bit.
1840 Configures this object to send 7-bit device numbers, which is the default.
1846 Sends a "Multi-device error check" command but does not read any
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.
1854 if device_count < 0
or device_count > 0x3FFF:
1855 raise RuntimeError(
'Invalid device count.')
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,
1864 if device_count < 0
or device_count > 0x7F:
1865 raise RuntimeError(
'Invalid device count.')
1867 CMD_MULTI_DEVICE_ERROR_CHECK,
1868 starting_device_number & 0x7F,
1877 Sends a "Multi-device error check" command and reads the responses.
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).
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.
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.
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)
1900 command_byte, data):
1902 Sends a "Multi-device write" command.
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
1911 if device_count < 0
or device_count > 0x3FFF:
1912 raise RuntimeError(
'Invalid device count.')
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,
1921 if device_count < 0
or device_count > 0x7F:
1922 raise RuntimeError(
'Invalid device count.')
1924 CMD_MULTI_DEVICE_WRITE,
1925 starting_device_number & 0x7F,
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.')
1936 cmd += [bytes_per_device, command_byte & 0x7F]
1941 def _send_command_core(self, cmd, send_crc):
1957 if send_crc: cmd += [calculate_crc(cmd)]
1959 self.
port.write(bytes(cmd))
1961 def _read_response(self, length):
1962 crc_enabled = bool(self.
protocol_options & (1 << PROTOCOL_OPTION_CRC_FOR_RESPONSES))
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.')
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)}.")
1978 response = response[:-1]
1979 if crc != calculate_crc(response):
1980 raise RuntimeError(
'Incorrect CRC received.')
1984 response = bytearray(response[:-1])
1985 for i
in range(length):
1986 if msbs & 1: response[i] |= 0x80
1988 response = bytes(response)
Represents a connection to a Pololu Motoron Motoron Controller.
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.
get_var_u16(self, motor, offset)
Reads two bytes from the Motoron using a "Get variables" command and returns the result as an unsigne...
get_pwm_mode(self, motor)
Reads the PWM mode of the specified motor.
set_all_speeds_now(self, *speeds)
Sets the target and current speeds of all the motors at the same time.
get_error_response(self)
Reads the "Error response" variable, which defines how the Motoron will stop its motors when an error...
get_target_speed(self, motor)
Reads the target speed of the specified motor, which is the speed at which the motor has been command...
get_max_deceleration_reverse(self, motor)
Reads the maximum deceleration of the specified motor for the reverse direction.
set_current_sense_offset(self, motor, offset)
Sets the current sense offset setting for the specified motor.
write_eeprom_disable_alternative_device_number(self)
Writes to EEPROM to disable the alternative device number.
set_max_deceleration(self, motor, decel)
Sets the maximum deceleration of the specified motor (both directions).
get_crc_error_flag(self)
Returns the "CRC error" bit from get_status_flags().
set_direction_change_delay(self, motor, duration)
Sets the direction change delay of the specified motor (both directions), in units of 10 ms.
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 ...
set_variable(self, motor, offset, value)
Configures the Motoron using a "Set variable" command.
get_current_sense_reading(self, motor)
Reads all the results from the last current sense measurement for the specified motor.
get_command_timeout_latched_flag(self)
Returns the "Command timeout latched" bit from get_status_flags().
get_vin_voltage(self)
Reads voltage on the Motoron's VIN pin, in raw device units.
get_current_sense_minimum_divisor(self, motor)
Reads the current sense minimum divisor setting and returns it as a speed between 0 and 800.
set_all_buffered_speeds(self, *speeds)
Sets the buffered speeds of all the motors.
disable_i2c_general_call(self)
Disables the I2C general call address.
get_error_active_flag(self)
Returns the "Error active" bit from get_status_flags().
set_max_deceleration_reverse(self, motor, decel)
Sets the maximum deceleration of the specified motor for the reverse direction.
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...
enable_i2c_general_call(self)
Enables the I2C general call address.
write_eeprom_device_number(self, number)
Writes to the EEPROM device number, changing it to the specified value.
write_eeprom_alternative_device_number(self, number)
Writes to the alternative device number stored in EEPROM, changing it to the specified value.
get_current_sense_processed(self, motor)
Reads the processed current sense reading for the specified motor.
clear_reset_flag(self)
Clears the Motoron's reset flag.
set_buffered_speed(self, motor, speed)
Sets the buffered speed of the specified motor.
read_eeprom(self, offset, length)
Reads the specified bytes from the Motoron's EEPROM memory.
get_status_flags(self)
Reads the "Status flags" variable from the Motoron.
set_starting_speed(self, motor, speed)
Sets the starting speed of the specified motor (both directions).
reset(self, ignore_nack=True)
Sends a "Reset" command to the Motoron, which does a full hardware reset.
get_motor_faulting_flag(self)
Returns the "Motor faulting" bit from get_status_flags().
set_speed_now(self, motor, speed)
Sets the target and current speed of the specified motor, ignoring any acceleration and deceleration ...
get_jumper_state(self)
Reads the "Jumper state" variable.
get_no_power_flag(self)
Returns the "No power" bit from get_status_flags().
clear_motor_fault_unconditional(self)
Sends a "Clear motor fault" command to the Motoron with the "unconditional" flag set,...
get_var_u8(self, motor, offset)
Reads one byte from the Motoron using a "Get variables" command and returns the result as an unsigned...
set_all_speeds_using_buffers(self)
Sets each motor's target speed equal to the buffered speed.
get_protocol_error_flag(self)
Returns the "Protocol error" bit from get_status_flags().
get_direction_change_delay_forward(self, motor)
Reads the direction change delay for the specified motor in the forward direction.
coast_now(self)
Sends a "Coast now" command to the Motoron, causing all of the motors to immediately start coasting.
set_current_limit(self, motor, limit)
Sets the current limit for the specified motor.
get_starting_speed_forward(self, motor)
Reads the starting speed for the specified motor in the forward direction.
set_speed(self, motor, speed)
Sets the target speed of the specified motor.
clear_latched_status_flags(self, flags)
Clears the specified flags in get_status_flags().
set_braking_now(self, motor, amount)
Commands the motor to brake, coast, or something in between.
reinitialize(self)
Sends a "Reinitialize" command to the Motoron, which resets most of the Motoron's variables to their ...
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...
get_direction_change_delay_reverse(self, motor)
Reads the direction change delay for the specified motor in the reverse direction.
get_variables(self, motor, offset, length)
Reads information from the Motoron using a "Get variables" command.
get_target_brake_amount(self, motor)
Reads the target brake amount for the specified motor.
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.
clear_motor_fault(self, flags=0)
Sends a "Clear motor fault" command to the Motoron.
set_max_acceleration_forward(self, motor, accel)
Sets the maximum acceleration of the specified motor for the forward direction.
read_eeprom_device_number(self)
Reads the EEPROM device number from the device.
set_starting_speed_reverse(self, motor, speed)
Sets the starting speed of the specified motor for the reverse direction.
get_no_power_latched_flag(self)
Returns the "No power latched" bit from get_status_flags().
get_command_timeout_milliseconds(self)
Reads the "Command timeout" variable and converts it to milliseconds.
write_eeprom(self, offset, value)
Writes a value to one byte in the Motoron's EEPROM memory.
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...
write_eeprom_response_delay(self, delay)
Writes to the serial response delay setting stored in EEPROM, changing it to the specified value,...
get_buffered_speed(self, motor)
Reads the buffered speed of the specified motor.
set_protocol_options_locally(self, options)
Sets the protocol options for this object, without sending a command to the Motoron.
write_eeprom_communication_options(self, options)
Writes to the serial options byte stored in EEPROM, changing it to the specified value.
get_reset_flag(self)
Returns the "Reset" bit from get_status_flags().
get_max_deceleration_forward(self, motor)
Reads the maximum deceleration of the specified motor for the forward direction.
set_command_timeout_milliseconds(self, ms)
Sets the command timeout period, in milliseconds.
write_eeprom16(self, offset, value)
Writes a 2-byte value in the Motoron's EEPROM memory.
set_all_speeds_now_using_buffers(self)
Sets each motor's target speed and current speed equal to the buffered speed.
set_pwm_mode(self, motor, mode)
Sets the PWM mode for the specified motor.
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.
get_motor_driving_flag(self)
Returns the "Motor driving" bit from get_status_flags().
disable_command_timeout(self)
This disables the Motoron's command timeout feature by resetting the the "Error mask" variable to its...
disable_crc(self)
Disables CRC for commands and responses.
get_motor_output_enabled_flag(self)
Returns the "Motor output enabled" bit from get_status_flags().
enable_crc(self)
Enables CRC for commands and responses.
get_starting_speed_reverse(self, motor)
Reads the starting speed for the specified motor in the reverse direction.
set_protocol_options(self, options)
Sends the "Set protocol options" command to the device to specify options related to how the device p...
set_error_response(self, response)
Sets the error response, which defines how the Motoron will stop its motors when an error is happenin...
set_all_speeds(self, *speeds)
Sets the target speeds of all the motors at the same time.
set_error_mask(self, mask)
Sets the "Error mask" variable, which defines which status flags are considered to be errors.
set_starting_speed_forward(self, motor, speed)
Sets the starting speed of the specified motor for the forward direction.
get_current_sense_offset(self, motor)
Reads the current sense offset setting.
enable_crc_for_responses(self)
Enables CRC for responses.
disable_crc_for_commands(self)
Disables CRC for commands.
set_max_acceleration(self, motor, accel)
Sets the maximum acceleration of the specified motor (both directions).
set_max_deceleration_forward(self, motor, decel)
Sets the maximum deceleration of the specified motor for the forward direction.
get_max_acceleration_forward(self, motor)
Reads the maximum acceleration of the specified motor for the forward direction.
write_eeprom_baud_rate(self, baud)
Writes to the baud rate stored in EEPROM, changing it to the specified value.
set_braking(self, motor, amount)
Commands the motor to brake, coast, or something in between.
set_max_acceleration_reverse(self, motor, accel)
Sets the maximum acceleration of the specified motor for the reverse direction.
enable_crc_for_commands(self)
Enables CRC for commands.
_send_command_and_read_response(self, cmd, response_length)
tuple protocol_options
The bits in this variable are defined by the motoron.PROTOCOL_OPTION_* constants.
disable_crc_for_responses(self)
Disables CRC for responses.
reset_command_timeout(self)
Resets the command timeout.
get_current_limit(self, motor)
Reads the current limit for the specified motor.
set_latched_status_flags(self, flags)
Sets the specified flags in get_status_flags().
get_max_acceleration_reverse(self, motor)
Reads the maximum acceleration of the specified motor for the reverse direction.
get_motor_fault_latched_flag(self)
Returns the "Motor fault latched" bit from get_status_flags().
get_error_mask(self)
Reads the "Error mask" variable, which defines which status flags are considered to be errors.
get_current_speed(self, motor)
Reads the current speed of the specified motor, which is the speed that the Motoron is currently tryi...
get_firmware_version(self)
Sends the "Get firmware version" command to get the device's firmware product ID and firmware version...
get_current_sense_raw(self, motor)
Reads the raw current sense measurement for the specified motor.
Represents an I2C connection to a Pololu Motoron Motor Controller.
_mpy_send_command_core(self, cmd, send_crc)
_smbus_read_response(self, length)
_smbus_send_command_core(self, cmd, send_crc)
__init__(self, *, bus=1, address=16)
Creates a new MotoronI2C object to communicate with the Motoron over I2C.
address
The 7-bit I2C address used by this object.
_mpy_read_response(self, length)
Represents a serial connection to a Pololu Motoron Motor Controller.
multi_device_error_check_start(self, starting_device_number, device_count)
Sends a "Multi-device error check" command but does not read any responses.
multi_device_error_check(self, starting_device_number, device_count)
Sends a "Multi-device error check" command and reads the responses.
use_7bit_device_number(self)
Configures this object to send 7-bit device numbers, which is the default.
__init__(self, *, port=None, device_number=None)
Creates a new MotoronSerial object.
use_14bit_device_number(self)
Configures this object to send 14-bit device numbers when using the Pololu protocol,...
device_number
The device number that will be included in commands sent by this object.
expect_8bit_responses(self)
Configures this object to work with Motorons that are configured to send responses in the normal 8-bi...
port
The serial port used by this object.
expect_7bit_responses(self)
Configures this object to work with Motorons that are configured to send 7-bit serial responses.
multi_device_write(self, starting_device_number, device_count, command_byte, data)
Sends a "Multi-device write" command.
int communication_options
The serial options used by this object.
set_port(self, port)
Configures this object to use the specified serial port object.