MCPcopy Create free account
hub / github.com/DexterInd/GoPiGo3 / validate

Method validate

Software/Python/Examples/gps_reading.py:99–156  ·  view source on GitHub ↗

Runs regex validation on a GPGAA sentence. Returns False if the sentence is mangled Return True if everything is all right and sets internal class members.

(self,in_line)

Source from the content-addressed store, hash-verified

97 return []
98
99 def validate(self,in_line):
100 '''
101 Runs regex validation on a GPGAA sentence.
102 Returns False if the sentence is mangled
103 Return True if everything is all right and sets internal
104 class members.
105 '''
106 if in_line == "":
107 return False
108 if in_line[:6] != "$GPGGA":
109 return False
110
111 self.gga = in_line.split(",")
112 debug (self.gga)
113
114 #Sometimes multiple GPS data packets come into the stream. Take the data only after the last '$GPGGA' is seen
115 try:
116 ind=self.gga.index('$GPGGA',5,len(self.gga))
117 self.gga=self.gga[ind:]
118 except ValueError:
119 pass
120
121 if len(self.gga) != 15:
122 debug ("Failed: wrong number of parameters ")
123 debug (self.gga)
124 return False
125
126 for i in range(len(self.validation)-1):
127 if len(self.gga[i]) == 0:
128 debug ("Failed: empty string %d"%i)
129 return False
130 test = self.validation[i].match(self.gga[i])
131 if test == False:
132 debug ("Failed: wrong format on parameter %d"%i)
133 return False
134 else:
135 debug("Passed %d"%i)
136
137 try:
138 self.timestamp = self.gga[1]
139 self.lat = float(self.gga[2])
140 self.NS = self.gga[3]
141 self.lon = float(self.gga[4])
142 self.EW = self.gga[5]
143 self.quality = int(self.gga[6])
144 self.satellites = int(self.gga[7])
145 self.altitude = float(self.gga[9])
146
147 self.latitude = self.lat // 100 + self.lat % 100 / 60
148 if self.NS == "S":
149 self.latitude = - self.latitude
150 self.longitude = self.lon // 100 + self.lon % 100 / 60
151 if self.EW == "W":
152 self.longitude = -self.longitude
153 except ValueError:
154 debug( "FAILED: invalid value")
155
156 return True

Callers 1

readMethod · 0.95

Calls 1

debugFunction · 0.70

Tested by

no test coverage detected