motor_message.h
Go to the documentation of this file.
1 
31 #ifndef MOTORMESSAGE_H
32 #define MOTORMESSAGE_H
33 
34 #include <stdint.h>
35 #include <boost/array.hpp>
36 #include <vector>
37 
38 typedef boost::array<uint8_t, 8> RawMotorMessage;
39 
40 // To support enhanced firmware we identify the fw version for new registers
41 // The idea is we do not want to make firmware message requests till a feature is supported
42 #define MIN_FW_RECOMMENDED 32
43 #define MIN_FW_MOT_POW_ACTIVE 32
44 #define MIN_FW_ESTOP_SUPPORT 32
45 #define MIN_FW_HW_VERSION_SET 32
46 #define MIN_FW_MAX_SPEED_AND_PWM 34
47 #define MIN_FW_ENC_6_STATE 35
48 #define MIN_FW_FIRMWARE_DATE 35
49 #define MIN_FW_DEADZONE 35
50 #define MIN_FW_PID_V_TERM 35
51 
52 // It is CRITICAL that the values in the Registers enum remain in sync with Firmware register numbers.
53 // In fact once a register is defined and released, it should NOT be re-used at a later time for another purpose
54 //
55 class MotorMessage {
56 public:
57  // Using default constructor and destructor
60 
61  // MessageTypes enum in class to avoid global namespace pollution
62  enum MessageTypes {
63  TYPE_READ = 0xA,
64  TYPE_WRITE = 0xB,
66  TYPE_ERROR = 0xD
67  };
68 
69  // Registers enum in class to avoid global namespace pollution
70  enum Registers {
74 
75  REG_LEFT_PWM = 0x03,
76  REG_RIGHT_PWM = 0x04,
77 
78  // skip 0x05 and 0x06
79 
82 
83  REG_LEFT_RAMP = 0x09,
85 
86  REG_LEFT_ODOM = 0x0B,
88 
89  REG_DEADMAN = 0x0D,
90 
93 
104 
105  REG_PARAM_V = 0x1A, // New in final v35 firmware and was an obsolite value before then
106  REG_PARAM_P = 0x1B,
107  REG_PARAM_I = 0x1C,
108  REG_PARAM_D = 0x1D,
109  REG_PARAM_C = 0x1E,
110 
111  REG_LED_1 = 0x1F,
112  REG_LED_2 = 0x20,
113 
114  REG_HARDWARE_VERSION = 0x21, // The hardware revision time 10. This must be read from motor controler to initialize
116 
122 
125 
128 
132  REG_ROBOT_ID = 0x31, // Indicates 0 for Magni controller or 1 for Loki robot controller as of late 2018
133 
134  REG_MOT_PWR_ACTIVE = 0x32, // Readback register for host to know if motor controller thinks motor power is active
135  REG_ESTOP_ENABLE = 0x33, // An override that may be set to 0 to force motor controller firmware to NOT use some ESTOP logic
136  REG_PID_MAX_ERROR = 0x34, // A value that when non-zero enables motor firmware to limit harsh restarts in position after ESTOP release
137 
138  REG_MAX_SPEED_FWD = 0x35, // Max forward speed cap in a speed message
139  REG_MAX_SPEED_REV = 0x36, // Max reverse speed cap in a speed message (This is negative)
140  REG_MAX_PWM = 0x37, // The maximum wheel driver PWM value that will be used on the motor driver
141 
142  REG_HW_OPTIONS = 0x38, // Bitfield with options the firmware has been setup to use
143  REG_DEADZONE = 0x39, // Set to non zero to enable deadzone when stopped and speeds are zero
144  REG_FIRMWARE_DATE = 0x3a, // Read only firmware date as of version 35 firmware. 0x20190705 is July 5 2019
145 
146  DEBUG_50 = 0x50,
147  DEBUG_51 = 0x51,
148  DEBUG_52 = 0x52,
149  DEBUG_53 = 0x53,
150  DEBUG_54 = 0x54,
151  DEBUG_55 = 0x55,
152  DEBUG_56 = 0x56,
153  DEBUG_57 = 0x57,
154  DEBUG_58 = 0x58
155  };
156 
157  // Bitfield values for hardware options enabled in the firmware
158  enum HwOptions {
160  };
161 
162  // Bitfield indicating which limits have been reached
163  enum Limits {
164  LIM_M1_PWM = 0x10,
165  LIM_M2_PWM = 0x01,
171  };
172 
173  // State bits for motor power
174  const static int32_t MOT_POW_ACTIVE = 0x0001;
175 
178 
181 
182  void setData(int32_t data);
183  int32_t getData() const;
184 
185  RawMotorMessage serialize() const;
186 
187 
188  // Error Codes that can be returned by deserializaion
189  enum ErrorCodes {
190  ERR_NONE = 0, // Success code
196  };
197 
199 
200  const static uint8_t delimeter = 0x7E;
201 
202 private:
203  // Type of message should be in MotorMessage::MessageTypes
204  uint8_t type;
205  // Register address should be in MotorMessage::Registers
206  uint8_t register_addr;
207 
208  // 4 bytes of data, numbers should be in big endian format
209  boost::array<uint8_t, 4> data;
210 
211  const static uint8_t protocol_version =
212  0x03; // Hard coded for now, should be parameterized
213 
214  const static uint8_t valid_types[];
215  const static uint8_t valid_registers[];
216 
217  static int verifyType(uint8_t t);
218  static int verifyRegister(uint8_t r);
219 
220  static uint8_t generateChecksum(const std::vector<uint8_t> &data);
221  static uint8_t generateChecksum(const RawMotorMessage &data);
222 };
223 
224 #endif
static const uint8_t delimeter
void setData(int32_t data)
static uint8_t generateChecksum(const std::vector< uint8_t > &data)
boost::array< uint8_t, 8 > RawMotorMessage
Definition: motor_message.h:38
static const uint8_t protocol_version
static const uint8_t valid_types[]
MotorMessage::MessageTypes getType() const
static int verifyType(uint8_t t)
uint8_t register_addr
MotorMessage::ErrorCodes deserialize(const RawMotorMessage &serialized)
boost::array< uint8_t, 4 > data
int32_t getData() const
void setRegister(MotorMessage::Registers reg)
RawMotorMessage serialize() const
void setType(MotorMessage::MessageTypes type)
static const uint8_t valid_registers[]
static int verifyRegister(uint8_t r)
MotorMessage::Registers getRegister() const
static const int32_t MOT_POW_ACTIVE


ubiquity_motor
Author(s):
autogenerated on Mon Feb 28 2022 23:57:06