urcontrol/urosc/urosc.py

160 lines
3.4 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
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()
2024-05-10 09:17:19 +00:00
ur.receive(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 09:17:19 +00:00
oscsend.send(looptime,ur.getReceivedQ())
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))
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 08:14:29 +00:00
if loopduration < loop_dt:
time.sleep(loop_dt - loopduration)
loopcounter += 1
except KeyboardInterrupt:
print("\nKeyboardInterrupt")
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("--clientip",
default="127.0.0.1", help="The ip to connect to")
parser.add_argument("--port",
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)
oscreceive = OSCReceive(args.listenip, args.listenport,ur)
2024-05-10 08:14:29 +00:00
2024-05-10 08:33:02 +00:00
oscsend.setupReceivers([args.clientip],args.port)
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