yumi_hw_ifce_node.cpp
Go to the documentation of this file.
00001 // SYS
00002 #include <sys/mman.h>
00003 #include <cmath>
00004 #include <time.h>
00005 #include <signal.h>
00006 #include <stdexcept>
00007 
00008 // ROS headers
00009 #include <ros/ros.h>
00010 #include <controller_manager/controller_manager.h>
00011 
00012 // the lwr hw fri interface
00013 #include "yumi_hw/yumi_hw_rapid.h"
00014 
00015 bool g_quit = false;
00016 
00017 void quitRequested(int sig)
00018 {
00019   g_quit = true;
00020 }
00021 
00022 // Get the URDF XML from the parameter server
00023 std::string getURDF(ros::NodeHandle &model_nh_, std::string param_name)
00024 {
00025   std::string urdf_string;
00026   std::string robot_description = "/robot_description";
00027 
00028   // search and wait for robot_description on param server
00029   while (urdf_string.empty())
00030   {
00031     std::string search_param_name;
00032     if (model_nh_.searchParam(param_name, search_param_name))
00033     {
00034       ROS_INFO_ONCE_NAMED("LWRHWFRI", "LWRHWFRI node is waiting for model"
00035         " URDF in parameter [%s] on the ROS param server.", search_param_name.c_str());
00036 
00037       model_nh_.getParam(search_param_name, urdf_string);
00038     }
00039     else
00040     {
00041       ROS_INFO_ONCE_NAMED("LWRHWFRI", "LWRHWFRI node is waiting for model"
00042         " URDF in parameter [%s] on the ROS param server.", robot_description.c_str());
00043 
00044       model_nh_.getParam(param_name, urdf_string);
00045     }
00046 
00047     usleep(100000);
00048   }
00049   ROS_DEBUG_STREAM_NAMED("LWRHWFRI", "Received URDF from param server, parsing...");
00050 
00051   return urdf_string;
00052 }
00053 
00054 int main( int argc, char** argv )
00055 {
00056   // initialize ROS
00057   ros::init(argc, argv, "yumi_hw_interface", ros::init_options::NoSigintHandler);
00058 
00059   // ros spinner
00060   ros::AsyncSpinner spinner(4);
00061   spinner.start();
00062 
00063   // custom signal handlers
00064   signal(SIGTERM, quitRequested);
00065   signal(SIGINT, quitRequested);
00066   signal(SIGHUP, quitRequested);
00067 
00068   // create a node
00069   ros::NodeHandle yumi_nh ("~");
00070 
00071   // get params or give default values
00072   int port;
00073   std::string hintToRemoteHost;
00074   std::string name;
00075   //yumi_nh.param("port", port, 49939);
00076   yumi_nh.param("ip", hintToRemoteHost, std::string("192.168.125.1") );
00077   yumi_nh.param("name", name, std::string("yumi"));
00078 
00079   // get the general robot description, the lwr class will take care of parsing what's useful to itself
00080   std::string urdf_string = getURDF(yumi_nh, "/robot_description");
00081 
00082   YumiHWRapid yumi_robot;
00083   yumi_robot.create(name, urdf_string);
00084   yumi_robot.setup(hintToRemoteHost);
00085   
00086   if(!yumi_robot.init())
00087   {
00088     ROS_FATAL_NAMED("yumi_hw","Could not initialize robot real interface");
00089     return -1;
00090   }
00091 
00092   // timer variables
00093   struct timespec ts = {0, 0};
00094   ros::Time last(ts.tv_sec, ts.tv_nsec), now(ts.tv_sec, ts.tv_nsec);
00095   ros::Duration period(1.0);
00096 
00097   float sampling_time = yumi_robot.getSampleTime();
00098   ROS_INFO("Sampling time on robot: %f", sampling_time);
00099 
00100   //the controller manager
00101   controller_manager::ControllerManager manager(&yumi_robot);
00102 
00103   // run as fast as possible
00104   while( !g_quit )
00105   {
00106     // get the time / period
00107     //if (!clock_gettime(CLOCK_MONOTONIC, &ts))
00108     //{
00109       now = ros::Time::now();   
00110       //now.sec = ts.tv_sec;
00111       //now.nsec = ts.tv_nsec;
00112       period = now - last;
00113       last = now;
00114     //} 
00115     //else
00116     //{
00117     //  ROS_FATAL("Failed to poll realtime clock!");
00118     //  break;
00119     //}
00120 
00121     // read the state from the lwr
00122     yumi_robot.read(now, period);
00123     
00124     // update the controllers
00125     manager.update(now, period);
00126 
00127     // write the command to the lwr
00128     yumi_robot.write(now, period);
00129     
00130     //std::cout<<"Period is "<<period.toSec()<<std::endl;
00131     //ros::Duration(sampling_time).sleep();
00132   }
00133 
00134   std::cerr<<"Stopping spinner..."<<std::endl;
00135   spinner.stop();
00136 
00137   std::cerr<<"Bye!"<<std::endl;
00138 
00139   return 0;
00140 }


yumi_hw
Author(s):
autogenerated on Sat Jun 8 2019 20:47:40