ruby-player 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/NEWS.md ADDED
@@ -0,0 +1,7 @@
1
+ ## 2012-02-16 0.2.0
2
+
3
+ * Added power interface
4
+
5
+ ## 2012-02-13 version 0.1.0
6
+
7
+ Code is rewrited in pure ruby!
data/README.md CHANGED
@@ -2,7 +2,8 @@ Ruby Player - Ruby client library for Player (tools for robots) [![Build Status]
2
2
 
3
3
  Summary
4
4
  -------------------------------------
5
- Ruby Player provide high level client library to access to Player server in pure Ruby
5
+ Ruby Player provide high level client library to access to Player server in pure Ruby.
6
+
6
7
  Currently (2012-01-07) the Ruby Player are developing and testing with Player 3.1.0 latest svn version
7
8
 
8
9
  API coverage
@@ -11,6 +12,8 @@ The list of support objects and devices of Player.
11
12
 
12
13
  * Client object
13
14
  * Position2d
15
+ * Power
16
+ * Ranger
14
17
 
15
18
  Install
16
19
  -------------------------------------
data/TODO.md CHANGED
@@ -59,8 +59,6 @@ Device proxies are not started to develop
59
59
 
60
60
  **position3d** - The position3d proxy provides an interface to a mobile robot base, such as the Segway RMP series.
61
61
 
62
- **power** - The power proxy provides an interface through which battery levels can be monitored.
63
-
64
62
  **ptz** - The ptz proxy provides an interface to pan-tilt units such as the Sony PTZ camera.
65
63
 
66
64
  **wifi** - The wifi proxy is used to query the state of a wireless network.
data/lib/ruby-player.rb CHANGED
@@ -24,7 +24,7 @@ require "ruby-player/client"
24
24
 
25
25
  #interfaces
26
26
  require "ruby-player/position2d"
27
+ require "ruby-player/power"
27
28
  require "ruby-player/ranger"
28
29
 
29
30
 
30
-
@@ -29,6 +29,8 @@ module Player
29
29
  class Client < Device
30
30
  include Common
31
31
 
32
+ attr_reader :log_level
33
+
32
34
  # Initialize client
33
35
  # @param [String] host host of Player server
34
36
  # @param [Hash] opts client options
@@ -153,7 +155,7 @@ module Player
153
155
 
154
156
  private
155
157
  def make_device(dev)
156
- instance_eval(dev.interface_name.capitalize).send(:new, dev, self, @log_level)
158
+ instance_eval(dev.interface_name.capitalize).send(:new, dev, self)
157
159
  end
158
160
 
159
161
  def read
@@ -446,7 +446,7 @@ module Player
446
446
  PLAYER_POSITION3D_SPEED_PROF = 9
447
447
 
448
448
  PLAYER_POWER_DATA_STATE = 1
449
- PLAYER_POWER_SET_CHARGING_POLICY_REQ = 1
449
+ PLAYER_POWER_REQ_SET_CHARGING_POLICY = 1
450
450
  PLAYER_POWER_MASK_VOLTS = 1
451
451
  PLAYER_POWER_MASK_WATTS = 2
452
452
  PLAYER_POWER_MASK_JOULES = 4
@@ -20,16 +20,9 @@ module Player
20
20
  # Device address
21
21
  attr_reader :addr
22
22
 
23
- # Device geometry
24
- # @return [Hash] geometry { :px, :py. :pz, :proll, :ppitch, :pyaw, :sw, :sl, :sh }
25
- attr_reader :geom
26
-
27
-
28
- def initialize(addr, client, log_level)
23
+ def initialize(addr, client)
29
24
  @addr, @client = addr, client
30
- @log_level = log_level
31
-
32
- @geom = {px: 0.0, py: 0.0, pz: 0.0, proll: 0.0, ppitch: 0.0, pyaw: 0.0, sw: 0.0, sl: 0.0, sh: 0.0}
25
+ @log_level = client.log_level
33
26
  end
34
27
 
35
28
  def fill(hdr,msg)
@@ -48,13 +41,5 @@ module Player
48
41
  subtype: subtype,
49
42
  size: msg.bytesize), msg)
50
43
  end
