example_gazebo_node.cpp
Go to the documentation of this file.
1 //Repeat example 3 by contructing a group from a urdf
2 //This node can be run on either gazebo/physical modules with
3 //the appropriate urdf and launch file/hardware
4 
5 #include "ros/ros.h"
6 #include "sensor_msgs/JointState.h"
7 #include "hebiros/AddGroupFromURDFSrv.h"
8 
9 using namespace hebiros;
10 
11 
12 //Global variable and callback function used to store feedback data
13 sensor_msgs::JointState feedback;
14 
15 void feedback_callback(sensor_msgs::JointState data) {
16  feedback = data;
17 }
18 
19 
20 int main(int argc, char **argv) {
21 
22  //Initialize ROS node
23  ros::init(argc, argv, "example_gazebo_node");
25  ros::Rate loop_rate(200);
26 
27  std::string group_name = "x_demo";
28 
29  //Create a client which uses the service to create a group
30  //The urdf for x_demo must have already been set in the robot_description parameter
31  ros::ServiceClient add_group_client = n.serviceClient<AddGroupFromURDFSrv>(
32  "/hebiros/add_group_from_urdf");
33 
34  //Create a subscriber to receive feedback from a group
35  //Register feedback_callback as a callback which runs when feedback is received
36  ros::Subscriber feedback_subscriber = n.subscribe(
37  "/hebiros/"+group_name+"/feedback/joint_state", 100, feedback_callback);
38 
39  //Create a publisher to send desired commands to a group
40  ros::Publisher command_publisher = n.advertise<sensor_msgs::JointState>(
41  "/hebiros/"+group_name+"/command/joint_state", 100);
42 
43  AddGroupFromURDFSrv add_group_srv;
44 
45  //Construct a group using a urdf
46  add_group_srv.request.group_name = group_name;
47  //Call the add_group_from_urdf service to create a group until it succeeds
48  //Specific topics and services will now be available under this group's namespace
49  while(!add_group_client.call(add_group_srv)) {}
50 
51  //Construct a JointState to command to the modules
52  //This may potentially contain a name, position, velocity, and effort for each module
53  sensor_msgs::JointState command_msg;
54  command_msg.name.push_back("HEBI/base");
55  command_msg.name.push_back("HEBI/elbow");
56  command_msg.name.push_back("HEBI/shoulder");
57  command_msg.effort.resize(3);
58 
59  feedback.position.reserve(3);
60 
61  double spring_constant = -10;
62 
63  while(ros::ok()) {
64 
65  //Apply Hooke's Law: F = -k * x to all modules and publish as a command
66  command_msg.effort[0] = spring_constant * feedback.position[0];
67  command_msg.effort[1] = spring_constant * feedback.position[1];
68  command_msg.effort[2] = spring_constant * feedback.position[2];
69  command_publisher.publish(command_msg);
70 
71  ros::spinOnce();
72  loop_rate.sleep();
73  }
74 
75  return 0;
76 }
77 
78 
79 
80 
81 
82 
83 
84 
85 
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
sensor_msgs::JointState feedback
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
int main(int argc, char **argv)
int n
Definition: key_read.py:85
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool sleep()
void feedback_callback(sensor_msgs::JointState data)
ROSCPP_DECL void spinOnce()


hebiros_advanced_examples
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:13:22