ROS2 Python节点回调中标准IMU消息字段访问正确性咨询
关于ROS2 sensor_msgs/Imu消息字段访问的问题
你已经正确导入了sensor_msgs.msg.Imu,且回调函数的参数msg是Imu类型的实例,这里正确的写法是 q = msg.orientation,msg.imu.orientation是错误的。
原因很明确:标准sensor_msgs/Imu消息的结构中,orientation是消息本身的直接字段,不存在嵌套的imu子对象。msg.imu.orientation这种写法通常出现在自定义复合消息场景中——比如你自己定义了一个包含Imu消息的上层消息,才需要通过子字段访问,但你明确使用的是标准Imu消息,因此直接访问msg.orientation即可。
修正后的回调函数代码:
def imu_cb(self, msg: Imu): """ Update the joint states message to be published using info from imu msg. @param msg imu message @return processing return code (True: could be processed; False: could not) """ q = msg.orientation # 正确的字段访问方式 try: r = R.from_quat([q.x, q.y, q.z, q.w]).as_rotvec() except ValueError as e: self.logger.warn(f'{e}') return False self._joint_states.position[0] = -r[2] if self._inv_imu else r[2] return True
内容的提问来源于stack exchange,提问作者Macedon971
相关产品推荐
相关产品推荐

