mm_gps 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,29 +1,50 @@
1
- module MmGPS
1
+ module MmGPS
2
+
3
+ # Main interface. Represents a connection to a MarvelMind beacon/hedgehog.
4
+ # You may want (and can) to have more than one instance.
2
5
  class Beacon
3
6
  START_TOKEN = "\xFFG".force_encoding(Encoding::BINARY)
4
7
  EMPTY = ''.force_encoding(Encoding::BINARY)
5
8
 
9
+ # Open a new connection on the given serial port. It also encapsulates
10
+ # the underlying SerialPort object instance, setting a read timeout of 1 s
11
+ # and enabling the binary mode.
12
+ #
13
+ # @param port [String] 'COM1' on Windows, '/dev/ttyNNN' on *nix
14
+ # @param baus [Fixnum] baudrate, value must be supported by the platform
6
15
  def initialize(port, baud = 115200)
7
16
  @sp = SerialPort.new(port, "baud" => baud)
8
17
  @sp.read_timeout = 1000 #1 sec
9
18
  @sp.binmode
10
19
  end
11
20
 
12
- def trap
13
- Signal.trap("INT") do
21
+ # Istalls a signal handler for the given signal, default to SIGINT,
22
+ # which closes the serialport connection. Further readings are likely to
23
+ # trigger an IOError.
24
+ #
25
+ # @param signal [String] the signal to be trapped, default to 'SIGINT'
26
+ def trap(signal="INT")
27
+ Signal.trap(signal) do
14
28
  @sp.close
15
- puts "Beacon port: #{@sp.inspect}"
29
+ puts "\nBeacon port: #{@sp.inspect}"
16
30
  end
17
31
  end
18
-
32
+
33
+ # Close the serialport
19
34
  def close
20
35
  @sp.close
21
36
  end
22
37
 
38
+ # Check wether the serialport is closed
39
+ #
40
+ # @return [Bool] true if closed, false if open
23
41
  def closed?
24
42
  return @sp.closed?
25
43
  end
26
44
 
45
+ # Reads and discards incoming bytes until the START_TOKEN marrker is
46
+ # received. Call this metod immediately after opening the connection
47
+ # and before start reading the data.
27
48
  def sync
28
49
  buf = EMPTY
29
50
  begin
@@ -32,6 +53,9 @@ module MmGPS
32
53
  end while buf != START_TOKEN
33
54
  end
34
55
 
56
+ # Reads a raw packet.
57
+ #
58
+ # @return [String] a byte stream encoded with Encoding::BINARY
35
59
  def get_raw_packet
36
60
  buf = START_TOKEN.dup
37
61
  while true do
@@ -41,6 +65,9 @@ module MmGPS
41
65
  return buf[0...-2]
42
66
  end
43
67
 
68
+ # Reads a raw packet, checks its CRC, and returns its contents as a Hash.
69
+ #
70
+ # @return [Hash] typically in the form `%I(ts x y z f).zip(payload).to_h`
44
71
  def get_packet
45
72
  return MmGPS::parse_packet(self.get_raw_packet)
46
73
  end
@@ -1,12 +1,24 @@
1
- class GPSException < Exception; end
1
+ # Local Exception class.
2
+ class MmGPSException < Exception; end
2
3
 
4
+ # Manage connection and data decoding with a MarvelMind beacon or hedgehog.
3
5
  module MmGPS
6
+ # Checks a sbuffer for valid CRC16.
7
+ #
8
+ # @param str [String] the buffer to be checked
9
+ # @return [Bool] true if the buffer is self-consistent
4
10
  def self.valid_crc16?(str)
5
11
  crc16(str) == 0
6
12
  end
7
13
 
14
+ # Parse the given buffer according to the MarvelMind protocol.
15
+ # See http://www.marvelmind.com/pics/marvelmind_beacon_interfaces_v2016_03_07a.pdf
16
+ #
17
+ # @param buf [String] the String buffer to be parsed
18
+ # @return [Hash|Array] if the system is running, return a Hash with
19
+ # timestamp, coordinates, and error code (data code 0x0001). Otherwise returns an Array of Hashes for beacons status (data code 0x0002).
8
20
  def self.parse_packet(buf)
9
- raise GPSException, "Invalid CRC" unless valid_crc16?(buf)
21
+ raise MmGPSException, "Invalid CRC" unless valid_crc16?(buf)
10
22
  # warn "Invalid CRC" unless valid_crc16?(buf)
11
23
  header = buf[0..5].unpack('CCS<C')
12
24
  if header[2] == 1 then # Regular GPS Data
@@ -25,7 +37,7 @@ module MmGPS
25
37
  %I(x y z).each {|k| result.last[k] /= 100.0}
26
38
  end
27
39
  else
28
- raise GPSException, "Unexpected packet type #{header[2]}"
40
+ raise MmGPSException, "Unexpected packet type #{header[2]}"
29
41
  end
30
42
  return result
31
43
  end
@@ -1,3 +1,3 @@
1
1
  module MmGPS
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/test.rb CHANGED
@@ -8,16 +8,23 @@ beacon = MmGPS::Beacon.new(PORT, BAUD)
8
8
  beacon.trap # installs signal handler for CTRL-C
9
9
 
10
10
  puts "Syncing..."
11
- beacon.sync # discards any byte until the starting sequence "\xFFG" arrives
11
+ begin
12
+ beacon.sync # discards any byte until the starting sequence "\xFFG" arrives
13
+ rescue MmGPSException => e
14
+ puts "Packet Error: #{e.inspect}"
15
+ rescue IOError => e
16
+ puts "Port closed #{e.inspect}"
17
+ exit
18
+ end
12
19
 
13
20
  puts "Reading..."
14
21
  while not beacon.closed? do
15
22
  begin
16
23
  p beacon.get_packet
17
- rescue GPSException => e
24
+ rescue MmGPSException => e
18
25
  puts "Packet Error: #{e.inspect}"
19
26
  rescue IOError => e
20
- puts "Port closed? #{e.inspect}"
27
+ puts "Port closed #{e.inspect}"
21
28
  exit
22
29
  end
23
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mm_gps
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paolo Bosetti
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-17 00:00:00.000000000 Z
11
+ date: 2016-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: serialport
@@ -84,6 +84,7 @@ files:
84
84
  - ext/mm_gps/extconf.rb
85
85
  - ext/mm_gps/mm_gps.c
86
86
  - ext/mm_gps/mm_gps.h
87
+ - formulas.nb
87
88
  - lib/mm_gps.rb
88
89
  - lib/mm_gps/mm_gps_beacon.rb
89
90
  - lib/mm_gps/mm_gps_module.rb