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

can call back based simple action client subscribe to other topics?

asked 2018-01-04 15:01:19 -0500

avcx gravatar image

updated 2018-01-04 15:06:04 -0500

jayess gravatar image

Is it possible for a call back based simple action client to subscribe to other topics?

I want the client to subscribe to /topicA that publishes sensor_msg::JointState and pass the name, velocity, position, effort to the server.

I tried the following: (partial code): no compilation error but I'm getting segmentation fault immediately when I run the client

//client side code

sensor_msgs::JointState j_msg;   // global variable

void subCallback(const sensor_msgs::JointState::ConstPtr& msg){

    ROS_INFO("joint_name: %f",msg->position);

    j_msg.name.push_back(msg->name[0]);
    j_msg.position.push_back(msg->position[0]);
    j_msg.velocity.push_back(msg->velocity[0]);
    j_msg.effort.push_back(msg->effort[0]);

}

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

    ros::init(argc, argv, "action_client_node");

    std::string topicName = "topicA";
    ros::NodeHandle nh;

    ros::Subscriber sub = nh.subscribe(topicName,1000, subCallback);

    std::string joint_name = j_msg.name[0];
    float position = j_msg.position[0];
    float velocity = j_msg.velocity[0];
    float effort = j_msg.effort[0];

    std::cout<<"joint_name: "<<joint_name<<std::endl;
    std::cout<<"position: "<<position<<std::endl;
    std::cout<<"velocity: "<<velocity<<std::endl;
    std::cout<<"effort: "<<effort<<std::endl;

    //============================================================================================
    actionlib::SimpleActionClient<my_action::MyAction> ac_("my_action_server");
    .....
    goal.target.name.push_back(joint_name);
    goal.target.position.push_back(position);
    goal.target.velocity.push_back(velocity);
    goal.target.effort.push_back(effort);

    .....

}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-01-04 16:54:29 -0500

You're getting a segmentation fault because you're trying to access the first element of the name vector of the j_msg object. Because the callback function you've registered to 'topicA' has not been called yet the vectors within the j_msg object are all empty so trying to access the first element will segmentation fault.

If you create a global variable which is a pointer to the SimpleActionClient object and set it when you create this in main then you should be able to update this within the callback function without any problems.

It's also worth noting that the 'topicA' callback adds extra elements into the name, position, velocity and effort vectors each time it's called, but you're only ever reading out the first element. This might not result in the behaviour you expect.

Let us know if you get this working.

edit flag offensive delete link more

Comments

I did exactly the same and it worked like a charm. Thanks for a great answer. Here is what I did: briefly

typedef actionlib::SimpleActionClient<my_action::myaction> Client; //global Client *clientPointer; //global .. inside main Client ac_(serverName); clientPointer = &ac_; ac_.waitForServer(); ..

avcx gravatar image avcx  ( 2018-01-05 00:59:12 -0500 )edit

Great stuff, glad it's working for you now.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-01-05 08:17:25 -0500 )edit

Hii i'm also geting this error but i don't understand the solving can you explain to me how to fix that code? thanks

frizh gravatar image frizh  ( 2020-11-10 22:54:14 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2018-01-04 15:01:19 -0500

Seen: 537 times

Last updated: Jan 04 '18