Motoron Motor Controller library for Raspberry Pi
Loading...
Searching...
No Matches
motoron_protocol.py
Go to the documentation of this file.
1
9
10CMD_GET_FIRMWARE_VERSION = 0x87
11CMD_SET_PROTOCOL_OPTIONS = 0x8B
12CMD_READ_EEPROM = 0x93
13CMD_WRITE_EEPROM = 0x95
14CMD_REINITIALIZE = 0x96
15CMD_RESET = 0x99
16CMD_GET_VARIABLES = 0x9A
17CMD_SET_VARIABLE = 0x9C
18CMD_COAST_NOW = 0xA5
19CMD_CLEAR_MOTOR_FAULT = 0xA6
20CMD_CLEAR_LATCHED_STATUS_FLAGS = 0xA9
21CMD_SET_LATCHED_STATUS_FLAGS = 0xAC
22CMD_SET_BRAKING = 0xB1
23CMD_SET_BRAKING_NOW = 0xB2
24CMD_SET_SPEED = 0xD1
25CMD_SET_SPEED_NOW = 0xD2
26CMD_SET_BUFFERED_SPEED = 0xD4
27CMD_SET_ALL_SPEEDS = 0xE1
28CMD_SET_ALL_SPEEDS_NOW = 0xE2
29CMD_SET_ALL_BUFFERED_SPEEDS = 0xE4
30CMD_SET_ALL_SPEEDS_USING_BUFFERS = 0xF0
31CMD_SET_ALL_SPEEDS_NOW_USING_BUFFERS = 0xF3
32CMD_RESET_COMMAND_TIMEOUT = 0xF5
33CMD_MULTI_DEVICE_ERROR_CHECK = 0xF9
34CMD_MULTI_DEVICE_WRITE = 0xFA
35
36SETTING_FACTORY_RESET_CODE = 0
37SETTING_DEVICE_NUMBER = 1
38SETTING_ALTERNATIVE_DEVICE_NUMBER = 3
39SETTING_COMMUNICATION_OPTIONS = 5
40SETTING_BAUD_DIVIDER = 6
41SETTING_RESPONSE_DELAY = 8
42
43VAR_PROTOCOL_OPTIONS = 0
44VAR_STATUS_FLAGS = 1
45VAR_VIN_VOLTAGE = 3
46VAR_COMMAND_TIMEOUT = 5
47VAR_ERROR_RESPONSE = 7
48VAR_ERROR_MASK = 8
49VAR_JUMPER_STATE = 10
50VAR_UART_FAULTS = 11
51
52MVAR_PWM_MODE = 1
53MVAR_TARGET_SPEED = 2
54MVAR_TARGET_BRAKE_AMOUNT = 4
55MVAR_CURRENT_SPEED = 6
56MVAR_BUFFERED_SPEED = 8
57MVAR_MAX_ACCEL_FORWARD = 10
58MVAR_MAX_ACCEL_REVERSE = 12
59MVAR_MAX_DECEL_FORWARD = 14
60MVAR_MAX_DECEL_REVERSE = 16
61MVAR_STARTING_SPEED_FORWARD = 18
62MVAR_STARTING_SPEED_REVERSE = 20
63MVAR_DIRECTION_CHANGE_DELAY_FORWARD = 22
64MVAR_DIRECTION_CHANGE_DELAY_REVERSE = 23
65MVAR_MAX_DECEL_TMP = 24
66MVAR_CURRENT_LIMIT = 26
67MVAR_CURRENT_SENSE_RAW = 28
68MVAR_CURRENT_SENSE_SPEED = 30
69MVAR_CURRENT_SENSE_PROCESSED = 32
70MVAR_CURRENT_SENSE_OFFSET = 34
71MVAR_CURRENT_SENSE_MINIMUM_DIVISOR = 35
72
73PROTOCOL_OPTION_CRC_FOR_COMMANDS = 0
74PROTOCOL_OPTION_CRC_FOR_RESPONSES = 1
75PROTOCOL_OPTION_I2C_GENERAL_CALL = 2
76
77COMMUNICATION_OPTION_7BIT_RESPONSES = 0
78COMMUNICATION_OPTION_14BIT_DEVICE_NUMBER = 1
79COMMUNICATION_OPTION_ERR_IS_DE = 2
80
81STATUS_FLAG_PROTOCOL_ERROR = 0
82STATUS_FLAG_CRC_ERROR = 1
83STATUS_FLAG_COMMAND_TIMEOUT_LATCHED = 2
84STATUS_FLAG_MOTOR_FAULT_LATCHED = 3
85STATUS_FLAG_NO_POWER_LATCHED = 4
86STATUS_FLAG_UART_ERROR = 5
87STATUS_FLAG_RESET = 9
88STATUS_FLAG_COMMAND_TIMEOUT = 10
89STATUS_FLAG_MOTOR_FAULTING = 11
90STATUS_FLAG_NO_POWER = 12
91STATUS_FLAG_ERROR_ACTIVE = 13
92STATUS_FLAG_MOTOR_OUTPUT_ENABLED = 14
93STATUS_FLAG_MOTOR_DRIVING = 15
94
95UART_FAULT_FRAMING = 0
96UART_FAULT_NOISE = 1
97UART_FAULT_HARDWARE_OVERRUN = 2
98UART_FAULT_SOFTWARE_OVERRUN = 3
99
100ERROR_RESPONSE_COAST = 0
101ERROR_RESPONSE_BRAKE = 1
102ERROR_RESPONSE_COAST_NOW = 2
103ERROR_RESPONSE_BRAKE_NOW = 3
104
105PWM_MODE_DEFAULT = 0
106PWM_MODE_1_KHZ = 1
107PWM_MODE_2_KHZ = 2
108PWM_MODE_4_KHZ = 3
109PWM_MODE_5_KHZ = 4
110PWM_MODE_10_KHZ = 5
111PWM_MODE_20_KHZ = 6
112PWM_MODE_40_KHZ = 7
113PWM_MODE_80_KHZ = 8
114
115JMP1_INSTALLED = 0
116JMP1_NOT_INSTALLED = 1
117
118CLEAR_MOTOR_FAULT_UNCONDITIONAL = 0
119
120ERROR_CHECK_CONTINUE = 0x3C
121ERROR_CHECK_DONE = 0x00
122
123MAX_SPEED = 800
124MAX_ACCEL = 6400
125MAX_DIRECTION_CHANGE_DELAY = 250
126
127LATCHED_STATUS_FLAGS = 0x03FF
128MAX_ERROR_MASK = 0x07FF
129
130MAX_COMMAND_TIMEOUT = 16250
131
132MIN_BAUD_RATE = 245
133MAX_BAUD_RATE = 1000000
134
135CRC_TABLE = [
136 0x00, 0x41, 0x13, 0x52, 0x26, 0x67, 0x35, 0x74,
137 0x4c, 0x0d, 0x5f, 0x1e, 0x6a, 0x2b, 0x79, 0x38,
138 0x09, 0x48, 0x1a, 0x5b, 0x2f, 0x6e, 0x3c, 0x7d,
139 0x45, 0x04, 0x56, 0x17, 0x63, 0x22, 0x70, 0x31,
140 0x12, 0x53, 0x01, 0x40, 0x34, 0x75, 0x27, 0x66,
141 0x5e, 0x1f, 0x4d, 0x0c, 0x78, 0x39, 0x6b, 0x2a,
142 0x1b, 0x5a, 0x08, 0x49, 0x3d, 0x7c, 0x2e, 0x6f,
143 0x57, 0x16, 0x44, 0x05, 0x71, 0x30, 0x62, 0x23,
144 0x24, 0x65, 0x37, 0x76, 0x02, 0x43, 0x11, 0x50,
145 0x68, 0x29, 0x7b, 0x3a, 0x4e, 0x0f, 0x5d, 0x1c,
146 0x2d, 0x6c, 0x3e, 0x7f, 0x0b, 0x4a, 0x18, 0x59,
147 0x61, 0x20, 0x72, 0x33, 0x47, 0x06, 0x54, 0x15,
148 0x36, 0x77, 0x25, 0x64, 0x10, 0x51, 0x03, 0x42,
149 0x7a, 0x3b, 0x69, 0x28, 0x5c, 0x1d, 0x4f, 0x0e,
150 0x3f, 0x7e, 0x2c, 0x6d, 0x19, 0x58, 0x0a, 0x4b,
151 0x73, 0x32, 0x60, 0x21, 0x55, 0x14, 0x46, 0x07,
152 0x48, 0x09, 0x5b, 0x1a, 0x6e, 0x2f, 0x7d, 0x3c,
153 0x04, 0x45, 0x17, 0x56, 0x22, 0x63, 0x31, 0x70,
154 0x41, 0x00, 0x52, 0x13, 0x67, 0x26, 0x74, 0x35,
155 0x0d, 0x4c, 0x1e, 0x5f, 0x2b, 0x6a, 0x38, 0x79,
156 0x5a, 0x1b, 0x49, 0x08, 0x7c, 0x3d, 0x6f, 0x2e,
157 0x16, 0x57, 0x05, 0x44, 0x30, 0x71, 0x23, 0x62,
158 0x53, 0x12, 0x40, 0x01, 0x75, 0x34, 0x66, 0x27,
159 0x1f, 0x5e, 0x0c, 0x4d, 0x39, 0x78, 0x2a, 0x6b,
160 0x6c, 0x2d, 0x7f, 0x3e, 0x4a, 0x0b, 0x59, 0x18,
161 0x20, 0x61, 0x33, 0x72, 0x06, 0x47, 0x15, 0x54,
162 0x65, 0x24, 0x76, 0x37, 0x43, 0x02, 0x50, 0x11,
163 0x29, 0x68, 0x3a, 0x7b, 0x0f, 0x4e, 0x1c, 0x5d,
164 0x7e, 0x3f, 0x6d, 0x2c, 0x58, 0x19, 0x4b, 0x0a,
165 0x32, 0x73, 0x21, 0x60, 0x14, 0x55, 0x07, 0x46,
166 0x77, 0x36, 0x64, 0x25, 0x51, 0x10, 0x42, 0x03,
167 0x3b, 0x7a, 0x28, 0x69, 0x1d, 0x5c, 0x0e, 0x4f,
168]
169
170
171
172def calculate_crc(buffer):
173 """
174 This method calculates the 7-bit CRC needed for a Motoron
175 command or response. Most users will not need to use this, since most
176 methods in this library automatically append a CRC byte or check
177 received CRC bytes when appropriate.
178 """
179 crc = 0
180 for byte in buffer:
181 crc = CRC_TABLE[crc ^ byte]
182 return crc
183