-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Driver for JSN-SR04T waterproof ultrasonic sensor
- Loading branch information
Showing
4 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# | ||
# Example of reading a JSN-SR04T ultrasonic sensor, in mode 2. | ||
# | ||
require 'bundler/setup' | ||
require 'denko' | ||
|
||
board = Denko::Board.new(Denko::Connection::Serial.new) | ||
uart = Denko::UART::Hardware.new(board: board, index: 1, baud: 9600) | ||
ultrasonic = Denko::Sensor::JSNSR04T.new(board: board, uart: uart) | ||
|
||
ultrasonic.poll(1) do |distance| | ||
puts "Distance: #{distance} mm" | ||
end | ||
|
||
sleep |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
module Denko | ||
module Sensor | ||
# | ||
# For JSN-SR04T sensor in mode 2 ONLY. | ||
# | ||
class JSNSR04T | ||
include Behaviors::Component | ||
include Behaviors::Lifecycle | ||
include Behaviors::Poller | ||
|
||
UART_CLASSES = [Denko::UART::Hardware, Denko::UART::BitBang] | ||
TIMEOUT = 0.500 | ||
MATCHER = /(\d*)mm/ | ||
|
||
attr_reader :uart | ||
|
||
after_initialize do | ||
unless (params[:uart] && UART_CLASSES.include?(params[:uart].class)) | ||
raise ArgumentError, "JSN-SR04T driver only works in mode 2, and expects a UART in the :uart key" | ||
end | ||
|
||
raise StandardError, "UART baud must be 9600 for JSN-SR04T" unless params[:uart].baud == 9600 | ||
|
||
@uart = params[:uart] | ||
end | ||
|
||
def _read | ||
# Trigger read | ||
uart.write("U") | ||
sleep 0.100 | ||
|
||
# Get line from UART | ||
start = Time.now | ||
line = nil | ||
until line || (Time.now - start > TIMEOUT) | ||
line = uart.gets | ||
sleep 0.010 | ||
end | ||
|
||
# Extract mm as integer | ||
if line && match = line.match(MATCHER) | ||
value = match[1].to_i | ||
if value | ||
self.update(value) | ||
return value | ||
end | ||
else | ||
return nil | ||
end | ||
end | ||
end | ||
end | ||
end |