diff --git a/cflib_groundstation/LogfileHandler.py b/cflib_groundstation/LogfileHandler.py index 4dca8f9244e239668cda07156626d5414148ddb9..59fea4953be8636f7a99d7b0d024f35db4622378 100644 --- a/cflib_groundstation/LogfileHandler.py +++ b/cflib_groundstation/LogfileHandler.py @@ -41,69 +41,45 @@ class LogfileHandler: self.is_connected = False self.param_callback_count = 0 self.logging_configs = [] + self.data_log = "" - 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(self): - return self.read_all_active_blocks() - - def load_logging_configs(self): - return self.read_all_active_blocks() - - 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() - + def CopyTocToFile(self, toc :dict, isParams): + if isParams: + logType = "Param" + else: + logType = "Log" + + filename = os.getcwd() + filename += f"/logs/cflie1_{logType}_toc_{time.strftime('%Y_%m_%d_%H:%M:%S', time.localtime())}.txt" + # print(f"TOCtoFILE:{filename}") + + types = {'uint8_t' : 0x08, + 'uint16_t' : 0x09, + 'uint32_t' : 0x0A, + 'uint64_t' : 0x0B, + 'int8_t' : 0x00, + 'int16_t' : 0x01, + 'int32_t' : 0x02, + 'int64_t' : 0x03, + 'FP16' : 0x05, + 'float' : 0x06, + 'double' : 0x07} + + file = open(filename, "w") + file.write(f"{logType} ID Type Group Identifier Name\n") + file.close() + file = open(filename, "a") + for group in list(toc.keys()): + for name in list(toc[group].keys()): + file.write(f"{toc[group][name].ident} {types.get(toc[group][name].ctype)} {group} {name}\n") + #print(f" Identity: {toc[group][name].ident} Packet Type:{toc[group][name].pytype} CType:{toc[group][name].ctype} Group:{group} Name:{name}\n") + + + + file.close() + return filename def read_all_active_blocks(self): active_blocks = [] @@ -119,7 +95,6 @@ class LogfileHandler: i = 0 newLine = logBlockFile.readline().strip() while newLine != "END BLOCK": - print(newLine) if newLine == "" or newLine == "END BLOCK": print("New Line: " + newLine) raise Exception("loggingBlocks.txt is not formatted correctly") @@ -136,8 +111,23 @@ class LogfileHandler: config = LogConfig(data[1], data[2]) for i in range(3, len(data)): config.add_variable(data[i]) - config.create() active_blocks.append(config) return active_blocks + def create_log_data_file(self): + raise Exception + + def add_config_headers(self): + raise Exception + + def start_logging(self): + raise Exception + + def stop_logging(self): + raise Exception + + def write_data_point(self): + raise Exception + + diff --git a/cflib_groundstation/__pycache__/LogfileHandler.cpython-38.pyc b/cflib_groundstation/__pycache__/LogfileHandler.cpython-38.pyc index 7fcdd75366634ed8a67ceb0bc0249148df63257c..6467201807fee9556118b139b0fd672116df9b80 100644 Binary files a/cflib_groundstation/__pycache__/LogfileHandler.cpython-38.pyc and b/cflib_groundstation/__pycache__/LogfileHandler.cpython-38.pyc differ diff --git a/cflib_groundstation/__pycache__/SetpointHandler.cpython-38.pyc b/cflib_groundstation/__pycache__/SetpointHandler.cpython-38.pyc index 0c7e0a74ab5cc30a12251e4bb5914b570dbe5755..4b6fa575d603a6f5965726a792f3559d6bdcb65f 100644 Binary files a/cflib_groundstation/__pycache__/SetpointHandler.cpython-38.pyc and b/cflib_groundstation/__pycache__/SetpointHandler.cpython-38.pyc differ diff --git a/cflib_groundstation/__pycache__/crazyflie_connection.cpython-38.pyc b/cflib_groundstation/__pycache__/crazyflie_connection.cpython-38.pyc index c0374ef88a85fc8fd2568c1ec8d3a508f778406b..a880844f863cc0e4376eae434e19710590875b17 100644 Binary files a/cflib_groundstation/__pycache__/crazyflie_connection.cpython-38.pyc and b/cflib_groundstation/__pycache__/crazyflie_connection.cpython-38.pyc differ diff --git a/cflib_groundstation/__pycache__/groundstation_socket.cpython-38.pyc b/cflib_groundstation/__pycache__/groundstation_socket.cpython-38.pyc index c7682e1b85a3353b38ec590272b6af5937d17a5e..be803194c29d9124de995ab5d59fc3d0dbd15cd0 100644 Binary files a/cflib_groundstation/__pycache__/groundstation_socket.cpython-38.pyc and b/cflib_groundstation/__pycache__/groundstation_socket.cpython-38.pyc differ diff --git a/cflib_groundstation/__pycache__/uCartCommander.cpython-38.pyc b/cflib_groundstation/__pycache__/uCartCommander.cpython-38.pyc index 80519f2ef307568ffdfa9785fe9f2aa52894b146..85e268143825e20c74f5e47f55281d03cd536423 100644 Binary files a/cflib_groundstation/__pycache__/uCartCommander.cpython-38.pyc and b/cflib_groundstation/__pycache__/uCartCommander.cpython-38.pyc differ diff --git a/cflib_groundstation/crazyflie_connection.py b/cflib_groundstation/crazyflie_connection.py index 5c427193efe233d41b3569d0e1ecefcd138be148..4e414f4eda5071b62b3a832571374322fb4058b8 100644 --- a/cflib_groundstation/crazyflie_connection.py +++ b/cflib_groundstation/crazyflie_connection.py @@ -16,6 +16,7 @@ import os import cflib.crtp from cflib.crazyflie import Crazyflie from cflib.crazyflie.syncCrazyflie import SyncCrazyflie +from cflib.crazyflie.syncLogger import SyncLogger from queue import Queue #import groundstation_socket as gs import uCartCommander @@ -23,6 +24,7 @@ from groundstation_socket import MessageTypeID from SetpointHandler import SetpointHandler, FlightMode from cflib.crazyflie.log import LogConfig import numpy as np +from LogfileHandler import LogfileHandler class CrazyflieConnection: @@ -51,6 +53,7 @@ class CrazyflieConnection: self.logging_configs = [] self.setpoint_handler = SetpointHandler() + self.logfile_handler = LogfileHandler() # self.timer = QTimer() # self.timer.timeout.connect(self.update_plot) @@ -250,10 +253,11 @@ class CrazyflieConnection: id = command['data'][0] print(id) if id == 0: # logdata? - raise Exception # Not implemented + for config in self.logging_configs: + self.simple_log(self.scf, config) elif id == 1: # param toc params = self.get_param_toc() - filename = self.CopyTocToFile(params, True) + filename = self.logfile_handler.CopyTocToFile(params, True) data = bytearray() data += bytes(filename, 'utf-8') responsedata = { @@ -265,7 +269,7 @@ class CrazyflieConnection: outputQueue.put(responsedata) elif id == 2: # logging toc logs = self.get_logging_toc() - filename = self.CopyTocToFile(logs, False) + filename = self.logfile_handler.CopyTocToFile(logs, False) data = bytearray() data += bytes(filename, 'utf-8') responsedata = { @@ -281,43 +285,54 @@ class CrazyflieConnection: raise Exception # Not implemented else : raise Exception - def LogBlockCommand(): - #TODO - raise Exception + + def LogBlockCommand(self, command): + print("Log Block Command") + id = command['data'][0] + print(id) + if id == 0: + self.delete_log_blocks() + elif id == 1: + self.delete_log_blocks() + self.logging_configs = self.logfile_handler.read_all_active_blocks() + elif id == 2: + self.logging_configs = self.logfile_handler.read_all_active_blocks() + elif id == 3: + block_id = command['data'][1] + self.logging_configs.remove(self.logging_configs[block_id]) + elif id == 4: + self.enable_logging() + elif id == 5: + self.disable_logging() + + def simple_log(self, scf, logconf): + print("Logging...") + with SyncLogger(scf, logconf) 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)) + + break + + 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() + + def delete_log_blocks(self): + for block in self.logging_configs: + block.delete() + self.logging_configs.remove(block) + - def CopyTocToFile(self, toc :dict, isParams): - if isParams: - logType = "Param" - else: - logType = "Log" - - filename = os.getcwd() - filename += f"/logs/cflie1_{logType}_toc_{time.strftime('%Y_%m_%d_%H:%M:%S', time.localtime())}.txt" - # print(f"TOCtoFILE:{filename}") - - types = {'uint8_t' : 0x08, - 'uint16_t' : 0x09, - 'uint32_t' : 0x0A, - 'uint64_t' : 0x0B, - 'int8_t' : 0x00, - 'int16_t' : 0x01, - 'int32_t' : 0x02, - 'int64_t' : 0x03, - 'FP16' : 0x05, - 'float' : 0x06, - 'double' : 0x07} - - file = open(filename, "w") - file.write(f"{logType} ID Type Group Identifier Name\n") - file.close() - file = open(filename, "a") - for group in list(toc.keys()): - for name in list(toc[group].keys()): - file.write(f"{toc[group][name].ident} {types.get(toc[group][name].ctype)} {group} {name}\n") - #print(f" Identity: {toc[group][name].ident} Packet Type:{toc[group][name].pytype} CType:{toc[group][name].ctype} Group:{group} Name:{name}\n") - - - - file.close() - return filename diff --git a/cflib_groundstation/groundstation_socket.py b/cflib_groundstation/groundstation_socket.py index d003fb7832f9226804e2011546379b9f6637a7d4..8e769525d932b2c51a9e5b0537ff8cf3961fb3a2 100644 --- a/cflib_groundstation/groundstation_socket.py +++ b/cflib_groundstation/groundstation_socket.py @@ -168,6 +168,7 @@ class MessageTypeID(Enum): #MAX_TYPE_ID = 21 GETLOGFILE_ID = 21 RESPLOGFILE_ID = 22 + LOGBLOCKCOMMAND_ID = 23 diff --git a/cflib_groundstation/main.py b/cflib_groundstation/main.py index 212ed6b46ae07712e7716922fcb8971f15c2bae8..7b12911f7e1ca3a394fc1d3d675708b53af2e3f1 100644 --- a/cflib_groundstation/main.py +++ b/cflib_groundstation/main.py @@ -65,6 +65,8 @@ class main(): self.cfConnect.LogBlockCommand() elif msg_type == MessageTypeID.GETLOGFILE_ID.value: self.cfConnect.GetLogFile(command, self.outputQueue) + elif msg_type == MessageTypeID.LOGBLOCKCOMMAND_ID.value: + self.cfConnect.LogBlockCommand(command) @@ -76,8 +78,8 @@ class main(): if __name__ == '__main__': - lf = LogfileHandler() - lf.read_all_active_blocks() - #main().start() + #lf = LogfileHandler() + #lf.read_all_active_blocks() + main().start() \ No newline at end of file