Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Tools/autotest/fakepos.py
Views: 1798
#!/usr/bin/env python1from __future__ import print_function2import errno3import socket4import struct5import sys6import time7from math import cos, fabs, radians, sin, sqrt8910class udp_out(object):11"""A UDP output socket."""12def __init__(self, device):13a = device.split(':')14if len(a) != 2:15print("UDP ports must be specified as host:port")16sys.exit(1)17self.port = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)18self.destination_addr = (a[0], int(a[1]))19self.port.setblocking(0)20self.last_address = None2122def recv(self, n=None):23try:24data, self.last_address = self.port.recvfrom(300)25except socket.error as e:26if e.errno in [errno.EAGAIN, errno.EWOULDBLOCK]:27return ""28raise29return data3031def write(self, buf):32try:33self.port.sendto(buf, self.destination_addr)34except socket.error:35pass363738udp = udp_out("127.0.0.1:5501")3940latitude = -3541longitude = 14942altitude = 600.043heading = 0.044speedN = 045speedE = 0.046speedD = 0.047rollRate = 0.048yawRate = 0.049yawDeg = 0.050airspeed = 051magic = 0x4c56414f5253deltaT = 0.00554rollDeg = 4555pitchDeg = 05657pitchMax = None58rollMax = None5960if True:61pitchRate = 162pitchMax = 4563rollMax = 456465while True:6667xAccel = sin(radians(pitchDeg))68yAccel = -sin(radians(rollDeg)) * cos(radians(pitchDeg))69zAccel = -cos(radians(rollDeg)) * cos(radians(pitchDeg))70scale = 9.81 / sqrt((xAccel*xAccel) + (yAccel*yAccel) + (zAccel*zAccel))71xAccel *= scale72yAccel *= scale73zAccel *= scale7475struc_buf = struct.pack('<17dI',76latitude, longitude, altitude, heading,77speedN, speedE, speedD,78xAccel, yAccel, zAccel,79rollRate, pitchRate, yawRate,80rollDeg, pitchDeg, yawDeg,81airspeed, magic)82udp.write(struc_buf)83time.sleep(deltaT)8485yawDeg += yawRate * deltaT86if yawDeg > 180:87yawDeg -= 36088if yawDeg < -180:89yawDeg += 36090heading = yawDeg9192if pitchMax is not None and fabs(pitchDeg) > pitchMax:93pitchRate = -pitchRate9495if rollMax is not None and fabs(rollDeg) > rollMax:96rollRate = -rollRate9798pitchDeg += pitchRate * deltaT99if pitchDeg > 180:100pitchDeg -= 360101if pitchDeg < -180:102pitchDeg += 360103104rollDeg += rollRate * deltaT105if rollDeg > 180:106rollDeg -= 360107if rollDeg < -180:108rollDeg += 360109110111