This question comes on the heels of this question
In the last question I learned how to use an initializer list to solve my issue, but working with ros::Subscriber
is requiring a different solution.
I'm dealing with a callback in this scenario. If I leave the callback as a standalone function then the program runs but I have no access to class members and methods inside the function which defeats the purpose. Here is my standalone callback:
void setPRYCallback( const std_msgs::Empty& toggle_msg) {}
Once I move this into my class is when I encounter the error.
steps to implementing into class
// add member variable for callback
protected:
void setPRYCallback( const std_msgs::Empty& );
...
// ass method
void ROSController::setPRYCallback( const std_msgs::Empty& toggle_msg) {}
...
// set initializer list
ROSController::ROSController() : sub("setPRY", &setPRYCallback )
How do I thwart this error while keeping my callback as a class method?
Program with Standalone Function Callback (program runs)
#include <ros.h> // https://github.com/ros-drivers/rosserial/blob/jade-devel/rosserial_arduino/src/ros_lib/ros.h
#include <std_msgs/Empty.h>
class ROSController {
protected:
ros::Subscriber<std_msgs::Empty> sub;
ros::NodeHandle _nh;
public:
void loop();
ROSController();
};
void setPRYCallback( const std_msgs::Empty& toggle_msg) {}
ROSController::ROSController() {
this->_nh.initNode();
this->_nh.subscribe(sub);
}
void ROSController::loop() {
this->_nh.spinOnce();
delay(1000);
}
Program with Method Callback (program fails)
#include <ros.h> // https://github.com/ros-drivers/rosserial/blob/jade-devel/rosserial_arduino/src/ros_lib/ros.h
#include <std_msgs/Empty.h>
class ROSController {
protected:
ros::Subscriber<std_msgs::Empty> sub;
ros::NodeHandle _nh;
void setPRYCallback( const std_msgs::Empty& );
public:
void loop();
ROSController();
};
void ROSController::setPRYCallback( const std_msgs::Empty& toggle_msg) {}
ROSController::ROSController() : sub("setPRY", &setPRYCallback ) {
this->_nh.initNode();
this->_nh.subscribe(sub);
}
void ROSController::loop() {
this->_nh.spinOnce();
delay(1000);
}
arduino mega