[SOLVED] Logging accelerometer values

Post here to get support
erget
Beginner
Posts: 29
Joined: Thu Apr 04, 2013 6:10 am

[SOLVED] Logging accelerometer values

Post by erget »

Hi all,

My Crazyflie arrived and I've flashed the newest firmware - worked like a charm :) I'm looking forward to soldering everything together on the weekend.

I have a question about the Crazyflie Client. I notice that it prints out pitch, roll, yaw, and I'll bet that it's pretty easy to log that stuff as well, although I haven't tried it yet. It's been fun just looking at the flight data as I wave the board around, even though the propellors aren't attached yet ;) However, I'd like to be able to access not only the gyroscope values but also the accelerometer values - forward/back, left/right, up/down. Is there a way of getting those too?

Sorry if this is a dumb question, I haven't done more than just glance over the library yet but I am very curious about it. I'm going to be reading the API in the train tomorrow (looks nicely documented, that makes my day! :) )

Best,
Daniel
Last edited by erget on Thu May 23, 2013 3:50 pm, edited 2 times in total.
tobias
Bitcraze
Posts: 2339
Joined: Mon Jan 28, 2013 7:17 pm
Location: Sweden

Re: Logging accelerometer values

Post by tobias »

The logging framework is not really working yet but when it is this will be a piece of cake ;)
marcus
Bitcraze
Posts: 659
Joined: Mon Jan 28, 2013 7:02 pm
Location: Sweden
Contact:

Re: Logging accelerometer values

Post by marcus »

Hi Daniel,

Have a look at this example. The function names for the Python API might not be 100% (we changed it to follow PEP-8), but they are very similar.

We have been working on the plot tab but we haven't gotten very far yet since there's been lots of other stuff to do. There's a task for it here and we are hoping we get time to get it in soon since we think it would be very nice to get a visual view of the values that are logged.

/Marcus
erget
Beginner
Posts: 29
Joined: Thu Apr 04, 2013 6:10 am

Re: Logging accelerometer values

Post by erget »

Okay, thanks for the tips :) I'll be looking forward for the logging framework :) I think plotting's great, but what's really important to me is getting the numbers. Have a nice weekend!
marcus
Bitcraze
Posts: 659
Joined: Mon Jan 28, 2013 7:02 pm
Location: Sweden
Contact:

Re: Logging accelerometer values

Post by marcus »

If you don't want the plot you can do the same as in the example above, but instead add acc.x, acc.y, acc.z in stabilizer.c, then you will log the raw accelerometer values. And instead of printing you can add text-boxes to the UI and output the values there. Just be careful manipulating the UI from the context of the callback thread, this will cause problems. Wrap the call using a signal/slot like it's done in the FlightTab.py for motors and imu values.

Have a nice weekend!

/Marcus
erget
Beginner
Posts: 29
Joined: Thu Apr 04, 2013 6:10 am

Re: Logging accelerometer values

Post by erget »

Okay, thanks, I'll get on it next week :) Never worked on firmware before, this'll get interesting ;)

I take it that I need to recompile and reflash the firmware once I've added those variables, right?
marcus
Bitcraze
Posts: 659
Joined: Mon Jan 28, 2013 7:02 pm
Location: Sweden
Contact:

Re: Logging accelerometer values

Post by marcus »

erget wrote: I take it that I need to recompile and reflash the firmware once I've added those variables, right?
Yes, after you add the logging to the variables the firmware has to be re-compiles and flashed. If you don't have the development environment already set up then I can recommend using the Bitcraze VM. It's quick to get going. To flash the firmware you build have a look here.

Good luck!
erget
Beginner
Posts: 29
Joined: Thu Apr 04, 2013 6:10 am

Re: [SOLVED] Logging accelerometer values

Post by erget »

Alright, I've got the new variables in the TOC and the firmware recompiled. I'll bet the values could interest a lot of people, so I'll send a pull request when I've tested it out (might take a while due to hardware issues, those are being handled though :) ). Thanks again!

Daniel
erget
Beginner
Posts: 29
Joined: Thu Apr 04, 2013 6:10 am

Re: Logging accelerometer values

Post by erget »

Hi!

Sadly, I have to reopen the issue. I've tried to follow the instructions from this thread and the Wiki, but no luck. Here's what's going on:

1. I added the following to crazyflie-firmware/modules/src/stabilizer.c:

Code: Select all

LOG_GROUP_START(acc)
LOG_ADD(LOG_FLOAT, x, &acc.x)
LOG_ADD(LOG_FLOAT, y, &acc.y)
LOG_ADD(LOG_FLOAT, z, &acc.z)
LOG_GROUP_STOP(acc)
After compiling and flashing the firmware, I can see the variables acc.x, acc.y and acc.z as floats in the Log TOC in the CF client.

2. I tried to get the accelerometer values to log with a Python script, but it didn't work. I think that part of the problem is that the example for adding loggable variables in the Wiki might base on an older API in the PC client, but I'm not sure. Here's what I've got:

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()

		# 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"))

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

        # 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")

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

    def connectSetupFinished(self, linkURI):
        Thread(target=self.log_accel_data).start()

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

Main()
That code's not pretty, I know, I just wanted to test the principle of whether I could get the data. Here are my two questions concerning this step:

* When I run it, "acc.x/y/z not found in log TOC" is printed. But I can see the variables in the log TOC in the GUI! What's going on?
* I pretty much took the function log_accel_data() from the Wiki 1:1. I don't understand how the example in the wiki would work, though. The function "gyroData" in the Wiki is added as a callback. I assume the function is called as a Callable at some point in the code. But the function requires "data" to run. Where is it called?

Am I on the completely wrong path here?

Thanks a bunch!
Daniel
tobias
Bitcraze
Posts: 2339
Joined: Mon Jan 28, 2013 7:17 pm
Location: Sweden

Re: Logging accelerometer values

Post by tobias »

I know the logging functionality still is a bit buggy but I'm not the expert so I can't help you much more then giving you those encouraging words :)
Post Reply