Class: Crubyflie::Crazyflie

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

Overview

This is the main entry point for interacting with a Crazyflie It allows to connect to the Crazyflie on the specified radio URL and make use of the different facilities: log, param, commander and console. Facilities are instantiated and packages delivered to their queues from here. For example, you can use the @crazyflie.commander.set_sendpoint() to send a new setpoint to a Crazyflie

Constant Summary collapse

CALLBACKS =

Groups of callbacks available

[
 :received_packet,
 :disconnected,
 :connection_failed,
 :connection_initiated,
 :connection_setup_finished,
 :link
]

Constants included from CRTPConstants

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

logger, #logger, #logger=

Constructor Details

#initialize(cache_folder = nil) ⇒ Crazyflie

Initialize a Crazyflie by registering default received-packet callbacks and intializing Queues for every facility. Packets will be queued for each facility depending on their port

Parameters:

  • cache_folder (String) (defaults to: nil)

    folder path to store logging TOC cache



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/crubyflie/crazyflie.rb', line 58

def initialize(cache_folder=nil)
    @cache_folder = cache_folder
    # Callbacks will fire in some specific situations
    # Specially when receiving packages
    @callbacks = {}
    CALLBACKS.each do |cb|
        @callbacks[cb] = {}
    end
    register_default_callbacks()

    @crtp_queues = {}
    CRTP_PORTS.keys().each do |k|
        @crtp_queues[k] = Queue.new
    end

    # Thread that regularly checks for packages
    @receive_packet_thread = nil
    @retry_packets_thread = nil

    # A hash with keys "port_<portnumber>"
    @retry_packets = {}

    @commander = Commander.new(self)
    @console   = Console.new(self)
    @param     = Param.new(self)
    @log       = Log.new(self)

    @link = nil
end

Instance Attribute Details

#cache_folderObject (readonly)

Returns the value of attribute cache_folder.



52
53
54
# File 'lib/crubyflie/crazyflie.rb', line 52

def cache_folder
  @cache_folder
end

#callbacksObject

Returns the value of attribute callbacks.



51
52
53
# File 'lib/crubyflie/crazyflie.rb', line 51

def callbacks
  @callbacks
end

#commanderObject (readonly)

Returns the value of attribute commander.



52
53
54
# File 'lib/crubyflie/crazyflie.rb', line 52

def commander
  @commander
end

#consoleObject (readonly)

Returns the value of attribute console.



52
53
54
# File 'lib/crubyflie/crazyflie.rb', line 52

def console
  @console
end

#crtp_queuesObject (readonly)

Returns the value of attribute crtp_queues.



53
54
55
# File 'lib/crubyflie/crazyflie.rb', line 53

def crtp_queues
  @crtp_queues
end

Returns the value of attribute link.



53
54
55
# File 'lib/crubyflie/crazyflie.rb', line 53

def link
  @link
end

#logObject (readonly)

Returns the value of attribute log.



52
53
54
# File 'lib/crubyflie/crazyflie.rb', line 52

def log
  @log
end

#paramObject (readonly)

Returns the value of attribute param.



52
53
54
# File 'lib/crubyflie/crazyflie.rb', line 52

def param
  @param
end

Instance Method Details

#active?TrueClass, FalseClass

Checks if there is an open link

Returns:

  • (TrueClass, FalseClass)

    true if there is an open link



136
137
138
# File 'lib/crubyflie/crazyflie.rb', line 136

def active?
    return !@link.nil?
end

Close the link and clean up Attemps to disconnect from the crazyflie.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/crubyflie/crazyflie.rb', line 117

def close_link
    @commander.send_setpoint(0,0,0,0) if @link
    sleep 0.05
    uri = @link ? @link.uri.to_s : "nowhere"
    @link.disconnect(force=true) if @link
    @link = nil
    @receive_packet_thread.kill() if @receive_packet_thread
    @receive_packet_thread = nil
    @retry_packets_thread.kill() if @retry_packets_thread
    @log.stop_packet_reader_thread()
    @retry_packets.clear()
    @crtp_queues.each do |k,q|
        q.clear()
    end
    call_cb(:disconnected, uri)
end

Connect to a Crazyflie using the radio driver

Parameters:

  • uri (String)

    radio uri to connect to



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/crubyflie/crazyflie.rb', line 91

def open_link(uri)
    call_cb(:connection_initiated, uri)
    begin
        @link = RadioDriver.new()
        link_cbs = {
            :link_quality_cb => @callbacks[:link][:quality],
            :link_error_cb  => @callbacks[:link][:error]
        }

        @link.connect(uri, link_cbs)
        @callbacks[:received_packet][:connected] = Proc.new do |packet|
            logger.info "Connected!"
            @callbacks[:received_packet].delete(:connected)
        end
        receive_packet_thread()
        sleep 0.5 # Allow setup and failures
        setup_connection() if @link
    rescue Exception
        #logger.warn $!.backtrace.join("\n")
        call_cb(:connection_failed, $!.message)
        close_link()
    end
end

#scan_interfaceArray

Calls #RadioDriver::scan_interface()

Returns:

  • (Array)

    List of radio URIs where a crazyflie was found



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/crubyflie/crazyflie.rb', line 142

def scan_interface
    if @link
        logger.error "Cannot scan when link is open. Disconnect first"
        return []
    end
    begin
        RadioDriver.new().scan_interface()
    rescue Exception
        logger.error("Cannot scan interfaces: #{$!}")
        return []
    end
end

#send_packet(packet, expect_answer = false) ⇒ Object

Send a packet

Parameters:

  • packet (CRTPPacket)

    packet to send

  • expect_answer (TrueClass, FalseClass) (defaults to: false)

    if set to true, a timer will be set up to resend the package if no response has been received in 0.1secs



162
163
164
165
166
# File 'lib/crubyflie/crazyflie.rb', line 162

def send_packet(packet, expect_answer=false)
    return if @link.nil?
    @link.send_packet(packet)
    setup_retry(packet) if expect_answer
end