Class: Crubyflie::InputReader

Inherits:
Object
  • Object
show all
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 actioins associated to them, like sending a setpoint or shutting down the connection or altering the calibration.

Direct Known Subclasses

Joystick

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_xmode,  :close_link]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(axis, buttons) ⇒ InputReader

An input is composed by several necessary axis, buttons and calibrations.

Parameters:

  • axis (Hash)

    A hash of keys identifying axis IDs (the controller should know to what the

    ID maps, and values from #INPUT_ACTIONS
    
  • buttons (Hash)

    A hash of keys identifying button IDs (the controller should know to what the ID maps, and values from #INPUT_ACTIONS



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/crubyflie/input/input_reader.rb', line 53

def initialize(axis, buttons)
    @axis = axis
    @buttons = buttons
    @calibrations = {}
    @xmode = false

    # Calibrate defaults to 0
    INPUT_ACTIONS.each do |action|
        @calibrations[action] = 0
    end

    @axis_readings = {}
    @button_readings = {}
end

Instance Attribute Details

#axisObject (readonly)

Returns the value of attribute axis.



43
44
45
# File 'lib/crubyflie/input/input_reader.rb', line 43

def axis
  @axis
end

#axis_readingsObject (readonly)

Returns the value of attribute axis_readings.



43
44
45
# File 'lib/crubyflie/input/input_reader.rb', line 43

def axis_readings
  @axis_readings
end

#button_readingsObject (readonly)

Returns the value of attribute button_readings.



43
44
45
# File 'lib/crubyflie/input/input_reader.rb', line 43

def button_readings
  @button_readings
end

#buttonsObject (readonly)

Returns the value of attribute buttons.



43
44
45
# File 'lib/crubyflie/input/input_reader.rb', line 43

def buttons
  @buttons
end

#xmodeObject

Returns the value of attribute xmode.



44
45
46
# File 'lib/crubyflie/input/input_reader.rb', line 44

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.

Parameters:

  • crazyflie (Crazyflie)

    A crazyflie instance to send the setpoint to.



93
94
95
96
97
98
99
100
101
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
# File 'lib/crubyflie/input/input_reader.rb', line 93

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

    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_inputObject

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/crubyflie/input/input_reader.rb', line 72

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|
        @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|
        @button_readings[action] = read_button(button_id)
        @button_readings[action] += @calibrations[action]
    end
end