From 346fdc8c558df340220e1dd88cabfc553a3d2790 Mon Sep 17 00:00:00 2001 From: Adam Date: Wed, 26 Mar 2025 20:42:26 +0000 Subject: [PATCH] Incorperated rsi visualiser code. --- src/RSIPI/__init__.py | 0 src/RSIPI/cli.py | 17 +++++++++++++++++ src/RSIPI/rsi_api.py | 17 +++++++++++++++++ src/RSIPI/test_rsipi.py | 18 ++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 src/RSIPI/__init__.py diff --git a/src/RSIPI/__init__.py b/src/RSIPI/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/RSIPI/cli.py b/src/RSIPI/cli.py index fb52af4..843047d 100644 --- a/src/RSIPI/cli.py +++ b/src/RSIPI/cli.py @@ -1,4 +1,5 @@ from .rsi_client import RSIClient +from .kuka_visualizer import KukaRSIVisualizer class RSICommandLineInterface: """Command-Line Interface for controlling RSI Client.""" @@ -73,6 +74,10 @@ class RSICommandLineInterface: self.running = False elif cmd == "help": self.show_help() + elif cmd == "visualize" and len(parts) >= 2: + csv_file = parts[1] + export = ("export" in parts) + self.visualize(csv_file, export) else: print("❌ Unknown command. Type 'help' for a list of commands.") @@ -136,6 +141,18 @@ Available Commands: set_alert_threshold """) + def visualize(self, csv_file, export=False): + try: + visualizer = KukaRSIVisualizer(csv_file) + visualizer.plot_trajectory() + visualizer.plot_joint_positions() + visualizer.plot_force_trends() + + if export: + visualizer.export_graphs() + print(f"✅ Visualisations exported for '{csv_file}'") + except Exception as e: + print(f"❌ Failed to visualize '{csv_file}': {e}") if __name__ == "__main__": config_file = "RSI_EthernetConfig.xml" diff --git a/src/RSIPI/rsi_api.py b/src/RSIPI/rsi_api.py index 64cb006..6a9c71c 100644 --- a/src/RSIPI/rsi_api.py +++ b/src/RSIPI/rsi_api.py @@ -5,6 +5,7 @@ import json import matplotlib.pyplot as plt from .rsi_client import RSIClient from .graphing import RSIGraphing +from .kuka_visualizer import KukaRSIVisualizer class RSIAPI: """RSI API for programmatic control, including alerts, logging, graphing, and data retrieval.""" @@ -196,3 +197,19 @@ class RSIAPI: plt.savefig(f"{filename}.pdf") return f"✅ Report saved as {filename}.{format_type}" + + def visualize_csv_log(self, csv_file, export=False): + """ + Visualize CSV log file directly via RSIAPI. + + Args: + csv_file (str): Path to CSV log file. + export (bool): Whether to export the plots. + """ + visualizer = KukaRSIVisualizer(csv_file) + visualizer.plot_trajectory() + visualizer.plot_joint_positions() + visualizer.plot_force_trends() + + if export: + visualizer.export_graphs() \ No newline at end of file diff --git a/src/RSIPI/test_rsipi.py b/src/RSIPI/test_rsipi.py index 878e311..81e7df7 100644 --- a/src/RSIPI/test_rsipi.py +++ b/src/RSIPI/test_rsipi.py @@ -85,5 +85,23 @@ class TestRSIPI(unittest.TestCase): response_threshold = self.api.set_alert_threshold("deviation", 3.5) self.assertIn("✅ Deviation alert threshold set to 3.5", response_threshold) + def test_visualization_methods(self): + csv_file = "test_log.csv" + # Create a dummy CSV file for testing + pd.DataFrame({ + "RIst.X": [0, 1, 2], "RIst.Y": [0, 1, 2], "RIst.Z": [0, 1, 2], + "AIPos.A1": [10, 20, 30], "PosCorr.X": [0.1, 0.2, 0.3] + }).to_csv(csv_file, index=False) + + try: + self.api.visualize_csv_log(csv_file, export=True) + except Exception as e: + self.fail(f"Visualisation test failed: {e}") + finally: + import os, shutil + os.remove(csv_file) + if os.path.exists("exports"): + shutil.rmtree("exports") + if __name__ == '__main__': unittest.main() \ No newline at end of file