This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
projects:crazyflie:pc_utils:pylib [2013-02-10 23:57] macke |
projects:crazyflie:pc_utils:pylib [2021-06-24 16:58] (current) kimberly |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== The Crazyflie Python API ====== | + | <WRAP center round important 60%> |
- | + | This page has deprecated | |
- | + | </WRAP> | |
- | ====== Examples ====== | + | This topic has been moved [[doc:crazyflie:api:python:index|here]]. |
- | Below are the two examples described in the video <insert link here> that shows the parameter and logging framework. | + | |
- | + | ||
- | ===== Adding a parameter ===== | + | |
- | In this example we will add a parameter that will be used to " | + | |
- | + | ||
- | First of all we need to add the parameter to the firmware, this is done by using the macros PARAM_GROUP_START, | + | |
- | <code c> | + | |
- | #include " | + | |
- | + | ||
- | bool ledFreeze = false; | + | |
- | + | ||
- | PARAM_GROUP_START(led) | + | |
- | PARAM_ADD(PARAM_UINT8, | + | |
- | PARAM_GROUP_STOP(led) | + | |
- | </ | + | |
- | + | ||
- | This will add a parameter in the TOC named // | + | |
- | + | ||
- | Now we should use this parameter on the client side. This can either be done by reading or writing the parameter. First we add the code to write the parameter: | + | |
- | <code python> | + | |
- | # crazyflie is an instance of the Crazyflie class that has been instantiated | + | |
- | crazyflie.param.setParamValue(" | + | |
- | </ | + | |
- | + | ||
- | The parameter-framework relies on the fact that the parameters are changed from the client or that the client polls the value of parameters. If you are interested in logging changes that the Crazyflie does itself then the logging-framework is better. If you are interested in reading a parameter this should be done using a callback that will be called from the framework when the value for the parameter is updated. This happens in two cases: | + | |
- | * When the Crazyflie has connected values for all the parameters in the TOC are fetched | + | |
- | * When the client changes a value for a parameter the new value will be sent back from the Crazyflie | + | |
- | + | ||
- | To register for a callback and to implement the callback the following is used: | + | |
- | <code python> | + | |
- | crazyflie = Crazyflie() | + | |
- | crazyflie.param.addParamUpdateCallback(" | + | |
- | + | ||
- | def paramUpdateCallback(name, | + | |
- | print "%s has value %s" % (name, value) # This will in our example print: led.freeze has value True | + | |
- | </code> | + | |
- | + | ||
- | ===== Adding loggable variables ===== | + | |
- | In this example we will add logging for the raw gyro values read from the sensor. | + | |
- | + | ||
- | First of all we add the variables to the logging TOC by using the macros LOG_GROUP_START, | + | |
- | <code c> | + | |
- | #include "log.h" | + | |
- | // The raw gyro values are stored in the gyro struct | + | |
- | LOG_GROUP_START(gyro) | + | |
- | LOG_ADD(LOG_FLOAT, | + | |
- | LOG_ADD(LOG_FLOAT, | + | |
- | LOG_ADD(LOG_FLOAT, | + | |
- | LOG_GROUP_STOP(gyro) | + | |
- | </code> | + | |
- | + | ||
- | This will add the variables //gyro.x//, //gyro.y// and //gyro.z// to the logging TOC as floats. | + | |
- | + | ||
- | On the client side we now add the log configuration, | + | |
- | <code python> | + | |
- | # Callback called when the connection is established to the Crazyflie | + | |
- | def connected(linkURI): | + | |
- | gyroconf = LogConfig(" | + | |
- | gyroconf.addVariable(LogVariable(" | + | |
- | gyroconf.addVariable(LogVariable(" | + | |
- | gyroconf.addVariable(LogVariable(" | + | |
- | + | ||
- | # crazyflie is an instance of the Crazyflie class that has been instantiated and connected | + | |
- | gyrolog = crazyflie.log.newLogPacket(gyroconf) | + | |
- | + | ||
- | if (gyrolog != None): | + | |
- | gyrolog.dataReceived.addCallback(self.gyroData) | + | |
- | gyrolog.startLogging() | + | |
- | else: | + | |
- | print " | + | |
- | + | ||
- | def gyroData(data): | + | |
- | print " | + | |
- | + | ||
- | </ | + |