51
-
52
- def read_geom(msg)
53
- data = msg.unpack("G*")
54
- [:px,:py,:pz, :proll,:ppitch,:pyaw, :sw,:sl,:sh].each_with_index do |k,i|
55
- @geom[k] = data[i]
56
- end
57
- debug("Get geom px=%.2f py=%.2f pz=%.2f; proll=%.2f, ppitch=%.2f, pyaw=%.2f, sw=%.2f, sl=%.2f, sh=%.2f" % @geom.values)
58
- end
59
44
  end
60
45
  end
@@ -31,10 +31,16 @@ module Player
31
31
  # Position of robot
32
32
  # @return [Hash] hash position {:px, :py, :pa, :vx, :vy, :va, :stall }
33
33
  attr_reader :position
34
+
35
+ # Device geometry
36
+ # @return [Hash] geometry { :px, :py. :pz, :proll, :ppitch, :pyaw, :sw, :sl, :sh }
37
+ attr_reader :geom
38
+
34
39
 
35
- def initialize(addr, client, log_level)
40
+ def initialize(addr, client)
36
41
  super
37
42
  @position = {px: 0.0, py: 0.0, pa: 0.0, vx: 0.0, vy: 0.0, va: 0.0, stall: 0}
43
+ @geom = {px: 0.0, py: 0.0, pz: 0.0, proll: 0.0, ppitch: 0.0, pyaw: 0.0, sw: 0.0, sl: 0.0, sh: 0.0}
38
44
  end
39
45
 
40
46
  # Query robot geometry
@@ -241,5 +247,14 @@ module Player
241
247
  end
242
248
  debug("Get position px=%.2f py=%.2f pa=%.2f; vx=%.2f, vy=%.2f, va=%.2f, stall=%d" % position.values)
243
249
  end
250
+
251
+ def read_geom(msg)
252
+ data = msg.unpack("G*")
253
+ [:px,:py,:pz, :proll,:ppitch,:pyaw, :sw,:sl,:sh].each_with_index do |k,i|
254
+ @geom[k] = data[i]
255
+ end
256
+ debug("Get geom px=%.2f py=%.2f pz=%.2f; proll=%.2f, ppitch=%.2f, pyaw=%.2f, sw=%.2f, sl=%.2f, sh=%.2f" % @geom.values)
257
+ end
258
+
244
259
  end
245
260
  end
@@ -0,0 +1,105 @@
1
+ # Ruby Player - Ruby client library for Player (tools for robots)
2
+ #
3
+ # Copyright (C) 2012 Timin Aleksey
4
+ #
5
+ # This program is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+
15
+ module Player
16
+ # The power interface provides access to a robot's power subsystem
17
+ class Power < Device
18
+
19
+ # Power state
20
+ #
21
+ # *:valid* status bits. The driver will set the bits to indicate which fields it is using. Bitwise-and with PLAYER_POWER_MASK_X values to see which fields are being set.
22
+ #
23
+ # *:volts* Battery voltage [V].
24
+ #
25
+ # *:percent* Percent of full charge [%].
26
+ #
27
+ # *:joules* Energy stored [J].
28
+ #
29
+ # *:watts* Estimated current energy consumption (negative values) or aquisition (positive values) [W].
30
+ #
31
+ # *:charging* Charge exchange status: if 1, the device is currently receiving charge from another energy device
32
+ #
33
+ # @return [Hash] state
34
+ attr_reader :state
35
+
36
+ def initialize(addr, client)
37
+ super
38
+ @state = { valid: 0, volts: 0.0, percent: 0.0, joules: 0.0, watts: 0.0, charging: 0 }
39
+ end
40
+
41
+ # Request to change the charging policy
42
+ # @param [Hash] policy
43
+ # @option policy [Boolean] :enable_input boolean controlling recharging
44
+ # @option policy [Boolean] :enable_output bolean controlling whether others can recharge from this device
45
+ def set_charging_policy(policy={})
46
+ data = [
47
+ policy[:enable_input] ? 1 : 0,
48
+ policy[:enable_output] ? 1 : 0
49
+ ]
50
+ send_message(PLAYER_MSGTYPE_REQ, PLAYER_POWER_REQ_SET_CHARGING_POLICY, data.pack("NN"))
51
+ end
52
+
53
+ # Check volts valid
54
+ def volts_valid?
55
+ state[:valid].to_i & PLAYER_POWER_MASK_VOLTS > 0
56
+ end
57
+
58
+ # Check watts valid
59
+ def watts_valid?
60
+ state[:valid].to_i & PLAYER_POWER_MASK_WATTS > 0
61
+ end
62
+
63
+ # Check joules valid
64
+ def joules_valid?
65
+ state[:valid].to_i & PLAYER_POWER_MASK_JOULES > 0
66
+ end
67
+
68
+ # Check perecent valid
69
+ def percent_valid?
70
+ state[:valid].to_i & PLAYER_POWER_MASK_PERCENT > 0
71
+ end
72
+
73
+ # Check charging valid
74
+ def charging_valid?
75
+ state[:valid].to_i & PLAYER_POWER_MASK_CHARGING > 0
76
+ end
77
+
78
+ def fill(hdr, msg)
79
+ case hdr.subtype
80
+ when PLAYER_POWER_DATA_STATE
81
+ read_state(msg)
82
+ else
83
+ undexpected_message hdr
84
+ end
85
+ end
86
+
87
+ def handle_response(hdr, msg)
88
+ case hdr.subtype
89
+ when PLAYER_POWER_REQ_SET_CHARGING_POLICY
90
+ nil
91
+ else
92
+ undexpected_message hdr
93
+ end
94
+ end
95
+
96
+ private
97
+ def read_state(msg)
98
+ data = msg.unpack("NggggN")
99
+ @state.keys.each_with_index do |k,i|
100
+ @state[k] = data[i]
101
+ end
102
+ debug("Get power state valid=%x volts=%.2f, percent=%.2f, joules=%.2f; watts=%.2f, charging=%d" % @state.values)
103
+ end
104
+ end
105
+ end
@@ -33,11 +33,16 @@ module Player
33
33
  # @see set_config
