How do I log while flying?

Discussions about quadcopters/multi-rotors
Post Reply
JamesClark
Beginner
Posts: 8
Joined: Mon Sep 27, 2021 8:21 am

How do I log while flying?

Post by JamesClark »

I wanto log while flying and have the ramp example to go by. I have seen this logging example https://www.bitcraze.io/documentation/r ... log_param/ however I cannot put them together as they are two different threads. How do I log while flying then? I want the roll values.


import logging
import time
from threading import Thread

import cflib
from cflib.crazyflie import Crazyflie
from cflib.utils import uri_helper
from cflib.crazyflie.syncCrazyflie import SyncCrazyflie


from cflib.crazyflie.log import LogConfig
from cflib.crazyflie.syncLogger import SyncLogger

uri = uri_helper.uri_from_env(default='radio://0/80/2M/E7E7E7E7E8')

logging.basicConfig(level=logging.ERROR)


class MotorRampExample:
"""Example that connects to a Crazyflie and ramps the motors up/down and
the disconnects"""

def __init__(self, link_uri):
""" Initialize and run the example with the specified link_uri """

self._cf = Crazyflie(rw_cache='./cache')

self._cf.connected.add_callback(self._connected)
self._cf.disconnected.add_callback(self._disconnected)
self._cf.connection_failed.add_callback(self._connection_failed)
self._cf.connection_lost.add_callback(self._connection_lost)

self._cf.open_link(link_uri)

print('Connecting to %s' % link_uri)

def _connected(self, link_uri):
""" This callback is called form the Crazyflie API when a Crazyflie
has been connected and the TOCs have been downloaded."""

# Start a separate thread to do the motor test.
# Do not hijack the calling thread!
Thread(target=self._ramp_motors).start()

def _connection_failed(self, link_uri, msg):
"""Callback when connection initial connection fails (i.e no Crazyflie
at the specified address)"""
print('Connection to %s failed: %s' % (link_uri, msg))

def _connection_lost(self, link_uri, msg):
"""Callback when disconnected after a connection has been made (i.e
Crazyflie moves out of range)"""
print('Connection to %s lost: %s' % (link_uri, msg))

def _disconnected(self, link_uri):
"""Callback when the Crazyflie is disconnected (called in all cases)"""
print('Disconnected from %s' % link_uri)

def _ramp_motors(self):

# Unlock startup thrust protection
self._cf.commander.send_setpoint(0, 0, 0, 0)
time.sleep(0.1)
i=0
while i<3000:
self._cf.commander.send_setpoint(0, 0, 0, 1000)
print(i)
i=i+1

while True:
self._cf.commander.send_setpoint(0, 20, 0, 1000)
# Make sure that the last packet leaves before the link is closed
# since the message queue is not flushed before closing
self._cf.close_link()

def simple_log(scf, logconf):

with SyncLogger(scf, lg_stab) as logger:

for log_entry in logger:

timestamp = log_entry[0]
data = log_entry[1]
logconf_name = log_entry[2]

print('[%d][%s]: %s' % (timestamp, logconf_name, data))
if __name__ == '__main__':
# Initialize the low-level drivers
cflib.crtp.init_drivers()


le = MotorRampExample(uri)
kristoffer
Bitcraze
Posts: 630
Joined: Tue Jun 30, 2015 7:47 am

Re: How do I log while flying?

Post by kristoffer »

Hi,

to log while flying you must use the asynchronous API and not the SyncLogger class.

You can find an example of how to do this in https://github.com/bitcraze/crazyflie-l ... asiclog.py
Post Reply