class Crubyflie::InputReader
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.
Constants
- INPUT_ACTIONS
List of current recognized actions that controllers can declare
Attributes
Public Class Methods
An input is composed by several necessary axis and buttons. @param axis [Hash] A hash of keys identifying axis IDs
(the controller should know to what the ID maps), and values from #INPUT_ACTIONS
@param buttons [Hash] A hash of keys identifying button IDs (the
controller should know to what the ID maps), and values from #INPUT_ACTIONS
# File lib/crubyflie/input/input_reader.rb, line 52 def initialize(axis, buttons) @axis = axis @buttons = buttons @calibrations = {} @xmode = false @hover = false @output_scale = 0 # off # Calibrate defaults to 0 INPUT_ACTIONS.each do |action| @calibrations[action] = 0 end @axis_readings = {} @button_readings = {} end
Public Instance Methods
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. @param crazyflie [Crazyflie] A crazyflie instance to send the
setpoint to.
# 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, :hover => 0 } set_althold = false @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 :hover if value > 0 @hover = !@hover logger.info("Hover is #{@hover}") set_althold = true end 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 if set_althold crazyflie.param.set_value('flightmode.althold', @hover ? 1 : 0) end end
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
# 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 actions_to_buttons = @buttons.invert() actions_to_buttons.each do |action, button_id| if !INPUT_ACTIONS.include?(action) logger.error("Unknown action #{action}. Skipping") next end @button_readings[action] = read_button(button_id) @button_readings[action] += @calibrations[action] end end
Private Instance Methods
# File lib/crubyflie/input/input_reader.rb, line 199 def poll end
# File lib/crubyflie/input/input_reader.rb, line 191 def read_axis(axis_id) raise Exception.new("Not implemented!") end