[SOLVED] Logging accelerometer values

Post here to get support
marcus
Bitcraze
Posts: 659
Joined: Mon Jan 28, 2013 7:02 pm
Location: Sweden
Contact:

Re: Logging accelerometer values

Post 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()
erget
Beginner
Posts: 29
Joined: Thu Apr 04, 2013 6:10 am

Re: Logging accelerometer values

Post 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 :)
marcus
Bitcraze
Posts: 659
Joined: Mon Jan 28, 2013 7:02 pm
Location: Sweden
Contact:

Re: Logging accelerometer values

Post by marcus »

Great that it's working :D
legalnonsense
Beginner
Posts: 9
Joined: Sun Jul 21, 2013 5:30 pm

Re: [SOLVED] Logging accelerometer values

Post 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?
legalnonsense
Beginner
Posts: 9
Joined: Sun Jul 21, 2013 5:30 pm

Re: [SOLVED] Logging accelerometer values

Post 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
tobias
Bitcraze
Posts: 2339
Joined: Mon Jan 28, 2013 7:17 pm
Location: Sweden

Re: [SOLVED] Logging accelerometer values

Post 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?
legalnonsense
Beginner
Posts: 9
Joined: Sun Jul 21, 2013 5:30 pm

Re: [SOLVED] Logging accelerometer values

Post 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.
marcus
Bitcraze
Posts: 659
Joined: Mon Jan 28, 2013 7:02 pm
Location: Sweden
Contact:

Re: [SOLVED] Logging accelerometer values

Post 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.
Attachments
cflie.zip
Clean build of 49:669962cac073
(33.38 KiB) Downloaded 275 times
Post Reply