00001 /********************************************************************* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2017-2018, Dataspeed Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Dataspeed Inc. nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 *********************************************************************/ 00034 00035 // ROS and messages 00036 #include <ros/ros.h> 00037 #include <can_msgs/Frame.h> 00038 00039 // CAN message definitions 00040 #include <dataspeed_pds_can/dispatch.h> 00041 using namespace dataspeed_pds_can; 00042 00043 // Publisher 00044 ros::Publisher g_pub; 00045 00046 can_msgs::Frame buildMsg(int unit_id, uint32_t can_id) { 00047 can_msgs::Frame msg; 00048 msg.header.stamp = ros::Time::now(); 00049 msg.dlc = 8; 00050 msg.id = unit_id + can_id; 00051 return msg; 00052 } 00053 00054 void timerCallback(const ros::TimerEvent&, int id) 00055 { 00056 ROS_ASSERT((0 <= id) && (id <= 3)); 00057 g_pub.publish(buildMsg(id, ID_STATUS1_MASTER)); 00058 g_pub.publish(buildMsg(id, ID_CURRENT1_MASTER)); 00059 g_pub.publish(buildMsg(id, ID_CURRENT2_MASTER)); 00060 g_pub.publish(buildMsg(id, ID_CURRENT3_MASTER)); 00061 g_pub.publish(buildMsg(id, ID_STATUS2_MASTER)); 00062 } 00063 00064 int main(int argc, char **argv) 00065 { 00066 ros::init(argc, argv, "sim"); 00067 ros::NodeHandle nh; 00068 ros::NodeHandle nh_priv("~"); 00069 00070 // Configurable period 00071 double hz = 20; // 20 Hz 00072 nh_priv.getParam("hz", hz); 00073 00074 // Multiple units (optional) 00075 int unit_id = 0; 00076 nh_priv.getParam("unit_id", unit_id); 00077 if (unit_id < 0) { 00078 unit_id = 0; 00079 } else if (unit_id > 3) { 00080 unit_id = 3; 00081 } 00082 00083 // Setup Publishers 00084 g_pub = nh.advertise<can_msgs::Frame>("can_tx", 100); 00085 00086 // Setup Timers 00087 std::vector<ros::Timer> timers; 00088 for (int i = 0; i <= unit_id; i++) { 00089 timers.push_back(nh.createTimer(ros::Duration(1.0 / hz), boost::bind(&timerCallback, _1, i))); 00090 } 00091 00092 // Handle callbacks until shutdown 00093 ros::spin(); 00094 00095 return 0; 00096 } 00097