我使用订阅者从 mavros 接收消息,并希望从用于修改私有类变量的订阅者回调中获取值
class MavrosListener
{
private:
geometry_msgs::Vector3 position;
public:
void Listen_From_MAVROS_Callback(const mavros_msgs::DRONESENSOR::ConstPtr& dronesensor);
void Print_Variables();
};
//Callback definition
void MavrosListener::Listen_From_MAVROS_Callback(const mavros_msgs::DRONESENSOR::ConstPtr& dronesensor)
{
position.x = (dronesensor -> position)[0];
}
void MavrosListener::Print_Variables() {
std::cout << "x position: " << position.x << std::endl;
}
//manipulator
int main(int argc, char **argv)
{
ros::init(argc, argv, "GeoCtrlListener");
ros::NodeHandle n;
MavrosListener listener;
ros::Subscriber Sub_From_Mavros =
n.subscribe("/mavros/odroid/odroid_input", 1, &MavrosListener::Listen_From_MAVROS_Callback, &listener);
listener.Print_Variables();
std::cout << "connection end" << std::endl;
listener.Print_Variables();
return 0;
}
对于上面的代码,当我运行它时,spin()之前的打印语句都返回0,这意味着没有修改任何属性。但是,当我按 ctrl c 并结束连接时,控制台返回正确的值。我很困惑我的对象监听器是否被正确修改。我需要存储回调函数中的数据并进行一些操作,然后发布回另一个主题。
有什么建议吗?非常感谢。