class Artoo::Drivers::Hmc6352Compass

Hmc6352 digital compass driver behaviors for i2c

Constants

COMMANDS

Attributes

heading[R]

Public Class Methods

new(params={}) click to toggle source
Calls superclass method
# File lib/artoo/drivers/hmc_6352_compass.rb, line 13
def initialize(params={})
  @heading = 0.0
  super
end

Public Instance Methods

address() click to toggle source
# File lib/artoo/drivers/hmc_6352_compass.rb, line 11
def address; 0x42; end
start_driver() click to toggle source
Calls superclass method
# 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) click to toggle source
# 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

Protected Instance Methods

parse(val=[0, 0]) click to toggle source
# File lib/artoo/drivers/hmc_6352_compass.rb, line 47
def parse(val=[0, 0])
  (val[1] + val[0] * 256) / 10.0
end