34
34
  attr_reader :config
35
35
 
36
- def initialize(addr, client, log_level)
36
+
37
+ # Device geometry
38
+ # @return [Hash] geometry { :px, :py. :pz, :proll, :ppitch, :pyaw, :sw, :sl, :sh, :sensors => [geom of sensors] }
39
+ attr_reader :geom
40
+
41
+ def initialize(addr, client)
37
42
  super
38
43
  @rangers = []
39
44
  @intensities = []
40
- @geom[:sensors] = []
45
+ @geom = {px: 0.0, py: 0.0, pz: 0.0, proll: 0.0, ppitch: 0.0, pyaw: 0.0, sw: 0.0, sl: 0.0, sh: 0.0, sensors: []}
41
46
  @config = { min_angle: 0.0, max_angle: 0.0, angular_res: 0.0, min_range: 0.0, max_range: 0.0, range_res: 0.0, frequecy: 0.0 }
42
47
  end
43
48
 
@@ -135,7 +140,12 @@ module Player
135
140
 
136
141
  private
137
142
  def read_geom(msg)
138
- super(msg[0,72])
143
+ data = msg[0,72].unpack("G*")
144
+ [:px,:py,:pz, :proll,:ppitch,:pyaw, :sw,:sl,:sh].each_with_index do |k,i|
145
+ @geom[k] = data[i]
146
+ end
147
+ debug("Get geom px=%.2f py=%.2f pz=%.2f; proll=%.2f, ppitch=%.2f, pyaw=%.2f, sw=%.2f, sl=%.2f, sh=%.2f" % @geom.values)
148
+
139
149
 
140
150
  p_count = msg[72,8].unpack("NN")
141
151
  p_count = p_count[0] + p_count[1] * 256
@@ -13,5 +13,5 @@
13
13
  # GNU General Public License for more details.
14
14
 
15
15
  module Player
16
- VERSION = "0.1.0"
16
+ VERSION = "0.2.0"
17
17
  end
data/spec/client_spec.rb CHANGED
@@ -133,6 +133,15 @@ describe Player::Client do
133
133
  ranger.addr.interface_name.should eql("ranger")
134
134
  ranger.addr.index.should eql(1)
135
135
  end
136
+
137
+ it "should describe to power:2" do
138
+ mock_subscribe(PLAYER_POWER_CODE, 2)
139
+
140
+ power = @cl.subscribe(:power, index: 2)
141
+ power.addr.interface_name.should eql("power")
142
+ power.addr.index.should eql(2)
143
+ end
144
+
136
145
  end
137
146
 
