seed_r7_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 #include <ros/ros.h>
41 
42 #define NSEC_PER_SEC 1000000000L
43 
44 
45 int main(int argc, char** argv)
46 {
47  ros::init(argc, argv, "seed_r7_ros_controller");
48 
49  ros::NodeHandle nh;
50  ros::NodeHandle robot_nh("~");
51 
53  if (!hw.init(nh, robot_nh)) {
54  ROS_ERROR("Failed to initialize hardware");
55  exit(1);
56  }
57 
58  //add extra controller
59  robot_hardware::MoverController mover_node(nh, &hw);
60  robot_hardware::HandController hand_node(robot_nh, &hw);
61 
63  spinner.start();
64 
65  double period = hw.getPeriod();
67 
68  ROS_INFO("ControllerManager start with %f Hz", 1.0/period);
69  // TODO: realtime loop
70 
71  int cntr = 0;
72  long main_thread_period_ns = period*1000*1000*1000;
73  double max_interval = 0.0;
74  double ave_interval = 0.0;
75  timespec m_t;
76  clock_gettime(CLOCK_MONOTONIC, &m_t);
77 
78  ros::Time ros_tm = ros::Time::now();
79  while (ros::ok()) {
80  {
81  static timespec tm;
82  tm.tv_nsec = m_t.tv_nsec;
83  tm.tv_sec = m_t.tv_sec;
84  tm.tv_nsec += main_thread_period_ns;
85  while (tm.tv_nsec >= NSEC_PER_SEC) {
86  tm.tv_nsec -= NSEC_PER_SEC;
87  ++tm.tv_sec;
88  }
89  clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &tm, NULL);
90 
91  if (cntr > 100) {
92  ROS_INFO("max: %f [ms], ave: %f [ms]", max_interval/1000, ave_interval/1000);
93  cntr = 0;
94  max_interval = 0.0;
95  }
96  static timespec n_t;
97  clock_gettime(CLOCK_MONOTONIC, &n_t);
98  const double measured_interval
99  = ((n_t.tv_sec - m_t.tv_sec)*NSEC_PER_SEC + (n_t.tv_nsec - m_t.tv_nsec))/1000.0; // usec
100  if (measured_interval > max_interval) max_interval = measured_interval;
101  if (ave_interval == 0.0) {
102  ave_interval = measured_interval;
103  } else {
104  ave_interval = (measured_interval + (100 - 1)*ave_interval)/100.0;
105  }
106  m_t.tv_sec = n_t.tv_sec;
107  m_t.tv_nsec = n_t.tv_nsec;
108  ++cntr;
109  }
110 
111  ros::Time now = ros::Time::now();
112  ros::Duration d_period = now - ros_tm;
113  hw.read (now, d_period);
114  cm.update(now, d_period);
115  hw.write (now, d_period);
116  ros_tm = now;
117  }
118 
119  return 0;
120 }
int main(int argc, char **argv)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void spinner()
#define NSEC_PER_SEC
#define ROS_INFO(...)
ROSCPP_DECL bool ok()
static Time now()
void update(const ros::Time &time, const ros::Duration &period, bool reset_controllers=false)
virtual void read(const ros::Time &time, const ros::Duration &period)
#define ROS_ERROR(...)
virtual void write(const ros::Time &time, const ros::Duration &period)
virtual bool init(ros::NodeHandle &root_nh, ros::NodeHandle &robot_hw_nh)


seed_r7_ros_controller
Author(s): Yohei Kakiuchi
autogenerated on Sun Apr 18 2021 02:40:34