hz.cpp
Go to the documentation of this file.
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 
00038 // LCM and messages
00039 #include <lcm/lcm-cpp.hpp>
00040 #include <dataspeed_pds_lcm/status_t.hpp>
00041 using namespace dataspeed_pds_lcm;
00042 
00043 // LCM handle
00044 lcm::LCM * g_lcm = NULL;
00045 
00046 void timerCallback(const ros::TimerEvent&, int id)
00047 {
00048   ROS_ASSERT((0 <= id) && (id <= 3));
00049   status_t msg;
00050   memset(&msg, 0x00, sizeof(msg));
00051   msg.unit_id = id;
00052   g_lcm->publish("STATUS", &msg);
00053 }
00054 
00055 int main(int argc, char **argv)
00056 {
00057   ros::init(argc, argv, "sim");
00058   ros::NodeHandle nh;
00059   ros::NodeHandle nh_priv("~");
00060 
00061   // Configurable period
00062   double hz = 50; // 50 Hz
00063   nh_priv.getParam("hz", hz);
00064 
00065   // Multiple units (optional)
00066   int unit_id = 0;
00067   nh_priv.getParam("unit_id", unit_id);
00068   if (unit_id < 0) {
00069     unit_id = 0;
00070   } else if (unit_id > 3) {
00071     unit_id = 3;
00072   }
00073 
00074   // LCM URL
00075   std::string lcm_url;
00076   nh_priv.getParam("lcm_url", lcm_url);
00077   if (lcm_url.empty()) {
00078     lcm_url = "udpm://225.0.0.0:7667?ttl=0";
00079   }
00080 
00081   // Initialize LCM
00082   lcm::LCM * lcm;
00083   while(ros::ok()) {
00084     lcm = new lcm::LCM(lcm_url);
00085     if(lcm->good()) {
00086       break;
00087     } else {
00088       ROS_WARN_THROTTLE(10.0,"lcm is not initialized, is the network ready?");
00089       delete lcm;
00090     }
00091     ros::Duration(1.0).sleep();
00092   }
00093   ROS_INFO("LCM connected to %s", lcm_url.c_str());
00094 
00095   // Hold onto the LCM handle
00096   g_lcm = lcm;
00097 
00098   // Setup Timers
00099   std::vector<ros::Timer> timers;
00100   for (int i = 0; i <= unit_id; i++) {
00101     timers.push_back(nh.createTimer(ros::Duration(1.0 / hz), boost::bind(&timerCallback, _1, i)));
00102   }
00103 
00104   // Handle callbacks until shutdown
00105   ros::spin();
00106 
00107   return 0;
00108 }
00109 


dataspeed_pds_lcm
Author(s): Kevin Hallenbeck , Eric Myllyoja
autogenerated on Thu Apr 4 2019 02:42:50