From a3f034cc496cece113e4988c6aae6f90e026654f Mon Sep 17 00:00:00 2001 From: Philipp Kramer Date: Fri, 10 May 2024 10:33:02 +0200 Subject: [PATCH] move osc send to class --- urosc/oscsend.py | 32 ++++++++++++++++++++++++++++++++ urosc/ui.py | 16 ++++++++++------ urosc/urosc.py | 38 +++++++++----------------------------- 3 files changed, 51 insertions(+), 35 deletions(-) create mode 100644 urosc/oscsend.py diff --git a/urosc/oscsend.py b/urosc/oscsend.py new file mode 100644 index 0000000..a764cf2 --- /dev/null +++ b/urosc/oscsend.py @@ -0,0 +1,32 @@ +from helpfunctions import * + +class OSCSend: + from pythonosc import udp_client + + def __init__(self,_frequency_oscsend=50): + self.oscreceivers = [] + self.frequency_oscsend=_frequency_oscsend + self.last_sendosc=0 + + def setupReceivers(self,serverips,port): + self.oscreceivers = [] + + # Set up OSC client + for cip in serverips: + print("connecting to "+str(cip)) + connection = self.udp_client.SimpleUDPClient(cip, port) + self.oscreceivers.append(connection) + + + + def send(self,looptime,q): + + if looptime-self.last_sendosc<1.0/self.frequency_oscsend: + + return + + self.last_sendosc=looptime + + for c in self.oscreceivers: + for jointi,joint in enumerate(q): + c.send_message("/j"+str(jointi), toDeg(joint)) \ No newline at end of file diff --git a/urosc/ui.py b/urosc/ui.py index 8be622b..cce0480 100644 --- a/urosc/ui.py +++ b/urosc/ui.py @@ -2,14 +2,10 @@ from helpfunctions import * class UI: - #def __init__(self, name, age): - # self.name = name - # self.age = age - import curses - def __init__(self,_joint_min,_joint_max): + def __init__(self,_joint_min,_joint_max,_frequency_scr=25): self.stdscr=None self.debugtext=["","",""] self.width=0 @@ -18,6 +14,8 @@ class UI: self.joint_max=None self.joint_min=_joint_min self.joint_max=_joint_max + self.frequency_scr=_frequency_scr + self.last_updatescr=0 def setDebugtext(self,line,text): @@ -45,7 +43,13 @@ class UI: self.height, self.width = self.stdscr.getmaxyx() - def update(self,q): + def update(self,q,looptime): + + if looptime-self.last_updatescr<1.0/self.frequency_scr: + return + + self.last_updatescr=looptime + key = self.stdscr.getch() self.stdscr.clear() diff --git a/urosc/urosc.py b/urosc/urosc.py index 12f4967..516ec68 100644 --- a/urosc/urosc.py +++ b/urosc/urosc.py @@ -1,7 +1,7 @@ import argparse from pythonosc import dispatcher from pythonosc import osc_server -from pythonosc import udp_client + import threading import time import math @@ -14,21 +14,20 @@ import rtde_control from helpfunctions import * from ui import UI +from oscsend import OSCSend -clients = [] rtde_r=None rtde_c=None server=None #Looptimings frequency_loop=500 -frequency_oscsend=50 frequency_rtde=None -frequency_scr=25 + #Robot Control Parameters # Parameters @@ -71,16 +70,6 @@ def setupOSC(ip, port): return server,server_thread -def setupClients(clientips,port): - clients = [] - - # Set up OSC client - for cip in clientips: - print("connecting to "+str(cip)) - client = udp_client.SimpleUDPClient(cip, port) - clients.append(client) - - return clients def setupRTDE(robotip,frequency): @@ -97,6 +86,7 @@ joint_max=[toRad(360),toRad(360),toRad(360),toRad(360),toRad(360),toRad(360)] ui = UI(joint_min,joint_max) +oscsend = OSCSend() @@ -113,11 +103,9 @@ def main(stdscr): loopcounter = 0 loopdurations = np.zeros(100) loopdurations_pos=0 - - last_sendosc=0 last_receivertde=0 last_sendrtde=0 - last_updatescr=0 + q=[0,0,0,0,0,0] @@ -143,12 +131,8 @@ def main(stdscr): q[_i]=(math.sin(time.time()*_factors[_i]+_offset)/2.0+0.5)*(joint_max[_i]-joint_min[_i])+joint_min[_i] - #send data to all osc clients - if looptime-last_sendosc>=1.0/frequency_oscsend: - last_sendosc=looptime - for c in clients: - for jointi in range(6): - c.send_message("/j"+str(jointi), toDeg(q[jointi])) + #send data to all osc receivers + oscsend.send(looptime,q) if looptime-last_sendrtde>=1.0/frequency_sendrtde: last_sendrtde=looptime @@ -161,10 +145,7 @@ def main(stdscr): #Display - if looptime-last_updatescr>=1.0/frequency_scr: - last_updatescr=looptime - - ui.update(q) + ui.update(q,looptime) #Looptiming end = time.time() @@ -232,13 +213,12 @@ if __name__ == "__main__": frequency_receivertde=args.frequencyreceive frequency_sendrtde=args.frequencysend - clientips=[args.clientip] if args.test is not True: server,server_thread=setupOSC(args.listenip, args.listenport) - clients=setupClients(clientips,args.port) + oscsend.setupReceivers([args.clientip],args.port) rtde_r=setupRTDE(args.robotip,frequency_rtde)