Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Partial work on implementing I2C protocol, low speed commands, and ul…
…trasonic sonar sensor.
- Loading branch information
1 parent
00dc000
commit 3e0c738
Showing
11 changed files
with
264 additions
and
29 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'lego-nxt' | ||
|
||
NXTBrick.new(:usb) do |nxt| | ||
nxt.add_ultrasonic_input(:one, :sonar) | ||
|
||
nxt.sonar.start | ||
|
||
sleep(2) | ||
|
||
while nxt.sonar.distance < 5 | ||
sleep(2) | ||
puts "Distance: #{nxt.sonar.distance}" | ||
end | ||
|
||
puts 'Got it!' | ||
end |
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
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,39 @@ | ||
module NXT | ||
module Command | ||
# An implementation of all the low speed I2C related NXT commands: | ||
# | ||
# * LSGETSTATUS | ||
# * LSWRITE | ||
# * LSREAD | ||
# | ||
# This class can also be used to talk to other third-party accessories | ||
# connected in the input ports on the NXT brick that talk using I2C | ||
# low speed master/slave communication. | ||
module LowSpeed | ||
include NXT::Command::Base | ||
extend NXT::Utils::Accessors | ||
|
||
COMMAND_IDENTIFIER = { | ||
ls_get_status: 0x0E, | ||
ls_write: 0x0F, | ||
ls_read: 0x10 | ||
}.freeze | ||
|
||
def command_type | ||
COMMAND_TYPES[:direct] | ||
end | ||
|
||
def ls_get_status(response_required = false) | ||
send_and_receive(COMMAND_IDENTIFIER[:ls_get_status]) | ||
end | ||
|
||
def ls_write(response_required = false) | ||
send_and_receive(COMMAND_IDENTIFIER[:ls_write]) | ||
end | ||
|
||
def ls_read(response_required = false) | ||
send_and_receive(COMMAND_IDENTIFIER[:ls_read]) | ||
end | ||
end | ||
end | ||
end |
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
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
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,87 @@ | ||
module NXT | ||
module Protocols | ||
module I2C | ||
COMMAND_IDENTIFIER = { | ||
ls_get_status: 0x0E, | ||
ls_write: 0x0F, | ||
ls_read: 0x10 | ||
}.freeze | ||
|
||
I2C_HEADER = 0x02 | ||
|
||
# Format is I2C address, followed by the number of bytes expected in the response. | ||
I2C_CONSTANT_OPS = { | ||
read_version: [0x00, 8], | ||
read_product_id: [0x08, 8], | ||
read_sensor_type: [0x10, 8], | ||
read_factory_zero: [0x11, 1], | ||
read_factory_scale_factor: [0x12, 1], | ||
read_factory_scale_divisor: [0x13, 1], | ||
read_measurement_units: [0x14, 7] | ||
}.freeze | ||
|
||
# Format is the I2C address (all variable operations expect a 1 byte reponse). | ||
I2C_VARIABLE_OPS = { | ||
read_continuous_measurements_interval: 0x40, | ||
read_command_state: 0x41, | ||
read_measurement_byte_0: 0x42, | ||
read_measurement_byte_1: 0x43, | ||
read_measurement_byte_2: 0x44, | ||
read_measurement_byte_3: 0x45, | ||
read_measurement_byte_4: 0x46, | ||
read_measurement_byte_5: 0x47, | ||
read_measurement_byte_6: 0x48, | ||
read_measurement_byte_7: 0x49, | ||
read_actual_zero: 0x50, | ||
read_actual_scale_factor: 0x51, | ||
read_actual_scale_divisor: 0x52 | ||
}.freeze | ||
|
||
# Format is I2C address, followed by the command. If the array is only | ||
# one member large, then the second byte is instead a value passed at | ||
# run-time, eg. the length of the interval to run the continuous | ||
# measuring on the ultrasonic sensor for. | ||
I2C_COMMANDS = { | ||
off_command: [0x41, 0x00], | ||
single_shot_command: [0x41, 0x01], | ||
continuous_measurement_command: [0x41, 0x02], | ||
event_capture_command: [0x41, 0x03], | ||
request_warm_reset: [0x41, 0x04], | ||
set_continuous_measurement_interval: [0x40], | ||
set_actual_zero: [0x50], | ||
set_actual_scale_factor: [0x51], | ||
set_actual_scale_divisor: [0x52] | ||
}.freeze | ||
|
||
def self.method_missing(name, *args) | ||
data = [] | ||
|
||
if I2C_CONSTANT_OPS.has_key?(name) | ||
op = I2C_CONSTANT_OPS[name] | ||
addr = op[0] | ||
rx_len = op[1] | ||
elsif I2C_VARIABLE_OPS.has_key?(name) | ||
op = I2C_VARIABLE_OPS[name] | ||
addr = op | ||
rx_len = 1 | ||
elsif I2C_COMMANDS.has_key?(name) | ||
op = I2C_COMMANDS[name] | ||
addr = op[0] | ||
rx_len = 0 | ||
|
||
if op[1] | ||
data = [op[1]] | ||
elsif args[0] | ||
data = [args[0]] | ||
else | ||
raise "Missing argument for command #{name}" | ||
end | ||
else | ||
raise "Unknown ultrasonic sensor command: #{name}" | ||
end | ||
|
||
[data.size, rx_len, I2C_HEADER, addr] + data | ||
end | ||
end | ||
end | ||
end |