138
147
  private
@@ -1,16 +1,15 @@
1
- require "ruby-player"
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
2
 
3
3
  include Player
4
4
  describe Player::Position2d do
5
5
  before do
6
- @client = mock("Client")
7
- @client.stub!(:write)
8
-
6
+ client = mock_client
9
7
  @pos2d = Player::Position2d.new(
10
8
  Player::DevAddr.new(host: 0, robot:0, interface: 4, index: 0),
11
- @client,
12
- :debug
9
+ client
13
10
  )
11
+
12
+ mock_sending_message(@pos2d)
14
13
  end
15
14
 
16
15
  it 'should have default values' do
@@ -139,8 +138,4 @@ describe Player::Position2d do
139
138
  end
140
139
  end
141
140
 
142
- def should_send_message(*args)
143
- @pos2d.should_receive(:send_message)
144
- .with(*args)
145
- end
146
141
  end
@@ -0,0 +1,88 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ include Player
4
+ describe Player::Power do
5
+ before do
6
+ client = mock_client
7
+
8
+ @power = Player::Power.new(
9
+ Player::DevAddr.new(host: 0, robot:0, interface: PLAYER_POWER_CODE, index: 0),
10
+ client
11
+ )
12
+
13
+ mock_sending_message(@power)
14
+ end
15
+
16
+ it 'should have default values' do
17
+ @power.state.should eql(valid: 0, volts: 0.0, percent: 0.0, joules: 0.0, watts: 0.0, charging: 0)
18
+ end
19
+
20
+ it 'should have volts_valid? attribute' do
21
+ @power.should_receive(:state).and_return(valid: 1)
22
+ @power.volts_valid?.should be_true
23
+
24
+ @power.should_receive(:state).and_return(valid: 0)
25
+ @power.volts_valid?.should be_false
26
+ end
27
+
28
+ it 'should have watts_valid? attribute' do
29
+ @power.should_receive(:state).and_return(valid: 2)
30
+ @power.watts_valid?.should be_true
31
+
32
+ @power.should_receive(:state).and_return(valid: 0)
33
+ @power.watts_valid?.should be_false
34
+ end
35
+
36
+ it 'should have joules_valid? attribute' do
37
+ @power.should_receive(:state).and_return(valid: 4)
38
+ @power.joules_valid?.should be_true
39
+
40
+ @power.should_receive(:state).and_return(valid: 0)
41
+ @power.joules_valid?.should be_false
42
+ end
43
+
44
+ it 'should have percent_valid? attribute' do
45
+ @power.should_receive(:state).and_return(valid: 8)
46
+ @power.percent_valid?.should be_true
47
+
48
+ @power.should_receive(:state).and_return(valid: 0)
49
+ @power.percent_valid?.should be_false
50
+ end
51
+
52
+ it 'should have charging_valid? attribute' do
53
+ @power.should_receive(:state).and_return(valid: 16)
54
+ @power.charging_valid?.should be_true
55
+
56
+ @power.should_receive(:state).and_return(valid: 0)
57
+ @power.charging_valid?.should be_false
58
+ end
59
+
60
+ it 'should set charging policy' do
61
+ should_send_message(PLAYER_MSGTYPE_REQ, PLAYER_POWER_REQ_SET_CHARGING_POLICY, [0,1].pack("NN"))
62
+ @power.set_charging_policy(enable_input: false, enable_output: true)
63
+ should_send_message(PLAYER_MSGTYPE_REQ, PLAYER_POWER_REQ_SET_CHARGING_POLICY, [1,0].pack("NN"))
64
+ @power.set_charging_policy(enable_input: true, enable_output: false)
65
+ end
66
+
67
+ it 'should fill power state data' do
68
+ state = {
69
+ valid: 1, volts: 1.0, percent: 2.0,
70
+ joules: 3.0, watts: 4.0, charging: 1
71
+ }
72
+ msg = state.values.pack("NggggN")
73
+ @power.fill(
74
+ Player::Header.from_a([0,0,PLAYER_POWER_CODE,0, PLAYER_MSGTYPE_DATA, PLAYER_POWER_DATA_STATE, 0.0, 0, msg.bytesize]),
75
+ msg
76
+ )
77
+ @power.state.should eql(state)
78
+ end
79
+
80
+ it 'should not puts warn message for ACK subtypes 1' do
81
+ @power.should_not_receive(:unexpected_message)
82
+ @power.handle_response(
83
+ Player::Header.from_a([0,0,PLAYER_POWER_CODE,0, PLAYER_MSGTYPE_RESP_ACK, PLAYER_POWER_REQ_SET_CHARGING_POLICY, 0.0, 0, 0]),
84
+ "")
85
+ end
86
+
87
+ end
88
+
data/spec/ranger_spec.rb CHANGED
@@ -1,16 +1,15 @@
1
- require "ruby-player"
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
2
 
