socketcan_to_topic.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2016, Ivor Wanders
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *   * Redistributions of source code must retain the above copyright notice,
00007  *     this list of conditions and the following disclaimer.
00008  *   * Redistributions in binary form must reproduce the above copyright
00009  *     notice, this list of conditions and the following disclaimer in the
00010  *     documentation and/or other materials provided with the distribution.
00011  *   * Neither the name of the copyright holder nor the names of its
00012  *     contributors may be used to endorse or promote products derived from
00013  *     this software without specific prior written permission.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00025  * POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 
00028 #include <socketcan_bridge/socketcan_to_topic.h>
00029 #include <socketcan_interface/string.h>
00030 #include <can_msgs/Frame.h>
00031 #include <string>
00032 
00033 namespace socketcan_bridge
00034 {
00035   SocketCANToTopic::SocketCANToTopic(ros::NodeHandle* nh, ros::NodeHandle* nh_param,
00036       boost::shared_ptr<can::DriverInterface> driver)
00037     {
00038       can_topic_ = nh->advertise<can_msgs::Frame>("received_messages", 10);
00039       driver_ = driver;
00040     };
00041 
00042   void SocketCANToTopic::setup()
00043     {
00044       // register handler for frames and state changes.
00045       frame_listener_ = driver_->createMsgListener(
00046               can::CommInterface::FrameDelegate(this, &SocketCANToTopic::frameCallback));
00047 
00048       state_listener_ = driver_->createStateListener(
00049               can::StateInterface::StateDelegate(this, &SocketCANToTopic::stateCallback));
00050     };
00051 
00052   void SocketCANToTopic::frameCallback(const can::Frame& f)
00053     {
00054       // ROS_DEBUG("Message came in: %s", can::tostring(f, true).c_str());
00055       can::Frame frame = f;  // copy the frame first, cannot call isValid() on const.
00056       if (!frame.isValid())
00057       {
00058         ROS_ERROR("Invalid frame from SocketCAN: id: %#04x, length: %d, is_extended: %d, is_error: %d, is_rtr: %d",
00059                   f.id, f.dlc, f.is_extended, f.is_error, f.is_rtr);
00060         return;
00061       }
00062       else
00063       {
00064         if (f.is_error)
00065         {
00066           // can::tostring cannot be used for dlc > 8 frames. It causes an crash
00067           // due to usage of boost::array for the data array. The should always work.
00068           ROS_WARN("Received frame is error: %s", can::tostring(f, true).c_str());
00069         }
00070       }
00071 
00072       can_msgs::Frame msg;
00073       // converts the can::Frame (socketcan.h) to can_msgs::Frame (ROS msg)
00074       convertSocketCANToMessage(frame, msg);
00075 
00076       msg.header.frame_id = "";  // empty frame is the de-facto standard for no frame.
00077       msg.header.stamp = ros::Time::now();
00078 
00079       can_topic_.publish(msg);
00080     };
00081 
00082 
00083   void SocketCANToTopic::stateCallback(const can::State & s)
00084     {
00085       std::string err;
00086       driver_->translateError(s.internal_error, err);
00087       if (!s.internal_error)
00088       {
00089         ROS_INFO("State: %s, asio: %s", err.c_str(), s.error_code.message().c_str());
00090       }
00091       else
00092       {
00093         ROS_ERROR("Error: %s, asio: %s", err.c_str(), s.error_code.message().c_str());
00094       }
00095     };
00096 };  // namespace socketcan_bridge


socketcan_bridge
Author(s): Ivor Wanders
autogenerated on Thu Jun 6 2019 20:44:10