Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Question regarding use of API in Virtual Machine

#11
Can you try this code:
Code:
import time
from datetime import datetime
from robodk import robolink
import csv

# Connect with ROBODK
RDK = robolink.Robolink()

# Hardware setup
KUKA = RDK.Item('KUKA KR 30 HA')
if not KUKA.Valid():
    raise Exception("Robot not selected or not available")

# File path for the CSV
file_path = r'C:\Users\XXX\Desktop\Position4.csv'

# Time interval for monitoring in seconds
interval = 0.01

def monitor_position(rdk, robot, interval, file_path):
    with open(file_path, 'w', newline='') as file:
        csv_writer = csv.writer(file)
        csv_writer.writerow(["Timestamp", "Position"])
        #csv_writer.writerow(["Timestamp"])
        rdk.setRunMode(6)
        if robot.Connect() > 0:
            csv_writer.writerow(["Connected", rdk.RunMode()])

        try:
             while True:
                pos = robot.Joints().list()
                current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

                print(f"Timestamp: {current_time} | Current Position: {pos}")
                csv_writer.writerow([current_time, pos])
                file.flush()  # Ensure data is written to file
                time.sleep(interval)
        except KeyboardInterrupt:
            print("Stopped position tracking")

thread = threading.Thread(target=monitor_position, args=(RDK, KUKA, interval, file_path))
thread.start()
  




Users browsing this thread:
1 Guest(s)