Error implementing ros timer
My goal was to make a Time Stamp on each message published from the commlink node and subscribe them to my pd_controller. I need the Time Stamps to apply numerical methods. In this case is a simple differentiation. But as I go further into the book there are lots of cool methods.
The problem is that my code compiles, but it does not run. I probably did something wrong, and I could not find a tutorial doing something similar with ROS in order to understand what I made.
So, how do I write my own controller? What are the best practices? How do I identify and fix my code?
This my pd_controller node
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <std_msgs/Float32.h>
#include <std_msgs/Float64.h>
#include <std_msgs/Int32.h>
#include "surp/Int32Stamped.h"
#include <math.h>
surp::Int32Stamped encoder;
ros::Subscriber encoder_sub;
ros::Publisher vel_pub;
geometry_msgs::Twist vel;
// subscriber from comm encoder
void encoderCallback(const surp::Int32Stamped::ConstPtr& tk){
//Modify this equation to be the Controller Function
double timelapse = double(encoder.header.stamp) - double(encoder.header.stamp);
double kd = (double(encoder.data) - double(encoder.data))/timelapse;
vel.linear.x = float(((encoder.data*234)*5886) + kd);
encoder.data = tk->data;
encoder.header.stamp = tk->header;
vel_pub.publish(vel);
}
//make publisher for cmd_vel
//create a rotary encoder to send cmd_vel
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);
ros::spin();
return 0;
}
I get this error:
/home/rmb/gopigo_ws/src/pd_controller/src/pd_controller.cpp: In function ‘void encoderCallback(const ConstPtr&)’:
/home/rmb/gopigo_ws/src/pd_controller/src/pd_controller.cpp:21:61: error: no match for ‘operator/’ (operand types are ‘double’ and ‘ros::Duration’)
double kd = (double(encoder.data) - double(encoder.data))/timelapse;
^
/home/rmb/gopigo_ws/src/pd_controller/src/pd_controller.cpp:24:25: error: no match for ‘operator=’ (operand types are ‘std_msgs::Header_<std::allocator<void> >::_stamp_type {aka ros::Time}’ and ‘const _header_type {aka const std_msgs::Header_<std::allocator<void> >}’)
encoder.header.stamp = tk->header;
^
In file included from /opt/ros/kinetic/include/ros/ros.h:38:0,
from /home/rmb/gopigo_ws/src/pd_controller/src/pd_controller.cpp:1:
/opt/ros/kinetic/include/ros/time.h:176:22: note: candidate: ros::Time& ros::Time::operator=(const ros::Time&)
class ROSTIME_DECL Time : public TimeBase<Time, Duration>
^
/opt/ros/kinetic/include/ros/time.h:176:22: note: no known conversion for argument 1 from ‘const _header_type {aka const std_msgs::Header_<std::allocator<void> >}’ to ‘const ros::Time&’
pd_controller/CMakeFiles/pd_controller.dir/build.make:62: recipe for target 'pd_controller/CMakeFiles/pd_controller.dir/src/pd_controller.cpp.o' failed
make[2]: *** [pd_controller/CMakeFiles/pd_controller.dir/src/pd_controller.cpp.o] Error 1
CMakeFiles/Makefile2:784: recipe for target 'pd_controller/CMakeFiles/pd_controller.dir/all' failed
make[1]: *** [pd_controller/CMakeFiles/pd_controller.dir/all] Error 2
Makefile:138: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j4 -l4" failed
Contact GitHub API Training Shop Blog About ...
What book are you referring to?
You say 'it does not run'? What doesn't run, and how do you know it doesn't run? Have you verified that the topics that trigger your callbacks are being published to? Like the topic "encoder"?
Yes, I verified. I have a node called test where I do a version much simpler to test all nodes and my robot. It worked. My pd_controller node does not run (rosrun give error and close) when I tried to use the TimeStamps. I believe I implemented the msg right. So it might be the ros::Timer.
Right Know I am practicing using Modern Control from Ogata.
I have other books that I will study, Introduction to Robotics-Craig, Theory of Applied Robotics - Jazar, Automatic control system - Benajmin C. Kuo, Robots Vision and Control - Peter Corke, Robot Modeling and Control - Mark W. Spong.
You said that it gives errors, can you please post them and anything else that the terminal prints out?
To set timestamps in the header of a message you should be using a ROS data type like
ros::Time::now()
ornh.now()
instead ofsurp::Int32Stamped
I got this error on commlink node:
On the code that publishes the Time Stamp I have ros::Time::now() surp::Int32Stamped is my custom msg. I believe I need it to be able to subscribe the topic right?