qb_device_control.h
Go to the documentation of this file.
1 /***
2  * Software License Agreement: BSD 3-Clause License
3  *
4  * Copyright (c) 2016-2018, qbrobotics®
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
8  * following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this list of conditions and the
11  * following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
14  * following disclaimer in the documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
17  * products derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
25  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef QB_DEVICE_CONTROL_H
29 #define QB_DEVICE_CONTROL_H
30 
31 // Standard libraries
32 #include <regex>
33 #include <mutex>
34 
35 // ROS libraries
37 #include <control_msgs/FollowJointTrajectoryAction.h>
40 #include <ros/callback_queue.h>
41 #include <std_msgs/Int32.h>
42 
43 // internal libraries
45 
46 namespace qb_device_control {
60  public:
72 
76  virtual ~qbDeviceControl();
77 
85  void move(const trajectory_msgs::JointTrajectory &joint_trajectory, const std::string &controller);
86 
96  std::vector<trajectory_msgs::JointTrajectoryPoint> getSinusoidalPoints(const double &amplitude, const double &period, const int &samples_per_period, const int &periods);
97 
108  std::vector<trajectory_msgs::JointTrajectoryPoint> getTrapezoidalPoints(const double &amplitude, const double &period, const double &ramp_duration, const int &periods);
109 
118  trajectory_msgs::JointTrajectory getCustomTrajectory(const std::vector<std::vector<trajectory_msgs::JointTrajectoryPoint>> &joint_points, const std::string &controller);
119 
138  trajectory_msgs::JointTrajectory getWaypointTrajectory(ros::NodeHandle &node_handle, const std::string &controller);
139 
146  bool waitForResult(const ros::Duration &timeout, const std::string &controller);
147 
148  protected:
164 
165  std::mutex sync_protector_;
166  std::vector<std::string> device_names_;
167  std::vector<std::string> controllers_;
168  std::map<std::string, std::string> controller_device_name_;
169  std::map<std::string, std::vector<std::string>> controller_joints_;
170  std::map<std::string, std::unique_ptr<actionlib::SimpleActionClient<control_msgs::FollowJointTrajectoryAction>>> action_clients_;
171  std::map<std::string, trajectory_msgs::JointTrajectory> joint_trajectories_;
172 
173  // do not change the following variables order
177 
182  void move();
183 
184  private:
185  int counter_; // control loop counter (just to check the frequency)
186 
191  void actionActiveCallback(const std::string &controller);
192 
200  void actionDoneCallback(const actionlib::SimpleClientGoalState &state, const control_msgs::FollowJointTrajectoryResultConstPtr &result, const std::string &controller);
201 
207  void actionFeedbackCallback(const control_msgs::FollowJointTrajectoryFeedbackConstPtr &feedback, const std::string &controller);
208 
214  void controlCallback(const ros::WallTimerEvent &timer_event);
215 
221  void controlSetupCallback(const ros::WallTimerEvent &timer_event);
222 
229  std::string extractDeviceName(const std::string &controller);
230 
236  void frequencyCallback(const ros::WallTimerEvent &timer_event);
237 
249  bool getAsyncMeasurementsCallback(qb_device_srvs::GetMeasurementsRequest &request, qb_device_srvs::GetMeasurementsResponse &response);
250 
255  void initActionClients();
256 
266  bool parseVector(const XmlRpc::XmlRpcValue &xml_value, const std::string &controller, std::vector<double> &vector);
267 
272  void parseWaypoints();
273 
285  bool setAsyncCommandsCallback(qb_device_srvs::SetCommandsRequest &request, qb_device_srvs::SetCommandsResponse &response);
286 
298  bool setAsyncPIDCallback(qb_device_srvs::SetPIDRequest &request, qb_device_srvs::SetPIDResponse &response);
299 
308  void update(const ros::WallTime &time, const ros::WallDuration &period);
309 
319  template<class T> T xmlCast(XmlRpc::XmlRpcValue xml_value);
320 };
321 } // namespace qb_device_control
322 
323 #endif // QB_DEVICE_CONTROL_H
ros::ServiceServer set_async_commands_server_
The qbrobotics Control interface provides all the common structures to control both the qbhand and qb...
bool getAsyncMeasurementsCallback(qb_device_srvs::GetMeasurementsRequest &request, qb_device_srvs::GetMeasurementsResponse &response)
Make a call to the same type of service provided by the Communication Handler.
trajectory_msgs::JointTrajectory getCustomTrajectory(const std::vector< std::vector< trajectory_msgs::JointTrajectoryPoint >> &joint_points, const std::string &controller)
Build a joint trajectory for the given controller using the given points, which must match the contro...
bool setAsyncCommandsCallback(qb_device_srvs::SetCommandsRequest &request, qb_device_srvs::SetCommandsResponse &response)
Make a call to the same type of service provided by the Communication Handler.
bool waitForResult(const ros::Duration &timeout, const std::string &controller)
Wait until the action is completed or the given timeout is reached.
void initActionClients()
Retrieve all the controllers which have their parameters set in the Parameter Server and initialize t...
std::vector< trajectory_msgs::JointTrajectoryPoint > getTrapezoidalPoints(const double &amplitude, const double &period, const double &ramp_duration, const int &periods)
Build a single-joint trapezoidal wave point trajectory from the given parameters. ...
std::map< std::string, std::string > controller_device_name_
std::map< std::string, trajectory_msgs::JointTrajectory > joint_trajectories_
std::map< std::string, std::unique_ptr< actionlib::SimpleActionClient< control_msgs::FollowJointTrajectoryAction > > > action_clients_
ros::ServiceServer get_async_measurements_server_
std::vector< std::string > controllers_
T xmlCast(XmlRpc::XmlRpcValue xml_value)
Cast an XmlRpcValue from TypeDouble, TypeInt or TypeBoolean to the specified template type...
bool parseVector(const XmlRpc::XmlRpcValue &xml_value, const std::string &controller, std::vector< double > &vector)
Parse the given XmlRpcValue as a std::vector, since the XmlRpc::XmlRpcValue class does not handle thi...
void update(const ros::WallTime &time, const ros::WallDuration &period)
Read the current state from the HW, update all active controllers, and send the new references to the...
std::string extractDeviceName(const std::string &controller)
Extract the device names associated to the given controller (each controller name is assumed to start...
void controlSetupCallback(const ros::WallTimerEvent &timer_event)
Initialize the control timer and automatically start the waypoint trajectory if it is not empty...
void actionDoneCallback(const actionlib::SimpleClientGoalState &state, const control_msgs::FollowJointTrajectoryResultConstPtr &result, const std::string &controller)
Restart the waypoint trajectory automatically if waypoints have been retrieved during the initializat...
void controlCallback(const ros::WallTimerEvent &timer_event)
Call the update() each time the timer triggers.
std::vector< trajectory_msgs::JointTrajectoryPoint > getSinusoidalPoints(const double &amplitude, const double &period, const int &samples_per_period, const int &periods)
Build a single-joint sine wave point trajectory from the given parameters.
std::map< std::string, std::vector< std::string > > controller_joints_
qbDeviceControl()
Initialize the control structures needed by ros_control: the combined_robot_hw::CombinedRobotHW to co...
bool setAsyncPIDCallback(qb_device_srvs::SetPIDRequest &request, qb_device_srvs::SetPIDResponse &response)
Make a call to the same type of service provided by the Communication Handler.
void move()
Make all the calls to the Action Servers relative to all the waypoint trajectories previously parsed...
void frequencyCallback(const ros::WallTimerEvent &timer_event)
Publish the real control loop frequency in Hz in the relative topic.
void actionFeedbackCallback(const control_msgs::FollowJointTrajectoryFeedbackConstPtr &feedback, const std::string &controller)
Do nothing apart from debug info.
void parseWaypoints()
Parse all the waypoints set up in the Parameter Server at <robot_namespace>/waypoints.
virtual ~qbDeviceControl()
Stop timer and spinner structures.
void actionActiveCallback(const std::string &controller)
Do nothing apart from debug info.
std::vector< std::string > device_names_
controller_manager::ControllerManager controller_manager_
trajectory_msgs::JointTrajectory getWaypointTrajectory(ros::NodeHandle &node_handle, const std::string &controller)
Retrieve a set of reference waypoints from the Parameter Server.
combined_robot_hw::CombinedRobotHW devices_


qb_device_control
Author(s): qbrobotics®
autogenerated on Wed Oct 9 2019 03:45:41