Help threading basiclog

Firmware/software/electronics/mechanics
Post Reply
GeEom
Beginner
Posts: 7
Joined: Wed Dec 17, 2014 7:19 pm

Help threading basiclog

Post by GeEom »

Hello everyone!

I've been trying to interface the crazyflie 2.0 with other software.
To begin with I've tried to thread the basiclog example script. I want to store incoming 'flie data in one thread, allowing the main thread to display/pass on/work with the data in real time.

At present the main returning is ending my thread, halting the crazyflie communication. A simple sleep loop propping open the main does not destroy the thread, but does defeat its purpose.

Whilst I'm not experienced in python, I've made a simple numeric calculation persist in a nondaemon thread beyond the main, and then referenced its output. This leads me to believe that there is something in the crazyflie libraries interfering with me.

Any insight would be much appreciated!

My code follows, it's a simple variant of basiclog.py from the client examples

Code: Select all

import sys
sys.path.append("C:\Users\David\Desktop\crazyflie-clients-python\lib")
import cflib.crtp
cflib.crtp.init_drivers(enable_debug_driver=False)
available = cflib.crtp.scan_interfaces()
print "Scan found:"
for i in available:
    print i[0]

import threading, Queue, time, logging
from threading import Thread
from threading import Timer
from cfclient.utils.logconfigreader import LogConfig
from cflib.crazyflie import Crazyflie
queue = Queue.Queue(1)

class LoggingExample(threading.Thread):
    def __init__(self, link_uri):
        super(LoggingExample, self).__init__()
        self.daemon = False

        global queue

        import sys
        sys.path.append("C:\Users\David\Desktop\crazyflie-clients-python\lib")
        import threading, Queue, time, logging, cflib.crtp
        from threading import Timer
        from cfclient.utils.logconfigreader import LogConfig
        from cflib.crazyflie import Crazyflie
        logging.basicConfig(level=logging.ERROR)

        self._cf = Crazyflie()
        self._cf.connected.add_callback(self._connected)
        self._cf.disconnected.add_callback(self._disconnected)
        self._cf.connection_failed.add_callback(self._connection_failed)
        self._cf.open_link(link_uri)
        self.is_connected = True

    def _connected(self, link_uri):
        self._lg_stab = LogConfig(name="Stabilizer", period_in_ms=10)
        self._lg_stab.add_variable("stabilizer.roll", "float")
        self._lg_stab.add_variable("stabilizer.pitch", "float")
        self._lg_stab.add_variable("stabilizer.yaw", "float")
        self._cf.log.add_config(self._lg_stab)
        if self._lg_stab.valid:
            self._lg_stab.data_received_cb.add_callback(self._stab_log_data)
            self._lg_stab.start()

    def _stab_log_data(self, timestamp, data, logconf):
        try:
            task = queue.put("[%d][%s]: %s" % (timestamp, logconf.name, data),False)
            queue.task_done()
        except Queue.Full:
            pass

    def _connection_failed(self, link_uri, msg):
        self.is_connected = False

    def _disconnected(self, link_uri):
        self.is_connected = False

LoggingExample(available[0][0])
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Help threading basiclog

Post by arnaud »

Hi,

The crayflie lib launches a brunch of threads in the background and all of them are marked as "daemon", this means that you have to keep at least one non-deamon thread alive, sleeping can actually be a viable solution sometime.

The log callbacks are called from the packet dispatcher thread. So it means that you should not lock in a callback but writing to file should be ok. In your example the function _stab_log_data is not run from your thread but from the packet dispatcher thread.

For example you can look in the zmq server we made recently: https://github.com/bitcraze/crazyflie-c ... b/cfzmq.py, it stays alive thanks to 2 threads that have "while True:" in there run function. By the way this zmq server is made allow easy communication with third party software so it could be interesting for you.

I hope I answered your question.
BR
Arnaud
qubitter
Beginner
Posts: 28
Joined: Wed Mar 11, 2015 8:47 pm

Re: Help threading basiclog

Post by qubitter »

I'm trying to get the API set up on my MacBook Air 2014 and I keep getting the error that I am missing the Queue module. The script is just an order to import all of crtp and this is what I get:

Traceback (most recent call last):
File "/Users/me/Documents/cflibim.py", line 1, in <module>
from cflib import crtp
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/cflib/crtp/__init__.py", line 38, in <module>
from .radiodriver import RadioDriver
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/cflib/crtp/radiodriver.py", line 46, in <module>
import Queue
ImportError: No module named 'Queue

This is the error code from IDLE. Please help! :(
whoenig
Expert
Posts: 395
Joined: Mon Oct 27, 2014 2:55 am

Re: Help threading basiclog

Post by whoenig »

The official SDK uses Python 2.7.x. You can install python2 besides python3 and use python2 instead of python3.

Python3 changed a bunch of things in a non-compatible way. In this case they renamed Queue to queue (i.e. lowercase). I would expect that there are more such issues in the code, hence it is easier to just use python2 instead.
Post Reply