Class: Artoo::Drivers::Hmc6352Compass

Inherits:
Driver
  • Object
show all
Defined in:
lib/artoo/drivers/hmc_6352_compass.rb

Overview

Hmc6352 digital compass driver behaviors for i2c

Constant Summary collapse

COMMANDS =
[:heading].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Hmc6352Compass

Returns a new instance of Hmc6352Compass.



13
14
15
16
# File 'lib/artoo/drivers/hmc_6352_compass.rb', line 13

def initialize(params={})
  @heading = 0.0
  super
end

Instance Attribute Details

#headingObject (readonly)

Returns the value of attribute heading.



9
10
11
# File 'lib/artoo/drivers/hmc_6352_compass.rb', line 9

def heading
  @heading
end

Instance Method Details

#addressObject



11
# File 'lib/artoo/drivers/hmc_6352_compass.rb', line 11

def address; 0x42; end

#start_driverObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/artoo/drivers/hmc_6352_compass.rb', line 18

def start_driver
  begin
    connection.i2c_start(address >> 1)
    connection.i2c_write("A".bytes.first)

    every(interval) do
      connection.i2c_write("A".bytes.first)
      new_value = connection.i2c_read(2)
      update(new_value) unless new_value.nil? || new_value.empty?
    end

    super
  rescue Exception => e
    Logger.error "Error starting HMC6352 driver!"
    Logger.error e.message
    Logger.error e.backtrace.inspect
  end
end

#update(val) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/artoo/drivers/hmc_6352_compass.rb', line 37

def update(val)
  puts val.inspect
  return if val.nil? || val == "bad byte"
  @heading = parse(val)
  publish(event_topic_name("update"), "heading", heading)
  publish(event_topic_name("heading"), heading)
end