Go to the documentation of this file.00001
00031 #include <ubiquity_motor/motor_message.h>
00032
00033 #include <ros/console.h>
00034
00035 uint8_t const MotorMessage::valid_types[] = {
00036 TYPE_READ,
00037 TYPE_WRITE,
00038 TYPE_RESPONSE
00039 };
00040
00041 uint8_t const MotorMessage::valid_registers[] = {
00042 REG_STOP_START,
00043 REG_BRAKE_STOP,
00044 REG_CRUISE_STOP,
00045 REG_LEFT_PWM,
00046 REG_RIGHT_PWM,
00047 REG_LEFT_SPEED_SET,
00048 REG_RIGHT_SPEED_SET,
00049 REG_LEFT_RAMP,
00050 REG_RIGHT_RAMP,
00051 REG_LEFT_ODOM,
00052 REG_RIGHT_ODOM,
00053 REG_DEADMAN,
00054 REG_LEFT_CURRENT,
00055 REG_RIGHT_CURRENT,
00056 REG_ERROR_COUNT,
00057 REG_5V_MAIN_ERROR,
00058 REG_5V_AUX_ERROR,
00059 REG_12V_MAIN_ERROR,
00060 REG_12V_AUX_ERROR,
00061 REG_5V_MAIN_OL,
00062 REG_5V_AUX_OL,
00063 REG_12V_MAIN_OL,
00064 REG_12V_AUX_OL,
00065 REG_LEFT_MOTOR_ERROR,
00066 REG_RIGHT_MOTOR_ERROR,
00067 REG_PARAM_P,
00068 REG_PARAM_I,
00069 REG_PARAM_D,
00070 REG_PARAM_C,
00071 REG_LED_1,
00072 REG_LED_2,
00073 REG_HARDWARE_VERSION,
00074 REG_FIRMWARE_VERSION,
00075 REG_BATTERY_VOLTAGE,
00076 REG_5V_MAIN_CURRENT,
00077 REG_12V_MAIN_CURRENT,
00078 REG_5V_AUX_CURRENT,
00079 REG_12V_AUX_CURRENT,
00080 REG_LEFT_SPEED_MEASURED,
00081 REG_RIGHT_SPEED_MEASURED
00082 };
00083
00084 void MotorMessage::setType(MotorMessage::MessageTypes type){
00085 if (verifyType(type)){
00086 this->type = type;
00087 }
00088 }
00089
00090 MotorMessage::MessageTypes MotorMessage::getType() const{
00091 if (verifyType(this->type)){
00092 return static_cast<MotorMessage::MessageTypes>(this->type);
00093 }
00094 }
00095
00096 void MotorMessage::setRegister(MotorMessage::Registers reg){
00097 if (verifyRegister(reg)){
00098 this->register_addr = reg;
00099 }
00100 }
00101
00102 MotorMessage::Registers MotorMessage::getRegister() const{
00103 return static_cast<MotorMessage::Registers>(this->register_addr);
00104 }
00105
00106
00107
00108
00109
00110 void MotorMessage::setData(int32_t data){
00111
00112 this->data[3] = (data >> 0) & 0xFF;
00113 this->data[2] = (data >> 8) & 0xFF;
00114 this->data[1] = (data >> 16) & 0xFF;
00115 this->data[0] = (data >> 24) & 0xFF;
00116 }
00117
00118 int32_t MotorMessage::getData() const{
00119
00120 return (this->data[0] << 24)
00121 | (this->data[1] << 16)
00122 | (this->data[2] << 8)
00123 | (this->data[3] << 0);
00124 }
00125
00126 std::vector<uint8_t> MotorMessage::serialize() const{
00127 std::vector<uint8_t> out(9);
00128 out[0] = delimeter;
00129 out[1] = protocol_version;
00130 out[2] = type;
00131 out[3] = register_addr;
00132 out[4] = data[0];
00133 out[5] = data[1];
00134 out[6] = data[2];
00135 out[7] = data[3];
00136 out[8] = generateChecksum(out);
00137 return out;
00138 }
00139
00140 int MotorMessage::deserialize(const std::vector<uint8_t> &serialized){
00141 if(serialized[0] == delimeter) {
00142 if (serialized[1] == protocol_version) {
00143 if (generateChecksum(serialized) == serialized[8]) {
00144 if (verifyType(serialized[2])) {
00145 if (verifyRegister(serialized[3])) {
00146 this->type = serialized[2];
00147 this->register_addr = serialized[3];
00148 this->data[0] = serialized[4];
00149 this->data[1] = serialized[5];
00150 this->data[2] = serialized[6];
00151 this->data[3] = serialized[7];
00152 return 0;
00153 }
00154 else
00155 return 5;
00156 }
00157 else
00158 return 4;
00159 }
00160 else
00161 return 3;
00162 }
00163 else return 2;
00164 }
00165 else
00166 return 1;
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 }
00177 int MotorMessage::verifyType(uint8_t t) {
00178
00179
00180 for (int i = 0; i < sizeof(valid_types) / sizeof(valid_types[0]); ++i)
00181 {
00182 if (t == valid_types[i])
00183 return 1;
00184 }
00185 return 0;
00186 }
00187
00188 int MotorMessage::verifyRegister(uint8_t r) {
00189
00190
00191 for (int i = 0; i < sizeof(valid_registers) / sizeof(valid_registers[0]); ++i)
00192 {
00193 if (r == valid_registers[i])
00194 return 1;
00195 }
00196 return 0;
00197 }
00198
00199 uint8_t MotorMessage::generateChecksum(const std::vector<uint8_t> &data) {
00200 int sum = data [1] + data[2] + data[3] + data[4] + data[5] + data[6] + data[7];
00201
00202 if (sum > 0xFF) {
00203 int tmp;
00204 tmp = sum >> 8;
00205 tmp = tmp << 8;
00206 return 0xFF - (sum-tmp);
00207 }
00208 else {
00209 return 0xFF - sum;
00210 }
00211
00212 }