Page 2 of 2

Re: Logging accelerometer values

Posted: Tue May 28, 2013 7:20 pm
by marcus
Hi,

Yeah, the example isn't that well illustrated :(

The reason for the error message is that you are creating the log configuration before you have connected and at this point you do not know what is in the TOC. If you try to add a log configuration then you will not be able to since it's not possible to verify that the variables are in the TOC. In the example on the wiki the log configuration is created in the connected callback and at this point the TOC has been downloaded so the configuration can be verified when it's created.

After the log configuration has been successfully created and you are connected the threads in the cflib will do callbacks to the selected function each time new data is received, you do not have to poll for it.

Here's a quick rewrite of the code (untested and dirty) but hopefully it shows the concept a bit better:

Code: Select all

import cflib.crtp
from cfclient.utils.logconfigreader import LogConfig
from cfclient.utils.logconfigreader import LogVariable
from cflib.crazyflie import Crazyflie

class Main:
    def __init__(self):
        self.crazyflie = Crazyflie()
        cflib.crtp.init_drivers()

        self.crazyflie.connectSetupFinished.add_callback(
                                                    self.connectSetupFinished)

        self.crazyflie.open_link("radio://0/10/250K")

    def connectSetupFinished(self, linkURI):
      # Set accelerometer logging config
        accel_log_conf = LogConfig("Accel", 10)
        accel_log_conf.addVariable(LogVariable("acc.x", "float"))
        accel_log_conf.addVariable(LogVariable("acc.y", "float"))
        accel_log_conf.addVariable(LogVariable("acc.z", "float"))

        # Now that the connection is established, start logging
        self.accel_log = self.crazyflie.log.create_log_packet(accel_log_conf)

        if self.accel_log is not None:
            self.accel_log.dataReceived.addCallback(log_accel_data)
            self.accel_log.start()
        else:
            print("acc.x/y/z not found in log TOC")

    def log_accel_data(self, data):
        logging.info("Accelerometer: x=%.2f, y=%.2f, z=%.2f" %
                     (data["acc.x"], data["acc.y"], data["acc.z"]))

Main()

Re: Logging accelerometer values

Posted: Wed May 29, 2013 4:58 pm
by erget
Thanks! That worked, albeit with some spelling changes. I've added some comments, this is the working version:

Code: Select all

import logging

import cflib.crtp
from cfclient.utils.logconfigreader import LogConfig
from cfclient.utils.logconfigreader import LogVariable
from cflib.crazyflie import Crazyflie

logging.basicConfig(level=logging.DEBUG)


class Main:
    """
    Class is required so that methods can access the object fields.
    """
    def __init__(self):
        """
        Connect to Crazyflie, initialize drivers and set up callback.

        The callback takes care of logging the accelerometer values.
        """
        self.crazyflie = Crazyflie()
        cflib.crtp.init_drivers()

        self.crazyflie.connectSetupFinished.add_callback(
                                                    self.connectSetupFinished)

        self.crazyflie.open_link("radio://0/10/250K")

    def connectSetupFinished(self, linkURI):
        """
        Configure the logger to log accelerometer values and start recording.

        The logging variables are added one after another to the logging
        configuration. Then the configuration is used to create a log packet
        which is cached on the Crazyflie. If the log packet is None, the
        program exits. Otherwise the logging packet receives a callback when
        it receives data, which prints the data from the logging packet's
        data dictionary as logging info.
        """
        # Set accelerometer logging config
        accel_log_conf = LogConfig("Accel", 10)
        accel_log_conf.addVariable(LogVariable("acc.x", "float"))
        accel_log_conf.addVariable(LogVariable("acc.y", "float"))
        accel_log_conf.addVariable(LogVariable("acc.z", "float"))

        # Now that the connection is established, start logging
        self.accel_log = self.crazyflie.log.create_log_packet(accel_log_conf)

        if self.accel_log is not None:
            self.accel_log.dataReceived.add_callback(self.log_accel_data)
            self.accel_log.start()
        else:
            print("acc.x/y/z not found in log TOC")

    def log_accel_data(self, data):
        logging.info("Accelerometer: x=%.2f, y=%.2f, z=%.2f" %
                     (data["acc.x"], data["acc.y"], data["acc.z"]))

Main()

Thanks a bunch! This makes my day :)

Re: Logging accelerometer values

Posted: Thu May 30, 2013 8:30 am
by marcus
Great that it's working :D

Re: [SOLVED] Logging accelerometer values

Posted: Tue Aug 13, 2013 2:54 am
by legalnonsense
The posted solution is not working for me. When i run the code I eventually get:
INFO:cflib.crazyflie:Callback->Connection setup finished [radio://0/10/250K]
WARNING:cflib.crazyflie.toc:Unable to find variable [acc.x]
WARNING:cflib.crazyflie.log:Log: acc.x not in TOC, this block cannot be used!
acc.x/y/z not found in log TOC
I am not even going to hazard a guess as to what is going wrong. Anyone?

Re: [SOLVED] Logging accelerometer values

Posted: Tue Aug 13, 2013 4:18 am
by legalnonsense
ok, I've got logging working for the stabilizer data.

accelerometer data does not appear in the log TOC through the cfclient

I must be missing something, as acc.x through z appear in stabilizer.c as described in the wiki

Re: [SOLVED] Logging accelerometer values

Posted: Tue Aug 13, 2013 6:49 am
by tobias
It's probably because the accelerometer values are not "exposed" from the Crazyflie firmware. If I recall correctly the latest stable firmware version (2013.4) doesn't expose the accelerometer values. What firmware version are you running?

Re: [SOLVED] Logging accelerometer values

Posted: Tue Aug 13, 2013 5:43 pm
by legalnonsense
I have not touched the firmware since purchase but I will check out this evening. Might have been looking at the wrong source code if it's not.

Re: [SOLVED] Logging accelerometer values

Posted: Wed Sep 11, 2013 3:35 pm
by marcus
legalnonsense wrote:I have not touched the firmware since purchase but I will check out this evening. Might have been looking at the wrong source code if it's not.
If you connect to the Crazyflie and open the Log TOC tab it will list what variables you are able to log. There's no firmware release that contains the accelerometer as loggable yet, but I've attached a build of the latest firmware that you can use to log it. Just un-zip it and use the cfclient to flash it.