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

Don't know how to fix my Callback function parameters in subscriber node that is subscribed to a topic of message type ar_track_alvar_msgs/AlvarMarkers

asked 2018-07-02 15:27:34 -0500

randy2g gravatar image

updated 2018-07-02 16:23:30 -0500

(using ubuntu 16.04 using ros kinetic) This is my callback function and subscriber initialization. (Btw I am kind of a noob to c++ and ros). I come from basic experience in java. The main issues I see in my code are that the parameters for my callback function are probably wrong and i don't know how to access the poses and header inside of markers[] which is in AlvarMarkers.msg which is inside ar_track_alvar_msgs, which are being published to the ar_pose_marker topic. I just need to know what parameters I need to put into the callback function and why my parameters are incorrect and also if my subscriber initialization parameters look fine. I've already read and replicated the tutorials for Writing a simple publisher and subscriber in C++ the thing is that the messages being published over my topic are different types than the ones in the tutorial version of the subscriber.

void poseMarkerCallback(const ar_track_alvar_msgs::AlvarMarkersConstptr& markers[])
{

        if(!started)
        {

         positions[1] = msg->markers[sizeof markers / sizeof *markers -1].pose.position.y;
     times[1] = (msg->markers[sizeof markers / sizeof *markers -1].pose.header.stamp.sec) + (msg->               markers[sizeof markers / sizeof *markers -1].pose.header.stamp.nsec/1,000,000,000); //get ar_pose_marker value
     positions[2] = 0;
     times[2] = 0;
     started = true;
     }
     else
    {
     positions[2] = positions[1];
     times[2] = times[1];
     positions[1] = msg->markers[sizeof markers / sizeof *markers -1].pose.position.y;
      times[1] = (msg->markers[sizeof markers / sizeof *markers -1].header.stamp.sec) + (msg->markers[sizeof markers / sizeof *markers -1].header.stamp.nsec/1,000,000,000);  //get ar_pose_marker value 
     }
velocity = (positions[1]-positions[2])/(times[1]-times[2]);
  ROS_INFO("Velocity:[%d]" + velocity, msg->data.c_str());


}


  ros::Subscriber sub2 = n.subscribe("ar_pose_marker", 1000, poseMarkerCallback); //this is inside of int main
edit retag flag offensive close merge delete

Comments

1

Your code's indentation is all screwed up atm - making it hard to read. Getting the preformatted text to work is a bit tricky. Usually I copy-paste code directly from Editor/IDE, highlight code, and then hit 101010 button. Seem to break less often that way.

josephcoombe gravatar image josephcoombe  ( 2018-07-02 16:19:39 -0500 )edit
1

just edited the post, hopefully that looks a little better.

randy2g gravatar image randy2g  ( 2018-07-02 16:25:29 -0500 )edit

2 Answers

Sort by » oldest newest most voted
0

answered 2018-07-02 16:55:41 -0500

kartikmohta gravatar image

updated 2018-07-02 18:21:32 -0500

The callback function prototype should be

void poseMarkerCallback(const ar_track_alvar_msgs::AlvarMarkersConstPtr& msg)

Then you can access the markers inside the message as msg->markers.

One more thing, in your code I see sizeof markers / sizeof *markers being used at a bunch of places. In order to get the number of elements in markers (which is a std::vector), you can just use markers.size().

edit: Fixed a typo, the 'p' in 'ConstPtr' should be capital.

edit flag offensive delete link more

Comments

error: ‘AlvarMarkersConstptr’ in namespace ‘ar_track_alvar_msgs’ does not name a type void poseMarkerCallback(const ar_track_alvar_msgs::AlvarMarkersConstptr& msg) ^

randy2g gravatar image randy2g  ( 2018-07-02 17:08:22 -0500 )edit

Sorry, small typo there, the 'p' in 'Constptr' should be capital:

void poseMarkerCallback(const ar_track_alvar_msgs::AlvarMarkersConstPtr& msg)
kartikmohta gravatar image kartikmohta  ( 2018-07-02 18:18:55 -0500 )edit
0

