Subscribe to car's IMU sensor and monitor the orientation value to determine if the car is going straight, left, or right, and create a program that outputs it on the screen every second. Since the orientation value is in quaternion format, we need to use the 'Euler_from_quaternion' function to replace it with the Euler format of roll, pitch, and yaw. (FYI Running this program in ROS with Python) This is what I have got so far..
#!/usr/bin/env python
import rospy
import time
from sensor_msgs.msg import Imu
from tf.transformations import euler_from_quaternion
Imu_msg = None
def imu_callback(data):
global Imu_msg
Imu_msg = [data.orientation.x, data.orientation.y, data.orientation.z, data.orientation.w]
rospy.init_node("Imu_Print")
rospy.Subscriber("imu", Imu, imu_callback)
from time import time
while not rospy.is_shutdown():
current_time = time.time()- t
current_yaw = yaw
if current_time >= 2.0:
if current_yaw - last_yaw >0:
print("Left")
else:
print("Right")
t = time.time()
last_yaw = current_yaw
if Imu_msg == None:
continue
(roll, pitch, yaw) = euler_from_quaternion(Imu_msg)
print('Roll:%.4f, Pitch:%.4f, Yaw:%.4f' % (roll, pitch, yaw))
time.sleep(1.0)
Based on your script, this following code is a suggestion: