Tetrapod Project
tx_node.cpp
Go to the documentation of this file.
1 #include "ros/ros.h"
2 #include "sensor_msgs/JointState.h"
3 
4 class SubAndPub
5 {
6  public:
8  {
9  pub = nh.advertise<sensor_msgs::JointState>("/motor/commands", 1);
10 
11  sub = nh.subscribe("/motor/states", 1, &SubAndPub::callback, this);
12 
13  tx_msg.header.seq = 0;
14 
15  for(int i = 0; i < NUMBER_OF_MOTORS; i++)
16  {
17  tx_msg.position.push_back(0.0);
18  tx_msg.velocity.push_back(1.000);
19  tx_msg.effort.push_back(100.0);
20  }
21  }
22 
23  void callback(const sensor_msgs::JointState &rx_msg)
24  {
25  double recv_time = ros::Time::now().toSec();
26 
27  double duration = recv_time - send_time;
28 
29  ROS_INFO("Time [s]: %f\tTx: %f\tRx: %f\tErr: %f", duration, tx_msg.position[0], rx_msg.position[0], tx_msg.position[0] - rx_msg.position[0]);
30  }
31 
32  void sendMessage()
33  {
34  tx_msg.position[0]++;
35  send_time = ros::Time::now().toSec();
36  pub.publish(tx_msg);
37  }
38 
39 
40 
41  private:
42  ros::NodeHandle nh;
43  ros::Publisher pub;
44  ros::Subscriber sub;
45 
46  sensor_msgs::JointState tx_msg;
47  double send_time = 0.0;
49 };
50 
51 int main(int argc, char **argv)
52 {
53  ros::init(argc, argv, "master_node");
54 
55  SubAndPub master;
56 
57  ros::Rate loop_rate(1000);
58 
59  while(ros::ok())
60  {
61  master.sendMessage();
62  loop_rate.sleep();
63  ros::spinOnce();
64  }
65 }
double send_time
Definition: tx_node.cpp:47
SubAndPub()
Definition: tx_node.cpp:7
void sendMessage()
Definition: tx_node.cpp:32
ros::Subscriber sub
Definition: rx_node.cpp:22
void callback(const sensor_msgs::JointState &_msg)
Definition: rx_node.cpp:14
int NUMBER_OF_MOTORS
Definition: tx_node.cpp:48
void callback(const sensor_msgs::JointState &rx_msg)
Definition: tx_node.cpp:23
sensor_msgs::JointState tx_msg
Definition: tx_node.cpp:46
ros::Publisher pub
Definition: rx_node.cpp:21
ros::NodeHandle nh
Definition: rx_node.cpp:20
int main(int argc, char **argv)
Definition: tx_node.cpp:51