CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/autotest/fakepos.py
Views: 1798
1
#!/usr/bin/env python
2
from __future__ import print_function
3
import errno
4
import socket
5
import struct
6
import sys
7
import time
8
from math import cos, fabs, radians, sin, sqrt
9
10
11
class udp_out(object):
12
"""A UDP output socket."""
13
def __init__(self, device):
14
a = device.split(':')
15
if len(a) != 2:
16
print("UDP ports must be specified as host:port")
17
sys.exit(1)
18
self.port = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
19
self.destination_addr = (a[0], int(a[1]))
20
self.port.setblocking(0)
21
self.last_address = None
22
23
def recv(self, n=None):
24
try:
25
data, self.last_address = self.port.recvfrom(300)
26
except socket.error as e:
27
if e.errno in [errno.EAGAIN, errno.EWOULDBLOCK]:
28
return ""
29
raise
30
return data
31
32
def write(self, buf):
33
try:
34
self.port.sendto(buf, self.destination_addr)
35
except socket.error:
36
pass
37
38
39
udp = udp_out("127.0.0.1:5501")
40
41
latitude = -35
42
longitude = 149
43
altitude = 600.0
44
heading = 0.0
45
speedN = 0
46
speedE = 0.0
47
speedD = 0.0
48
rollRate = 0.0
49
yawRate = 0.0
50
yawDeg = 0.0
51
airspeed = 0
52
magic = 0x4c56414f
53
54
deltaT = 0.005
55
rollDeg = 45
56
pitchDeg = 0
57
58
pitchMax = None
59
rollMax = None
60
61
if True:
62
pitchRate = 1
63
pitchMax = 45
64
rollMax = 45
65
66
while True:
67
68
xAccel = sin(radians(pitchDeg))
69
yAccel = -sin(radians(rollDeg)) * cos(radians(pitchDeg))
70
zAccel = -cos(radians(rollDeg)) * cos(radians(pitchDeg))
71
scale = 9.81 / sqrt((xAccel*xAccel) + (yAccel*yAccel) + (zAccel*zAccel))
72
xAccel *= scale
73
yAccel *= scale
74
zAccel *= scale
75
76
struc_buf = struct.pack('<17dI',
77
latitude, longitude, altitude, heading,
78
speedN, speedE, speedD,
79
xAccel, yAccel, zAccel,
80
rollRate, pitchRate, yawRate,
81
rollDeg, pitchDeg, yawDeg,
82
airspeed, magic)
83
udp.write(struc_buf)
84
time.sleep(deltaT)
85
86
yawDeg += yawRate * deltaT
87
if yawDeg > 180:
88
yawDeg -= 360
89
if yawDeg < -180:
90
yawDeg += 360
91
heading = yawDeg
92
93
if pitchMax is not None and fabs(pitchDeg) > pitchMax:
94
pitchRate = -pitchRate
95
96
if rollMax is not None and fabs(rollDeg) > rollMax:
97
rollRate = -rollRate
98
99
pitchDeg += pitchRate * deltaT
100
if pitchDeg > 180:
101
pitchDeg -= 360
102
if pitchDeg < -180:
103
pitchDeg += 360
104
105
rollDeg += rollRate * deltaT
106
if rollDeg > 180:
107
rollDeg -= 360
108
if rollDeg < -180:
109
rollDeg += 360
110
111