Class: Player::Power

Inherits:
Device show all
Defined in:
lib/ruby-player/power.rb

Overview

The power interface provides access to a robot’s power subsystem

Instance Attribute Summary collapse

Attributes inherited from Device

#addr

Instance Method Summary collapse

Methods inherited from Device

#send_message

Constructor Details

#initialize(addr, client) ⇒ Power

Returns a new instance of Power.



36
37
38
39
# File 'lib/ruby-player/power.rb', line 36

def initialize(addr, client)
  super
  @state = { valid: 0, volts: 0.0, percent: 0.0, joules: 0.0, watts: 0.0, charging: 0 }
end

Instance Attribute Details

#stateHash (readonly)

Power state

: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.

:volts Battery voltage [V].

:percent Percent of full charge [%].

:joules Energy stored [J].

:watts Estimated current energy consumption (negative values) or aquisition (positive values) [W].

:charging Charge exchange status: if 1, the device is currently receiving charge from another energy device

Returns:

  • (Hash)

    state



34
35
36
# File 'lib/ruby-player/power.rb', line 34

def state
  @state
end

Instance Method Details

#charging_valid?Boolean

Check charging valid

Returns:

  • (Boolean)


104
105
106
# File 'lib/ruby-player/power.rb', line 104

def charging_valid?
  state[:valid].to_i & PLAYER_POWER_MASK_CHARGING > 0
end

#fill(hdr, msg) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/ruby-player/power.rb', line 108

def fill(hdr, msg)
  case hdr.subtype
  when PLAYER_POWER_DATA_STATE
    read_state(msg)
  else
    undexpected_message hdr
  end
end

#handle_response(hdr, msg) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/ruby-player/power.rb', line 117

def handle_response(hdr, msg)
  case hdr.subtype
  when PLAYER_POWER_REQ_SET_CHARGING_POLICY
    nil
  else
    undexpected_message hdr
  end
end

#joulesObject

Energy stored [J].

See Also:



58
59
60
# File 'lib/ruby-player/power.rb', line 58

def joules
  state[:joules]
end

#joules_valid?Boolean

Check joules valid

Returns:

  • (Boolean)


94
95
96
# File 'lib/ruby-player/power.rb', line 94

def joules_valid?
  state[:valid].to_i & PLAYER_POWER_MASK_JOULES > 0
end

#percentObject

Percent of full charge [%].

See Also:



51
52
53
# File 'lib/ruby-player/power.rb', line 51

def percent
  state[:percent]
end

#percent_valid?Boolean

Check perecent valid

Returns:

  • (Boolean)


99
100
101
# File 'lib/ruby-player/power.rb', line 99

def percent_valid?
  state[:valid].to_i & PLAYER_POWER_MASK_PERCENT > 0
end

#set_charging_policy(policy = {}) ⇒ Power

Request to change the charging policy

Parameters:

  • policy (Hash) (defaults to: {})

Options Hash (policy):

  • :enable_input (Boolean)

    boolean controlling recharging

  • :enable_output (Boolean)

    bolean controlling whether others can recharge from this device

Returns:



74
75
76
77
78
79
80
81
# File 'lib/ruby-player/power.rb', line 74

def set_charging_policy(policy={})
  data = [
    policy[:enable_input] ? 1 : 0,
    policy[:enable_output] ? 1 : 0
  ]
  send_message(PLAYER_MSGTYPE_REQ, PLAYER_POWER_REQ_SET_CHARGING_POLICY, data.pack("NN"))
  self
end

#voltsObject

Battery voltage [V].

See Also:



44
45
46
# File 'lib/ruby-player/power.rb', line 44

def volts
  state[:volts]
end

#volts_valid?Boolean

Check volts valid

Returns:

  • (Boolean)


84
85
86
# File 'lib/ruby-player/power.rb', line 84

def volts_valid?
  state[:valid].to_i & PLAYER_POWER_MASK_VOLTS > 0
end

#wattsObject

Estimated current energy consumption (negative values) or aquisition (positive values) [W].

See Also:



65
66
67
# File 'lib/ruby-player/power.rb', line 65

def watts
  state[:watts]
end

#watts_valid?Boolean

Check watts valid

Returns:

  • (Boolean)


89
90
91
# File 'lib/ruby-player/power.rb', line 89

def watts_valid?
  state[:valid].to_i & PLAYER_POWER_MASK_WATTS > 0
end