USB2_F_7001_node.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  *
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2012, ISR University of Coimbra.
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the ISR University of Coimbra nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  * Author: Gonçalo Cabrita on 01/03/2012
00036  *********************************************************************/
00037 
00038 #include <iostream>
00039 
00040 #include <ros/ros.h>
00041 #include <can_msgs/CANFrame.h>
00042 
00043 #include "USB2_F_7001.h"
00044 
00045 USB2_F_7001 * can_usb_adapter;
00046 
00047 ros::Publisher rx_pub;
00048 
00049 void CANFrameToSend(const can_msgs::CANFrame::ConstPtr& msg)
00050 {
00051     char buffer[5];
00052     
00053     sprintf(buffer, "%03X%d", msg->cob_id, (int)msg->data.size());
00054    
00055     std::string frame = buffer;
00056     for(int i=0 ; i<msg->data.size() ; i++)
00057     {
00058         sprintf(buffer, "%02X", msg->data[i]);
00059         frame.append(buffer);
00060     }
00061 
00062     ROS_INFO("USB2_F_7001 - %s - Sending CAN Frame: %s", __FUNCTION__, frame.c_str());
00063     
00064         can_usb_adapter->transmitStandardFrame(&frame);
00065 }
00066 
00067 void CANFrameReceived(std::string * frame)
00068 {
00069     ROS_INFO("USB2_F_7001 - %s - Received CAN Frame: %s", __FUNCTION__, frame->c_str());
00070     
00071     char cob_id_string[4];
00072     frame->copy(cob_id_string, 3, 1);
00073     int cob_id;
00074     sscanf(cob_id_string, "%X", &cob_id);
00075     
00076     ROS_INFO("USB2_F_7001 - %s - COB ID %s %d", __FUNCTION__, cob_id_string, cob_id);
00077 
00078     int data_size = (frame->length()-5)/2;
00079     
00080     can_msgs::CANFrame frame_msg;
00081     frame_msg.stamp = ros::Time::now();
00082     frame_msg.cob_id = cob_id;
00083     
00084     char buffer[3];
00085     int byte;
00086     frame_msg.data.resize(data_size);
00087     for(int i=0 ; i<data_size ; i++)
00088     {
00089         frame->copy(buffer, 2, 5+i*2);
00090         sscanf(buffer, "%X", &byte);
00091         frame_msg.data[i] = byte;
00092     }
00093     
00094     rx_pub.publish(frame_msg);
00095 }
00096 
00097 int main(int argc, char** argv)
00098 {
00099         ros::init(argc, argv, "USB2_F_7001_node");
00100         ros::NodeHandle n;
00101         ros::NodeHandle pn("~");
00102         
00103         rx_pub = n.advertise<can_msgs::CANFrame>("/can_bus_rx", 10);
00104     ros::Subscriber tx_sub = n.subscribe<can_msgs::CANFrame>("/can_bus_tx", 10, CANFrameToSend);
00105         
00106         std::string port;
00107         pn.param<std::string>("port", port, "/dev/tty.USB0");
00108     int bit_rate;
00109         pn.param("bit_rate", bit_rate, 5);
00110     
00111     can_usb_adapter = new USB2_F_7001(&port, &CANFrameReceived);
00112     
00113     ROS_INFO("USB2_F_7001 -- Opening CAN bus...");
00114     if( !can_usb_adapter->openCANBus(bit_rate) )
00115     {
00116         ROS_FATAL("USB2_F_7001 -- Failed to open the CAN bus!");
00117                 ROS_BREAK();
00118     }
00119     ROS_INFO("USB2_F_7001 -- The CAN bus is now open!");
00120     
00121         ros::spin();
00122     
00123     delete can_usb_adapter;
00124     
00125         return(0);
00126 }
00127 
00128 // EOF
00129 


can_usb_adapters
Author(s): Gonçalo Cabrita
autogenerated on Mon Jan 6 2014 11:25:18