Skip to content

Commit

Permalink
Generate PGRMZ sentences if Stratux is equipped with a baro, prepare …
Browse files Browse the repository at this point in the history
…POGNS configuration of ogn-rx-eu for transmit module
  • Loading branch information
b3nn0 committed Jan 9, 2021
1 parent 6263529 commit 5849ed8
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 33 deletions.
1 change: 1 addition & 0 deletions main/gen_gdl90.go
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,7 @@ type status struct {
NightMode bool // For turning off LEDs.
OGN_noise_db float32
OGN_gain_db float32
OGN_tx_enabled bool // If ogn-rx-eu uses a local tx module for transmission
}

var globalSettings settings
Expand Down
10 changes: 6 additions & 4 deletions main/gps.go
Original file line number Diff line number Diff line change
Expand Up @@ -1697,23 +1697,25 @@ func processNMEALine(l string) (sentenceUsed bool) {
return false
}

func getOgnTrackerConfigString() string {
return fmt.Sprintf("$POGNS,Address=0x%s,AddrType=%d,AcftType=%d,Pilot=%s", globalSettings.OGNAddr, globalSettings.OGNAddrType, globalSettings.OGNAcftType, globalSettings.OGNPilot)
}

func configureOgnTrackerFromSettings() {
if serialPort == nil {
return
}

cfg := fmt.Sprintf("$POGNS,Address=0x%s,AddrType=%d,AcftType=%d,Pilot=%s\r\n", globalSettings.OGNAddr, globalSettings.OGNAddrType, globalSettings.OGNAcftType, globalSettings.OGNPilot)
cfg := getOgnTrackerConfigString()
log.Printf("Configuring OGN Tracker: " + cfg)

serialPort.Write([]byte(cfg))
serialPort.Write([]byte(cfg + "\r\n"))
serialPort.Write([]byte("$POGNS\r\n")) // re-read settings from tracker
serialPort.Flush()
}





var gnssBaroAltDiffs = make(map [int]int)
// Little helper function to dump the gnssBaroAltDiffs map to CSV for plotting
//func dumpValues() {
Expand Down
91 changes: 63 additions & 28 deletions main/ogn.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type OgnMessage struct {
// Status message (Sys=status):
Bkg_noise_db float32
Gain_db float32
Tx_enabled bool
}


Expand All @@ -55,16 +56,20 @@ var ognReadWriter *bufio.ReadWriter


func ognPublishNmea(nmea string) {
if ognReadWriter != nil {
if globalStatus.OGN_connected {
// TODO: we could filter a bit more to only send RMC/GGA, but for now it's just everything
if len(nmea) > 5 && nmea[3:6] == "GGA" || nmea[3:6] == "RMC" {
if len(nmea) > 5 && nmea[3:6] == "GGA" || nmea[3:6] == "RMC" || nmea[1:6] == "POGNS" {
//log.Printf(nmea)
ognReadWriter.Write([]byte(nmea + "\r\n"))
ognReadWriter.Flush()
ognOutgoingMsgChan <- nmea + "\r\n"
}
}
}

var ognOutgoingMsgChan chan string = make(chan string, 100)
var ognIncomingMsgChan chan string = make(chan string, 100)
var ognExitChan chan bool = make(chan bool, 1)


func ognListen() {
//go predTest()
for {
Expand All @@ -81,33 +86,58 @@ func ognListen() {
continue
}
log.Printf("ogn-rx-eu successfully connected")
globalStatus.OGN_connected = true
ognReadWriter = bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
for globalSettings.OGN_Enabled {
buf, err := ognReadWriter.ReadBytes('\n')
if err != nil {
log.Printf("ogn-rx-eu connection lost.")
break
}
globalStatus.OGN_connected = true

var thisMsg msg
thisMsg.MessageClass = MSGCLASS_OGN
thisMsg.TimeReceived = stratuxClock.Time
thisMsg.Data = string(buf)

var msg OgnMessage
err = json.Unmarshal(buf, &msg)
if err != nil {
log.Printf("Invalid Data from OGN: " + string(buf))
continue
go func() {
for {
buf, err := ognReadWriter.ReadBytes('\n')
if err != nil {
log.Printf("ogn-rx-eu connection lost.")
log.Printf(err.Error())
ognExitChan <- true
return
}
ognIncomingMsgChan <- string(buf)
}
}()

pgrmzTimer := time.NewTicker(1 * time.Second)

loop: for globalSettings.OGN_Enabled {
select {
case data := <- ognOutgoingMsgChan:
//fmt.Printf(data)
ognReadWriter.Write([]byte(data))
ognReadWriter.Flush()
case data := <- ognIncomingMsgChan:
var thisMsg msg
thisMsg.MessageClass = MSGCLASS_OGN
thisMsg.TimeReceived = stratuxClock.Time
thisMsg.Data = data

var msg OgnMessage
err = json.Unmarshal([]byte(data), &msg)
if err != nil {
log.Printf("Invalid Data from OGN: " + data)
continue
}

if msg.Sys == "status" {
importOgnStatusMessage(msg)
} else {
msgLogAppend(thisMsg)
logMsg(thisMsg) // writes to replay logs
importOgnTrafficMessage(msg, data)
}
case <- pgrmzTimer.C:
if isTempPressValid() && mySituation.BaroSourceType != BARO_TYPE_NONE && mySituation.BaroSourceType != BARO_TYPE_ADSBESTIMATE {
ognOutgoingMsgChan <- makePGRMZString()
}
case <- ognExitChan:
break loop

if msg.Sys == "status" {
importOgnStatusMessage(msg)
} else {
msgLogAppend(thisMsg)
logMsg(thisMsg) // writes to replay logs
importOgnTrafficMessage(msg, buf)
}
}
globalStatus.OGN_connected = false
Expand All @@ -120,9 +150,14 @@ func ognListen() {
func importOgnStatusMessage(msg OgnMessage) {
globalStatus.OGN_noise_db = msg.Bkg_noise_db
globalStatus.OGN_gain_db = msg.Gain_db
globalStatus.OGN_tx_enabled = msg.Tx_enabled

if msg.Tx_enabled {
ognPublishNmea(getOgnTrackerConfigString())
}
}

func importOgnTrafficMessage(msg OgnMessage, buf []byte) {
func importOgnTrafficMessage(msg OgnMessage, data string) {
var ti TrafficInfo
addressBytes, _ := hex.DecodeString(msg.Addr)
addressBytes = append([]byte{0}, addressBytes...)
Expand Down Expand Up @@ -169,7 +204,7 @@ func importOgnTrafficMessage(msg OgnMessage, buf []byte) {
}
ti.Age = time.Now().UTC().Sub(ti.Timestamp).Seconds()
if ti.Age > 30 || ti.Age < -2 {
log.Printf("Discarding likely invalid OGN target: %s", string(buf))
log.Printf("Discarding likely invalid OGN target: %s", data)
return
}

Expand Down
2 changes: 1 addition & 1 deletion web/plates/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function SettingsCtrl($rootScope, $scope, $state, $location, $window, $http) {
$http.get(URL_STATUS_GET).then(function(response) {
var status = angular.fromJson(response.data);
var gpsHardwareCode = (status.GPS_detected_type & 0x0f);
if (gpsHardwareCode == 3)
if (gpsHardwareCode == 3 || status.OGN_tx_enabled)
$scope.hasOgnTracker = true;
else
$scope.hasOgnTracker = false;
Expand Down

0 comments on commit 5849ed8

Please sign in to comment.