3
3
  include Player
4
4
  describe Player::Ranger do
5
5
  before do
6
- @client = mock("Client")
7
- @client.stub!(:write)
8
-
6
+ client = mock_client
9
7
  @ranger = Player::Ranger.new(
10
8
  Player::DevAddr.new(host: 0, robot:0, interface: PLAYER_RANGER_CODE, index: 0),
11
- @client,
12
- :debug
9
+ client
13
10
  )
11
+
12
+ mock_sending_message(@ranger)
14
13
  end
15
14
 
16
15
  it 'should have default values' do
@@ -150,9 +149,4 @@ describe Player::Ranger do
150
149
  "")
151
150
  end
152
151
  end
153
-
154
- def should_send_message(*args)
155
- @ranger.should_receive(:send_message)
156
- .with(*args)
157
- end
158
152
  end
@@ -0,0 +1,15 @@
1
+ require 'ruby-player'
2
+
3
+ def mock_client
4
+ client = mock("Client")
5
+ client.stub!(:write)
6
+ client.stub!(:log_level).and_return :debug
7
+ client
8
+ end
9
+
10
+ def mock_sending_message(device)
11
+ define_singleton_method(:should_send_message) do |*args|
12
+ device.should_receive(:send_message)
13
+ .with(*args)
14
+ end
15
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-player
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-13 00:00:00.000000000 Z
12
+ date: 2012-02-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: isna
16
- requirement: &5675900 !ruby/object:Gem::Requirement
16
+ requirement: &12972860 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.0.4
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *5675900
24
+ version_requirements: *12972860
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &5675380 !ruby/object:Gem::Requirement
27
+ requirement: &12972000 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '2.7'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *5675380
35
+ version_requirements: *12972000
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &5674800 !ruby/object:Gem::Requirement
38
+ requirement: &12971460 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0.9'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *5674800
46
+ version_requirements: *12971460
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: pry
49
- requirement: &5674360 !ruby/object:Gem::Requirement
49
+ requirement: &12970860 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *5674360
57
+ version_requirements: *12970860
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: yard
60
- requirement: &5673740 !ruby/object:Gem::Requirement
60
+ requirement: &12970220 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *5673740
68
+ version_requirements: *12970220
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: redcarpet
71
- requirement: &5673080 !ruby/object:Gem::Requirement
71
+ requirement: &12969640 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *5673080
79
+ version_requirements: *12969640
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: guard-rspec
82
- requirement: &5672240 !ruby/object:Gem::Requirement
82
+ requirement: &12968920 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,7 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *5672240
90
+ version_requirements: *12968920
91
91
  description: Ruby Player - Ruby client library for Player (tools for robots)
92
92
  email:
93
93
  - atimin@gmail.com
@@ -100,6 +100,7 @@ files:
100
100
  - Gemfile
101
101
  - Guardfile
102
102
  - LICENSE
103
+ - NEWS.md
103
104
  - README.md
104
105
  - Rakefile
105
106
  - TODO.md
@@ -114,12 +115,15 @@ files:
114
115
  - lib/ruby-player/device.rb
115
116
  - lib/ruby-player/header.rb
116
117
  - lib/ruby-player/position2d.rb
118
+ - lib/ruby-player/power.rb
117
119
  - lib/ruby-player/ranger.rb
118
120
  - lib/ruby-player/version.rb
119
121
  - ruby-player.gemspec
120
122
  - spec/client_spec.rb
121
123
  - spec/position2d_spec.rb
124
+ - spec/power_spec.rb
122
125
  - spec/ranger_spec.rb
126
+ - spec/spec_helper.rb
123
127
  homepage: http://www.github.com/flipback/ruby-player
124
128
  licenses: []
125
129
  post_install_message: