urcontrol/urosc/urosc.py

172 lines
4.3 KiB
Python
Raw Normal View History

2024-05-10 08:14:29 +00:00
import argparse
2024-05-10 08:33:02 +00:00
2024-05-10 09:17:19 +00:00
2024-05-10 08:14:29 +00:00
import time
import math
import numpy as np
import curses
from helpfunctions import *
from ui import UI
2024-05-10 08:33:02 +00:00
from oscsend import OSCSend
2024-05-10 09:17:19 +00:00
from oscreceive import OSCReceive
from ur import UR
2024-05-10 08:14:29 +00:00
#Looptimings
frequency_loop=500
joint_min=[toRad(-360),toRad(-360),toRad(-360),toRad(-360),toRad(-360),toRad(0)]
joint_max=[toRad(360),toRad(360),toRad(360),toRad(360),toRad(360),toRad(360)]
ui = UI(joint_min,joint_max)
2024-05-10 09:17:19 +00:00
oscsend = OSCSend()
oscreceive = None
ur = UR(joint_min,joint_max)
2024-05-10 08:14:29 +00:00
def main(stdscr):
ui.begin(stdscr)
loop_dt = 1 / frequency_loop
2024-05-10 08:35:53 +00:00
2024-05-10 08:14:29 +00:00
loopcounter = 0
loopdurations = np.zeros(100)
loopdurations_pos=0
2024-05-10 08:33:02 +00:00
keyDown=False
2024-05-10 08:14:29 +00:00
try:
2024-05-10 08:35:53 +00:00
while ui.getKey() != ord('q'):
2024-05-10 08:14:29 +00:00
looptime = time.time()
start = time.time()
ur.receiveJoints(looptime)
2024-05-10 15:18:08 +00:00
#ur.receiveStatus(looptime)
2024-05-10 08:14:29 +00:00
2024-05-10 08:33:02 +00:00
#send data to all osc receivers
2024-05-10 15:18:08 +00:00
oscsend.send(looptime,ur.getReceivedQ(),ur.getReceivedTCPPose())
2024-05-10 08:14:29 +00:00
2024-05-10 09:17:19 +00:00
if ur is not None:
ur.send(looptime)
2024-05-10 08:14:29 +00:00
#Display
2024-05-10 09:17:19 +00:00
ui.update(ur.getReceivedQ(),looptime)
2024-05-10 08:14:29 +00:00
#Looptiming
end = time.time()
loopduration = end - start
loopdurations[loopdurations_pos]=loopduration
loopdurations_pos+=1
loopdurations_pos%=np.size(loopdurations)
ui.setDebugtext(0,"loopcounter="+str(loopcounter)+"\tControlEnabled="+str(ur.isControlEnabled()))
2024-05-10 08:14:29 +00:00
ui.setDebugtext(1,"loop usage="+str(round(np.mean(loopdurations)/loop_dt,3)))
2024-05-10 09:17:19 +00:00
if oscreceive is not None:
ui.setDebugtext(2,"osc receive frequency="+str(round(oscreceive.getCurrentReceiveFrequency(),0)))
2024-05-10 15:18:08 +00:00
#ui.setDebugtext(3,"invkin"+str(ur.getForwardKinematics()))
tcp=ur.getReceivedTCPPose()
tcp_deg=[tcp[0],tcp[1],tcp[2],toDeg(tcp[3]),toDeg(tcp[4]),toDeg(tcp[5])]
#ui.setDebugtext(3,"osc test:"+str(oscreceive.getDebug())+"\t tcp_deg="+str(tcp_deg))
ui.setDebugtext(3,"osc test:"+str(oscreceive.getDebug()))
if ui.getKey() == ord('e') and keyDown==False:
keyDown=True
ur.enableControl()
if ui.getKey() == ord('d') and keyDown==False:
keyDown=True
ur.disableControl()
if ui.getKey()==-1:
keyDown=False
2024-05-10 08:14:29 +00:00
if loopduration < loop_dt:
time.sleep(loop_dt - loopduration)
loopcounter += 1
except (KeyboardInterrupt):
2024-05-10 08:14:29 +00:00
print("\nKeyboardInterrupt")
except Exception as e:
pass
2024-05-10 08:14:29 +00:00
curses.endwin()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--listenip",
default="127.0.0.1", help="The ip to listen on")
parser.add_argument("--listenport",
type=int, default=5005, help="The port to listen on")
parser.add_argument("--oscsendip",
2024-05-10 08:14:29 +00:00
default="127.0.0.1", help="The ip to connect to")
parser.add_argument("--oscsendport",
2024-05-10 08:14:29 +00:00
type=int, default=5005, help="The port to connect to")
parser.add_argument("--robotip",
default="127.0.0.1", help="The ip to connect to")
parser.add_argument("--test", action='store_true', default=False, help="Disable network and robot communication")
2024-05-10 09:17:19 +00:00
'''
2024-05-10 08:14:29 +00:00
parser.add_argument(
"-fs",
"--frequencysend",
dest="frequencysend",
help="the frequency at which the robot control data is send",
type=float,
default=500.0,
metavar="<frequency>")
parser.add_argument(
"-fr",
"--frequencyreceive",
dest="frequencyreceive",
help="the frequency at which the robot actualq data is received",
type=float,
default=500.0,
metavar="<frequency>")
2024-05-10 09:17:19 +00:00
'''
2024-05-10 08:14:29 +00:00
args = parser.parse_args()
2024-05-10 09:17:19 +00:00
2024-05-10 08:14:29 +00:00
if args.test is not True:
2024-05-10 09:17:19 +00:00
ur.connect(args.robotip)
2024-05-10 15:18:08 +00:00
oscreceive = OSCReceive(args.listenip, args.listenport,oscsend,ur)
2024-05-10 08:14:29 +00:00
oscsend.setupReceivers([args.oscsendip],args.oscsendport)
2024-05-10 08:14:29 +00:00
2024-05-10 09:17:19 +00:00
2024-05-10 08:14:29 +00:00
curses.wrapper(main)
2024-05-10 09:17:19 +00:00
if oscreceive is not None:
oscreceive.disconnect()
if ur is not None:
ur.disconnect()
2024-05-10 08:14:29 +00:00