Class: SGS::GPS

Inherits:
RedisBase show all
Defined in:
lib/sgs/gps.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RedisBase

#count, #count_name, #load, load, #make_redis_name, #publish, redis, redis_handle, #redis_read_var, #save, #save_and_publish, setup, subscribe, to_redis, var_init

Constructor Details

#initialize(lat = nil, long = nil) ⇒ GPS

Create a new GPS record. Lat/Long are in radians.



43
44
45
46
47
48
49
50
51
# File 'lib/sgs/gps.rb', line 43

def initialize(lat = nil, long = nil)
  @time = Time.new(2000, 1, 1)
  @location = Location.new(lat, long)
  @sog = 0.0
  @cmg = 0.0
  @magvar = nil
  @valid = false
  super()
end

Instance Attribute Details

#cmgObject

Returns the value of attribute cmg.



39
40
41
# File 'lib/sgs/gps.rb', line 39

def cmg
  @cmg
end

#locationObject

Returns the value of attribute location.



39
40
41
# File 'lib/sgs/gps.rb', line 39

def location
  @location
end

#magvarObject

Returns the value of attribute magvar.



39
40
41
# File 'lib/sgs/gps.rb', line 39

def magvar
  @magvar
end

#sogObject

Returns the value of attribute sog.



39
40
41
# File 'lib/sgs/gps.rb', line 39

def sog
  @sog
end

#timeObject

Returns the value of attribute time.



39
40
41
# File 'lib/sgs/gps.rb', line 39

def time
  @time
end

#valObject

Returns the value of attribute val.



39
40
41
# File 'lib/sgs/gps.rb', line 39

def val
  @val
end

Class Method Details

.daemonObject

Main daemon function (called from executable)



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sgs/gps.rb', line 55

def self.daemon
  puts "GPS reader starting up. Version #{SGS::VERSION}"
  config = Config.load

  sp = SerialPort.new config.gps_device, config.gps_speed
  sp.read_timeout = 10000

  loop do
    line = sp.readline
    nmea = NMEA.parse line
    if nmea.is_gprmc?
      gps = nmea.parse_gprmc
      p gps
      if gps.cmg < 0.0
        puts "CMG ERROR?!? #{gps.cmg}"
        p line
      end
      gps.save_and_publish if gps and gps.valid?
    end
  end
end

Instance Method Details

#force(lat, long, time = nil) ⇒ Object

Hard-code a GPS value (usually for debugging purposes)



79
80
81
82
83
# File 'lib/sgs/gps.rb', line 79

def force(lat, long, time = nil)
  @time = time || Time.now
  @location = Location.new(lat, long)
  @valid = true
end

#is_validObject

Set the validity



87
88
89
# File 'lib/sgs/gps.rb', line 87

def is_valid
  @valid = true
end

#to_sObject

Display the GPS data as a useful string (in degrees)



99
100
101
102
103
104
105
# File 'lib/sgs/gps.rb', line 99

def to_s
  if valid?
    "@#{@time.strftime('%Y%m%d-%T')}, #{@location}, SOG:#{@sog}, CMG:#{@cmg}"
  else
    "GPS error"
  end
end

#valid?Boolean

Is the GPS data valid?

Returns:

  • (Boolean)


93
94
95
# File 'lib/sgs/gps.rb', line 93

def valid?
  @valid == true
end