Class: SGS::NMEA

Inherits:
Object
  • Object
show all
Defined in:
lib/sgs/nmea.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNMEA

Returns a new instance of NMEA.



46
47
48
49
50
# File 'lib/sgs/nmea.rb', line 46

def initialize
  @args = Array.new
  @valid = false
  @checksum = 0
end

Instance Attribute Details

#argsObject

Parse and create NMEA strings for various purposes.



44
45
46
# File 'lib/sgs/nmea.rb', line 44

def args
  @args
end

#checksumObject

Parse and create NMEA strings for various purposes.



44
45
46
# File 'lib/sgs/nmea.rb', line 44

def checksum
  @checksum
end

#validObject

Parse and create NMEA strings for various purposes.



44
45
46
# File 'lib/sgs/nmea.rb', line 44

def valid
  @valid
end

Class Method Details

.parse(str) ⇒ Object

Parse an NMEA string into its component parts.



54
55
56
57
58
59
60
# File 'lib/sgs/nmea.rb', line 54

def self.parse(str)
  nmea = new
  if nmea.parse(str) < 0
    nmea = nil
  end
  nmea
end

Instance Method Details

#compute_csum(str) ⇒ Object

Compute an NMEA checksum



136
137
138
139
140
# File 'lib/sgs/nmea.rb', line 136

def compute_csum(str)
  @checksum = 0
  str.each_byte {|ch| @checksum ^= ch}
  @checksum
end

#is_gprmc?Boolean

Is the current line a GPRMC message?

Returns:

  • (Boolean)


79
80
81
# File 'lib/sgs/nmea.rb', line 79

def is_gprmc?
  @args[0] == "GPRMC"
end

#make_gprmc(gps) ⇒ Object

Output a GPRMC message



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/sgs/nmea.rb', line 112

def make_gprmc(gps)
  @valid = true
  @args = Array.new
  @args[0] = "GPRMC"
  @args[1] = gps.time.strftime("%H%M%S.") + "%03d" % (gps.time.usec / 1000)
  @args[2] = 'A'
  @args.concat gps.location.latitude_array
  @args.concat gps.location.longitude_array("%03d%07.4f")
  @args[7] = "%.2f" % gps.sog
  @args[8] = "%.2f" % Bearing.radians_to_d(gps.cmg)
  @args[9] = gps.time.strftime("%d%m%y")
  @args.concat ['', '']
  @args << 'A'
end

#parse(str) ⇒ Object

Parse an NMEA string into its component parts.



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sgs/nmea.rb', line 64

def parse(str)
  str.chomp!
  if str[0] != "$"
    return -1
  end
  str, sum = str[1..-1].split('*')
  if sum.nil? or sum.to_i(16) != compute_csum(str)
    return -1
  end
  @args = str.split(',')
  @args.count
end

#parse_gprmcObject

Parse a GPRMC message

“GPRMC”, “211321.000”, “A”, “5309.7743”, “N”, “00904.5576”, “W”, “0.17”, “78.41”, “200813”, “”, “”, “A”


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/sgs/nmea.rb', line 86

def parse_gprmc
  if @args.count < 12 or @args.count > 13
    return nil
  end
  gps = GPS.new
  gps.is_valid if @args[2] == "A"
  hh = @args[1][0..1].to_i
  mm = @args[1][2..3].to_i
  ss = @args[1][4..-1].to_f
  us = (ss % 1.0 * 1000000)
  ss = ss.to_i
  dd = @args[9][0..1].to_i
  mn = @args[9][2..3].to_i
  yy = @args[9][4..5].to_i + 2000
  gps.time = Time.gm(yy, mn, dd, hh, mm, ss, us)
  pos = {"latitude" => ll_nmea(@args[3,4]),
         "longitude" => ll_nmea(@args[5,6])}
  gps.location = Location.new
  gps.location.parse_hash(pos)
  gps.sog = @args[7].to_f
  gps.cmg = Bearing.dtor @args[8].to_f
  gps
end

#to_sObject

Convert an array of component parts into an NMEA string.



129
130
131
132
# File 'lib/sgs/nmea.rb', line 129

def to_s
  str = @args.join(',')
  "$%s*%02X" % [str, compute_csum(str)]
end