Sha256: a8e7871d5da0d9ab19967a81111cb0bf65cdd85e2f8a04e5b49ebfb52aec77ba

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

require 'artoo/drivers/driver'

module Artoo
  module Drivers
    # Maxbotix ultrasonic range finder driver behaviors for Firmata
    class Maxbotix < Driver
      COMMANDS = [:range].freeze

      attr_accessor :last_reading

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

      # @return [float] last range reading in inches
      def range
        return ( 254.0 / 1024.0 ) * 2.0 * last_reading
      end

      # @return [float] last range reading in cm
      def range_cm
        return (last_reading / 2.0) * 2.54
      end

      # Sets values to read from ultrasonic range finder
      # and starts driver
      def start_driver
        every(interval) do
          update(connection.analog_read(pin))
        end

        super
      end

      # Publishes events according to the ultrasonic rangefinder value
      def update(value)
        last_reading = value
        publish(event_topic_name("update"), "range", range)
        publish(event_topic_name("range"), range)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
artoo-gpio-0.0.1 lib/artoo/drivers/maxbotix.rb
artoo-arduino-1.1.0 lib/artoo/drivers/maxbotix.rb