Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/aircraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self):
self.turn_rate = 0
self.wind_speed = None
self.wind_dir = None
self.norm_wind_dir = 0 #corrected for aircraft heading for wind direction pointer TODO: Add wind direction pointer to HUD screen
self.norm_wind_dir = None #corrected for aircraft heading for wind direction pointer TODO: Add wind direction pointer to HUD screen
self.mag_decl = None #magnetic declination

self.gps = GPSData()
Expand Down
101 changes: 101 additions & 0 deletions lib/inputs/_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/usr/bin/env python

##Library of useful functions that can be used inside of input modules
##02/06/2019 Brian Chesteen

import math
from lib.geomag import declination

#############################################
## Function: ias2tas By: Brian Chesteen
## Converts indicated airspeed to true airspeed based on
## outside air temp and pressure altitude.

def ias2tas(ias, oat, palt):
tas = ias * (math.sqrt((273.0 + oat) / 288.0)) * ((1.0 - palt / 144000.0) ** -2.75)

return tas

#############################################
## Function: geomag By: Brian Chesteen
## Called Function By: Christopher Weiss cmweiss@gmail.com
## Looks up magnetic declination dynamically from GPS Lat and Lon
## Uses coefficients from the World Magnetic Model of the NOAA
## Satellite and Information Service, National Geophysical Data Center
## http://www.ngdc.noaa.gov/geomag/WMM/DoDWMM.shtml

def geomag(LatHemi, LatDeg, LatMin, LonHemi, LonDeg, LonMin):
if LatHemi == "N":
GeoMagLat = LatDeg + (LatMin / 60)
else:
GeoMagLat = ((LatDeg + (LatMin / 60)) * -1)
if LonHemi == "W":
GeoMagLon = ((LonDeg + (LonMin/60)) * -1)
else:
GeoMagLon = LonDeg + (LonMin/60)
mag_decl = declination(GeoMagLat, GeoMagLon)

return mag_decl

#############################################
## Function: gndspeed By: Brian Chesteen
## Calculates GPS ground speed in knots from GPS EW NS velocities in meters/sec.

def gndspeed(EWVelmag, NSVelmag):
gndspeed = (math.sqrt(((int(EWVelmag) * 0.1)**2) + ((int(NSVelmag) * 0.1)**2))) * 1.94384

return gndspeed

#############################################
## Function: gndtrack By: Brian Chesteen
## Calculates GPS ground track in knots from GPS EW NS quadrant direction and
## velocities in meters/sec.

def gndtrack(EWVelDir, EWVelmag, NSVelDir, NSVelmag):
if EWVelDir == "W":
EWVelmag = int(EWVelmag) * -0.1
else:
EWVelmag = int(EWVelmag) * 0.1
if NSVelDir == "S":
NSVelmag = int(NSVelmag) * -0.1
else:
NSVelmag = int(NSVelmag) * 0.1

gndtrack = (math.degrees(math.atan2(EWVelmag, NSVelmag))) % 360

return gndtrack

#############################################
## Function: windSpdDir By: Brian Chesteen
## Calculates wind speed and direction

def windSpdDir(tas, gndspeed, gndtrack, mag_head, mag_decl):
if tas > 30 and gndspeed > 30:
crs = math.radians(gndtrack) #convert degrees to radians
head = math.radians(mag_head + mag_decl) #convert degrees to radians
wind_speed = math.sqrt(math.pow(tas - gndspeed, 2) + 4 * tas * gndspeed * math.pow(math.sin((head - crs) / 2), 2))
wind_dir = crs + math.atan2(tas * math.sin(head-crs), tas * math.cos(head-crs) - gndspeed)
if wind_dir < 0:
wind_dir = wind_dir + 2 * math.pi
if wind_dir > 2 * math.pi:
wind_dir = wind_dir - 2 * math.pi
wind_dir = math.degrees(wind_dir) #convert radians to degrees
norm_wind_dir = (wind_dir + mag_head + mag_decl) % 360 #normalize the wind direction to the airplane heading

else:
wind_speed = None
wind_dir = None
norm_wind_dir = None

return (wind_speed, wind_dir, norm_wind_dir)











