-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_origin.py
113 lines (93 loc) · 2.55 KB
/
set_origin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python
##
#
# Send SET_GPS_GLOBAL_ORIGIN and SET_HOME_POSITION messages
#
##
import rospy
from pymavlink.dialects.v10 import ardupilotmega as MAV_APM
from mavros.mavlink import convert_to_rosmsg
from mavros_msgs.msg import Mavlink
# Global position of the origin
lat = 37.86351 * 1e7 # Terni
lon = 32.41625 * 1e7 # Terni
alt = 113 * 1e3
class fifo(object):
""" A simple buffer """
def __init__(self):
self.buf = []
def write(self, data):
self.buf += data
return len(data)
def read(self):
return self.buf.pop(0)
def send_message(msg, mav, pub):
"""
Send a mavlink message
"""
msg.pack(mav)
rosmsg = convert_to_rosmsg(msg)
pub.publish(rosmsg)
print("sent message %s" % msg)
def set_global_origin(mav, pub):
"""
Send a mavlink SET_GPS_GLOBAL_ORIGIN message, which allows us
to use local position information without a GPS.
"""
target_system = mav.srcSystem
#target_system = 0 # 0 --> broadcast to everyone
lattitude = lat
longitude = lon
altitude = alt
msg = MAV_APM.MAVLink_set_gps_global_origin_message(
target_system,
lattitude,
longitude,
altitude)
send_message(msg, mav, pub)
def set_home_position(mav, pub):
"""
Send a mavlink SET_HOME_POSITION message, which should allow
us to use local position information without a GPS
"""
target_system = mav.srcSystem
#target_system = 0 # broadcast to everyone
lattitude = lat
longitude = lon
altitude = alt
x = 0
y = 0
z = 0
q = [1, 0, 0, 0] # w x y z
approach_x = 0
approach_y = 0
approach_z = 1
msg = MAV_APM.MAVLink_set_home_position_message(
target_system,
lattitude,
longitude,
altitude,
x,
y,
z,
q,
approach_x,
approach_y,
approach_z)
send_message(msg, mav, pub)
if __name__=="__main__":
try:
rospy.init_node("origin_publisher")
mavlink_pub = rospy.Publisher("/mavlink/to", Mavlink, queue_size=20)
# Set up mavlink instance
f = fifo()
mav = MAV_APM.MAVLink(f, srcSystem=1, srcComponent=1)
# wait to initialize
while mavlink_pub.get_num_connections() <= 0:
pass
for _ in range(2):
rospy.sleep(1)
set_global_origin(mav, mavlink_pub)
set_home_position(mav, mavlink_pub)
except rospy.ROSInterruptException:
pass