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

how can i save rostopic in array?

asked 2020-08-14 01:50:57 -0600

Ray_Shim gravatar image

updated 2020-08-14 17:49:25 -0600

jayess gravatar image

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
edit retag flag offensive close merge delete

Comments

I would recommend you to use vector instead. Something like: std::vector<geometry_msgs::pointstamped>

Teo Cardoso gravatar image Teo Cardoso  ( 2020-08-15 21:56:28 -0600 )edit

could you tell me a reason? I don` know that what is good for this.

Ray_Shim gravatar image Ray_Shim  ( 2020-08-16 22:20:46 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-08-14 02:21:41 -0600

HappyBit gravatar image

updated 2020-08-14 05:47:36 -0600

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
edit flag offensive delete link more

Comments

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.

Ray_Shim gravatar image Ray_Shim  ( 2020-08-14 03:03:48 -0600 )edit

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?)

Ray_Shim gravatar image Ray_Shim  ( 2020-08-16 22:19:27 -0600 )edit

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.

HappyBit gravatar image HappyBit  ( 2020-08-17 01:54:24 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-08-14 01:50:57 -0600

Seen: 387 times

Last updated: Aug 14 '20