A question about the "Hello World" for Crazyflie

Firmware/software/electronics/mechanics
Post Reply
internguy
Beginner
Posts: 11
Joined: Fri Aug 16, 2013 1:20 am

A question about the "Hello World" for Crazyflie

Post by internguy »

Hello Everyone! I will move onto the heart of the problem as fast as possible. Now background introduction. I am a Mechanical Engineer B.Sc and studiying for M.S Degree in Control Engineering at Tokyo Institute of Technology university. RIght now i am having an internship at another Research Institute, where i am given 3 of 10 DOF crazyflie copters. My task is first to autonomously control (hover etc) one copter then have 3 copters work in sync. So far what i have done

1) I have succesfuly installed the client provided by the Bitcraze.
2) I run cfclient without problems and can connect to the Crazyflie copter
3) I can fly the copter itself by using the controller :P

So far nothing extraordianry. The thing is source code is in Python and I HAVE NEVER worked with python before. So i am learning right now python :) never would have guessed a whitespace could be this dangerous ;p anyways i have picked up quite a bit of python in a couple of days.

REAL QUESTION STARTS FROM HERE (for people who doesnt want to read the story)

For starters i wanted to compile the source code given at the end of the page http://wiki.bitcraze.se/projects:crazyf ... tils:pylib
i compiled it however it doesnt work here is the output when i run in terminal python first.py

DEBUG:cflib.crazyflie.toc:Added element [25]
DEBUG:cflib.crazyflie.toc:[2]: More variables, requesting index 26
DEBUG:cflib.crazyflie.toc:Requesting index 26 on port 2
DEBUG:cflib.crazyflie:ExpectAnswer: Will expect answer on port [2]
DEBUG:cflib.crazyflie:ExpectAnswer: Got answer back on port [2], cancelling timer
DEBUG:cflib.crazyflie.toc:Added element [26]
WARNING:cflib.crazyflie.toccache:Could not save cache, no writable directory
DEBUG:cflib.crazyflie:Removing callback on port [2] to [<bound method TocFetcher._new_packet_cb of <cflib.crazyflie.toc.TocFetcher instance at 0x9cd16ec>>]
DEBUG:cflib.crazyflie.toc:[2]: Done!
INFO:cflib.crazyflie:Param TOC finished updating
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


in long story short how to write a Hello World for crazyfle
tobias
Bitcraze
Posts: 2339
Joined: Mon Jan 28, 2013 7:17 pm
Location: Sweden

Re: A question about the "Hello World" for Crazyflie

Post by tobias »

Sound like a lot of fun, can we switch jobs :D.

First thing python is great for making things happen quickly but if you are very familiar with C/C++ Jan has done a great job on a C lib for the Crazyflie which is an alternative.

Secondly you need a FW where the accelerometer variables are exposed to the client. You can see which variables are exposed in the Log TOC tab in the cfclient. The 2013.4 FW does not have them exposed which means you have to build the latest on you own. I will update the example with this information.

When you want to write a "Hello World" does that mean you want to print something from the Crazyflie FW to the cfclient console?
internguy
Beginner
Posts: 11
Joined: Fri Aug 16, 2013 1:20 am

Re: A question about the "Hello World" for Crazyflie

Post by internguy »

Thanks for the quick reply!

When i said "Hello World", i simply meant my own program for the crazyflie :D sorry for the confusion ;P As we all know the first thing to do when learning new is to create a hello world application, it was a referecen to that.

I will check the alternative C library thanks for suggestion.

By the way on crazyflie i have the cflie-2013.4.bin installed, does it mean acc variables are not exposed? (My native language is not Englsih sorry about that) which means i need to write my own firmware?(or partially update the available one?)

In the logTOC tab in the cfclient i have m1 to m4 and yaw pitch roll and thrust i also have vbat which i didnt understand:P

I hope i will be able to realize my aim so i will be useful to community

Thanks
internguy
Beginner
Posts: 11
Joined: Fri Aug 16, 2013 1:20 am

Re: A question about the "Hello World" for Crazyflie

Post by internguy »

Hello again!

