From 9bc94bfe7cf3461a54b7f0d11463eb5f62d33806 Mon Sep 17 00:00:00 2001 From: yotia Date: Fri, 2 Aug 2024 18:32:33 +0300 Subject: [PATCH] Debugged collecting nmea data. --- api/modem/gps/nmea.go | 49 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/api/modem/gps/nmea.go b/api/modem/gps/nmea.go index 94b3ec0..554adf0 100644 --- a/api/modem/gps/nmea.go +++ b/api/modem/gps/nmea.go @@ -2,6 +2,7 @@ package gps import ( "fmt" + "log" "strings" "time" ) @@ -29,15 +30,55 @@ const ( nmeaFlagsMask = (1 << 18) - 1 - collectTimeout = 2 * time.Second + collectTimeout = 5 * time.Second ) +func secondCountDownTimer(title string, logger *log.Logger, t time.Duration) { + counter := 0 + for { + if counter > int(t.Seconds()) { + break + } + logger.Printf("%s: %d/%f\n", title, counter, t.Seconds()) + time.Sleep(time.Second) + counter += 1 + } +} + // Go... otherwise will throw warning var nmeaFlagsAll = gga | rmc | gpgsv | gpgsa | vtg | xfi | glgsa | gns | gagsv | bdpqgsa | bdpqgsv func (g *gps) rawCollect(flags nmeaFlags) (string, error) { // Need to omplement low level write/read here because collect must be atomic operation // If other command is executed(write/read) it will read a part of nmea report and cause an error + + // Setup gps + + // Receive data from all contries' satelites, enable DPO(Dynamic Power Optimization) mode + // if resp, err := g.port.Send("AT+CGNSSMODE=15,1"); err != nil { + // return "", fmt.Errorf("AT+CGNSSMODE= request: %w", err) + // } else { + // if !resp.Check() { + // return "", fmt.Errorf("AT+CGNSSMODE= error response: %s", resp) + // } + // } + // Receive all types of data + // if resp, err := g.port.Send("AT+CGPSNMEA=200191"); err != nil { + // return "", fmt.Errorf("AT+CGPSNMEA= request: %w", err) + // } else { + // if !resp.Check() { + // return "", fmt.Errorf("AT+CGPSNMEA= error response: %s", resp) + // } + // } + // Set output rate to 10Hz + if resp, err := g.port.Send("AT+CGPSNMEARATE=1"); err != nil { + return "", fmt.Errorf("AT+CGPSNMEARATE= request: %w", err) + } else { + if !resp.Check() { + return "", fmt.Errorf("AT+CGPSNMEARATE= error response: %s", resp) + } + } + g.switchGpsMode(true) g.port.Mutex().Lock() s := g.port.SerialPort() @@ -48,16 +89,16 @@ func (g *gps) rawCollect(flags nmeaFlags) (string, error) { // Send AT+CGPSINFOCFG=255, flags flags &= nmeaFlagsMask - if _, err := s.Write([]byte("AT+CGPSINFOCFG=255," + string(flags) + "\r\n")); err != nil { + if _, err := s.Write([]byte("AT+CGPSINFOCFG=1,31\r\n")); err != nil { return "", fmt.Errorf("serial port write 1: %w", err) } // Do I need to read answer // Wait - time.Sleep(collectTimeout) + secondCountDownTimer("Collecting NMEA data", g.logger, collectTimeout) // Send AT+CGPSINFOCFG=0, flags - if _, err := s.Write([]byte("AT+CGPSINFOCFG=0," + string(flags) + "\r\n")); err != nil { + if _, err := s.Write([]byte("AT+CGPSINFOCFG=0,31\r\n")); err != nil { return "", fmt.Errorf("serial port write 2: %w", err) }