Class: UltrasonicComm
- Inherits:
-
Object
- Object
- UltrasonicComm
- Defined in:
- lib/ultrasonic_comm.rb
Overview
Low-level interface for communicating with the NXT’s Ultrasonic sensor. Unlike the other sensors, the Ultrasonic sensor is digital and uses the low-speed I2C protocol for communication. This is defined in Appendix 7 of the Lego Mindstorms NXT SDK.
Constant Summary collapse
- @@i2c_dev =
0x02
- @@const_codes =
first value is the i2c address, second value is the expected number of bytes returned
{ '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], }
- @@var_codes =
value is the i2c address (all of these ops always expect to return 1 byte)
{ '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, }
- @@cmd_codes =
first value is the i2c address, second value is the command
{ '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] }
Class Method Summary collapse
Class Method Details
.method_missing(name, *args) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/ultrasonic_comm.rb', line 72 def self.method_missing(name, *args) name = name.to_s if @@const_codes.has_key? name type = :const op = @@const_codes[name] addr = op[0] rx_len = op[1] elsif @@var_codes.has_key? name type = :var op = @@var_codes[name] addr = op rx_len = 1 elsif @@cmd_codes.has_key? name type = :cmd op = @@cmd_codes[name] addr = op[0] if op[1] then value = op[1] elsif args[0] then value = args[0] else raise "Missing argument for command #{name}" end rx_len = 0 else raise "Unknown ultrasonic sensor command: #{name}" end data = [@@i2c_dev, addr] data += [value] if type == :cmd [data.size, rx_len] + data end |
.read_measurement_byte(num) ⇒ Object
68 69 70 |
# File 'lib/ultrasonic_comm.rb', line 68 def self.read_measurement_byte(num) eval "self.read_measurement_byte_#{num}" end |