00001
00046 #include "roch_base/core/Message_cmd.h"
00047
00048
00049 #include <cstring>
00050 #include <iostream>
00051 #include <typeinfo>
00052
00053 using namespace std;
00054
00055 #include "roch_base/core/Number.h"
00056
00057 #ifdef LOGGING_AVAIL
00058 #include "roch_base/core/Logger.h"
00059 #endif
00060 #ifdef DEBUG_INFO
00061 #include "roch_base/roch_hardware.h"
00062 #endif
00063 namespace sawyer
00064 {
00065
00066 long CmdMessage::total_destroyed = 0;
00067 long CmdMessage::total_sent = 0;
00068
00069 CmdMessage::~CmdMessage()
00070 {
00071 ++total_destroyed;
00072 if (!is_sent)
00073 {
00074 #ifdef LOGGING_AVAIL
00075 CPR_WARN() << "Command message destroyed without being sent. Type: "
00076 << "0x" << hex << getType() << dec
00077 << ". Total unsent: " << (total_destroyed-total_sent) << endl;
00078 #endif
00079 }
00080 else
00081 {
00082 total_sent++;
00083 }
00084 }
00085
00086
00087 CmdProcessorReset::CmdProcessorReset() : CmdMessage()
00088 {
00089 setPayloadLength(2);
00090 utob(getPayloadPointer(), 2, (uint16_t) 0x3A18);
00091 setType(CMD_PROCESSOR_RESET);
00092 makeValid();
00093 }
00094
00095 CmdProcessorReset::CmdProcessorReset(const CmdProcessorReset &other) : CmdMessage(other)
00096 {
00097 }
00098
00099
00100 CmdRestoreSettings::CmdRestoreSettings(enum restoreFlags flags) : CmdMessage()
00101 {
00102 #ifdef DEBUG_INFO
00103 ROS_DEBUG_STREAM("CmdRestoreSettings: flags:%x",(uint8_t) (flags));
00104 #endif
00105 setPayloadLength(3);
00106
00107 utob(getPayloadPointer(), 2, (uint16_t) (0x3a18));
00108 *getPayloadPointer(2) = (uint8_t) (flags);
00109 setType(CMD_RESTORE_SETTINGS);
00110
00111 makeValid();
00112 }
00113
00114 CmdRestoreSettings::CmdRestoreSettings(const CmdRestoreSettings &other) : CmdMessage(other)
00115 {
00116 }
00117
00118
00119 CmdStoreSettings::CmdStoreSettings() : CmdMessage()
00120 {
00121 setPayloadLength(2);
00122
00123 utob(getPayloadPointer(), 2, (uint16_t) (0x3a18));
00124 setType(CMD_STORE_SETTINGS);
00125
00126 makeValid();
00127 }
00128
00129 CmdStoreSettings::CmdStoreSettings(const CmdStoreSettings &other) : CmdMessage(other)
00130 {
00131 }
00132
00133
00134 SetAckermannOutput::SetAckermannOutput(double steer, double throt, double brake) : CmdMessage()
00135 {
00136 #ifdef DEBUG_INFO
00137 ROS_DEBUG_STREAM("SetAckermannOutput: steer:%.2lf, throt:%.2lf, brake:%.2lf",steer,throt,brake);
00138 #endif
00139 setPayloadLength(PAYLOAD_LEN);
00140
00141 ftob(getPayloadPointer(STEERING), 2, steer, 100);
00142 ftob(getPayloadPointer(THROTTLE), 2, throt, 100);
00143 ftob(getPayloadPointer(BRAKE), 2, brake, 100);
00144
00145 setType(SET_ACKERMANN_SETPT);
00146 makeValid();
00147 }
00148
00149 SetAckermannOutput::SetAckermannOutput(const SetAckermannOutput &other) : CmdMessage(other)
00150 {
00151 }
00152
00153
00154 SetDifferentialControl::SetDifferentialControl(
00155 double p,
00156 double i,
00157 double d)
00158 : CmdMessage()
00159 {
00160 #ifdef DEBUG_INFO
00161 ROS_DEBUG_STREAM("SetDifferentialControl: P:%.2lf,I:%.2lf,D:%.2lf.", p, i, d);
00162 #endif
00163
00164 setPayloadLength(PAYLOAD_LEN);
00165
00166 ftob(getPayloadPointer(LEFT_P), 2, p, 10000);
00167 ftob(getPayloadPointer(LEFT_I), 2, i, 10000);
00168 ftob(getPayloadPointer(LEFT_D), 2, d, 10000);
00169
00170 ftob(getPayloadPointer(RIGHT_P), 2, p, 10000);
00171 ftob(getPayloadPointer(RIGHT_I), 2, i, 10000);
00172 ftob(getPayloadPointer(RIGHT_D), 2, d, 10000);
00173
00174 setType(SET_DIFF_CTRL_CONSTS);
00175 makeValid();
00176 }
00177
00178 SetDifferentialControl::SetDifferentialControl(
00179 double left_p,
00180 double left_i,
00181 double left_d,
00182 double right_p,
00183 double right_i,
00184 double right_d)
00185 : CmdMessage()
00186 {
00187 #ifdef DEBUG_INFO
00188 ROS_DEBUG_STREAM("SetDifferentialControl: left_p:%.2lf,left_i:%.2lf,left_d:%.2lf"
00189 "right_p:%.2lf,right_i:%.2lf,right_d:%.2lf.", left_p,
00190 left_i,
00191 left_d,
00192 right_p,
00193 right_i,
00194 right_d);
00195 #endif
00196 setPayloadLength(PAYLOAD_LEN);
00197
00198 ftob(getPayloadPointer(LEFT_P), 2, left_p, 10000);
00199 ftob(getPayloadPointer(LEFT_I), 2, left_i, 10000);
00200 ftob(getPayloadPointer(LEFT_D), 2, left_d, 10000);
00201
00202 ftob(getPayloadPointer(RIGHT_P), 2, right_p, 10000);
00203 ftob(getPayloadPointer(RIGHT_I), 2, right_i, 10000);
00204 ftob(getPayloadPointer(RIGHT_D), 2, right_d, 10000);
00205
00206 setType(SET_DIFF_CTRL_CONSTS);
00207 makeValid();
00208 }
00209
00210 SetDifferentialControl::SetDifferentialControl(const SetDifferentialControl &other)
00211 : CmdMessage(other)
00212 {
00213 }
00214
00215
00216 SetDifferentialOutput::SetDifferentialOutput(double left, double right) : CmdMessage()
00217 {
00218 #ifdef DEBUG_INFO
00219 ROS_DEBUG_STREAM("SetDifferentialOutput: left:%.2lf,right:%.2lf",left,right);
00220 #endif
00221 setPayloadLength(PAYLOAD_LEN);
00222 ftob(getPayloadPointer(LEFT), 2, left, 100);
00223 ftob(getPayloadPointer(RIGHT), 2, right, 100);
00224
00225 setType(SET_DIFF_WHEEL_SETPTS);
00226 makeValid();
00227 }
00228
00229 SetDifferentialOutput::SetDifferentialOutput(const SetDifferentialOutput &other)
00230 : CmdMessage(other)
00231 {
00232 }
00233
00234
00235 SetDifferentialSpeed::SetDifferentialSpeed(double left_speed, double right_speed,
00236 double left_accel, double right_accel)
00237 : CmdMessage()
00238 {
00239 setPayloadLength(PAYLOAD_LEN);
00240 ftob(getPayloadPointer(LEFT_SPEED), 2, left_speed, 100);
00241 ftob(getPayloadPointer(LEFT_ACCEL), 2, left_accel, 100);
00242 ftob(getPayloadPointer(RIGHT_SPEED), 2, right_speed, 100);
00243 ftob(getPayloadPointer(RIGHT_ACCEL), 2, right_accel, 100);
00244 #ifdef DEBUG_SEND_SPEED
00245 ROS_DEBUG_STREAM("SetDifferentialSpeed: left_speed:%.2lf,right_speed:%.2lf,left_accel:%.2lf,right_accel:%.2lf",left_speed,right_speed,left_accel,right_accel);
00246 #endif
00247 setType(SET_DIFF_WHEEL_SPEEDS);
00248 makeValid();
00249 }
00250
00251 SetDifferentialSpeed::SetDifferentialSpeed(const SetDifferentialSpeed &other) : CmdMessage(other)
00252 {
00253 }
00254
00255
00256 SetGear::SetGear(uint8_t gear) : CmdMessage()
00257 {
00258 #ifdef DEBUG_INFO
00259 ROS_DEBUG_STREAM("SetGear: gear:%x",gear);
00260 #endif
00261 setPayloadLength(1);
00262 getPayloadPointer()[0] = gear;
00263 setType(SET_GEAR_SETPOINT);
00264 makeValid();
00265 }
00266
00267 SetGear::SetGear(const SetGear &other) : CmdMessage(other)
00268 {
00269 }
00270
00271
00272 SetMaxAccel::SetMaxAccel(double max_fwd, double max_rev) : CmdMessage()
00273 {
00274 setPayloadLength(PAYLOAD_LEN);
00275
00276 ftob(getPayloadPointer(MAX_FWD), 2, max_fwd, 100);
00277 ftob(getPayloadPointer(MAX_REV), 2, max_rev, 100);
00278 #ifdef DEBUG_INFO
00279 ROS_DEBUG_STREAM("SetMaxAccel: max_fwd:%.2lf, max_rev:%.2lf",max_fwd,max_rev);
00280 #endif
00281 setType(SET_MAX_ACCEL);
00282 makeValid();
00283 }
00284
00285 SetMaxAccel::SetMaxAccel(const SetMaxAccel &other) : CmdMessage(other)
00286 {
00287 }
00288
00289
00290 SetMaxSpeed::SetMaxSpeed(double max_fwd, double max_rev) : CmdMessage()
00291 {
00292 setPayloadLength(PAYLOAD_LEN);
00293
00294 ftob(getPayloadPointer(MAX_FWD), 2, max_fwd, 100);
00295 ftob(getPayloadPointer(MAX_REV), 2, max_rev, 100);
00296 #ifdef DEBUG_INFO
00297 ROS_DEBUG_STREAM("SetMaxSpeed: max_fwd:%.2lf, max_rev:%.2lf",max_fwd,max_rev);
00298 #endif
00299 setType(SET_MAX_SPEED);
00300 makeValid();
00301 }
00302
00303 SetMaxSpeed::SetMaxSpeed(const SetMaxSpeed &other) : CmdMessage(other)
00304 {
00305 }
00306
00307
00308 SetPlatformName::SetPlatformName(const char *name) : CmdMessage()
00309 {
00310 size_t cpy_len = strlen(name);
00311 size_t max_len = MAX_MSG_LENGTH - HEADER_LENGTH - CRC_LENGTH - 1 ;
00312 if (cpy_len > max_len) { cpy_len = max_len; }
00313 #ifdef DEBUG_INFO
00314 ROS_DEBUG_STREAM("SetPlatformName: name:%s",name);
00315 #endif
00316 setPayloadLength(cpy_len + 1);
00317 getPayloadPointer()[0] = cpy_len;
00318 memcpy(getPayloadPointer(1), name, cpy_len);
00319
00320 setType(SET_PLATFORM_NAME);
00321
00322 makeValid();
00323 }
00324
00325 SetPlatformName::SetPlatformName(const SetPlatformName &other) : CmdMessage(other)
00326 {
00327 }
00328
00329
00330 SetPlatformTime::SetPlatformTime(uint32_t time) : CmdMessage()
00331 {
00332 #ifdef DEBUG_INFO
00333 ROS_DEBUG_STREAM("SetPlatformTime: name:%d",time);
00334 #endif
00335 setPayloadLength(4);
00336 utob(getPayloadPointer(), 4, time);
00337 setType(SET_PLATFORM_TIME);
00338 makeValid();
00339 }
00340
00341 SetPlatformTime::SetPlatformTime(const SetPlatformTime &other) : CmdMessage(other)
00342 {
00343 }
00344
00345
00346 SetSafetySystem::SetSafetySystem(uint16_t flags) : CmdMessage()
00347 {
00348 #ifdef DEBUG_INFO
00349 ROS_DEBUG_STREAM("SetSafetySystem: flags:%x",flags);
00350 #endif
00351 setPayloadLength(2);
00352 utob(getPayloadPointer(), 2, flags);
00353 setType(SET_SAFETY_SYSTEM);
00354 makeValid();
00355 }
00356
00357 SetSafetySystem::SetSafetySystem(const SetSafetySystem &other) : CmdMessage(other)
00358 {
00359 }
00360
00361
00362 SetTurn::SetTurn(double trans, double rad, double accel) : CmdMessage()
00363 {
00364 setPayloadLength(PAYLOAD_LEN);
00365 #ifdef DEBUG_INFO
00366 ROS_DEBUG_STREAM("SetTurn: rad:%.2lf, accel:%.2lf",trans,accel);
00367 #endif
00368
00369 ftob(getPayloadPointer(TRANSLATIONAL), 2, trans, 100);
00370 ftob(getPayloadPointer(TURN_RADIUS), 2, rad, 100);
00371 ftob(getPayloadPointer(TRANS_ACCEL), 2, accel, 100);
00372
00373 setType(SET_TURN_SETPT);
00374 makeValid();
00375 }
00376
00377 SetTurn::SetTurn(const SetTurn &other) : CmdMessage(other)
00378 {
00379 }
00380
00381 SetWheelInfo::SetWheelInfo(double wheel_gauge, double wheel_diameter) : CmdMessage()
00382 {
00383 setPayloadLength(PAYLOAD_LEN);
00384 #ifdef DEBUG_INFO
00385 ROS_DEBUG_STREAM("Set Wheel gauge: %.2lf, wheel diameter: %.2lf .",wheel_gauge,wheel_diameter);
00386 #endif
00387
00388 ftob(getPayloadPointer(WHEEL_GAUGE), 2, wheel_gauge, 10000);
00389 ftob(getPayloadPointer(WHEEL_DIAMETER), 2, wheel_diameter, 10000);
00390
00391 setType(SET_WHEEL_INFO);
00392 makeValid();
00393 }
00394
00395 SetWheelInfo::SetWheelInfo(const SetWheelInfo &other) : CmdMessage(other)
00396 {
00397 }
00398
00399 SetVelocity::SetVelocity(double trans, double rot, double accel) : CmdMessage()
00400 {
00401 setPayloadLength(PAYLOAD_LEN);
00402 #ifdef DEBUG_INFO
00403 ROS_DEBUG_STREAM("SetVelocity: trans:%.2lf, rot:%.2lf, accel:%.2lf",trans,rot,accel);
00404 #endif
00405
00406 ftob(getPayloadPointer(TRANSLATIONAL), 2, trans, 100);
00407 ftob(getPayloadPointer(ROTATIONAL), 2, rot, 100);
00408 ftob(getPayloadPointer(TRANS_ACCEL), 2, accel, 100);
00409
00410 setType(SET_VELOCITY_SETPT);
00411 makeValid();
00412 }
00413
00414 SetVelocity::SetVelocity(const SetVelocity &other) : CmdMessage(other)
00415 {
00416 }
00417
00418
00419 };
00420