Class: Crubyflie::InputReader
- Inherits:
-
Object
- Object
- Crubyflie::InputReader
- Defined in:
- lib/crubyflie/input/input_reader.rb
Overview
This class provides functionality basic to all controllers. Specific controller classes inherit from here.
To read an input we must declare axis and buttons. The axis are analog float readings (range decided by the controller) while the buttons are integer where <= 0 means not pressed and > 0 means pressed.
The reading of the values is implemented by children classes.
The InputReader will also apply the #INPUT_ACTIONS to a given Crazyflie. In order to do that it will go through all the read values and perform actions associated to them, like sending a setpoint, shutting down the connection or altering the calibration.
Direct Known Subclasses
Constant Summary collapse
- INPUT_ACTIONS =
List of current recognized actions that controllers can declare
[:roll, :pitch, :yaw, :thrust, :roll_inc_cal, :roll_dec_cal, :pitch_inc_cal, :pitch_dec_cal, :switch_scaled_output_mode, :switch_xmode, :close_link]
Instance Attribute Summary collapse
-
#axis ⇒ Object
readonly
Returns the value of attribute axis.
-
#axis_readings ⇒ Object
readonly
Returns the value of attribute axis_readings.
-
#button_readings ⇒ Object
readonly
Returns the value of attribute button_readings.
-
#buttons ⇒ Object
readonly
Returns the value of attribute buttons.
-
#xmode ⇒ Object
Returns the value of attribute xmode.
Instance Method Summary collapse
-
#apply_input(crazyflie) ⇒ Object
This will act on current axis readings (by sendint a setpoint to the crazyflie) and on button readings (by, for example, shutting down the link or modifying the calibrations).
-
#initialize(axis, buttons) ⇒ InputReader
constructor
An input is composed by several necessary axis and buttons.
-
#read_input ⇒ Object
Read inputs will call read_axis() on all the declared axis and read_button() on all the declared buttons.
Constructor Details
#initialize(axis, buttons) ⇒ InputReader
An input is composed by several necessary axis and buttons.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/crubyflie/input/input_reader.rb', line 53 def initialize(axis, ) @axis = axis @buttons = @calibrations = {} @xmode = false @output_scale = 0 # off # Calibrate defaults to 0 INPUT_ACTIONS.each do |action| @calibrations[action] = 0 end @axis_readings = {} @button_readings = {} end |
Instance Attribute Details
#axis ⇒ Object (readonly)
Returns the value of attribute axis.
44 45 46 |
# File 'lib/crubyflie/input/input_reader.rb', line 44 def axis @axis end |
#axis_readings ⇒ Object (readonly)
Returns the value of attribute axis_readings.
44 45 46 |
# File 'lib/crubyflie/input/input_reader.rb', line 44 def axis_readings @axis_readings end |
#button_readings ⇒ Object (readonly)
Returns the value of attribute button_readings.
44 45 46 |
# File 'lib/crubyflie/input/input_reader.rb', line 44 def @button_readings end |
#buttons ⇒ Object (readonly)
Returns the value of attribute buttons.
44 45 46 |
# File 'lib/crubyflie/input/input_reader.rb', line 44 def @buttons end |
#xmode ⇒ Object
Returns the value of attribute xmode.
45 46 47 |
# File 'lib/crubyflie/input/input_reader.rb', line 45 def xmode @xmode end |
Instance Method Details
#apply_input(crazyflie) ⇒ Object
This will act on current axis readings (by sendint a setpoint to the crazyflie) and on button readings (by, for example, shutting down the link or modifying the calibrations). If the link to the crazyflie is down, it will not send anything.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/crubyflie/input/input_reader.rb', line 102 def apply_input(crazyflie) return if !crazyflie.active? setpoint = { :roll => nil, :pitch => nil, :yaw => nil, :thrust => nil } @button_readings.each do |action, value| case action when :roll setpoint[:roll] = value when :pitch setpoint[:pitch] = value when :yaw setpoint[:yaw] = value when :thrust setpoint[:thrust] = value when :roll_inc_cal @calibrations[:roll] += 1 when :roll_dec_cal @calibrations[:roll] -= 1 when :pitch_inc_cal @calibrations[:pitch] += 1 when :pitch_dec_cal @calibrations[:pitch] -= 1 when :switch_xmode @xmode = !@xmode if value > 0 logger.info("Xmode is #{@xmode}") if value > 0 when :switch_scaled_output_mode if value > 0 && @output_scale == 0 logger.info("Scaling output: x#{value}") @output_scale = value.to_f elsif value > 0 && @output_scale > 0 logger.info("Scaling output disabled") @output_scale = 0 end when :close_link crazyflie.close_link() if value > 0 end end return if !crazyflie.active? @axis_readings.each do |action, value| case action when :roll setpoint[:roll] = value when :pitch setpoint[:pitch] = value when :yaw setpoint[:yaw] = value when :thrust setpoint[:thrust] = value end end setpoint.keys().each do |k| next if k == :thrust setpoint[k] *= @output_scale end if @output_scale > 0 pitch = setpoint[:pitch] roll = setpoint[:roll] yaw = setpoint[:yaw] thrust = setpoint[:thrust] if pitch && roll && yaw && thrust m = "Sending R: #{roll} P: #{pitch} Y: #{yaw} T: #{thrust}" logger.debug(m) crazyflie.commander.send_setpoint(roll, pitch, yaw, thrust, @xmode) end end |
#read_input ⇒ Object
Read inputs will call read_axis() on all the declared axis and read_button() on all the declared buttons. After obtaining the reading, it will apply calibrations to the result. Apply the read values with #apply_input
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/crubyflie/input/input_reader.rb', line 73 def read_input poll() # In case we need to poll the device actions_to_axis = @axis.invert() actions_to_axis.each do |action, axis_id| if !INPUT_ACTIONS.include?(action) logger.error("Unknown action #{action}. Skipping") next end @axis_readings[action] = read_axis(axis_id) @axis_readings[action] += @calibrations[action] end = @buttons.invert() .each do |action, | if !INPUT_ACTIONS.include?(action) logger.error("Unknown action #{action}. Skipping") next end @button_readings[action] = () @button_readings[action] += @calibrations[action] end end |