joint_state_publisher_mock.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Pilz GmbH & Co. KG
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
19 
20 #include <pilz_utils/get_param.h>
21 
22 namespace pilz_testutils
23 {
24 static const std::string JOINT_NAMES_PARAMETER{ "controller_joint_names" };
25 static const std::string JOINT_STATES_TOPIC_NAME{ "joint_states" };
26 
27 static constexpr unsigned int JOINT_STATES_RATE{ 25 };
28 static constexpr unsigned int JOINT_STATES_QUEUE_SIZE{ 25 };
29 
30 static constexpr double JOINT1_ABSOLUTE_POSITION_LIMIT{ 3.0 };
31 
32 inline int signum(const double& value)
33 {
34  return (value > 0) - (0 > value);
35 }
36 
38 {
39  joint_names_ = pilz_utils::getParam<std::vector<std::string>>(nh_, JOINT_NAMES_PARAMETER);
40  assert(!joint_names_.empty());
41 
43 }
44 
46 {
48 }
49 
50 void JointStatePublisherMock::startPublishingAsync(const double& joint1_start_position)
51 {
52  stop_flag_ = false;
53  joint1_position_ = joint1_start_position;
54  publisher_thread_ = std::thread(&JointStatePublisherMock::run, this);
55 }
56 
58 {
59  go_home_flag_ = false;
60  std::lock_guard<std::mutex> lock(next_msg_mutex_);
61  joint1_velocity_ = vel;
62 }
63 
65 {
66  go_home_flag_ = true;
67 }
68 
70 {
71  if (publisher_thread_.joinable())
72  {
73  stop_flag_ = true;
74  publisher_thread_.join();
75  }
76 }
77 
78 sensor_msgs::JointStateConstPtr JointStatePublisherMock::getNextMessage()
79 {
80  std::lock_guard<std::mutex> lock(next_msg_mutex_);
81  return boost::make_shared<JointState>(next_msg_);
82 }
83 
85 {
88 
90  while (ros::ok() && !stop_flag_)
91  {
92  next_time_stamp_ = next_time_stamp_ + rate.expectedCycleTime();
93 
94  std::unique_lock<std::mutex> lock(next_msg_mutex_);
95  publish();
98  lock.unlock();
99 
100  rate.sleep();
101  }
102 }
103 
105 {
106  next_msg_.name = joint_names_;
107  next_msg_.header.stamp = next_time_stamp_;
108  next_msg_.position.resize(joint_names_.size(), 0.0);
109  next_msg_.velocity.resize(joint_names_.size(), 0.0);
110  next_msg_.effort.resize(joint_names_.size(), 0.0);
111 
112  next_msg_.position.at(0) = joint1_position_;
113 }
114 
116 {
118 }
119 
121 {
122  next_msg_.header.stamp = next_time_stamp_;
123  next_msg_.position.at(0) = joint1_position_;
124 }
125 
127 {
128  double delta = joint1_velocity_ / static_cast<double>(JOINT_STATES_RATE);
129 
130  if (go_home_flag_)
131  {
132  if (std::abs(delta) > std::abs(joint1_position_))
133  {
134  joint1_position_ = 0.0;
135  return;
136  }
137  double position_sign = static_cast<double>(signum(joint1_position_));
138  joint1_position_ -= position_sign * delta;
139  return;
140  }
141 
142  static double sign = 1.0;
144  {
145  sign *= -1.0;
146  }
147  joint1_position_ += sign * delta;
148 }
149 
150 } // namespace pilz_testutils
pilz_testutils::JointStatePublisherMock::~JointStatePublisherMock
~JointStatePublisherMock()
Definition: joint_state_publisher_mock.cpp:45
pilz_testutils::JointStatePublisherMock::run
void run()
Definition: joint_state_publisher_mock.cpp:84
pilz_testutils::signum
int signum(const double &value)
Definition: joint_state_publisher_mock.cpp:32
pilz_testutils::JointStatePublisherMock::createNextMessage
void createNextMessage()
Definition: joint_state_publisher_mock.cpp:104
pilz_testutils::JointStatePublisherMock::next_time_stamp_
ros::Time next_time_stamp_
Definition: joint_state_publisher_mock.h:86
pilz_testutils::JointStatePublisherMock::pub_
ros::Publisher pub_
Definition: joint_state_publisher_mock.h:79
pilz_testutils::JointStatePublisherMock::stop_flag_
std::atomic_bool stop_flag_
Definition: joint_state_publisher_mock.h:81
pilz_testutils::JointStatePublisherMock::go_home_flag_
std::atomic_bool go_home_flag_
Definition: joint_state_publisher_mock.h:82
pilz_testutils::JointStatePublisherMock::updateNextMessage
void updateNextMessage()
Definition: joint_state_publisher_mock.cpp:120
pilz_testutils::JOINT_STATES_TOPIC_NAME
static const std::string JOINT_STATES_TOPIC_NAME
Definition: joint_state_publisher_mock.cpp:25
pilz_testutils::JointStatePublisherMock::joint1_position_
double joint1_position_
Definition: joint_state_publisher_mock.h:84
pilz_testutils::JointStatePublisherMock::joint1_velocity_
double joint1_velocity_
Definition: joint_state_publisher_mock.h:85
get_param.h
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
ros::NodeHandle::advertise
Publisher advertise(AdvertiseOptions &ops)
ros::ok
ROSCPP_DECL bool ok()
pilz_testutils::JointStatePublisherMock::updateJoint1Position
void updateJoint1Position()
Definition: joint_state_publisher_mock.cpp:126
pilz_testutils::JOINT1_ABSOLUTE_POSITION_LIMIT
static constexpr double JOINT1_ABSOLUTE_POSITION_LIMIT
Definition: joint_state_publisher_mock.cpp:30
pilz_testutils::JointStatePublisherMock::getNextMessage
JointStateConstPtr getNextMessage()
Return the message which will be published next.
Definition: joint_state_publisher_mock.cpp:78
pilz_testutils::JOINT_STATES_QUEUE_SIZE
static constexpr unsigned int JOINT_STATES_QUEUE_SIZE
Definition: joint_state_publisher_mock.cpp:28
joint_state_publisher_mock.h
pilz_testutils::JointStatePublisherMock::stopPublishing
void stopPublishing()
Definition: joint_state_publisher_mock.cpp:69
pilz_testutils::JointStatePublisherMock::JointState
sensor_msgs::JointState JointState
Definition: joint_state_publisher_mock.h:40
pilz_testutils
Definition: joint_state_publisher_mock.h:31
pilz_testutils::JointStatePublisherMock::publish
void publish()
Definition: joint_state_publisher_mock.cpp:115
pilz_testutils::JointStatePublisherMock::joint_names_
std::vector< std::string > joint_names_
Definition: joint_state_publisher_mock.h:80
pilz_testutils::JointStatePublisherMock::next_msg_mutex_
std::mutex next_msg_mutex_
Definition: joint_state_publisher_mock.h:88
pilz_testutils::JointStatePublisherMock::nh_
ros::NodeHandle nh_
Definition: joint_state_publisher_mock.h:78
ros::Rate
pilz_testutils::JointStatePublisherMock::next_msg_
JointState next_msg_
Definition: joint_state_publisher_mock.h:87
ros::NodeHandle::getNamespace
const std::string & getNamespace() const
pilz_testutils::JOINT_NAMES_PARAMETER
static const std::string JOINT_NAMES_PARAMETER
Definition: joint_state_publisher_mock.cpp:24
pilz_testutils::JointStatePublisherMock::JointStatePublisherMock
JointStatePublisherMock()
Definition: joint_state_publisher_mock.cpp:37
pilz_testutils::JointStatePublisherMock::setJoint1Velocity
void setJoint1Velocity(const double &vel)
Definition: joint_state_publisher_mock.cpp:57
pilz_testutils::JOINT_STATES_RATE
static constexpr unsigned int JOINT_STATES_RATE
Definition: joint_state_publisher_mock.cpp:27
pilz_testutils::JointStatePublisherMock::publisher_thread_
std::thread publisher_thread_
Definition: joint_state_publisher_mock.h:83
pilz_testutils::JointStatePublisherMock::goHome
void goHome()
Go back to home position (position=velocity=0.0).
Definition: joint_state_publisher_mock.cpp:64
pilz_testutils::JointStatePublisherMock::startPublishingAsync
void startPublishingAsync(const double &joint1_start_position=0.0)
Definition: joint_state_publisher_mock.cpp:50
ros::Time::now
static Time now()


pilz_testutils
Author(s):
autogenerated on Sat Nov 25 2023 03:16:57