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

create_wall_timer bind's callback funtion won't work [closed]

asked 2021-11-11 02:25:25 -0500

utopia_david gravatar image

HI all:

i am new to ROS2-Foxy, i create a ros2 node class which inherit from rclcpp::Node as a compoment like this: RCLCPP_COMPONENTS_REGISTER_NODE(utopia::drivers::rtk::SerialNode), i decleared a walltime timer in two ways(lamda funtion or not lamda ), when i launch this node, the publisher inside this wall_timer's bind funtion never was called.

  • desicreption:

way 1 no lamda: first : define a timer as class member in Class's header like this : rclcpp::TimerBase::SharedPtr read_timer_;

then: implement timer in my class's constructor like this : `read_timer_ = this->create_wall_timer( 50ms, std::bind(&SerialNode::read_callback, this)); (Result: the callback never have called )

way 2(lamda):

   `read_timer_ = create_wall_timer(
    50ms, [this](){
         printf("read gpgga callback \n"); 
        unsigned char  buffer[BUFFER_SIZE] = {0};
        int serial_byte_num = serial_.read(buffer, BUFFER_SIZE);
        auto message = std_msgs::msg::ByteMultiArray();
        gpgga_publisher_->publish(message);
    }); `
   (**Result: the lamda  function  never have called also ** )

Is there anything i missed? i also try not to resigist node not as a compoment and wirte a main.cpp to build an excute node like this: (Still the function that i bind to wall_timer still not be called ):

-----main.cpp----

 `int main(int argc, char* argv[]){
 rclcpp::init(argc, argv);
 rclcpp::NodeOptions options(rclcpp::NodeOptions().use_intra_process_comms(true));
 rclcpp::spin(std::make_shared<SerialNode>(options));
 rclcpp::shutdown();
 return 0;
 } `

Anyone can help me , i will be really appreciate !!

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by utopia_david
close date 2021-12-04 01:58:55.052962

Comments

On this site you should not ordinarily close a question unless there is some problem. It's fine to answer your own question if you figure it out. What you (the author of the question) should do is click on the checkmark in the gray circle to indicate your approval of an Answer.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2021-12-04 08:41:40 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-12-04 01:58:24 -0500

utopia_david gravatar image

it seems the walltimer function can't include blocked thread/function something like serial read or socket read, in some times you can specific an NONBLOCK way to use function like that, maybe better to do such thing in another independed thread rather in walltimer lamda function or callback function.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2021-11-11 02:25:25 -0500

Seen: 709 times

Last updated: Dec 04 '21