simple_bot_hw.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2020, Franco Fusco.
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 Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 
36 #include <chrono>
37 #include <cmath>
38 #include <thread>
39 
40 // ROS
41 #include <ros/ros.h>
42 #include <rosgraph_msgs/Clock.h>
43 #include <std_msgs/Float64.h>
44 
45 // ros_control
46 #include <controller_manager/controller_manager.h>
51 
53 {
54 public:
55  static constexpr double period = 0.01;
56 
58  : pos_(0.0)
59  , vel_(0.0)
60  , eff_(0.0)
61  , max_eff_(10.0)
62  , cmd_(0.0)
63  {
64  // joint state interface
65  hardware_interface::JointStateHandle state_handle("joint", &pos_, &vel_, &eff_);
66  state_interface_.registerHandle(state_handle);
68  // effort interface
70  effort_interface_.registerHandle(effort_handle);
72  }
73 
74  void read() {
75  // update current state
76  pos_ += period * vel_ + 0.5*std::pow(period, 2.0) * eff_;
77  vel_ += period * eff_;
78  }
79 
80  void write() {
81  // store current command
82  eff_ = cmd_;
83  if(std::fabs(eff_) > max_eff_) {
84  ROS_WARN("Large effort command received: %f; cutting down to % f", eff_, max_eff_);
85  eff_ = eff_ > 0.0 ? max_eff_ : -max_eff_;
86  }
87  }
88 
89 private:
92  double cmd_, pos_, vel_, eff_;
93  double max_eff_;
94 };
95 
96 
97 int main(int argc, char **argv)
98 {
99  ros::init(argc, argv, "simple_bot_hw");
100  ros::NodeHandle nh;
101  SimpleBot bot;
102  ros::Publisher clock_publisher = nh.advertise<rosgraph_msgs::Clock>("/clock", 1);
103  nh.setParam("/use_sim_time", true);
104  controller_manager::ControllerManager cm(&bot, nh);
106  spinner.start();
107 
108  ros::Rate rate(1.0/bot.period);
110  double sim_time = 0.0;
111 
112  // main "simulation"
113  while(ros::ok()) {
114  auto begin = std::chrono::system_clock::now();
115  bot.read();
116  cm.update(ros::Time(sim_time), period);
117  bot.write();
118  auto end = std::chrono::system_clock::now();
119  double elapsed = std::chrono::duration_cast<std::chrono::duration<double> >((end - begin)).count();
120  if(bot.period > elapsed)
121  std::this_thread::sleep_for(std::chrono::duration<double>(bot.period - elapsed));
122  rosgraph_msgs::Clock clock;
123  clock.clock = ros::Time(sim_time);
124  clock_publisher.publish(clock);
125  sim_time += bot.period;
126  }
127 
128  spinner.stop();
129 
130  return 0;
131 }
hardware_interface::EffortJointInterface effort_interface_
hardware_interface::JointStateInterface state_interface_
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
#define ROS_WARN(...)
void spinner()
int main(int argc, char **argv)
double max_eff_
void publish(const boost::shared_ptr< M > &message) const
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void registerHandle(const JointStateHandle &handle)
JointStateHandle getHandle(const std::string &name)
void write()
void setParam(const std::string &key, const XmlRpc::XmlRpcValue &v) const
static constexpr double period


effort_controllers
Author(s): Vijay Pradeep
autogenerated on Fri Feb 3 2023 03:19:08