Incorperated rsi code parser

This commit is contained in:
Adam 2025-03-26 21:03:51 +00:00
parent ffc40b2f88
commit 1ca858febe
3 changed files with 64 additions and 1 deletions

View File

@ -1,5 +1,6 @@
from .rsi_client import RSIClient from .rsi_client import RSIClient
from .kuka_visualizer import KukaRSIVisualizer from .kuka_visualizer import KukaRSIVisualizer
from .krl_parser import KRLParser
class RSICommandLineInterface: class RSICommandLineInterface:
"""Command-Line Interface for controlling RSI Client.""" """Command-Line Interface for controlling RSI Client."""
@ -78,6 +79,9 @@ class RSICommandLineInterface:
csv_file = parts[1] csv_file = parts[1]
export = ("export" in parts) export = ("export" in parts)
self.visualize(csv_file, export) self.visualize(csv_file, export)
elif cmd == "krlparse" and len(parts) == 4:
src_file, dat_file, output_file = parts[1], parts[2], parts[3]
self.krl_parse(src_file, dat_file, output_file)
else: else:
print("❌ Unknown command. Type 'help' for a list of commands.") print("❌ Unknown command. Type 'help' for a list of commands.")
@ -154,6 +158,17 @@ Available Commands:
except Exception as e: except Exception as e:
print(f"❌ Failed to visualize '{csv_file}': {e}") print(f"❌ Failed to visualize '{csv_file}': {e}")
def krl_parse(self, src_file, dat_file, output_file):
"""CLI method to parse KRL files and output CSV."""
try:
parser = KRLParser(src_file, dat_file)
parser.parse_src()
parser.parse_dat()
parser.export_csv(output_file)
print(f"✅ KRL files parsed successfully. Output CSV: {output_file}")
except Exception as e:
print(f"❌ Failed to parse KRL files: {e}")
if __name__ == "__main__": if __name__ == "__main__":
config_file = "RSI_EthernetConfig.xml" config_file = "RSI_EthernetConfig.xml"
cli = RSICommandLineInterface(config_file) cli = RSICommandLineInterface(config_file)

View File

@ -6,6 +6,7 @@ import matplotlib.pyplot as plt
from .rsi_client import RSIClient from .rsi_client import RSIClient
from .graphing import RSIGraphing from .graphing import RSIGraphing
from .kuka_visualizer import KukaRSIVisualizer from .kuka_visualizer import KukaRSIVisualizer
from .krl_parser import KRLParser
class RSIAPI: class RSIAPI:
"""RSI API for programmatic control, including alerts, logging, graphing, and data retrieval.""" """RSI API for programmatic control, including alerts, logging, graphing, and data retrieval."""
@ -212,4 +213,22 @@ class RSIAPI:
visualizer.plot_force_trends() visualizer.plot_force_trends()
if export: if export:
visualizer.export_graphs() visualizer.export_graphs()
def parse_krl_to_csv(self, src_file, dat_file, output_file):
"""
Parses KRL files (.src, .dat) and exports coordinates to CSV.
Args:
src_file (str): Path to KRL .src file.
dat_file (str): Path to KRL .dat file.
output_file (str): Path for output CSV file.
"""
try:
parser = KRLParser(src_file, dat_file)
parser.parse_src()
parser.parse_dat()
parser.export_csv(output_file)
return f"✅ KRL data successfully exported to {output_file}"
except Exception as e:
return f"❌ Error parsing KRL files: {e}"

View File

@ -103,5 +103,34 @@ class TestRSIPI(unittest.TestCase):
if os.path.exists("exports"): if os.path.exists("exports"):
shutil.rmtree("exports") shutil.rmtree("exports")
def test_krl_parsing(self):
"""Test KRL parsing functionality."""
src_file = "test.src"
dat_file = "test.dat"
output_file = "test_output.csv"
# Create temporary dummy KRL files for testing
with open(src_file, "w") as f_src, open(dat_file, "w") as f_dat:
f_src.write("PDAT_ACT=PPDAT1\nPDAT_ACT=PPDAT2\n")
f_dat.write("DECL E6POS XP1={X 10,Y 20,Z 30,A 0,B 90,C 180,S 2,T 1,E1 0,E2 0}\n")
f_dat.write("DECL E6POS XP2={X 40,Y 50,Z 60,A 0,B 90,C 180,S 2,T 1,E1 0,E2 0}\n")
response = self.api.parse_krl_to_csv(src_file, dat_file, output_file)
self.assertIn("✅ KRL data successfully exported", response)
# Verify the CSV content
import pandas as pd
df = pd.read_csv(output_file)
self.assertEqual(len(df), 2)
self.assertEqual(df.iloc[0]["X"], 10)
self.assertEqual(df.iloc[1]["Y"], 50)
# Clean up temporary files
import os
os.remove(src_file)
os.remove(dat_file)
os.remove(output_file)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()