nxt 0.3.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. checksums.yaml +15 -0
  2. data/README.md +45 -0
  3. data/lib/communication/bluetooth_communication.rb +85 -0
  4. data/lib/communication/serial_port_profile.rb +71 -0
  5. data/lib/nxt.rb +111 -20
  6. data/lib/ruby-nxt.sublime-project +8 -0
  7. data/lib/ruby-nxt.sublime-workspace +288 -0
  8. data/lib/telegrams/commands/direct/get_battery_level.rb +8 -0
  9. data/lib/telegrams/commands/direct/get_current_program_name.rb +8 -0
  10. data/lib/{nxt/commands/sound.rb → telegrams/commands/direct/get_input_values.rb} +0 -0
  11. data/lib/{nxt/commands/tone.rb → telegrams/commands/direct/keep_alive.rb} +0 -0
  12. data/lib/telegrams/commands/direct/ls_get_status.rb +0 -0
  13. data/lib/telegrams/commands/direct/ls_read.rb +0 -0
  14. data/lib/telegrams/commands/direct/ls_write.rb +0 -0
  15. data/lib/telegrams/commands/direct/message_read.rb +0 -0
  16. data/lib/telegrams/commands/direct/message_write.rb +0 -0
  17. data/lib/telegrams/commands/direct/output_state.rb +229 -0
  18. data/lib/telegrams/commands/direct/play_sound_file.rb +38 -0
  19. data/lib/telegrams/commands/direct/play_tone.rb +34 -0
  20. data/lib/telegrams/commands/direct/replies/get_battery_level_reply.rb +28 -0
  21. data/lib/telegrams/commands/direct/replies/get_current_program_name_reply.rb +30 -0
  22. data/lib/telegrams/commands/direct/replies/play_sound_file_reply.rb +13 -0
  23. data/lib/telegrams/commands/direct/replies/play_tone_reply.rb +14 -0
  24. data/lib/telegrams/commands/direct/replies/reset_motor_position_reply.rb +13 -0
  25. data/lib/telegrams/commands/direct/replies/set_input_mode_reply.rb +11 -0
  26. data/lib/telegrams/commands/direct/replies/set_output_state_reply.rb +11 -0
  27. data/lib/telegrams/commands/direct/replies/start_program_reply.rb +13 -0
  28. data/lib/telegrams/commands/direct/replies/stop_program_reply.rb +13 -0
  29. data/lib/telegrams/commands/direct/replies/stop_sound_playback_reply.rb +13 -0
  30. data/lib/telegrams/commands/direct/reset_input_scaled_value.rb +0 -0
  31. data/lib/telegrams/commands/direct/reset_motor_position.rb +8 -0
  32. data/lib/telegrams/commands/direct/set_input_mode.rb +101 -0
  33. data/lib/telegrams/commands/direct/set_output_state.rb +29 -0
  34. data/lib/telegrams/commands/direct/start_program.rb +30 -0
  35. data/lib/telegrams/commands/direct/stop_program.rb +9 -0
  36. data/lib/telegrams/commands/direct/stop_sound_playback.rb +8 -0
  37. data/lib/telegrams/commands/direct_command.rb +8 -0
  38. data/lib/telegrams/commands/direct_command_reply.rb +10 -0
  39. data/lib/telegrams/commands/message_translator.rb +46 -0
  40. data/lib/telegrams/commands/system/get_device_info.rb +8 -0
  41. data/lib/telegrams/commands/system/replies/get_device_info_reply.rb +41 -0
  42. data/lib/telegrams/commands/system_command.rb +8 -0
  43. data/lib/telegrams/messages/error.rb +0 -0
  44. data/lib/telegrams/messages/message.rb +0 -0
  45. data/lib/telegrams/messages/success.rb +0 -0
  46. data/lib/telegrams/no_message_reply.rb +10 -0
  47. data/lib/telegrams/reply.rb +82 -0
  48. data/lib/telegrams/respondable_telegram.rb +29 -0
  49. data/lib/telegrams/telegram.rb +14 -0
  50. data/spec/communication/bluetooth_communication_spec.rb +170 -0
  51. data/spec/communication/serial_port_profile_spec.rb +139 -0
  52. data/spec/helper.rb +1 -0
  53. data/spec/nxt_spec.rb +438 -0
  54. data/spec/telegrams/commands/direct/get_battery_level_spec.rb +26 -0
  55. data/spec/telegrams/commands/direct/get_current_program_name_spec.rb +26 -0
  56. data/spec/telegrams/commands/direct/output_state_spec.rb +198 -0
  57. data/spec/telegrams/commands/direct/play_sound_file_spec.rb +75 -0
  58. data/spec/telegrams/commands/direct/play_tone_spec.rb +63 -0
  59. data/spec/telegrams/commands/direct/replies/get_battery_level_reply_spec.rb +40 -0
  60. data/spec/telegrams/commands/direct/replies/get_current_program_name_reply_spec.rb +33 -0
  61. data/spec/telegrams/commands/direct/replies/play_sound_file_reply_spec.rb +13 -0
  62. data/spec/telegrams/commands/direct/replies/play_tone_reply_spec.rb +14 -0
  63. data/spec/telegrams/commands/direct/replies/reset_motor_position_reply_spec.rb +13 -0
  64. data/spec/telegrams/commands/direct/replies/set_input_mode_reply_spec.rb +12 -0
  65. data/spec/telegrams/commands/direct/replies/set_output_state_reply_spec.rb +12 -0
  66. data/spec/telegrams/commands/direct/replies/start_program_reply_spec.rb +12 -0
  67. data/spec/telegrams/commands/direct/replies/stop_program_reply_spec.rb +13 -0
  68. data/spec/telegrams/commands/direct/replies/stop_sound_playback_reply_spec.rb +13 -0
  69. data/spec/telegrams/commands/direct/reset_motor_position_spec.rb +31 -0
  70. data/spec/telegrams/commands/direct/set_input_mode_spec.rb +122 -0
  71. data/spec/telegrams/commands/direct/set_output_state_spec.rb +72 -0
  72. data/spec/telegrams/commands/direct/start_program_spec.rb +58 -0
  73. data/spec/telegrams/commands/direct/stop_program_spec.rb +34 -0
  74. data/spec/telegrams/commands/direct/stop_sound_playback_spec.rb +34 -0
  75. data/spec/telegrams/commands/direct_command_reply_spec.rb +7 -0
  76. data/spec/telegrams/commands/direct_command_spec.rb +34 -0
  77. data/spec/telegrams/commands/system/get_device_info_spec.rb +16 -0
  78. data/spec/telegrams/commands/system/replies/get_device_info_reply_spec.rb +63 -0
  79. data/spec/telegrams/commands/system_command_spec.rb +26 -0
  80. data/spec/telegrams/no_message_reply_spec.rb +12 -0
  81. data/spec/telegrams/reply_spec.rb +63 -0
  82. data/spec/telegrams/respondable_telegram_spec.rb +66 -0
  83. data/spec/telegrams/telegram_spec.rb +38 -0
  84. metadata +97 -116
  85. data/README.markdown +0 -52
  86. data/Rakefile +0 -35
  87. data/lib/nxt/commands/base.rb +0 -51
  88. data/lib/nxt/commands/input.rb +0 -60
  89. data/lib/nxt/commands/output.rb +0 -105
  90. data/lib/nxt/commands/program.rb +0 -70
  91. data/lib/nxt/connectors/base.rb +0 -35
  92. data/lib/nxt/connectors/input/color.rb +0 -30
  93. data/lib/nxt/connectors/input/touch.rb +0 -11
  94. data/lib/nxt/connectors/input/ultrasonic.rb +0 -11
  95. data/lib/nxt/connectors/output/motor.rb +0 -114
  96. data/lib/nxt/errors.rb +0 -25
  97. data/lib/nxt/exceptions.rb +0 -26
  98. data/lib/nxt/interfaces/base.rb +0 -36
  99. data/lib/nxt/interfaces/serial_port.rb +0 -88
  100. data/lib/nxt/interfaces/usb.rb +0 -8
  101. data/lib/nxt/nxt_brick.rb +0 -167
  102. data/lib/nxt/patches/module.rb +0 -22
  103. data/lib/nxt/patches/string.rb +0 -29
  104. data/lib/nxt/utils/accessors.rb +0 -24
  105. data/spec/matchers.rb +0 -7
  106. data/spec/nxt/connectors/output/motor_spec.rb +0 -55
  107. data/spec/nxt/interfaces/serial_port_spec.rb +0 -73
  108. data/spec/nxt/nxt_brick_spec.rb +0 -199
  109. data/spec/spec_helper.rb +0 -4
