noid_ros_controller.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2018, Yohei Kakiuchi (JSK lab.)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the Open Source Robotics Foundation
18  * nor the names of its contributors may be
19  * used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
36 /*
37  Author: Yohei Kakiuchi
38 */
39 
40 #include <ros/ros.h>
42 #include "noid_robot_hardware.h"
43 #include "noid_mover_controller.h"
44 #include "noid_hand_controller.h"
45 
46 using namespace noid_robot_hardware;
47 
48 #define MAIN_THREAD_PERIOD_MS 50000 //50ms (20Hz)
49 #define NSEC_PER_SEC 1000000000L
50 
51 int main(int argc, char** argv)
52 {
53  ros::init(argc, argv, "noid_ros_controller");
54 
55  NoidRobotHW hw;
56 
57  ros::NodeHandle nh;
58  ros::NodeHandle robot_nh("~");
59 
60  if (!hw.init(nh, robot_nh)) {
61  ROS_ERROR("Faild to initialize hardware");
62  exit(1);
63  }
64 
65  //add extra controller
66  noid::mover::NoidMoverController mover_node(nh, &hw);
67  noid::grasp::NoidHandController hand_node(robot_nh, &hw);
68 
70  spinner.start();
71  double period = hw.getPeriod();
73 
74  ROS_INFO("ControllerManager start with %f Hz", 1.0/period);
75  // TODO: realtime loop
76 
77  int cntr = 0;
78  long main_thread_period_ns = period*1000*1000*1000;
79  double max_interval = 0.0;
80  double ave_interval = 0.0;
81  timespec m_t;
82  clock_gettime( CLOCK_MONOTONIC, &m_t );
83 
84  ros::Rate r(1/period);
86  while (ros::ok()) {
87  {
88  struct timespec tm;
89  tm.tv_nsec = m_t.tv_nsec;
90  tm.tv_sec = m_t.tv_sec;
91  tm.tv_nsec += main_thread_period_ns;
92  while( tm.tv_nsec >= NSEC_PER_SEC ){
93  tm.tv_nsec -= NSEC_PER_SEC;
94  tm.tv_sec++;
95  }
96  clock_nanosleep( CLOCK_MONOTONIC, TIMER_ABSTIME, &tm, NULL );
97 
98  if(cntr > 100) {
99  ROS_INFO("max: %f [ms], ave: %f [ms]", max_interval/1000, ave_interval/1000);
100  cntr = 0;
101  max_interval = 0.0;
102  }
103  static timespec n_t;
104  clock_gettime( CLOCK_MONOTONIC, &n_t );
105  const double measured_interval = ((n_t.tv_sec - m_t.tv_sec)*NSEC_PER_SEC + (n_t.tv_nsec - m_t.tv_nsec))/1000.0; // usec
106  if (measured_interval > max_interval) max_interval = measured_interval;
107  if(ave_interval == 0.0) {
108  ave_interval = measured_interval;
109  } else {
110  ave_interval = (measured_interval + (100 - 1)*ave_interval)/100.0;
111  }
112  m_t.tv_sec = n_t.tv_sec;
113  m_t.tv_nsec = n_t.tv_nsec;
114  cntr++;
115  }
116  //r.sleep();
117  ros::Time now = ros::Time::now();
118  ros::Duration period = now - tm;
119  hw.read (now, period);
120  cm.update(now, period);
121  hw.write (now, period);
122  tm = now;
123  }
124 
125  return 0;
126 }
int main(int argc, char **argv)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
virtual void write(const ros::Time &time, const ros::Duration &period)
void spinner()
#define ROS_INFO(...)
ROSCPP_DECL bool ok()
#define NSEC_PER_SEC
static Time now()
virtual bool init(ros::NodeHandle &root_nh, ros::NodeHandle &robot_hw_nh)
void update(const ros::Time &time, const ros::Duration &period, bool reset_controllers=false)
#define ROS_ERROR(...)
virtual void read(const ros::Time &time, const ros::Duration &period)


noid_ros_controller
Author(s): Yohei Kakiuchi
autogenerated on Sat Jul 20 2019 03:44:30