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

ROS2 Subscriber create no class

asked 2021-10-27 04:40:25 -0500

OlexStup1996 gravatar image

updated 2021-10-27 18:10:04 -0500

Geoff gravatar image

Hallo,

i cannot correct create subscriber without using classes like in ROS2-Tutorials (Creation of Publisher and subscriber).

I want to migrate my code to ros2 and have a problem by creation of Subscriber. example of ros1 code :

//my callback function
void robot_pose_callback(const geometry_msgs::Pose &call_back_target)
{
do what i want;
}
// in main:
ros::Subscriber sub = n.subscribe("robot_pose", 1000,
robot_pose_callback);

ROS2 Tutorial use classes, which i dont want to use. ( i dont check it and i dont want to check it)

in ROS2 it have to be look like :

#includes ...

//my callback - Parameter in brackets are wrong ?
void robot_pose_callback(geometry_msgs::msg::Pose::SharedPtr msg)

{
}

int main(int argc, char** argv)
{
  rclcpp::init(argc, argv);
  auto move_group_node = rclcpp::Node::make_shared("move_group_interface_tutorial");

(Parameter in brackets are wrong)
  auto sub = move_group_node->create_subscription<geometry_msgs::msg::Pose>("robot_pose",10, void(robot_pose_callback));

the right parameter i can look up on this website : https://docs.ros2.org/beta3/api/rclcp...

but i dont know what i have to write for :

Parameters (create subscriber 1/2 => there is another parameter list 2/2)
    [in]    topic_name  The topic to subscribe on.
    [in]    callback    The user-defined callback function.
    [in]    qos_profile The quality of service profile to pass on to the rmw implementation.
    [in]    group   The callback group for this subscription. NULL for no callback group.
    [in]    ignore_local_publications   True to ignore local publications.
    [in]    msg_mem_strat   The message memory strategy to use for allocating messages.
    [in]    allocator   Optional custom allocator.

can somebody show me simple example for this parameter. Thank you in advice !!!!!!!!!!

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2021-10-27 18:13:46 -0500

Geoff gravatar image

Have a look at this minimal_subscriber example for how to create a subscription without writing a class.

edit flag offensive delete link more

Comments

how can i access data in callback? its dont work =( my fail: i cannot use operator "=" for local variable in callback.

colcon build:  error: no match for ‘operator=’ (operand types are ‘geometry_msgs::msg::Pose’ {aka ‘geometry_msgs::msg::Pose_<std::allocator<void> >’} and ‘const ConstSharedPtr’ {aka ‘const std::shared_ptr<const geometry_msgs::msg::Pose_<std::allocator<void> > >’})
   37 |  target_pose1 = call_back_target;

my callback:

void robot_pose_callback(const geometry_msgs::msg::Pose::ConstSharedPtr call_back_target)

{

 geometry_msgs::msg::Pose target_pose1;
 target_pose1 = call_back_target;

 //check or change position of target_pose1

 move_group->setPoseTarget(target_pose1);
}
OlexStup1996 gravatar image OlexStup1996  ( 2021-11-03 07:27:49 -0500 )edit

call_back_target is a pointer. target_post1 is not. You need to dereference call_back_target:

target_pose1 = *call_back_target;

Geoff gravatar image Geoff  ( 2021-11-03 18:38:25 -0500 )edit

Thank you !

OlexStup1996 gravatar image OlexStup1996  ( 2021-11-24 02:16:23 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2021-10-27 04:40:25 -0500

Seen: 1,288 times

Last updated: Oct 27 '21