I have been finally be able to connect to the copter and send commands such as thrust roll etc. thanks for the help. In this research lab( every researcher is using python, if i am not mistaken, and/or usage of python is encouraged. So i kinda must choose python too :shock:

However, i still havent been able to get the actual values for roll pitch yaw real time from the copter (stabilizer.roll,yaw etc). I have checked out plotter tab, flight tab (because they already show those values) to understand how it is done. Still no chance :( I am not stupid but i am having trouble following the code :lol: please bear with me]

With the following code i was able print the values to the terminal

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 stabilizer logging config
        stab_log_conf = LogConfig("stabilizer", 10)
        stab_log_conf.addVariable(LogVariable("stabilizer.roll", "float"))
        stab_log_conf.addVariable(LogVariable("stabilizer.pitch", "float"))
        stab_log_conf.addVariable(LogVariable("stabilizer.yaw", "float"))
 
        # Now that the connection is established, start logging
        self.stab_log = self.crazyflie.log.create_log_packet(stab_log_conf)
 
        if self.stab_log is not None:
            self.stab_log.dataReceived.add_callback(self.log_stab_data)
            self.stab_log.start()
        else:
            print("acc.x/y/z not found in log TOC")
 
    def log_stab_data(self, data):
        logging.info("Stabilizer: Roll=%.2f, Pitch=%.2f, Yaw=%.2f" %
                        (data["stabilizer.roll"], data["stabilizer.pitch"], data["stabilizer.yaw"]))
 
Main()
marcus
Bitcraze
Posts: 659
Joined: Mon Jan 28, 2013 7:02 pm
Location: Sweden
Contact:

Re: A question about the "Hello World" for Crazyflie

Post by marcus »

Hi,

Yes, the firmware that you have will not expose the accelerometer values for logging. If you connect to the Crazyflie using the cfclient and go to the Log TOC tab you will see a list of all the variables that you are able to log with that firmware. You should be able to use the example that you have working now with any of these variables. So if you would like to for example log the motors instead you exchange "stabilizer.roll" for "motor.m1" and use the correct datatype witch is "uint32_t".
internguy
Beginner
Posts: 11
Joined: Fri Aug 16, 2013 1:20 am

Re: A question about the "Hello World" for Crazyflie

Post by internguy »

Thank you very much for the answers! I have moved forward with coding for crazyflie a bit, thanks to input from this forum :) now i compiled a custom firmware where TOC shows gyro xyz and acc xyz ^_^ i can read them and log them

however now something bugs me if gyro values were not exposed to the client how does cfclient show values for roll yaw pitch values?. I mean stabilizer group somehow must be related to gyro and accelaration? the orientation of crazyflie can be found in conjunction with acc sensor and gyro, right? so it was calculated internally on hte firmware and sent as roll pitch and yaw values?

Moreover, whenever i try to log more than 6 variables, i get error. here is a smaple example. I didnt see any limitation on that so i am kind of confused :o I can get any of them until there are 6 variables 7 and more throws error. I thought it could be a problem with the packet size and i increased maximum log data packet size to 1024 from 30 still no solution

# The max size of a CRTP packet payload
MAX_LOG_DATA_PACKET_SIZE = 1024

Code: Select all

 # Set stabilizer logging config
        stab_log_conf = LogConfig("GotumdenUydurdum", 10)	
        
        stab_log_conf.addVariable(LogVariable("acc.x", "float"))
        stab_log_conf.addVariable(LogVariable("acc.y", "float"))
        stab_log_conf.addVariable(LogVariable("acc.z", "float"))
        
        stab_log_conf.addVariable(LogVariable("gyro.x", "float"))
        stab_log_conf.addVariable(LogVariable("gyro.y", "float"))
        stab_log_conf.addVariable(LogVariable("gyro.z", "float"))
        
        stab_log_conf.addVariable(LogVariable("stabilizer.pitch", "float"))
        stab_log_conf.addVariable(LogVariable("stabilizer.roll", "float"))
        stab_log_conf.addVariable(LogVariable("stabilizer.yaw", "float"))
Post Reply