-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passing a reference to the subscriber callback method #873
Comments
Hello @liamhan0905 , the callback shall follow the following signature: https://github.com/ros2/rclc/blob/442c4e84bf9ce9c819aa371b2d01e0c4e0835e06/rclc/include/rclc/executor_handle.h#L63 |
isn't this suitable for subscriber with additional argument? |
Not sure if lambdas can be used. Any idea @JanStaschulat ? |
HI @liamhan0905 , see here an example with subscription with context replace:
void subscription_callback(const void * msgin, void * context_ptr) In your subscription callback you are not using are void pointer as second argument. |
If you are using C++, you can also refer to this example. There a static member function is used as subscription callback. The lambda function with state is a problem, because this state would be a second parameter to a C function. But the subscription function signature expects only one parameter, the For the same reason, normal class methods won't work as subscription callback functions, because the conversion from C++ to C does not know how to handle the implicit (this) pointer (which refers to the current class instantiation). Therefore the current work-around is to use subscripiton_with_context. The context can be any pointer an object (e.g. class). |
@liamhan0905 Yes, this function type declaration is used in the function rclc_executor_add_subscription_with_context, in which the callbackFunction -parameter expects two parameters. But this function type is not used in the default function rclc_executor_add_subscription, in which the callbackFunction-parameter expects only one parameter. In your source code above, the default |
How would I go about having the callback function be a method of a class using context? I'm working on a microros application to control dynamixel servos and want to organize the code running on my Teensy 4.1 as a class. During the initialization method of the class, I am initializing the subscriber like so:
This is how I try to add the subscription to the executor using rclc_executor_add_subscription_with_context:
Here commandsCallback is a method of the main class, with the following header and implementation:
Is it even possible to have the callback point to a method inside the class?? How do I do this?
By the way, using &RH3Controller::commandsCallback as a parameter for rclc_executor_add_subscription_with_context instead of &commandsCallback also doesn't work. |
Try with a Background: if you are using a (normal) C++ member function, then there is a hidden third parameter that points to the instance of that class. As the parameter in the rclc is a void pointer, and the conversion from C++ to C function creates an additional parameter, then the function definition is not correct. If a pointer to a static member function is used, there is no additional pointer to the class generated. This is the reason, why a pointer to normal C++ member function creates that error message. See also the rclc-issue#126 Feature request: C++ support |
Thank you very much for the answer, I managed to get things working. In case it helps anyone else (who might not be C++ experts), you only need the static definition on the header, but not on the cpp file. Also, you need to cast the context pointer as shown below:
One thing I did notice is that there is no equivalent for timer callbacks, is this correct? I could not find a way to add context to the timer callback function if we want this callback to be a method of our class. |
I'd like to pass an object into the subscriber_callback method via reference. I've tried couple different trials but couldn't figure out. Does microros_raspberrypi_pico_sdk support this feature?
I get the following error
The text was updated successfully, but these errors were encountered: