pacmod_core.cpp
Go to the documentation of this file.
00001 /*
00002 * Unpublished Copyright (c) 2009-2017 AutonomouStuff, LLC, All Rights Reserved.
00003 *
00004 * This file is part of the PACMod ROS 1.0 driver which is released under the MIT license.
00005 * See file LICENSE included with this software or go to https://opensource.org/licenses/MIT for full license details.
00006 */
00007 
00008 #include <pacmod_core.h>
00009 
00010 using namespace AS::Drivers::PACMod;
00011 
00012 const int64_t AS::Drivers::PACMod::TurnSignalCmdMsg::CAN_ID = 0x63;
00013 const int64_t AS::Drivers::PACMod::TurnSignalRptMsg::CAN_ID = 0x64;
00014 const int64_t AS::Drivers::PACMod::ShiftCmdMsg::CAN_ID = 0x65;
00015 const int64_t AS::Drivers::PACMod::ShiftRptMsg::CAN_ID = 0x66;
00016 const int64_t AS::Drivers::PACMod::AccelCmdMsg::CAN_ID = 0x67;
00017 const int64_t AS::Drivers::PACMod::AccelRptMsg::CAN_ID = 0x68;
00018 const int64_t AS::Drivers::PACMod::GlobalCmdMsg::CAN_ID = 0x69;
00019 const int64_t AS::Drivers::PACMod::GlobalRptMsg::CAN_ID = 0x6A;
00020 const int64_t AS::Drivers::PACMod::BrakeCmdMsg::CAN_ID = 0x6B;
00021 const int64_t AS::Drivers::PACMod::SteerCmdMsg::CAN_ID = 0x6D;
00022 const int64_t AS::Drivers::PACMod::BrakeRptMsg::CAN_ID = 0x6C;
00023 const int64_t AS::Drivers::PACMod::SteerRptMsg::CAN_ID = 0x6E;
00024 const int64_t AS::Drivers::PACMod::VehicleSpeedRptMsg::CAN_ID = 0x6F;
00025 const int64_t AS::Drivers::PACMod::BrakeMotorRpt1Msg::CAN_ID = 0x70;
00026 const int64_t AS::Drivers::PACMod::BrakeMotorRpt2Msg::CAN_ID = 0x71;
00027 const int64_t AS::Drivers::PACMod::BrakeMotorRpt3Msg::CAN_ID = 0x72;
00028 const int64_t AS::Drivers::PACMod::SteerMotorRpt1Msg::CAN_ID = 0x73;
00029 const int64_t AS::Drivers::PACMod::SteerMotorRpt2Msg::CAN_ID = 0x74;
00030 const int64_t AS::Drivers::PACMod::SteerMotorRpt3Msg::CAN_ID = 0x75;
00031 const int64_t AS::Drivers::PACMod::HeadlightCmdMsg::CAN_ID = 0x76;
00032 const int64_t AS::Drivers::PACMod::HeadlightRptMsg::CAN_ID = 0x77;
00033 const int64_t AS::Drivers::PACMod::HornCmdMsg::CAN_ID = 0x78;
00034 const int64_t AS::Drivers::PACMod::HornRptMsg::CAN_ID = 0x79;
00035 const int64_t AS::Drivers::PACMod::WheelSpeedRptMsg::CAN_ID = 0x7A;
00036 const int64_t AS::Drivers::PACMod::SteeringPIDRpt1Msg::CAN_ID = 0x7B;
00037 const int64_t AS::Drivers::PACMod::SteeringPIDRpt2Msg::CAN_ID = 0x7C;
00038 const int64_t AS::Drivers::PACMod::SteeringPIDRpt3Msg::CAN_ID = 0x7D;
00039 const int64_t AS::Drivers::PACMod::SteerRpt2Msg::CAN_ID = 0x7E;
00040 const int64_t AS::Drivers::PACMod::SteerRpt3Msg::CAN_ID = 0x7F;
00041 const int64_t AS::Drivers::PACMod::ParkingBrakeStatusRptMsg::CAN_ID = 0x80;
00042 const int64_t AS::Drivers::PACMod::YawRateRptMsg::CAN_ID = 0x81;
00043 const int64_t AS::Drivers::PACMod::LatLonHeadingRptMsg::CAN_ID = 0x82;
00044 const int64_t AS::Drivers::PACMod::DateTimeRptMsg::CAN_ID = 0x83;
00045 const int64_t AS::Drivers::PACMod::SteeringPIDRpt4Msg::CAN_ID = 0x84;
00046 const int64_t AS::Drivers::PACMod::WiperCmdMsg::CAN_ID = 0x90;
00047 const int64_t AS::Drivers::PACMod::WiperRptMsg::CAN_ID = 0x91;
00048 const int64_t AS::Drivers::PACMod::VinRptMsg::CAN_ID = 0xFF;
00049 
00050 std::shared_ptr<PacmodTxMsg> PacmodTxMsg::make_message(const int64_t& can_id)
00051 {
00052   switch (can_id)
00053   {
00054   case TurnSignalRptMsg::CAN_ID:
00055     return std::shared_ptr<PacmodTxMsg>(new TurnSignalRptMsg);
00056     break;
00057   case ShiftRptMsg::CAN_ID:
00058     return std::shared_ptr<PacmodTxMsg>(new ShiftRptMsg);
00059     break;
00060   case AccelRptMsg::CAN_ID:
00061     return std::shared_ptr<PacmodTxMsg>(new AccelRptMsg);
00062     break;
00063   case GlobalRptMsg::CAN_ID:
00064     return std::shared_ptr<PacmodTxMsg>(new GlobalRptMsg);
00065     break;
00066   case BrakeRptMsg::CAN_ID:
00067     return std::shared_ptr<PacmodTxMsg>(new BrakeRptMsg);
00068     break;
00069   case SteerRptMsg::CAN_ID:
00070     return std::shared_ptr<PacmodTxMsg>(new SteerRptMsg);
00071     break;
00072   case VehicleSpeedRptMsg::CAN_ID:
00073     return std::shared_ptr<PacmodTxMsg>(new VehicleSpeedRptMsg);
00074     break;
00075   case BrakeMotorRpt1Msg::CAN_ID:
00076     return std::shared_ptr<PacmodTxMsg>(new BrakeMotorRpt1Msg);
00077     break;
00078   case BrakeMotorRpt2Msg::CAN_ID:
00079     return std::shared_ptr<PacmodTxMsg>(new BrakeMotorRpt2Msg);
00080     break;
00081   case BrakeMotorRpt3Msg::CAN_ID:
00082     return std::shared_ptr<PacmodTxMsg>(new BrakeMotorRpt3Msg);
00083     break;
00084   case SteerMotorRpt1Msg::CAN_ID:
00085     return std::shared_ptr<PacmodTxMsg>(new SteerMotorRpt1Msg);
00086     break;
00087   case SteerMotorRpt2Msg::CAN_ID:
00088     return std::shared_ptr<PacmodTxMsg>(new SteerMotorRpt2Msg);
00089     break;
00090   case SteerMotorRpt3Msg::CAN_ID:
00091     return std::shared_ptr<PacmodTxMsg>(new SteerMotorRpt3Msg);
00092     break;
00093   case HeadlightRptMsg::CAN_ID:
00094     return std::shared_ptr<PacmodTxMsg>(new HeadlightRptMsg);
00095     break;
00096   case HornRptMsg::CAN_ID:
00097     return std::shared_ptr<PacmodTxMsg>(new HornRptMsg);
00098     break;
00099   case WheelSpeedRptMsg::CAN_ID:
00100     return std::shared_ptr<PacmodTxMsg>(new WheelSpeedRptMsg);
00101     break;
00102   case SteeringPIDRpt1Msg::CAN_ID:
00103     return std::shared_ptr<PacmodTxMsg>(new SteeringPIDRpt1Msg);
00104     break;
00105   case SteeringPIDRpt2Msg::CAN_ID:
00106     return std::shared_ptr<PacmodTxMsg>(new SteeringPIDRpt2Msg);
00107     break;
00108   case SteeringPIDRpt3Msg::CAN_ID:
00109     return std::shared_ptr<PacmodTxMsg>(new SteeringPIDRpt3Msg);
00110     break;
00111   case SteerRpt2Msg::CAN_ID:
00112     return std::shared_ptr<PacmodTxMsg>(new SteerRpt2Msg);
00113     break;
00114   case SteerRpt3Msg::CAN_ID:
00115     return std::shared_ptr<PacmodTxMsg>(new SteerRpt3Msg);
00116     break;
00117   case ParkingBrakeStatusRptMsg::CAN_ID:
00118     return std::shared_ptr<PacmodTxMsg>(new ParkingBrakeStatusRptMsg);
00119     break;
00120   case YawRateRptMsg::CAN_ID:
00121     return std::shared_ptr<PacmodTxMsg>(new YawRateRptMsg);
00122     break;
00123   case LatLonHeadingRptMsg::CAN_ID:
00124     return std::shared_ptr<PacmodTxMsg>(new LatLonHeadingRptMsg);
00125     break;
00126   case DateTimeRptMsg::CAN_ID:
00127     return std::shared_ptr<PacmodTxMsg>(new DateTimeRptMsg);
00128     break;
00129   case SteeringPIDRpt4Msg::CAN_ID:
00130     return std::shared_ptr<PacmodTxMsg>(new SteeringPIDRpt4Msg);
00131     break;
00132   case WiperRptMsg::CAN_ID:
00133     return std::shared_ptr<PacmodTxMsg>(new WiperRptMsg);
00134     break;
00135   case VinRptMsg::CAN_ID:
00136     return std::shared_ptr<PacmodTxMsg>(new VinRptMsg);
00137     break;
00138   default:
00139     return NULL;
00140   }
00141 }
00142 
00143 // TX Messages
00144 void GlobalRptMsg::parse(uint8_t *in)
00145 {
00146   enabled = in[0] & 0x01;
00147   override_active = ((in[0] & 0x02) >> 1) != 0;
00148   user_can_timeout = ((in[0] & 0x20) >> 5) != 0;
00149   brake_can_timeout = ((in[0] & 0x10) >> 4) != 0;
00150   steering_can_timeout = ((in[0] & 0x08) >> 3) != 0;
00151   vehicle_can_timeout = ((in[0] & 0x04) >> 2) != 0;
00152   user_can_read_errors = ((in[6] << 8) | in[7]);
00153 }
00154 
00155 void VinRptMsg::parse(uint8_t *in)
00156 {
00157   std::ostringstream oss;
00158   oss << in[0] << in[1] << in[2];
00159   mfg_code = oss.str();
00160 
00161   if (mfg_code == "52C")
00162     mfg = "POLARIS INDUSTRIES INC.";
00163   else if (mfg_code == "3HS")
00164     mfg = "NAVISTAR, INC.";
00165   else if (mfg_code == "2T2")
00166     mfg = "TOYOTA MOTOR MANUFACTURING CANADA";
00167 
00168   model_year_code = in[3];
00169 
00170   if (model_year_code >= '1' && model_year_code <= '9')
00171   {
00172     model_year = 2000 + model_year_code;
00173   }
00174   else if (model_year_code >= 'A' && model_year_code < 'Z')
00175   {
00176     switch (model_year_code)
00177     {
00178     case 'A':
00179       model_year = 2010;
00180       break;
00181     case 'B':
00182       model_year = 2011;
00183       break;
00184     case 'C':
00185       model_year = 2012;
00186       break;
00187     case 'D':
00188       model_year = 2013;
00189       break;
00190     case 'E':
00191       model_year = 2014;
00192       break;
00193     case 'F':
00194       model_year = 2015;
00195       break;
00196     case 'G':
00197       model_year = 2016;
00198       break;
00199     case 'H':
00200       model_year = 2017;
00201       break;
00202     case 'J':
00203       model_year = 2018;
00204       break;
00205     case 'K':
00206       model_year = 2019;
00207       break;
00208     case 'L':
00209       model_year = 2020;
00210       break;
00211     case 'M':
00212       model_year = 2021;
00213       break;
00214     case 'N':
00215       model_year = 2022;
00216       break;
00217     case 'P':
00218       model_year = 2023;
00219       break;
00220     case 'R':
00221       model_year = 2024;
00222       break;
00223     case 'S':
00224       model_year = 2025;
00225       break;
00226     case 'T':
00227       model_year = 2026;
00228       break;
00229     case 'V':
00230       model_year = 2027;
00231       break;
00232     case 'W':
00233       model_year = 2028;
00234       break;
00235     case 'X':
00236       model_year = 2029;
00237       break;
00238     case 'Y':
00239       model_year = 2030;
00240       break;
00241     }
00242   }
00243 
00244   serial = (in[4] & 0x0F);
00245   serial = (serial << 8) | in[5];
00246   serial = (serial << 8) | in[6];
00247 }
00248 
00249 void SystemRptIntMsg::parse(uint8_t *in)
00250 {
00251   manual_input = in[0];
00252   command = in[1];
00253   output = in[2];
00254 }
00255 
00256 void SystemRptFloatMsg::parse(uint8_t *in)
00257 {
00258   int16_t temp;
00259 
00260   temp = (static_cast<int16_t>(in[0]) << 8) | in[1];
00261   manual_input = static_cast<double>(temp / 1000.0);
00262 
00263   temp = (static_cast<int16_t>(in[2]) << 8) | in[3];
00264   command = static_cast<double>(temp / 1000.0);
00265 
00266   temp = (static_cast<int16_t>(in[4]) << 8) | in[5];
00267   output = static_cast<double>(temp / 1000.0);
00268 }
00269 
00270 void VehicleSpeedRptMsg::parse(uint8_t *in)
00271 {
00272   int16_t temp;
00273 
00274   temp = (static_cast<int16_t>(in[0]) << 8) | in[1];
00275   vehicle_speed = static_cast<double>(temp / 100.0);
00276 
00277   vehicle_speed_valid = (in[2] == 1);
00278   vehicle_speed_raw[0] = in[3];
00279   vehicle_speed_raw[1] = in[4];
00280 }
00281 
00282 void YawRateRptMsg::parse(uint8_t *in)
00283 {
00284   int16_t temp;
00285 
00286   temp = (static_cast<int16_t>(in[0]) << 8) | in[1];
00287   yaw_rate = static_cast<double>(temp / 100.0);
00288 }
00289 
00290 void LatLonHeadingRptMsg::parse(uint8_t *in)
00291 {
00292   latitude_degrees = static_cast<int8_t>(in[0]);
00293   latitude_minutes = in[1];
00294   latitude_seconds = in[2];
00295   longitude_degrees = static_cast<int8_t>(in[3]);
00296   longitude_minutes = in[4];
00297   longitude_seconds = in[5];
00298   heading = ((static_cast<int16_t>(in[6]) << 8) | in[7]) / 100.0;
00299 }
00300 
00301 void DateTimeRptMsg::parse(uint8_t *in)
00302 {
00303   year = in[0];
00304   month = in[1];
00305   day = in[2];
00306   hour = in[3];
00307   minute = in[4];
00308   second = in[5];
00309 }
00310 
00311 void WheelSpeedRptMsg::parse(uint8_t *in)
00312 {
00313   int16_t temp;
00314 
00315   temp = (static_cast<int16_t>(in[0]) << 8) | in[1];
00316   front_left_wheel_speed = static_cast<double>(temp / 100.0);
00317 
00318   temp = (static_cast<int16_t>(in[2]) << 8) | in[3];
00319   front_right_wheel_speed = static_cast<double>(temp / 100.0);
00320 
00321   temp = (static_cast<int16_t>(in[4]) << 8) | in[5];
00322   rear_left_wheel_speed = static_cast<double>(temp / 100.0);
00323 
00324   temp = (static_cast<int16_t>(in[6]) << 8) | in[7];
00325   rear_right_wheel_speed = static_cast<double>(temp / 100.0);
00326 }
00327 
00328 void MotorRpt1Msg::parse(uint8_t *in)
00329 {
00330   int32_t temp;
00331 
00332   temp = (static_cast<int32_t>(in[0]) << 24) |
00333          (static_cast<int32_t>(in[1]) << 16) |
00334          (static_cast<int32_t>(in[2]) << 8) | in[3];
00335   current = static_cast<double>(temp / 1000.0);
00336 
00337   temp = (static_cast<int32_t>(in[4]) << 24) |
00338          (static_cast<int32_t>(in[5]) << 16) |
00339          (static_cast<int32_t>(in[6]) << 8) | in[7];
00340   position = static_cast<double>(temp / 1000.0);
00341 }
00342 
00343 void MotorRpt2Msg::parse(uint8_t *in)
00344 {
00345   int16_t temp16;
00346   int32_t temp32;
00347 
00348   temp16 = (static_cast<int16_t>(in[0]) << 8) | in[1];
00349   encoder_temp = static_cast<double>(temp16);
00350 
00351   temp16 = (static_cast<int16_t>(in[2]) << 8) | in[3];
00352   motor_temp = static_cast<double>(temp16);
00353 
00354   temp32 = (static_cast<int32_t>(in[7]) << 24) |
00355            (static_cast<int32_t>(in[6]) << 16) |
00356            (static_cast<int32_t>(in[5]) << 8) | in[4];
00357   velocity = static_cast<double>(temp32 / 1000.0);
00358 }
00359 
00360 void MotorRpt3Msg::parse(uint8_t *in)
00361 {
00362   int32_t temp;
00363 
00364   temp = (static_cast<int32_t>(in[0]) << 24) |
00365          (static_cast<int32_t>(in[1]) << 16) |
00366          (static_cast<int32_t>(in[2]) << 8) | in[3];
00367   torque_output = static_cast<double>(temp / 1000.0);
00368 
00369   temp = (static_cast<int32_t>(in[4]) << 24) |
00370          (static_cast<int32_t>(in[5]) << 16) |
00371          (static_cast<int32_t>(in[6]) << 8) | in[7];
00372   torque_input = static_cast<double>(temp / 1000.0);
00373 }
00374 
00375 void SteeringPIDRpt1Msg::parse(uint8_t *in)
00376 {
00377   int16_t temp;
00378 
00379   temp = (static_cast<int16_t>(in[0]) << 8) | in[1];
00380   dt = static_cast<double>(temp / 1000.0);
00381 
00382   temp = (static_cast<int16_t>(in[2]) << 8) | in[3];
00383   Kp = static_cast<double>(temp / 1000.0);
00384 
00385   temp = (static_cast<int16_t>(in[4]) << 8) | in[5];
00386   Ki = static_cast<double>(temp / 1000.0);
00387 
00388   temp = (static_cast<int16_t>(in[6]) << 8) | in[7];
00389   Kd = static_cast<double>(temp / 1000.0);
00390 }
00391 
00392 void SteeringPIDRpt2Msg::parse(uint8_t *in)
00393 {
00394   int16_t temp;
00395 
00396   temp = (static_cast<int16_t>(in[0]) << 8) | in[1];
00397   P_term = static_cast<double>(temp / 1000.0);
00398 
00399   temp = (static_cast<int16_t>(in[2]) << 8) | in[3];
00400   I_term = static_cast<double>(temp / 1000.0);
00401 
00402   temp = (static_cast<int16_t>(in[4]) << 8) | in[5];
00403   D_term = static_cast<double>(temp / 1000.0);
00404 
00405   temp = (static_cast<int16_t>(in[6]) << 8) | in[7];
00406   all_terms = static_cast<double>(temp / 1000.0);
00407 }
00408 
00409 void SteeringPIDRpt3Msg::parse(uint8_t *in)
00410 {
00411   int16_t temp;
00412 
00413   temp = (static_cast<int16_t>(in[0]) << 8) | in[1];
00414   new_torque = static_cast<double>(temp / 1000.0);
00415 
00416   temp = (static_cast<int16_t>(in[2]) << 8) | in[3];
00417   str_angle_desired = static_cast<double>(temp / 1000.0);
00418 
00419   temp = (static_cast<int16_t>(in[4]) << 8) | in[5];
00420   str_angle_actual = static_cast<double>(temp / 1000.0);
00421 
00422   temp = (static_cast<int16_t>(in[6]) << 8) | in[7];
00423   error = static_cast<double>(temp / 1000.0);
00424 }
00425 
00426 void SteeringPIDRpt4Msg::parse(uint8_t *in)
00427 {
00428   int16_t temp;
00429 
00430   temp = (static_cast<int16_t>(in[0]) << 8) | in[1];
00431   angular_velocity = static_cast<double>(temp / 1000.0);
00432 
00433   temp = (static_cast<int16_t>(in[2]) << 8) | in[3];
00434   angular_acceleration = static_cast<double>(temp / 1000.0);
00435 }
00436 
00437 void ParkingBrakeStatusRptMsg::parse(uint8_t *in)
00438 {
00439   parking_brake_engaged = (in[0] == 0x01);
00440 }
00441 
00442 // RX Messages
00443 void GlobalCmdMsg::encode(bool enable, bool clear_override, bool ignore_overide)
00444 {
00445   data.assign(8, 0);
00446 
00447   if (enable)
00448     data[0] |= 0x01;
00449   if (clear_override)
00450     data[0] |= 0x02;
00451   if (ignore_overide)
00452     data[0] |= 0x04;
00453 }
00454 
00455 void TurnSignalCmdMsg::encode(uint8_t turn_signal_cmd)
00456 {
00457   data.assign(8, 0);
00458   data[0] = turn_signal_cmd;
00459 }
00460 
00461 void HeadlightCmdMsg::encode(uint8_t headlight_cmd)
00462 {
00463   data.assign(8, 0);
00464   data[0] = headlight_cmd;
00465 }
00466 
00467 void HornCmdMsg::encode(uint8_t horn_cmd)
00468 {
00469   data.assign(8, 0);
00470   data[0] = horn_cmd;
00471 }
00472 
00473 void WiperCmdMsg::encode(uint8_t wiper_cmd)
00474 {
00475   data.assign(8, 0);
00476   data[0] = wiper_cmd;
00477 }
00478 
00479 void ShiftCmdMsg::encode(uint8_t shift_cmd)
00480 {
00481   data.assign(8, 0);
00482   data[0] = shift_cmd;
00483 }
00484 
00485 void AccelCmdMsg::encode(double accel_cmd)
00486 {
00487   data.assign(8, 0);
00488   uint16_t cmdInt = static_cast<uint16_t>(accel_cmd * 1000.0);
00489   data[0] = (cmdInt & 0xFF00) >> 8;
00490   data[1] = cmdInt & 0x00FF;
00491 }
00492 
00493 void SteerCmdMsg::encode(double steer_pos, double steer_spd)
00494 {
00495   data.assign(8, 0);
00496   int32_t raw_pos = static_cast<int32_t>(1000.0 * steer_pos);
00497   uint32_t raw_spd = (uint32_t)(1000.0 * steer_spd);
00498 
00499   data[0] = (raw_pos & 0xFF000000) >> 24;
00500   data[1] = (raw_pos & 0x00FF0000) >> 16;
00501   data[2] = (raw_pos & 0x0000FF00) >> 8;
00502   data[3] = raw_pos & 0x000000FF;
00503   data[4] = (raw_spd & 0xFF000000) >> 24;
00504   data[5] = (raw_spd & 0x00FF0000) >> 16;
00505   data[6] = (raw_spd & 0x0000FF00) >> 8;
00506   data[7] = raw_spd & 0x000000FF;
00507 }
00508 
00509 void BrakeCmdMsg::encode(double brake_pct)
00510 {
00511   data.assign(8, 0);
00512   uint16_t raw_pct = static_cast<uint16_t>(1000.0 * brake_pct);
00513 
00514   data[0] = (raw_pct & 0xFF00) >> 8;
00515   data[1] = (raw_pct & 0x00FF);
00516 }


pacmod
Author(s): Joe Driscoll , Josh Whitley
autogenerated on Sat Jun 8 2019 20:34:19