Here is a sample code that modifies sensor data with ROS:
import rospy from sensor_msgs.msg import Imu def imu_callback(msg, pub): msg.linear_acceleration.z += 9.3 pub.publish(msg) def main(): rospy.init_node('imu_without_g') pub = rospy.Publisher('imu_without_g', Imu, queue_size=10) rospy.Subscriber('imu/data', Imu, imu_callback,callback_args=pub) rospy.spin() if __name__ == '__main__': main()
def imu_callback(msg, pub): msg.linear_acceleration.z += 9.3 pub.publish(msg)
The callback is whenever Imu msg arrive, with an additional argument of pub (which is specified in the rospy.Subscriber call). I can modify the msg using the dot notation (with names you can find out with command rosmsg show sensor_msgs/Imu).
def main(): rospy.init_node('imu_without_g') pub = rospy.Publisher('imu_without_g', Imu, queue_size=10) rospy.Subscriber('imu/data', Imu, imu_callback,callback_args=pub) rospy.spin()
I initialize a publisher to publish modified IMU data. Since this modified data will be published onto a different topic, you’ll have to redirect the topic of the imu for each nodes that use IMU data in the future.