pacmod_ros_msg_handler.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_ros_msg_handler.h>
00009 
00010 #include <string>
00011 #include <vector>
00012 
00013 using namespace AS::Drivers::PACMod;
00014 
00015 LockedData::LockedData() :
00016   _data(),
00017   _data_mut(),
00018   _is_valid(false),
00019   _valid_mut()
00020 {
00021 }
00022 
00023 bool LockedData::isValid() const
00024 {
00025   std::lock_guard<std::mutex> lck(_valid_mut);
00026   return _is_valid;
00027 }
00028 
00029 void LockedData::setIsValid(bool valid)
00030 {
00031   std::lock_guard<std::mutex> lck(_valid_mut);
00032   _is_valid = valid;
00033 }
00034 
00035 std::vector<unsigned char> LockedData::getData() const
00036 {
00037   std::lock_guard<std::mutex> lck(_data_mut);
00038   return _data;
00039 }
00040 
00041 void LockedData::setData(std::vector<unsigned char> new_data)
00042 {
00043   std::lock_guard<std::mutex> lck(_data_mut);
00044   _data = new_data;
00045 }
00046 
00047 void PacmodTxRosMsgHandler::fillAndPublish(const int64_t& can_id,
00048                                            std::string frame_id,
00049                                            const ros::Publisher& pub,
00050                                            const std::shared_ptr<PacmodTxMsg>& parser_class)
00051 {
00052   if (can_id == TurnSignalRptMsg::CAN_ID ||
00053       can_id == ShiftRptMsg::CAN_ID ||
00054       can_id == HeadlightRptMsg::CAN_ID ||
00055       can_id == HornRptMsg::CAN_ID ||
00056       can_id == WiperRptMsg::CAN_ID)
00057   {
00058     pacmod_msgs::SystemRptInt new_msg;
00059     fillSystemRptInt(parser_class, &new_msg, frame_id);
00060     pub.publish(new_msg);
00061   }
00062   else if (can_id == AccelRptMsg::CAN_ID ||
00063            can_id == BrakeRptMsg::CAN_ID ||
00064            can_id == SteerRptMsg::CAN_ID ||
00065            can_id == SteerRpt2Msg::CAN_ID ||
00066            can_id == SteerRpt3Msg::CAN_ID)
00067   {
00068     pacmod_msgs::SystemRptFloat new_msg;
00069     fillSystemRptFloat(parser_class, &new_msg, frame_id);
00070     pub.publish(new_msg);
00071   }
00072   else if (can_id == GlobalRptMsg::CAN_ID)
00073   {
00074     pacmod_msgs::GlobalRpt new_msg;
00075     fillGlobalRpt(parser_class, &new_msg, frame_id);
00076     pub.publish(new_msg);
00077   }
00078   else if (can_id == VehicleSpeedRptMsg::CAN_ID)
00079   {
00080     pacmod_msgs::VehicleSpeedRpt new_msg;
00081     fillVehicleSpeedRpt(parser_class, &new_msg, frame_id);
00082     pub.publish(new_msg);
00083   }
00084   else if (can_id == BrakeMotorRpt1Msg::CAN_ID ||
00085            can_id == SteerMotorRpt1Msg::CAN_ID)
00086   {
00087     pacmod_msgs::MotorRpt1 new_msg;
00088     fillMotorRpt1(parser_class, &new_msg, frame_id);
00089     pub.publish(new_msg);
00090   }
00091   else if (can_id == BrakeMotorRpt2Msg::CAN_ID ||
00092            can_id == SteerMotorRpt2Msg::CAN_ID)
00093   {
00094     pacmod_msgs::MotorRpt2 new_msg;
00095     fillMotorRpt2(parser_class, &new_msg, frame_id);
00096     pub.publish(new_msg);
00097   }
00098   else if (can_id == BrakeMotorRpt3Msg::CAN_ID ||
00099            can_id == SteerMotorRpt3Msg::CAN_ID)
00100   {
00101     pacmod_msgs::MotorRpt3 new_msg;
00102     fillMotorRpt3(parser_class, &new_msg, frame_id);
00103     pub.publish(new_msg);
00104   }
00105   else if (can_id == WheelSpeedRptMsg::CAN_ID)
00106   {
00107     pacmod_msgs::WheelSpeedRpt new_msg;
00108     fillWheelSpeedRpt(parser_class, &new_msg, frame_id);
00109     pub.publish(new_msg);
00110   }
00111   else if (can_id == SteeringPIDRpt1Msg::CAN_ID)
00112   {
00113     pacmod_msgs::SteeringPIDRpt1 new_msg;
00114     fillSteeringPIDRpt1(parser_class, &new_msg, frame_id);
00115     pub.publish(new_msg);
00116   }
00117   else if (can_id == SteeringPIDRpt2Msg::CAN_ID)
00118   {
00119     pacmod_msgs::SteeringPIDRpt2 new_msg;
00120     fillSteeringPIDRpt2(parser_class, &new_msg, frame_id);
00121     pub.publish(new_msg);
00122   }
00123   else if (can_id == SteeringPIDRpt3Msg::CAN_ID)
00124   {
00125     pacmod_msgs::SteeringPIDRpt3 new_msg;
00126     fillSteeringPIDRpt3(parser_class, &new_msg, frame_id);
00127     pub.publish(new_msg);
00128   }
00129   else if (can_id == ParkingBrakeStatusRptMsg::CAN_ID)
00130   {
00131     pacmod_msgs::ParkingBrakeStatusRpt new_msg;
00132     fillParkingBrakeStatusRpt(parser_class, &new_msg, frame_id);
00133     pub.publish(new_msg);
00134   }
00135   else if (can_id == YawRateRptMsg::CAN_ID)
00136   {
00137     pacmod_msgs::YawRateRpt new_msg;
00138     fillYawRateRpt(parser_class, &new_msg, frame_id);
00139     pub.publish(new_msg);
00140   }
00141   else if (can_id == LatLonHeadingRptMsg::CAN_ID)
00142   {
00143     pacmod_msgs::LatLonHeadingRpt new_msg;
00144     fillLatLonHeadingRpt(parser_class, &new_msg, frame_id);
00145     pub.publish(new_msg);
00146   }
00147   else if (can_id == DateTimeRptMsg::CAN_ID)
00148   {
00149     pacmod_msgs::DateTimeRpt new_msg;
00150     fillDateTimeRpt(parser_class, &new_msg, frame_id);
00151     pub.publish(new_msg);
00152   }
00153   else if (can_id == SteeringPIDRpt4Msg::CAN_ID)
00154   {
00155     pacmod_msgs::SteeringPIDRpt4 new_msg;
00156     fillSteeringPIDRpt4(parser_class, &new_msg, frame_id);
00157     pub.publish(new_msg);
00158   }
00159   else if (can_id == VinRptMsg::CAN_ID)
00160   {
00161     pacmod_msgs::VinRpt new_msg;
00162     fillVinRpt(parser_class, &new_msg, frame_id);
00163     pub.publish(new_msg);
00164   }
00165 }
00166 
00167 void PacmodTxRosMsgHandler::fillSystemRptInt(const std::shared_ptr<PacmodTxMsg>& parser_class,
00168                                              pacmod_msgs::SystemRptInt* new_msg,
00169                                              std::string frame_id)
00170 {
00171   auto dc_parser = std::dynamic_pointer_cast<SystemRptIntMsg>(parser_class);
00172 
00173   new_msg->manual_input = dc_parser->manual_input;
00174   new_msg->command = dc_parser->command;
00175   new_msg->output = dc_parser->output;
00176 
00177   new_msg->header.frame_id = frame_id;
00178   new_msg->header.stamp = ros::Time::now();
00179 }
00180 
00181 void PacmodTxRosMsgHandler::fillSystemRptFloat(const std::shared_ptr<PacmodTxMsg>& parser_class,
00182                                                pacmod_msgs::SystemRptFloat* new_msg,
00183                                                std::string frame_id)
00184 {
00185   auto dc_parser = std::dynamic_pointer_cast<SystemRptFloatMsg>(parser_class);
00186 
00187   new_msg->manual_input = dc_parser->manual_input;
00188   new_msg->command = dc_parser->command;
00189   new_msg->output = dc_parser->output;
00190 
00191   new_msg->header.frame_id = frame_id;
00192   new_msg->header.stamp = ros::Time::now();
00193 }
00194 
00195 void PacmodTxRosMsgHandler::fillGlobalRpt(const std::shared_ptr<PacmodTxMsg>& parser_class,
00196                                           pacmod_msgs::GlobalRpt* new_msg,
00197                                           std::string frame_id)
00198 {
00199   auto dc_parser = std::dynamic_pointer_cast<GlobalRptMsg>(parser_class);
00200 
00201   new_msg->enabled = dc_parser->enabled;
00202   new_msg->override_active = dc_parser->override_active;
00203   new_msg->user_can_timeout = dc_parser->user_can_timeout;
00204   new_msg->brake_can_timeout = dc_parser->brake_can_timeout;
00205   new_msg->steering_can_timeout = dc_parser->steering_can_timeout;
00206   new_msg->vehicle_can_timeout = dc_parser->vehicle_can_timeout;
00207   new_msg->user_can_read_errors = dc_parser->user_can_read_errors;
00208 
00209   new_msg->header.frame_id = frame_id;
00210   new_msg->header.stamp = ros::Time::now();
00211 }
00212 
00213 void PacmodTxRosMsgHandler::fillVehicleSpeedRpt(const std::shared_ptr<PacmodTxMsg>& parser_class,
00214                                                 pacmod_msgs::VehicleSpeedRpt* new_msg,
00215                                                 std::string frame_id)
00216 {
00217   auto dc_parser = std::dynamic_pointer_cast<VehicleSpeedRptMsg>(parser_class);
00218 
00219   new_msg->vehicle_speed = dc_parser->vehicle_speed;
00220   new_msg->vehicle_speed_valid = dc_parser->vehicle_speed_valid;
00221   new_msg->vehicle_speed_raw[0] = dc_parser->vehicle_speed_raw[0];
00222   new_msg->vehicle_speed_raw[1] = dc_parser->vehicle_speed_raw[1];
00223 
00224   new_msg->header.frame_id = frame_id;
00225   new_msg->header.stamp = ros::Time::now();
00226 }
00227 
00228 void PacmodTxRosMsgHandler::fillMotorRpt1(const std::shared_ptr<PacmodTxMsg>& parser_class,
00229                                           pacmod_msgs::MotorRpt1* new_msg,
00230                                           std::string frame_id)
00231 {
00232   auto dc_parser = std::dynamic_pointer_cast<MotorRpt1Msg>(parser_class);
00233 
00234   new_msg->current = dc_parser->current;
00235   new_msg->position = dc_parser->position;
00236 
00237   new_msg->header.frame_id = frame_id;
00238   new_msg->header.stamp = ros::Time::now();
00239 }
00240 
00241 void PacmodTxRosMsgHandler::fillMotorRpt2(const std::shared_ptr<PacmodTxMsg>& parser_class,
00242                                           pacmod_msgs::MotorRpt2* new_msg,
00243                                           std::string frame_id)
00244 {
00245   auto dc_parser = std::dynamic_pointer_cast<MotorRpt2Msg>(parser_class);
00246 
00247   new_msg->encoder_temp = dc_parser->encoder_temp;
00248   new_msg->motor_temp = dc_parser->motor_temp;
00249   new_msg->angular_velocity = dc_parser->velocity;
00250 
00251   new_msg->header.frame_id = frame_id;
00252   new_msg->header.stamp = ros::Time::now();
00253 }
00254 
00255 void PacmodTxRosMsgHandler::fillMotorRpt3(const std::shared_ptr<PacmodTxMsg>& parser_class,
00256                                           pacmod_msgs::MotorRpt3* new_msg,
00257                                           std::string frame_id)
00258 {
00259   auto dc_parser = std::dynamic_pointer_cast<MotorRpt3Msg>(parser_class);
00260 
00261   new_msg->torque_output = dc_parser->torque_output;
00262   new_msg->torque_input = dc_parser->torque_input;
00263 
00264   new_msg->header.frame_id = frame_id;
00265   new_msg->header.stamp = ros::Time::now();
00266 }
00267 
00268 void PacmodTxRosMsgHandler::fillWheelSpeedRpt(const std::shared_ptr<PacmodTxMsg>& parser_class,
00269                                               pacmod_msgs::WheelSpeedRpt* new_msg,
00270                                               std::string frame_id)
00271 {
00272   auto dc_parser = std::dynamic_pointer_cast<WheelSpeedRptMsg>(parser_class);
00273 
00274   new_msg->front_left_wheel_speed = dc_parser->front_left_wheel_speed;
00275   new_msg->front_right_wheel_speed = dc_parser->front_right_wheel_speed;
00276   new_msg->rear_left_wheel_speed = dc_parser->rear_left_wheel_speed;
00277   new_msg->rear_right_wheel_speed = dc_parser->rear_right_wheel_speed;
00278 
00279   new_msg->header.frame_id = frame_id;
00280   new_msg->header.stamp = ros::Time::now();
00281 }
00282 
00283 void PacmodTxRosMsgHandler::fillSteeringPIDRpt1(const std::shared_ptr<PacmodTxMsg>& parser_class,
00284                                                 pacmod_msgs::SteeringPIDRpt1* new_msg,
00285                                                 std::string frame_id)
00286 {
00287   auto dc_parser = std::dynamic_pointer_cast<SteeringPIDRpt1Msg>(parser_class);
00288 
00289   new_msg->dt = dc_parser->dt;
00290   new_msg->Kp = dc_parser->Kp;
00291   new_msg->Ki = dc_parser->Ki;
00292   new_msg->Kd = dc_parser->Kd;
00293 
00294   new_msg->header.frame_id = frame_id;
00295   new_msg->header.stamp = ros::Time::now();
00296 }
00297 
00298 void PacmodTxRosMsgHandler::fillSteeringPIDRpt2(const std::shared_ptr<PacmodTxMsg>& parser_class,
00299                                                 pacmod_msgs::SteeringPIDRpt2* new_msg,
00300                                                 std::string frame_id)
00301 {
00302   auto dc_parser = std::dynamic_pointer_cast<SteeringPIDRpt2Msg>(parser_class);
00303 
00304   new_msg->P_term = dc_parser->P_term;
00305   new_msg->I_term = dc_parser->I_term;
00306   new_msg->D_term = dc_parser->D_term;
00307   new_msg->all_terms = dc_parser->all_terms;
00308 
00309   new_msg->header.frame_id = frame_id;
00310   new_msg->header.stamp = ros::Time::now();
00311 }
00312 
00313 void PacmodTxRosMsgHandler::fillSteeringPIDRpt3(const std::shared_ptr<PacmodTxMsg>& parser_class,
00314                                                 pacmod_msgs::SteeringPIDRpt3* new_msg,
00315                                                 std::string frame_id)
00316 {
00317   auto dc_parser = std::dynamic_pointer_cast<SteeringPIDRpt3Msg>(parser_class);
00318 
00319   new_msg->new_torque = dc_parser->new_torque;
00320   new_msg->str_angle_desired = dc_parser->str_angle_desired;
00321   new_msg->str_angle_actual = dc_parser->str_angle_actual;
00322   new_msg->error = dc_parser->error;
00323 
00324   new_msg->header.frame_id = frame_id;
00325   new_msg->header.stamp = ros::Time::now();
00326 }
00327 
00328 void PacmodTxRosMsgHandler::fillParkingBrakeStatusRpt(const std::shared_ptr<PacmodTxMsg>& parser_class,
00329                                                       pacmod_msgs::ParkingBrakeStatusRpt* new_msg,
00330                                                       std::string frame_id)
00331 {
00332   auto dc_parser = std::dynamic_pointer_cast<ParkingBrakeStatusRptMsg>(parser_class);
00333 
00334   new_msg->parking_brake_engaged = dc_parser->parking_brake_engaged;
00335 
00336   new_msg->header.frame_id = frame_id;
00337   new_msg->header.stamp = ros::Time::now();
00338 }
00339 
00340 void PacmodTxRosMsgHandler::fillYawRateRpt(const std::shared_ptr<PacmodTxMsg>& parser_class,
00341                                            pacmod_msgs::YawRateRpt* new_msg,
00342                                            std::string frame_id)
00343 {
00344   auto dc_parser = std::dynamic_pointer_cast<YawRateRptMsg>(parser_class);
00345 
00346   new_msg->yaw_rate = dc_parser->yaw_rate;
00347 
00348   new_msg->header.frame_id = frame_id;
00349   new_msg->header.stamp = ros::Time::now();
00350 }
00351 
00352 void PacmodTxRosMsgHandler::fillLatLonHeadingRpt(const std::shared_ptr<PacmodTxMsg>& parser_class,
00353                                                  pacmod_msgs::LatLonHeadingRpt* new_msg,
00354                                                  std::string frame_id)
00355 {
00356   auto dc_parser = std::dynamic_pointer_cast<LatLonHeadingRptMsg>(parser_class);
00357 
00358   new_msg->latitude_degrees = dc_parser->latitude_degrees;
00359   new_msg->latitude_minutes = dc_parser->latitude_minutes;
00360   new_msg->latitude_seconds = dc_parser->latitude_seconds;
00361   new_msg->longitude_degrees = dc_parser->longitude_degrees;
00362   new_msg->longitude_minutes = dc_parser->longitude_minutes;
00363   new_msg->longitude_seconds = dc_parser->longitude_seconds;
00364   new_msg->heading = dc_parser->heading;
00365 
00366   new_msg->header.frame_id = frame_id;
00367   new_msg->header.stamp = ros::Time::now();
00368 }
00369 
00370 void PacmodTxRosMsgHandler::fillDateTimeRpt(const std::shared_ptr<PacmodTxMsg>& parser_class,
00371                                             pacmod_msgs::DateTimeRpt* new_msg,
00372                                             std::string frame_id)
00373 {
00374   auto dc_parser = std::dynamic_pointer_cast<DateTimeRptMsg>(parser_class);
00375 
00376   new_msg->year = dc_parser->year;
00377   new_msg->month = dc_parser->month;
00378   new_msg->day = dc_parser->day;
00379   new_msg->hour = dc_parser->hour;
00380   new_msg->minute = dc_parser->minute;
00381   new_msg->second = dc_parser->second;
00382 
00383   new_msg->header.frame_id = frame_id;
00384   new_msg->header.stamp = ros::Time::now();
00385 }
00386 
00387 void PacmodTxRosMsgHandler::fillSteeringPIDRpt4(const std::shared_ptr<PacmodTxMsg>& parser_class,
00388                                                 pacmod_msgs::SteeringPIDRpt4* new_msg,
00389                                                 std::string frame_id)
00390 {
00391   auto dc_parser = std::dynamic_pointer_cast<SteeringPIDRpt4Msg>(parser_class);
00392 
00393   new_msg->angular_velocity = dc_parser->angular_velocity;
00394   new_msg->angular_acceleration = dc_parser->angular_acceleration;
00395 
00396   new_msg->header.frame_id = frame_id;
00397   new_msg->header.stamp = ros::Time::now();
00398 }
00399 
00400 void PacmodTxRosMsgHandler::fillVinRpt(const std::shared_ptr<PacmodTxMsg>& parser_class,
00401                                        pacmod_msgs::VinRpt* new_msg,
00402                                        std::string frame_id)
00403 {
00404   auto dc_parser = std::dynamic_pointer_cast<VinRptMsg>(parser_class);
00405 
00406   new_msg->mfg_code = dc_parser->mfg_code;
00407   new_msg->mfg = dc_parser->mfg;
00408   new_msg->model_year_code = dc_parser->model_year_code;
00409   new_msg->model_year = dc_parser->model_year;
00410   new_msg->serial = dc_parser->serial;
00411 
00412   new_msg->header.frame_id = frame_id;
00413   new_msg->header.stamp = ros::Time::now();
00414 }
00415 
00416 std::vector<uint8_t> PacmodRxRosMsgHandler::unpackAndEncode(const int64_t& can_id,
00417                                                             const pacmod_msgs::PacmodCmd::ConstPtr& msg)
00418 {
00419   if (can_id == TurnSignalCmdMsg::CAN_ID)
00420   {
00421     TurnSignalCmdMsg encoder;
00422     encoder.encode(msg->ui16_cmd);
00423     return encoder.data;
00424   }
00425   else if (can_id == ShiftCmdMsg::CAN_ID)
00426   {
00427     ShiftCmdMsg encoder;
00428     encoder.encode(msg->ui16_cmd);
00429     return encoder.data;
00430   }
00431   else if (can_id == AccelCmdMsg::CAN_ID)
00432   {
00433     AccelCmdMsg encoder;
00434     encoder.encode(msg->f64_cmd);
00435     return encoder.data;
00436   }
00437   else if (can_id == GlobalCmdMsg::CAN_ID)
00438   {
00439     GlobalCmdMsg encoder;
00440     encoder.encode(msg->enable, msg->clear, msg->ignore);
00441     return encoder.data;
00442   }
00443   else if (can_id == BrakeCmdMsg::CAN_ID)
00444   {
00445     BrakeCmdMsg encoder;
00446     encoder.encode(msg->f64_cmd);
00447     return encoder.data;
00448   }
00449   else if (can_id == HeadlightCmdMsg::CAN_ID)
00450   {
00451     HeadlightCmdMsg encoder;
00452     encoder.encode(msg->ui16_cmd);
00453     return encoder.data;
00454   }
00455   else if (can_id == HornCmdMsg::CAN_ID)
00456   {
00457     HornCmdMsg encoder;
00458     encoder.encode(msg->ui16_cmd);
00459     return encoder.data;
00460   }
00461   else if (can_id == WiperCmdMsg::CAN_ID)
00462   {
00463     WiperCmdMsg encoder;
00464     encoder.encode(msg->ui16_cmd);
00465     return encoder.data;
00466   }
00467 }
00468 
00469 std::vector<uint8_t> PacmodRxRosMsgHandler::unpackAndEncode(const int64_t& can_id,
00470                                                             const pacmod_msgs::PositionWithSpeed::ConstPtr& msg)
00471 {
00472   std::vector<uint8_t> ret_vec;
00473 
00474   if (can_id == SteerCmdMsg::CAN_ID)
00475   {
00476     SteerCmdMsg encoder;
00477     encoder.encode(msg->angular_position, msg->angular_velocity_limit);
00478     return encoder.data;
00479   }
00480 }


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