From ae9599ebe8c5448d2c3650ce727dd18659789ca2 Mon Sep 17 00:00:00 2001 From: 488_MP-4 <488_MP-4@iastate.edu> Date: Wed, 8 Nov 2023 23:12:24 +0100 Subject: [PATCH] created LogfileHandler class --- cflib_groundstation/LogfileHandler.py | 105 ++++++++++++++++++++ cflib_groundstation/crazyflie_connection.py | 53 ---------- 2 files changed, 105 insertions(+), 53 deletions(-) diff --git a/cflib_groundstation/LogfileHandler.py b/cflib_groundstation/LogfileHandler.py index e69de29bb..c219a7d61 100644 --- a/cflib_groundstation/LogfileHandler.py +++ b/cflib_groundstation/LogfileHandler.py @@ -0,0 +1,105 @@ + +from datetime import datetime +from email.utils import localtime +from time import time +from typing import List +import time +import struct + +import cflib.crtp +from cflib.crazyflie import Crazyflie +from cflib.crazyflie.syncCrazyflie import SyncCrazyflie +from queue import Queue +#import groundstation_socket as gs +import uCartCommander +from groundstation_socket import MessageTypeID +from SetpointHandler import SetpointHandler, FlightMode +from cflib.crazyflie.log import LogConfig +import numpy as np + + +class LogfileHandler: + """ + Handles all interactions with cflib. + """ + + def __init__(self): + """ + Initialize the start time, the logging queue which is given to the + plotting window, and set the synchronous crazyflie connection by + default to None. This should be set to the crazyflie when connecting + to one. + """ + + # Get the start time, so that the timestamp returned to the user will + # be in seconds since startup. + self.start_time = time.time() + + # self.logging_queue = Queue() + + self.scf = None + self.is_connected = False + self.param_callback_count = 0 + self.logging_configs = [] + + def add_log_config(self, name: str, period: int, variables: List[str]): + """ Add a logging config. Used from logging tab when refreshing + logging variables. Add callback to route logged data to logging + queue. """ + + print("Name: " + name + ", period: " + str(period) + ", variables: " + + str(variables)) + logging_group = LogConfig(name=name, period_in_ms=period) + + for variable in variables: + logging_group.add_variable(variable, 'float') + + self.logging_configs.append(logging_group) + self.logging_configs[-1].data_received_cb.add_callback( + self.logging_callback) + self.scf.cf.log.add_config(self.logging_configs[-1]) + + + def refresh_logging_configs() : + raise Exception + + def load_logging_configs() : + raise Exception + + def clear_logging_configs(self): + """ Stop logging and clear configuration. Used when refreshing + logging to stop anything that has configured to be logged from + logging. """ + + self.stop_logging() + self.logging_configs = [] + # done refreshing toc is a callback function that is triggered when + # refresh toc is done executing. + self.scf.cf.log.refresh_toc( + self.done_refreshing_toc, self.scf.cf.log._toc_cache) + # Blocks until toc is done refreshing. + while self.param_callback_count < 1: + time.sleep(0.01) + self.param_callback_count = 0 + + # grabs new toc values + self.scf.wait_for_params() + + def resume_logging_configs(): + raise Exception + + def pause_logging_configs(): + raise Exception + + def enable_logging(self): + """ Begins logging all configured logging blocks. This is used from + the controls tab when hitting begin logging. """ + for i in range(0, len(self.logging_configs)): + self.logging_configs[i].start() + + def disable_logging(self): + """ Stops logging all configured logging blocks. This is used from + the controls tab when hitting pause logging. """ + for i in range(0, len(self.logging_configs)): + self.logging_configs[i].stop() + diff --git a/cflib_groundstation/crazyflie_connection.py b/cflib_groundstation/crazyflie_connection.py index b94d4e230..ecf005c9d 100644 --- a/cflib_groundstation/crazyflie_connection.py +++ b/cflib_groundstation/crazyflie_connection.py @@ -304,56 +304,3 @@ class CrazyflieConnection: file.close() return filename - def add_log_config(self, name: str, period: int, variables: List[str]): - """ Add a logging config. Used from logging tab when refreshing - logging variables. Add callback to route logged data to logging - queue. """ - - print("Name: " + name + ", period: " + str(period) + ", variables: " - + str(variables)) - logging_group = LogConfig(name=name, period_in_ms=period) - - for variable in variables: - logging_group.add_variable(variable, 'float') - - self.logging_configs.append(logging_group) - self.logging_configs[-1].data_received_cb.add_callback( - self.logging_callback) - self.scf.cf.log.add_config(self.logging_configs[-1]) - - def clear_logging_configs(self): - """ Stop logging and clear configuration. Used when refreshing - logging to stop anything that has configured to be logged from - logging. """ - - self.stop_logging() - self.logging_configs = [] - # done refreshing toc is a callback function that is triggered when - # refresh toc is done executing. - self.scf.cf.log.refresh_toc( - self.done_refreshing_toc, self.scf.cf.log._toc_cache) - # Blocks until toc is done refreshing. - while self.param_callback_count < 1: - time.sleep(0.01) - self.param_callback_count = 0 - - # grabs new toc values - self.scf.wait_for_params() - - def done_refreshing_toc(self, *_args): - """ Callback for flow control, increments param callback count to - allow exit of while loop. """ - self.param_callback_count = 1 - - def start_logging(self): - """ Begins logging all configured logging blocks. This is used from - the controls tab when hitting begin logging. """ - for i in range(0, len(self.logging_configs)): - self.logging_configs[i].start() - - def stop_logging(self): - """ Stops logging all configured logging blocks. This is used from - the controls tab when hitting pause logging. """ - for i in range(0, len(self.logging_configs)): - self.logging_configs[i].stop() - -- GitLab