cpp展开代码 ros::Publisher so3_command_pub_; // 发布SO3控制命令的发布器
ros::Subscriber odom_sub_; // 订阅里程计数据的订阅器
ros::Subscriber position_cmd_sub_; // 订阅位置命令的订阅器
ros::Subscriber enable_motors_sub_; // 订阅电机使能信号的订阅器
ros::Subscriber corrections_sub_; // 订阅校正参数的订阅器
ros::Subscriber imu_sub_; // 订阅IMU数据的订阅器
这些是消息处理函数,当ROS话题收到消息时会自动调用对应的回调函数。
cpp展开代码void publishSO3Command(void); // 发布SO3控制命令
void position_cmd_callback(const quadrotor_msgs::PositionCommand::ConstPtr& cmd); // 位置命令回调
void odom_callback(const nav_msgs::Odometry::ConstPtr& odom); // 里程计数据回调
void enable_motors_callback(const std_msgs::Bool::ConstPtr& msg); // 电机使能回调
void corrections_callback(const quadrotor_msgs::Corrections::ConstPtr& msg); // 校正参数回调
void imu_callback(const sensor_msgs::Imu& imu); // IMU数据回调
cpp展开代码void
SO3ControlNodelet::position_cmd_callback( const quadrotor_msgs::PositionCommand::ConstPtr& cmd)
quadrotor_msgs::PositionCommand::ConstPtr&
quadrotor_msgs::PositionCommand - 四旋翼位置控制命令
cpp展开代码void
SO3ControlNodelet::odom_callback(const nav_msgs::Odometry::ConstPtr& odom)
消息类型:nav_msgs::Odometry - 导航里程计消息
cpp展开代码// nav_msgs::Odometry 包含:
odom->pose.pose.position.x/y/z // 当前位置
odom->pose.pose.orientation // 当前姿态(四元数)
odom->twist.twist.linear.x/y/z // 当前线速度
odom->twist.twist.angular.x/y/z // 当前角速度
odom->header.frame_id // 坐标系ID
odom->header.stamp // 时间戳
cpp展开代码void
SO3ControlNodelet::enable_motors_callback(const std_msgs::Bool::ConstPtr& msg)
cpp展开代码msg->data // 布尔值:true=启用电机,false=禁用电机
作用:控制电机开关状态
cpp展开代码void
SO3ControlNodelet::corrections_callback(const quadrotor_msgs::Corrections::ConstPtr& msg)
cpp展开代码void
SO3ControlNodelet::imu_callback(const sensor_msgs::Imu& imu)
作用:提供加速度和角速度传感器数据
cpp展开代码// sensor_msgs::Imu 包含:
imu.linear_acceleration.x/y/z // 线性加速度 (m/s²)
imu.angular_velocity.x/y/z // 角速度 (rad/s)
imu.orientation // 姿态(四元数)
imu.header.frame_id // 坐标系ID
imu.header.stamp // 时间戳
text展开代码ROS标准消息类型: ├── std_msgs::Bool (基础类型) ├── sensor_msgs::Imu (传感器数据) ├── nav_msgs::Odometry (导航数据) └── quadrotor_msgs::* (四旋翼专用消息) ├── PositionCommand (位置控制命令) └── Corrections (校正参数)
cpp展开代码// 错误方式 - 会拷贝整个消息
void callback(quadrotor_msgs::PositionCommand msg) {
// 这里会拷贝整个消息对象,性能差
}
// 正确方式 - 只传递指针
void callback(const quadrotor_msgs::PositionCommand::ConstPtr& msg) {
// 只传递指针,不拷贝数据
}
如果使用值传递,可能导致:
cpp展开代码// 创建消息对象
quadrotor_msgs::SO3Command::Ptr so3_command(
new quadrotor_msgs::SO3Command);
// 设置消息头信息
so3_command->header.stamp = ros::Time::now();
so3_command->header.frame_id = frame_id_;
// 设置力向量
so3_command->force.x = force(0);
so3_command->force.y = force(1);
so3_command->force.z = force(2);
// 设置姿态四元数
so3_command->orientation.x = orientation.x();
so3_command->orientation.y = orientation.y();
so3_command->orientation.z = orientation.z();
so3_command->orientation.w = orientation.w();
// 设置控制增益
for (int i = 0; i < 3; i++) {
so3_command->kR[i] = kR_[i];
so3_command->kOm[i] = kOm_[i];
}
// 设置辅助信息
so3_command->aux.current_yaw = current_yaw_;
so3_command->aux.enable_motors = enable_motors_;
so3_command->aux.kf_correction = corrections_[0];
so3_command->aux.angle_corrections[0] = corrections_[1];
so3_command->aux.angle_corrections[1] = corrections_[2];
// 发布消息
so3_command_pub_.publish(so3_command);
cpp展开代码quadrotor_msgs::SO3Command::Ptr so3_command(new quadrotor_msgs::SO3Command);
动态创建一个名为 so3_command的智能指针,该指针指向一个类型为 quadrotor_msgs::SO3Command的全新的、空的消息对象。用于后续填充数据
quadrotor_msgs 是消息包名
SO3Command 是具体的消息类型
::Ptr:这是 SO3Command消息类型的智能指针
-> 是C++中用于访问指针指向的对象的成员的操作符。
cpp展开代码// 访问消息对象的成员
so3_command->header.stamp = ros::Time::now();
so3_command->force.x = force(0);
so3_command->orientation.x = orientation.x();
// 访问消息的辅助信息结构
so3_command->aux.current_yaw = current_yaw_;
so3_command->aux.enable_motors = enable_motors_;
so3_command->aux.kf_correction = corrections_[0];
cpp展开代码// 使用 -> 操作符
so3_command->aux.current_yaw = current_yaw_;
// 等价于使用 * 操作符
(*so3_command).aux.current_yaw = current_yaw_;
cpp展开代码odom_sub_ = n.subscribe("odom", 10, &SO3ControlNodelet::odom_callback, this,ros::TransportHints().tcpNoDelay());
this 是C++中的当前对象指针
指向当前 SO3ControlNodelet 类的实例
当ROS调用回调函数时,需要知道调用哪个对象的成员函数
this 告诉ROS:"当收到消息时,调用这个对象的odom_callback 函数"
ros::TransportHints() - 创建传输提示对象
.tcpNoDelay() - 设置TCP传输选项,禁用Nagle算法
作用:减少网络延迟,提高实时性
本文作者:cc
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!