Class: Crubyflie::Param

Inherits:
Object
  • Object
show all
Includes:
CRTPConstants, Logging
Defined in:
lib/crubyflie/crazyflie/param.rb

Overview

The parameter facility. Used to retrieve the table of contents, set the value of a parameter and read the value of a parameter

Constant Summary

Constants included from CRTPConstants

CRTPConstants::CMD_APPEND_BLOCK, CRTPConstants::CMD_CREATE_BLOCK, CRTPConstants::CMD_DELETE_BLOCK, CRTPConstants::CMD_RESET_LOGGING, CRTPConstants::CMD_START_LOGGING, CRTPConstants::CMD_STOP_LOGGING, CRTPConstants::CMD_TOC_ELEMENT, CRTPConstants::CMD_TOC_INFO, CRTPConstants::CRTP_PORTS, CRTPConstants::LOG_DATA_CHANNEL, CRTPConstants::LOG_SETTINGS_CHANNEL, CRTPConstants::PARAM_READ_CHANNEL, CRTPConstants::PARAM_WRITE_CHANNEL, CRTPConstants::TOC_CHANNEL, CRTPConstants::WAIT_PACKET_TIMEOUT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

logger, #logger, #logger=

Constructor Details

#initialize(crazyflie) ⇒ Param

Initialize the parameter facility

Parameters:



119
120
121
122
123
# File 'lib/crubyflie/crazyflie/param.rb', line 119

def initialize(crazyflie)
    @crazyflie = crazyflie
    @in_queue = crazyflie.crtp_queues[:param]
    @toc = TOC.new(@crazyflie.cache_folder, ParamTOCElement)
end

Instance Attribute Details

#tocObject (readonly)

Returns the value of attribute toc.



116
117
118
# File 'lib/crubyflie/crazyflie/param.rb', line 116

def toc
  @toc
end

Instance Method Details

#get_value(name, &block) {|value| ... } ⇒ Object Also known as: request_param_update

Request an update for a parameter and call the provided block with the value of the parameter. This call will block until the response has been received or the #CRTPConstants::WAIT_PACKET_TIMEOUT is reached.

Parameters:

  • name (String)

    a name in the form group.name

  • block (Proc)

    a block to be called with the value as argument

Yields:

  • (value)


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/crubyflie/crazyflie/param.rb', line 174

def get_value(name, &block)
    element = @toc[name]
    if element.nil?
        logger.error "Cannot update #{name}, not in TOC"
        return
    end
    packet = CRTPPacket.new()
    packet.modify_header(nil, CRTP_PORTS[:param],
                         PARAM_READ_CHANNEL)
    packet.data = [element.ident]
    @in_queue.clear()
    @crazyflie.send_packet(packet, true)

    # Pop packages until mine comes up
    begin
        response = wait_for_response()
        return if response.nil?

        ident = response.data()[0]
        if ident != element.ident()
            mesg = "Value expected for element with ID #{element.ident}"
            mesg << " but got for element with ID #{ident}. Requeing"
            logger.debug(mesg)
        end
    end until ident == element.ident()

    value = response.data_repack()[1..-1]
    value = value.unpack(element.directive).first
    yield(value)
end

#refresh_tocObject

Refreshes the TOC. It only returns when it is finished



126
127
128
129
130
# File 'lib/crubyflie/crazyflie/param.rb', line 126

def refresh_toc
    channel = TOC_CHANNEL
    port = Crazyflie::CRTP_PORTS[:param]
    @toc.fetch_from_crazyflie(@crazyflie, port, @in_queue)
end

#set_value(name, value, &block) ⇒ Object

Set the value of a paremeter. This call will only return after a ack response has been received, or will timeout if #CRTPConstants::WAIT_PACKET_TIMEOUT is reached.

Parameters:

  • name (String)

    parameter group.name

  • value (Numeric)

    a value. It must be packable as binary data, the type being set in the params TOC

  • block (Proc)

    an optional block that will be called with the response CRTPPacket received. Otherwise will log to debug



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
# File 'lib/crubyflie/crazyflie/param.rb', line 141

def set_value(name, value, &block)
    element = @toc[name]
    if element.nil?
        logger.error "Param #{name} not in TOC!"
        return
    end

    ident = element.ident
    packet = CRTPPacket.new()
    packet.modify_header(nil, CRTP_PORTS[:param],
                         PARAM_WRITE_CHANNEL)
    packet.data = [ident]
    packet.data += [value].pack(element.directive()).unpack('C*')
    @in_queue.clear()
    @crazyflie.send_packet(packet, true) # expect answer

    response = wait_for_response()
    return if response.nil?

    if block_given?
        yield response
    else
        mesg = "Got answer to setting param '#{name}' with '#{value}'"
        logger.debug(mesg)
    end
end