@@ -1,52 +0,0 @@
1
- # NXT
2
-
3
- Control a Lego NXT 2.0 brick using Ruby code. This library works by piping
4
- commands over a serialport connection to the brick, allowing you to write Ruby
5
- scripts to control your bot. This means you can use both the Bluetooth and USB
6
- serial ports provided on the brick as interfaces within your code.
7
-
8
- This project used to be based on "ruby-nxt", and Tony Buser's subsequent rewrite
9
- "nxt". It is now a complete rewrite, based heavily in some parts on the
10
- aforesaid projects internally, but with a brand new external API that should
11
- prove cleaner and easier to work with.
12
-
13
- This code implements direct command, as outlined in "Appendix 2-LEGO MINDSTORMS
14
- NXT Direct Commands.pdf". Not all functionality is implemented yet!
15
-
16
- ## Getting Started
17
-
18
- ### Connect to Your NXT Brick
19
-
20
- In order to start coding with your NXT, you'll need to set up either a
21
- Bluetooth or USB connection to it. Follow one of the below set of steps, and
22
- make a note of the `/dev/*` address you end up using to point to the NXT.
23
-
24
- ### Creating a Bluetooth Serialport Connection
25
-
26
- For instructions on creating a bluetooth serialport connection:
27
-
28
- * Linux: http://tonybuser.com/bluetooth-serial-port-to-nxt-in-linux
29
- * Max OSX: http://tonybuser.com/bluetooth-serial-port-to-nxt-in-osx
30
- * Windows: http://tonybuser.com/ruby-serialportnxt-on-windows
31
-
32
- ### Creating a USB Serialport Connection
33
-
34
- TODO
35
-
36
- Once you have your NXT Connected
37
-
38
- ## Documentation and Examples
39
-
40
- The NXT project has been heavily documented using nice, clean, human readable
41
- markdown. YARD is used to generated the docs, and the options have been included
42
- in our .yardopts file, so simply run a YARD server to read them:
43
-
44
- yard server
45
-
46
- This documents the API, both internal and external. For bite-sized chunks of NXT
47
- code that is much more appropriate for beginners,
48
- [have a look at the examples](https://github.com/nathankleyn/nxt/tree/master/examples).
49
-
50
- In addition to this, you might find the tests quite helpful. There are currently
51
- only RSpec unit tests, which can be found in the `spec` directory; the plan is
52
- to add some decent feature tests soon.
data/Rakefile DELETED
@@ -1,35 +0,0 @@
1
- require 'rake'
2
- require 'rake/clean'
3
- require 'rdoc/task'
4
- require 'rspec/core/rake_task'
5
-
6
- RDoc::Task.new do |rdoc|
7
- files = ['README.markdown', 'lib/**/*.rb']
8
- rdoc.rdoc_files.add(files)
9
- rdoc.main = 'README.markdown'
10
- rdoc.title = 'ruby_events Docs'
11
- rdoc.rdoc_dir = 'doc/rdoc'
12
- rdoc.options << '--line-numbers'
13
- end
14
-
15
- RSpec::Core::RakeTask.new(:spec)
16
-
17
- desc 'NXT related tasks'
18
- namespace :nxt do
19
- desc 'Detect a connected NXT brick within /dev.'
20
- task :detect do
21
- unless $DEV ||= ENV['NXT'] || ENV['DEV']
22
- begin
23
- devices = Dir['/dev/*NXT*']
24
- if devices.size > 0
25
- $DEV = devices[0]
26
- puts "Detected a NXT brick at '#{$DEV}'."
27
- else
28
- puts 'Could not detect any connected NXT bricks.'
29
- end
30
- rescue
31
- # FIXME: The /dev directory isn't there, possibly running on Windows.
32
- end
33
- end
34
- end
35
- end
@@ -1,51 +0,0 @@
1
- module NXT
2
- module Command
3
- module Base
4
-
5
- private
6
-
7
- COMMAND_TYPES = {
8
- direct: 0x00,
9
- system: 0x01,
10
- reply: 0x02
11
- }.freeze
12
-
13
- COMMANDS = {
14
- 0x00 => 'startprogram',
15
- 0x01 => 'stopprogram',
16
- 0x02 => 'playsoundfile',
17
- 0x03 => 'playtone',
18
- 0x04 => 'setoutputstate',
19
- 0x05 => 'setinputmode',
20
- 0x06 => 'getoutputstate',
21
- 0x07 => 'getinputvalues',
22
- 0x08 => 'resetinputscaledvalue',
23
- 0x09 => 'messagewrite',
24
- 0x0a => 'resetmotorposition',
25
- 0x0b => 'getbatterylevel',
26
- 0x0c => 'stopsoundplayback',
27
- 0x0d => 'keepalive',
28
- 0x0e => 'lsgetstatus',
29
- 0x0f => 'lswrite',
30
- 0x10 => 'lsread',
31
- 0x11 => 'getcurrentprogramname',
32
- 0x13 => 'messageread',
33
- }
34
-
35
- PORTS = {
36
- a: 0x00,
37
- b: 0x01,
38
- c: 0x02,
39
- one: 0x00,
40
- two: 0x01,
41
- three: 0x02,
42
- four: 0x03,
43
- all: 0xFF
44
- }
45
-
46
- def port_as_byte(port)
47
- PORTS[port]
48
- end
49
- end
50
- end
51
- end
@@ -1,60 +0,0 @@
1
- module NXT
2
- module Command
3
- module Input
4
- include NXT::Command::Base
5
- extend NXT::Utils::Accessors
6
-
7
- @@command_type = COMMAND_TYPES[:direct]
8
- @@command = 0x05
9
-
10
- SENSOR_TYPES = {}
11
- [:no_sensor,
12
- :switch,
13
- :temperature,
14
- :reflection,
15
- :angle,
16
- :light_active,
17
- :light_inactive,
18
- :sound_db,
19
- :sound_dba,
20
- :custom,
21
- :lowspeed,
22
- :lowspeed_9v,
23
- :highspeed,
24
- :colorfull,
25
- :colorred,
26
- :colorgreen,
27
- :colorblue,
28
- :colornone
29
- ].each_with_index do |key, index|
30
- SENSOR_TYPES[key] = index
31
- end
32
-
33
- SENSOR_MODES = {
34
- :rawmode => 0x00,
35
- :booleanmode => 0x20,
36
- :transitioncntmode => 0x40,
37
- :periodcountermode => 0x60,
38
- :pctfullscalemode => 0x80,
39
- :celsiusmode => 0xA0,
40
- :fahrenheitmode => 0xC0,
41
- :anglestepmode => 0xE0,
42
- :slopemask => 0x1F,
43
- :modemask => 0xE0
44
- }
45
-
46
- def set_input_mode(type, mode)
47
- raise "Invalid sensor type: #{type}" if !type.nil? && !SENSOR_TYPES.include?(type)
48
-
49
- @interface.send_and_receive([
50
- @@command_type,
51
- @@command,
52
- port_as_byte(@port),
53
- SENSOR_TYPES[type],
54
- SENSOR_MODES[mode]
55
- ])
56
- end
57
- end
58
- end
59
- end
60
-
@@ -1,105 +0,0 @@
1
- module NXT
2
- module Command
3
- # An implementation of all the output related NXT commands:
4
- #
5
- # * setoutputstate
6
- # * getoutputstate
7
- #
8
- # This is used predominantly to interface with the servo-motor connectors
9
- # that come prepackaged with NXT kits.
10
- #
11
- # This class can also be used to talk to other third-party accessories
12
- # connected in the output ports on the NXT brick.
13
- #
14
- # This class does not actually talk to the chosen interface for the NXT
15
- # brick. Instead, it outputs messages in byte arrays ready to be serialised
16
- # to the brick over the appropriate interface from within the {NXT::Brick}
17
- # class.
18
- module Output
19
- include NXT::Command::Base
20
- extend NXT::Utils::Accessors
21
-
22
- @@command_type = COMMAND_TYPES[:direct]
23
- @@command_identifier = 0x04
24
-
25
- # The mode enum. This is a list of possible values when setting the mode
26
- # byte.
27
- #
28
- # Reference: Appendix 2, Page 6
29
- MODE = {
30
- # Motor will rotate freely.
31
- # NOTE: This is not documented in the Appendixes.
32
- coast: 0x00,
33
- # Turn on the specified motor.
34
- motor_on: 0x01,
35
- # Use run/brake instead of run/float in PWM. This means the voltage is
36
- # not allowed to float between PWM pulses, improving accuracy at the
37
- # expense of greater power usage.
38
- brake: 0x02,
39
- # Turns on the regulation. This is required when setting a regulation
40
- # mode setting.
41
- regulated: 0x04
42
- }.freeze
43
-
44
- # The regulation mode enum. This is a list of possible values when
45
- # setting the regulation mode byte.
46
- #
47
- # Reference: Appendix 2, Page 6
48
- REGULATION_MODE = {
49
- # No regulation will be enabled.
50
- idle: 0x00,
51
- # Power control will be enabled on specific output.
52
- motor_speed: 0x01,
53
- # Synchronisation will be enabled. This requires two output ports to
54
- # have this enabled before it will work.
55
- motor_sync: 0x02
56
- }.freeze
57
-
58
- # The run state enum. This is a list of possible values when setting the
59
- # run state byte.
60
- #
61
- # Reference: Appendix 2, Page 6
62
- RUN_STATE = {
63
- # Output will be idle.
64
- idle: 0x00,
65
- # Output will ramp-up to the desired speed.
66
- ramp_up: 0x10,
67
- # Output will be running.
68
- running: 0x20,
69
- # Output will ramp-down to the desired speed.
70
- ramp_down: 0x40
71
- }.freeze
72
-
73
- attr_combined_accessor :power, 75
74
- attr_combined_accessor :mode, :motor_on
75
- attr_combined_accessor :regulation_mode, :idle
76
- attr_combined_accessor :run_state, :running
77
- attr_combined_accessor :tacho_limit, 0
78
-
79
- attr_setter :power, is: Integer
80
- attr_setter :mode, is_key_in: MODE
81
- attr_setter :regulation_mode, is_key_in: REGULATION_MODE
82
- attr_setter :run_state, is_key_in: RUN_STATE
83
- attr_setter :tacho_limit, is: Integer
84
-
85
- def set_output_state(response_required = false)
86
- # Pack this value into a 32-bit unsigned little-endian binary string,
87
- # then unpack it into 4 8 bit unsigned integer chunks. We are
88
- # converting the passed in value to a little endian, unsigned long
89
- # value.
90
- tacho_limit_as_bytes = [self.tacho_limit].pack('V').unpack('C4')
91
-
92
- @interface.send_and_receive([
93
- @@command_type,
94
- @@command_identifier,
95
- port_as_byte(self.port),
96
- self.power,
97
- MODE[self.mode],
98
- REGULATION_MODE[self.regulation_mode],
99
- 0, # turn ratio
100
- RUN_STATE[self.run_state]
101
- ] + tacho_limit_as_bytes, response_required)
102
- end
103
- end
104
- end
105
- end
@@ -1,70 +0,0 @@
1
- module NXT
2
- module Command
3
- module Output
4
- class Program
5
- include NXT::Command::Base
6
-
7
- @@command_type = COMMAND_TYPES[:direct]
8
- START_PROGRAM = 0x00
9
- STOP_PROGRAM = 0x01
10
- GET_CURRENT_PROGRAM_NAME = 0x11
11
-
12
- def initialize(interface, name=nil)
13
- @interface = interface
14
- @name = name
15
- end
16
-
17
- def start
18
- self.class.start(@interface, @name)
19
- end
20
-
21
- def stop
22
- self.class.stop(@interface)
23
- end
24
-
25
-
26
- class << self
27
-
28
- def start(interface, name)
29
- response = send_command(interface, START_PROGRAM, name)
30
-
31
- unless response[:status] == NXT::Errors::SUCCESS
32
- message = "#{response[:message]}; Try using '.rxe' as the filename extension"
33
- raise NXT::Exceptions::CommandError.new message
34
- end
35
-
36
- response
37
- end
38
-
39
- def stop(interface)
40
- send_command(interface, STOP_PROGRAM)
41
- end
42
-
43
- def get_running_name(interface)
44
- response = send_command(interface, GET_CURRENT_PROGRAM_NAME)
45
- raise NXT::Exceptions::CommandError.new(response[:message]) if response[:status] != NXT::Errors::SUCCESS
46
- convert_data_to_string(response[:data])
47
- end
48
-
49
- private
50
- def send_command(interface, command, name=nil)
51
-
52
- array = [@@command_type, command]
53
- array.concat convert_string_to_data(name) unless name.nil?
54
-
55
- response = interface.send_and_receive(array)
56
- end
57
-
58
- def convert_data_to_string(data)
59
- data.pack("C*").strip
60
- end
61
-
62
- def convert_string_to_data(name, max_length=20)
63
- name.ljust(max_length, "\0").unpack("C*")
64
- end
65
- end
66
- end
67
- end
68
- end
69
- end
70
-
@@ -1,35 +0,0 @@
1
- module NXT
2
- module Connector
3
- module Base
4
- SENSOR_TYPES = {}
5
- [:no_sensor,
6
- :switch,
7
- :temperature,
8
- :reflection,
9
- :angle,
10
- :light_active,
11
- :light_inactive,
12
- :sound_db,
13
- :sound_dba,
14
- :custom,
15
- :lowspeed,
16
- :lowspeed_9v,
17
- :highspeed,
18
- :colorfull,
19
- :colorred,
20
- :colorgreen,
21
- :colorblue,
22
- :colornone
23
- ].each_with_index do |key, index|
24
- SENSOR_TYPES[key] = index
25
- end
26
-
27
- def set_input_mode(interface, type, mode)
28
- raise "Invalid sensor type: #{mode}" if !mode.nil? && !SENSOR_TYPES.include?(mode)
29
-
30
-
31
- end
32
- end
33
- end
34
- end
35
-
@@ -1,30 +0,0 @@
1
- module NXT
2
- module Connector
3
- module Input
4
- class Color
5
- include NXT::Command::Input
6
-
7
- attr_accessor :port, :interface
8
-
9
- COLORS = {
10
- :off => :colornone,
11
- :red => :colorred,
12
- :green => :colorgreen,
13
- :blue => :colorblue,
14
- :all => :colorfull
15
- }
16
- def initialize(port, interface)
17
- @interface = interface
18
- @port = port
19
- end
20
-
21
- def set_color(color)
22
- raise "Invalid color #{color}" unless COLORS.include?(color)
23
-
24
- set_input_mode(COLORS[color], :booleanmode)
25
-
26
- end
27
- end
28
- end
29
- end
30
- end