test.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import rospy
3 from std_msgs.msg import String
4 from sensor_msgs.msg import MagneticField
5 from math import atan2, sin, cos, sqrt
6 
7 def callback(data):
8  #rospy.loginfo(rospy.get_caller_id() + "mx: %s", data.magnetic_field.x)
9  xmag = data.magnetic_field.x
10  ymag = data.magnetic_field.y
11  zmag = data.magnetic_field.z
12  mag_norm=sqrt((xmag*xmag)+(ymag*ymag)+(zmag*zmag))
13  # for normalization
14  magx=xmag/mag_norm
15  magy=ymag/mag_norm
16  magz=zmag/mag_norm
17 
18  Roll = 0;
19  Pitch = 0;
20  #yaw =atan2( (-ymag*cos(Roll) + zmag*sin(Roll) ) , (xmag*cos(Pitch) + ymag*sin(Pitch)*sin(Roll)+ zmag*sin(Pitch)*cos(Roll)) )
21  #yaw =atan2( (-magy*cos(Roll) + magz*sin(Roll) ) , (magx*cos(Pitch) + magy*sin(Pitch)*sin(Roll)+ magz*sin(Pitch)*cos(Roll)) )
22 
23  cos_pitch = cos(Pitch)
24  sin_pitch = sin(Pitch)
25  cos_roll = cos(Roll)
26  sin_roll = sin(Roll)
27  t_mag_x = magx * cos_pitch + magz * sin_pitch
28  t_mag_y = magx * sin_roll * sin_pitch + magy * cos_roll - magz * sin_roll * cos_pitch
29  head_x = t_mag_x
30  head_y = t_mag_y
31  yaw = atan2(head_x, head_y)
32 
33 
34  rospy.loginfo(rospy.get_caller_id() + "yaw: %s", yaw)
35 
36 def listener():
37 
38  rospy.init_node('test', anonymous=True)
39 
40  rospy.Subscriber("/mavros/imu/mag", MagneticField, callback)
41 
42  # spin() simply keeps python from exiting until this node is stopped
43  rospy.spin()
44 
45 if __name__ == '__main__':
46  listener()
def listener()
Definition: test.py:36
def callback(data)
Definition: test.py:7


summit_xl_localization
Author(s):
autogenerated on Tue Apr 27 2021 03:07:09