Subscribers blocking for performing calculations on data
Hi guys I've made a ROS node which subscribes on two topics and publishes a value on another topic, the problem is though i am not able to publish data to the topic since my callbacks keep blocking the publisher on doing so.. How can I overcome this issue? The data has to be processed before the publisher can have it...
Here is the code:
#include "ros/ros.h"
#include "std_msgs/String.h"
#include "sensor_msgs/JointState.h"
#include <vector>
#include <sstream>
using namespace std;
#define kp 1 // P
#define ki 1 // I
#define kd 1 // D
#define dt 0.1 // dt
sensor_msgs::JointState msg;
double previous_error;
double integral;
double setpoint;
double measuredValue;
double derivative;
void measuredValued(const sensor_msgs::JointState::ConstPtr& msg)
{
cout << "Measured value" << endl;
measuredValue = msg->velocity[0];
}
void setPoints(const sensor_msgs::JointState::ConstPtr& msg)
{
//cout << "setPoint value" << endl;
setpoint = msg->velocity[0];
}
int main(int argc, char **argv)
{
//previous_error = 0;
//integral = 0;
//setpoint = 0;
//measuredValue = 0;
//derivative = 0;
sensor_msgs::JointState msg;
std::vector<double> vel(2);
std::vector<double> pos(2);
pos[0] = 0;
pos[1] = 0;
ros::init(argc, argv, "pid");
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<sensor_msgs::JointState>("/ptu/cmd", 1);
ros::Subscriber sub_mes = n.subscribe("/joint_states", 1, measuredValued);
ros::Subscriber sub_set = n.subscribe("/pid",1,setPoints);
double error = setpoint - measuredValue;
integral = integral + error*dt;
derivative = (error-previous_error)/dt;
vel[0] = kp*error + ki*integral +kd*derivative;
vel[1] = 0;
previous_error = error;
msg.position = pos;
msg.velocity = vel;
cout << vel[0] << endl;
chatter_pub.publish(msg);
return 0;
}
The code itself is a simple PID controller, which reaceive feedback from a topic and a setpoint from another topic..
Where's your
ros.spin()
? That is, it's not a matter of 'blocking', it's a matter of exiting...