0
$\begingroup$

Rosanswers logo

Hi guys. I need help.

I want to make nodes that 'subscribe topic - save in array' and 'read array - publish topic'. (in my case it is geometry_msgs::Pointstamped) But there are no references and questions. Is it impossible?? If it`s possible. Could i get some advice or any reference link?

Thanks for reading :)

This is first node. : subscribe clicked_point and save in array. (not perfect yet. i`m still making.)

#include "ros/ros.h"
#include "geometry_msgs/Pointstamped.h"

int waypointList[10];

int i = 0;

void waypointCallback(const geometry_msgs::Pointstamped::ConstPtr& clicked_point)

int main(int argc, char **argv)
{

    ros::init(argc, argv, "waypoint_saver");
    ros::NodeHandle n;

    //subscriber
    ros::Subscriber waypoint_sub = n.subscribe("waypoint", 1, waypointCallback);
    
    ros::Rate loop_rate(10);

    while (ros::ok())
    {
        //waypoint_pub.header.stamp = ros::Time::now();
        
        if (i < 10){}
            waypointList[i]=clicked_point;
            i += 1;
        }
        else{        
            ROS_INFO("Full waypoint(10 waypoints) %s")
            return 0
        }
        ros::spinOnce();
        loop_rate.sleep();
    }


  return 0

Originally posted by Ray_Shim on ROS Answers with karma: 27 on 2020-08-14

Post score: 1


Original comments

Comment by Teo Cardoso on 2020-08-15:
I would recommend you to use vector instead. Something like: std::vector<geometry_msgs::Pointstamped>

Comment by Ray_Shim on 2020-08-16:
could you tell me a reason? I don` know that what is good for this.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Hi there,

Can you please describe your use-case in more detail? I'm not quite getting what you're trying to do. A minimal example should do. Subscribing to a topic and publishing a message is covered in the tutorials here: http://wiki.ros.org/ROS/Tutorials

That said two useful packages come to my mind, that may interest you:

http://wiki.ros.org/topic_tools

http://wiki.ros.org/message_filters


(edit after comment)

Just to clarify, you want to subscribe to the topic waypoint and store every message received, eg geometry_msg/PoseStamped in an array. After some condition, be it 10 messages received, you would like to do someting.

In this use-case one can design a class. initialise the subscriber, array, counter, ... at the constructor and add the callback as class method. At the callback you add the messages to the array and check if your condition is met. A few things here:

  • pass the ros::NodeHandle via the constructor of your class
  • unregister your subscriber to avoid receiving new messages and therefore triggering the callback

Originally posted by HappyBit with karma: 26 on 2020-08-14

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by Ray_Shim on 2020-08-14:
Thanks for your help.I upload my code.

I have been making patrol algorithm. This parts is saving waypoint by using rviz Publish point. So I need to save waypoint data. And i choose to use array for it. But, i really dont know that topic can be saved in array.

Comment by Ray_Shim on 2020-08-16:
Thanks for your help.

Now i find the way.

And i want to ask two more things.

  1. Is topic's data type string? ( I think its string, but i can't sure.)

  2. Can i save topic at array and publish from array? ( Is there possibility of deformation?)

Comment by HappyBit on 2020-08-17:
The topic name is a string, but the message type is in your case geometry_msg/PoseStamped. Please refer to the Tutorials for more details. I'm glad I could help. If this concludes your question please mark the answer as correct.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.