move osc send to class

This commit is contained in:
Philipp Kramer 2024-05-10 10:33:02 +02:00
parent dbef99afaf
commit a3f034cc49
3 changed files with 51 additions and 35 deletions

32
urosc/oscsend.py Normal file
View file

@ -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))

View file

@ -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()

View file

@ -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)