urcontrol/tests/pantilt.py

136 lines
3 KiB
Python
Raw Normal View History

2022-05-31 13:05:06 +00:00
import rtde_control
import time
import math
import socket
2022-05-31 14:18:56 +00:00
import keyboard # pip install keyboard
2022-05-31 13:05:06 +00:00
2022-05-31 14:18:56 +00:00
# Socket stuff
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host =""
port =30002
s.bind((host,port))
s.listen(1) # Number of connections
s.setblocking(False)
client = None
# Robot Control Stuff
2022-05-31 13:05:06 +00:00
2022-05-31 14:18:56 +00:00
rtde_c = rtde_control.RTDEControlInterface("192.168.1.101")
2022-05-31 13:05:06 +00:00
import rtde_receive
rtde_r = rtde_receive.RTDEReceiveInterface("192.168.1.101")
def toRad(d):
return d/360*2*math.pi
'''
actual_q = rtde_r.getActualQ() #current joint rotations in radians.
print("Actual Q:"+str(actual_q))
fw_actual_q = rtde_c.getForwardKinematics(actual_q)
print("forward Actual Q:"+str(fw_actual_q))
joint_q=rtde_c.getInverseKinematics(fw_actual_q)
print("Final Q:"+str(joint_q))
'''
pan=0 #-=left, +=right, degrees
tilt=0 #-=down, +=up, degrees
joint_q=[0,toRad(-90),toRad(90),toRad(-180),toRad(-90),toRad(0)] #start position
#rtde_c.moveJ([0,toRad(-90),toRad(90-tilt/2),toRad(-180-tilt/2),toRad(-90-pan),toRad(0)],0.1,0.1)
rtde_c.moveJ(joint_q,0.1,0.1) #move to initial position
# Parameters
2022-05-31 14:18:56 +00:00
velocity = 0.2 #0.5
2022-05-31 13:05:06 +00:00
acceleration = 0.1 #0.5
frequency= 100
dt = 1.0/frequency # 2ms
lookahead_time = 0.1
gain = 300
2022-05-31 14:18:56 +00:00
speed=1/frequency
2022-05-31 13:05:06 +00:00
def constrain(v,_min,_max):
return min(_max,max(_min,v))
2022-05-31 14:18:56 +00:00
print("Press s to start!")
while not keyboard.is_pressed("s"):
time.sleep(0.1)
2022-05-31 13:05:06 +00:00
print("Starting Servo")
2022-05-31 14:18:56 +00:00
while not keyboard.is_pressed("q"):
2022-05-31 13:05:06 +00:00
start = time.time()
2022-05-31 14:18:56 +00:00
# Receive Socket stuff
try:
client, address = s.accept()
print("Connected to", address)
except socket.error:
pass
if client is not None:
try:
data = client.recv( 1024 ).decode( 'utf-8' )
if data:
lastdata=data.split('\n')
#print("Received :", repr(data))
if (len(lastdata)>1):
splitdata=lastdata[-2].split(",")
try:
_pan=float(splitdata[0])
_tilt=float(splitdata[1])
winkellimit=45
if _pan>=-winkellimit and _pan <=winkellimit and _tilt>=-winkellimit and _tilt <=winkellimit:
pan=_pan
tilt=_tilt
print("Pan="+str(pan)+" Tilt="+str(tilt))
except ValueError:
print("Not a float")
except socket.error:
pass
# Robot Control stuff
2022-05-31 13:05:06 +00:00
2022-05-31 14:18:56 +00:00
joint_q_aim = [0,toRad(-90),toRad(90),toRad(-180-tilt),toRad(-90-pan),toRad(0)]
2022-05-31 13:05:06 +00:00
for i in range(6):
joint_q[i] += constrain(joint_q_aim[i]-joint_q[i], -speed,speed)
2022-05-31 14:18:56 +00:00
#print(joint_q)
2022-05-31 13:05:06 +00:00
rtde_c.servoJ(joint_q, velocity, acceleration, dt, lookahead_time, gain)
end = time.time()
stepduration = end - start
if stepduration < dt:
time.sleep(dt - stepduration)
print("Finished")
rtde_c.servoStop()
rtde_c.stopScript()
2022-05-31 14:18:56 +00:00
s.close()