answered 2018-07-02 17:06:41 -0500

randy2g gravatar image

After changing my callback parameters to this:

void poseMarkerCallback(const ar_track_alvar_msgs::AlvarMarkersConstptr& msg

I am still getting these errors related to my callback method. /home/eli/catkin_ws/src/beginner_tutorials/src/listener.cpp:23:52: error: ‘AlvarMarkersConstptr’ in namespace ‘ar_track_alvar_msgs’ does not name a type void poseMarkerCallback(const ar_track_alvar_msgs::AlvarMarkersConstptr& msg) ^ /home/eli/catkin_ws/src/beginner_tutorials/src/listener.cpp: In function ‘void poseMarkerCallback(const int&)’: /home/eli/catkin_ws/src/beginner_tutorials/src/listener.cpp:29:20: error: base operand of ‘->’ is not a pointer positions[1] = msg->markers[sizeof markers / sizeof *markers -1].pose.position.y; ^ /home/eli/catkin_ws/src/beginner_tutorials/src/listener.cpp:29:37: error: ‘markers’ was not declared in this scope positions[1] = msg->markers[sizeof markers / sizeof *markers -1].pose.position.y; ^ /home/eli/catkin_ws/src/beginner_tutorials/src/listener.cpp:30:17: error: base operand of ‘->’ is not a pointer times[1] = (msg->markers[sizeof markers / sizeof *markers -1].pose.header.stamp.sec) + (msg-> markers[sizeof markers / sizeof *markers -1].pose.header.stamp.nsec/1,000,000,000); //get ar_pose_marker value ^ /home/eli/catkin_ws/src/beginner_tutorials/src/listener.cpp:30:93: error: base operand of ‘->’ is not a pointer times[1] = (msg->markers[sizeof markers / sizeof *markers -1].pose.header.stamp.sec) + (msg-> markers[sizeof markers / sizeof *markers -1].pose.header.stamp.nsec/1,000,000,000); //get ar_pose_marker value ^ /home/eli/catkin_ws/src/beginner_tutorials/src/listener.cpp:40:20: error: base operand of ‘->’ is not a pointer positions[1] = msg->markers[sizeof markers / sizeof *markers -1].pose.position.y; ^ /home/eli/catkin_ws/src/beginner_tutorials/src/listener.cpp:40:37: error: ‘markers’ was not declared in this scope positions[1] = msg->markers[sizeof markers / sizeof *markers -1].pose.position.y; ^ /home/eli/catkin_ws/src/beginner_tutorials/src/listener.cpp:41:17: error: base operand of ‘->’ is not a pointer times[1] = (msg->markers[sizeof markers / sizeof *markers -1].header.stamp.sec) + (msg->markers[sizeof markers / sizeof *markers -1].header.stamp.nsec/1,000,000,000); //get ar_pose_marker value ^ /home/eli/catkin_ws/src/beginner_tutorials/src/listener.cpp:41:88: error: base operand of ‘->’ is not a pointer times[1] = (msg->markers[sizeof markers / sizeof *markers -1].header.stamp.sec) + (msg->markers[sizeof markers / sizeof *markers -1].header.stamp.nsec/1,000,000,000); //get ar_pose_marker value ^ In file included from /opt/ros/kinetic/include/ros/ros.h:40:0, from /home/eli/catkin_ws/src/beginner_tutorials/src/listener.cpp:1: /home/eli/catkin_ws/src/beginner_tutorials/src/listener.cpp:44:31: error: invalid operands of types ‘const char [14]’ and ‘float’ to binary ‘operator+’

edit flag offensive delete link more

Comments

Please don't use an answer to provide more information about your question. This isn't a forum. Please update your question with this information instead (and use the 101010 button).

jayess gravatar image jayess  ( 2018-07-02 20:11:02 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2018-07-02 15:27:34 -0500

Seen: 520 times

Last updated: Jul 02 '18