class Crubyflie::Param

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

Attributes

toc[R]

Public Class Methods

new(crazyflie) click to toggle source

Initialize the parameter facility @param crazyflie [Crazyflie]

# File lib/crubyflie/crazyflie/param.rb, line 118
def initialize(crazyflie)
    @crazyflie = crazyflie
    @in_queue = crazyflie.crtp_queues[:param]
    @toc = TOC.new(@crazyflie.cache_folder, ParamTOCElement)
end

Public Instance Methods

get_value(name) { |value| ... } click to toggle source

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. @param name [String] a name in the form group.name @param block [Proc] a block to be called with the value as argument

# File lib/crubyflie/crazyflie/param.rb, line 173
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
Also aliased as: request_param_update
refresh_toc() click to toggle source

Refreshes the TOC. It only returns when it is finished

# File lib/crubyflie/crazyflie/param.rb, line 125
def refresh_toc
    channel = TOC_CHANNEL
    port = Crazyflie::CRTP_PORTS[:param]
    @toc.fetch_from_crazyflie(@crazyflie, port, @in_queue)
end
request_param_update(name, &block)
Alias for: get_value
set_value(name, value) { |response| ... } click to toggle source

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. @param name [String] parameter group.name @param value [Numeric] a value. It must be packable as binary data,

the type being set in the params TOC

@param block [Proc] an optional block that will be called with the

response CRTPPacket received. Otherwise will
log to debug
# File lib/crubyflie/crazyflie/param.rb, line 140
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

Private Instance Methods

wait_for_response() click to toggle source
# File lib/crubyflie/crazyflie/param.rb, line 205
def wait_for_response
    begin
        wait = WAIT_PACKET_TIMEOUT
        response = timeout(wait, WaitTimeoutException) do
            @in_queue.pop()
        end
        return response
    rescue WaitTimeoutException
        logger.error("Param: waited too long to get response")
        return nil
    end
end