ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

If I recall correctly, from your previous question you defined surp::Int32Stamped to be

Header header
int32 data

The header portion of your message has a stamp attribute which carries the time, as you know. According to the wiki, the way that you convert from ros::Duration to floating point is by using the toSec() method, not by casting. For example:

double secs =ros::Time::now().toSec();

ros::Duration d(0.5);
secs = d.toSec();

Therefore, you should change

double timelapse = double(encoder.header.stamp) - double(encoder.header.stamp);

to

double timelapse = encoder.header.stamp.toSec() - encoder.header.stamp.toSec();

But, this raises another issue. Isn't this just going to be zero...

If I recall correctly, from your previous question you defined surp::Int32Stamped to be

Header header
int32 data

The header portion of your message has a stamp attribute which carries the time, as you know. According to the wiki, the way that you convert from ros::Duration to floating point is by using the toSec() method, not by casting. For example:

double secs =ros::Time::now().toSec();

ros::Duration d(0.5);
secs = d.toSec();

Therefore, you should change

double timelapse = double(encoder.header.stamp) - double(encoder.header.stamp);

to

double timelapse = encoder.header.stamp.toSec() - encoder.header.stamp.toSec();

But, this raises another issue. Isn't this just going to be zero...


Edit:

If I'm understanding you correctly, should either use a global (easiest, not necessarily the best way) to keep track of the previous time that the callback was called or use a class and have an attribute that does that.

I've used the ... for brevity, but here's an example (using a global):

#include <ros/ros.h>
#include <geometry_msgs/Twist.h>

...

double previous_time;

// subscriber from comm encoder
void encoderCallback(const surp::Int32Stamped::ConstPtr& tk){

  //Modify this equation to be the Controller Function
  double timelapse = encoder.header.stamp.toSec() - previous_time;
  prevoius_time = encoder.header.stamp.toSec();

...

int main(int argc, char** argv){
   ros::init(argc, argv, "pd_controller");
   ros::NodeHandle nh;
   vel_pub = nh.advertise<geometry_msgs::Twist>("cmd_vel", 1);
   encoder_sub = nh.subscribe<std_msgs::Int32> ("comm_encoder", 10, &encoderCallback); 

   previous_time = ros::Time::now().toSec();

   ros::spin();

   return 0;
}

If I recall correctly, from your previous question you defined surp::Int32Stamped to be

Header header
int32 data

The header portion of your message has a stamp attribute which carries the time, as you know. According to the wiki, the way that you convert from ros::Duration to floating point is by using the toSec() method, not by casting. For example:

double secs =ros::Time::now().toSec();

ros::Duration d(0.5);
secs = d.toSec();

Therefore, you should change

double timelapse = double(encoder.header.stamp) - double(encoder.header.stamp);

to

double timelapse = encoder.header.stamp.toSec() - encoder.header.stamp.toSec();

But, this raises another issue. Isn't this just going to be zero...


Edit:

If I'm understanding you correctly, should either use a global (easiest, not necessarily the best way) to keep track of the previous time that the callback was called or use a class and have an attribute that does that.

I've used the ... for brevity, but here's an example (using a global):

#include <ros/ros.h>
#include <geometry_msgs/Twist.h>

...

double previous_time;

// subscriber from comm encoder
void encoderCallback(const surp::Int32Stamped::ConstPtr& tk){

  //Modify this equation to be the Controller Function
  double timelapse = encoder.header.stamp.toSec() - previous_time;
  prevoius_time previous_time = encoder.header.stamp.toSec();

...

int main(int argc, char** argv){
   ros::init(argc, argv, "pd_controller");
   ros::NodeHandle nh;
   vel_pub = nh.advertise<geometry_msgs::Twist>("cmd_vel", 1);
   encoder_sub = nh.subscribe<std_msgs::Int32> ("comm_encoder", 10, &encoderCallback); 

   previous_time = ros::Time::now().toSec();

   ros::spin();

   return 0;
}