motor_message.cc
Go to the documentation of this file.
1 
32 // #include <ubiquity_motor/motor_message_registers.h>
33 #include <ros/console.h>
34 #include <numeric>
35 
36 uint8_t const MotorMessage::valid_types[] = {TYPE_READ, TYPE_WRITE,
37  TYPE_RESPONSE, TYPE_ERROR};
38 
39 uint8_t const MotorMessage::valid_registers[] = {REG_STOP_START,
40  REG_BRAKE_STOP,
41  REG_CRUISE_STOP,
42  REG_LEFT_PWM,
43  REG_RIGHT_PWM,
44  REG_LEFT_SPEED_SET,
45  REG_RIGHT_SPEED_SET,
46  REG_LEFT_RAMP,
47  REG_RIGHT_RAMP,
48  REG_LEFT_ODOM,
49  REG_RIGHT_ODOM,
50  REG_DEADMAN,
51  REG_LEFT_CURRENT,
52  REG_RIGHT_CURRENT,
53  REG_ERROR_COUNT,
54  REG_5V_MAIN_ERROR,
55  REG_5V_AUX_ERROR,
56  REG_12V_MAIN_ERROR,
57  REG_12V_AUX_ERROR,
58  REG_5V_MAIN_OL,
59  REG_5V_AUX_OL,
60  REG_12V_MAIN_OL,
61  REG_12V_AUX_OL,
62  REG_LEFT_MOTOR_ERROR,
63  REG_RIGHT_MOTOR_ERROR,
64  REG_PARAM_P,
65  REG_PARAM_I,
66  REG_PARAM_D,
67  REG_PARAM_C,
68  REG_LED_1,
69  REG_LED_2,
70  REG_HARDWARE_VERSION,
71  REG_FIRMWARE_VERSION,
72  REG_BATTERY_VOLTAGE,
73  REG_5V_MAIN_CURRENT,
74  REG_12V_MAIN_CURRENT,
75  REG_5V_AUX_CURRENT,
76  REG_12V_AUX_CURRENT,
77  REG_LEFT_SPEED_MEASURED,
78  REG_RIGHT_SPEED_MEASURED,
79  REG_BOTH_SPEED_SET,
80  REG_MOVING_BUF_SIZE,
81  REG_BOTH_ODOM,
82  REG_LIMIT_REACHED,
83  REG_BOTH_ERROR,
84  DEBUG_50,
85  DEBUG_51,
86  DEBUG_52,
87  DEBUG_53,
88  DEBUG_54,
89  DEBUG_55,
90  DEBUG_56,
91  DEBUG_57,
92  DEBUG_58};
93 
95  if (verifyType(type)) {
96  this->type = type;
97  }
98 }
99 
101  return static_cast<MotorMessage::MessageTypes>(this->type);
102 }
103 
105  if (verifyRegister(reg)) {
106  this->register_addr = reg;
107  }
108 }
109 
111  return static_cast<MotorMessage::Registers>(this->register_addr);
112 }
113 
115  // Spilt 32 bit data (system byte order) into 4 8bit elements in big endian
116  // (network byte order)
117  this->data[3] = (data >> 0) & 0xFF;
118  this->data[2] = (data >> 8) & 0xFF;
119  this->data[1] = (data >> 16) & 0xFF;
120  this->data[0] = (data >> 24) & 0xFF;
121 }
122 
123 int32_t MotorMessage::getData() const {
124  // Take big endian (network byte order) elements and return 32 bit int
125  return (this->data[0] << 24) | (this->data[1] << 16) |
126  (this->data[2] << 8) | (this->data[3] << 0);
127 }
128 
130  RawMotorMessage out;
131  out[0] = delimeter;
132  out[1] = (protocol_version << 4) | type;
133  out[2] = register_addr;
134  std::copy(data.begin(), data.end(), out.begin() + 3);
135  out[7] = generateChecksum(out);
136  return out;
137 }
138 
140  if (serialized[0] == delimeter) {
141  if ((serialized[1] & 0xF0) == (protocol_version << 4)) {
142  if (generateChecksum(serialized) == serialized[7]) {
143  if (verifyType(serialized[1] & 0x0F)) {
144  if (verifyRegister(serialized[2])) {
145  this->type = serialized[1] & 0x0F;
146  this->register_addr = serialized[2];
147  std::copy(serialized.begin() + 3,
148  serialized.begin() + 7, data.begin());
149  return 0;
150  } else
151  return 5;
152  } else
153  return 4;
154  } else
155  return 3;
156  } else
157  return 2;
158  } else
159  return 1;
160 
161  // TODO use exceptions instead of cryptic error codes
162 
163  // ERROR codes returned:
164  // 1 First char not delimiter
165  // 2 wrong protocol version
166  // 3 bad checksum
167  // 4 bad type
168  // 5 bad register
169 }
170 
171 int MotorMessage::verifyType(uint8_t t) {
172  // Return 1 good
173  // Return 0 for bad
174  for (size_t i = 0; i < sizeof(valid_types) / sizeof(valid_types[0]); ++i) {
175  if (t == valid_types[i]) return 1;
176  }
177  return 0;
178 }
179 
181  // Return 1 good
182  // Return 0 for bad
183  for (size_t i = 0; i < sizeof(valid_registers) / sizeof(valid_registers[0]);
184  ++i) {
185  if (r == valid_registers[i]) return 1;
186  }
187  return 0;
188 }
189 
191  int sum = std::accumulate(data.begin() + 1, data.end() - 1, 0);
192 
193  if (sum > 0xFF) {
194  int tmp;
195  tmp = sum >> 8;
196  tmp = tmp << 8;
197  return 0xFF - (sum - tmp);
198  } else {
199  return 0xFF - sum;
200  }
201 }
static const uint8_t delimeter
MotorMessage::MessageTypes getType() const
void setData(int32_t data)
static uint8_t generateChecksum(const std::vector< uint8_t > &data)
MotorMessage::Registers getRegister() const
boost::array< uint8_t, 8 > RawMotorMessage
Definition: motor_message.h:38
static const uint8_t protocol_version
static const uint8_t valid_types[]
static int verifyType(uint8_t t)
uint8_t register_addr
RawMotorMessage serialize() const
boost::array< uint8_t, 4 > data
void setRegister(MotorMessage::Registers reg)
void setType(MotorMessage::MessageTypes type)
static const uint8_t valid_registers[]
static int verifyRegister(uint8_t r)
int deserialize(const RawMotorMessage &serialized)
int32_t getData() const


ubiquity_motor
Author(s):
autogenerated on Mon Jun 10 2019 15:37:24