45 changes: 10 additions & 35 deletions lib/inputs/serial_g3x.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from lib import hud_utils
from lib import hud_text
from lib import geomag
import _utils
import serial
import struct
import math
Expand Down Expand Up @@ -84,40 +85,14 @@ def readMessage(self, aircraft):
aircraft.gps.NSVelmag = int(NSVelmag) * 0.1
aircraft.gps.VVelDir = VVelDir # U or D
aircraft.gps.VVelmag = int(VVelmag) * 0.1
if aircraft.gps.LatHemi == "N":
GeoMagLat = aircraft.gps.LatDeg + (aircraft.gps.LatMin / 60)
else:
GeoMagLat = ((aircraft.gps.LatDeg + (aircraft.gps.LatMin / 60)) * -1)
if aircraft.gps.LonHemi == "W":
GeoMagLon = ((aircraft.gps.LonDeg + (aircraft.gps.LonMin/60)) * -1)
else:
GeoMagLon = aircraft.gps.LonDeg + (aircraft.gps.LonMin/60)
aircraft.mag_decl = geomag.declination(GeoMagLat, GeoMagLon)
aircraft.gndspeed = (math.sqrt(((int(EWVelmag) * 0.1)**2) + ((int(NSVelmag) * 0.1)**2))) * 1.94384
if EWVelDir == "W":
EWVelmag = int(EWVelmag) * -0.1
else:
EWVelmag = int(EWVelmag) * 0.1
if NSVelDir == "S":
NSVelmag = int(NSVelmag) * -0.1
else:
NSVelmag = int(NSVelmag) * 0.1
aircraft.gndtrack = (math.degrees(math.atan2(EWVelmag, NSVelmag))) % 360
if aircraft.tas > 30 and aircraft.gndspeed > 30:
crs = math.radians(aircraft.gndtrack) #convert degrees to radians
head = math.radians(aircraft.mag_head + aircraft.mag_decl) #convert degrees to radians
aircraft.wind_speed = math.sqrt(math.pow(aircraft.tas - aircraft.gndspeed, 2) + 4 * aircraft.tas * aircraft.gndspeed * math.pow(math.sin((head - crs) / 2), 2))
aircraft.wind_dir = crs + math.atan2(aircraft.tas * math.sin(head-crs), aircraft.tas * math.cos(head-crs) - aircraft.gndspeed)
if aircraft.wind_dir < 0:
aircraft.wind_dir = aircraft.wind_dir + 2 * math.pi
if aircraft.wind_dir > 2 * math.pi:
aircraft.wind_dir = aircraft.wind_dir - 2 * math.pi
aircraft.wind_dir = math.degrees(aircraft.wind_dir) #convert radians to degrees
aircraft.norm_wind_dir = (aircraft.wind_dir + aircraft.mag_head + aircraft.mag_decl) % 360 #normalize the wind direction to the airplane heading

else:
aircraft.wind_speed = None
aircraft.wind_dir = None
aircraft.mag_decl = _utils.geomag(aircraft.gps.LatHemi, aircraft.gps.LatDeg,
aircraft.gps.LatMin, aircraft.gps.LonHemi,
aircraft.gps.LonDeg, aircraft.gps.LonMin)
aircraft.gndspeed = _utils.gndspeed(EWVelmag, NSVelmag)
aircraft.gndtrack = _utils.gndtrack(EWVelDir, EWVelmag, NSVelDir, NSVelmag)
aircraft.wind_speed, aircraft.wind_dir, aircraft.norm_wind_dir = _utils.windSpdDir(aircraft.tas, aircraft.gndspeed,
aircraft.gndtrack, aircraft.mag_head,
aircraft.mag_decl)

else:
aircraft.msg_bad += 1
Expand Down Expand Up @@ -151,7 +126,7 @@ def readMessage(self, aircraft):
) # 0.00108 of inches of mercury change per foot.
aircraft.BALT = aircraft.alt
aircraft.vsi = int(VertSpeed) * 10
aircraft.tas = aircraft.ias * (math.sqrt((273.0 + aircraft.oat) / 288.0)) * ((1.0 - aircraft.PALT / 144000.0) ** -2.75)
aircraft.tas = _utils.ias2tas(aircraft.ias, aircraft.oat, aircraft.PALT)
aircraft.vert_G = int(VertAcc) * 0.1
aircraft.turn_rate = int(RateofTurn) * 0.1
aircraft.msg_count += 1
Expand Down