urcontrol/urosc/ur.py

142 lines
4.8 KiB
Python
Raw Normal View History

2024-05-10 09:17:19 +00:00
from helpfunctions import *
import time
class UR:
import rtde_receive
import rtde_control
def __init__(self,_joint_min,_joint_max,_frequency_rtde_send=500,_frequency_rtde_receive=500):
self.frequency_rtde_send=_frequency_rtde_send
self.frequency_rtde_receive=_frequency_rtde_receive
self.controlEnabled=False
2024-05-10 09:17:19 +00:00
self.last_receivertde=0
self.last_sendrtde=0
self.joint_min=_joint_min
self.joint_max=_joint_max
self.rtde_r=None
self.rtde_c=None
2024-05-10 15:18:08 +00:00
self.last_forwardkinematics=None
self.last_forwardkinematics_joint_q_received=None
2024-05-10 09:17:19 +00:00
# Parameters
self.velocity = 0.1
self.acceleration = 0.1
self.lookahead_time = 0.1
self.gain = 300
self.joint_q_aim=[0,0,0,0,0,0] #joint rotations aiming for
self.joint_q = [0,0,0,0,0,0] #joint rotation last sent
2024-05-10 15:18:08 +00:00
self.velocitylimit=[1,1,1,3,3,3] #joint velocity limits
#self.velocitylimit=[0.2,0.2,0.2,0.2,0.2,0.2] #joint velocity limits
2024-05-10 09:17:19 +00:00
self.joint_q_received=[0,0,0,0,0,0]
2024-05-10 15:18:08 +00:00
self.tcppose_received=[0,0,0,0,0,0]
2024-05-10 09:17:19 +00:00
def connect(self,robotip):
self.rtde_r = self.rtde_receive.RTDEReceiveInterface(robotip, self.frequency_rtde_receive)
2024-05-10 09:17:19 +00:00
self.rtde_c = self.rtde_control.RTDEControlInterface(robotip)
2024-05-10 09:17:19 +00:00
def setJointQAim(self,q):
self.joint_q_aim=q
def isJointsWithinSafetyLimits(self,q):
return self.rtde_c.isJointsWithinSafetyLimits(q)
def enableControl(self):
if time.time()-self.last_receivertde<0.1: #if received position is recent
self.joint_q=self.joint_q_received #update current position to received position
self.joint_q_aim=self.joint_q_received
self.controlEnabled=True
def disableControl(self):
self.controlEnabled=False
2024-05-10 09:17:19 +00:00
def isControlEnabled(self):
return self.controlEnabled
2024-05-10 09:17:19 +00:00
def send(self,looptime):
if looptime-self.last_sendrtde<1.0/self.frequency_rtde_send:
return
self.last_sendrtde=looptime
if self.controlEnabled:
for i in range(6): #update joint_q with max speeds
self.joint_q[i] += constrain(self.joint_q_aim[i]-self.joint_q[i], -self.velocitylimit[i]/self.frequency_rtde_send,self.velocitylimit[i]/self.frequency_rtde_send) #constrain joint speeds
2024-05-10 09:17:19 +00:00
if self.rtde_c is not None:
self.rtde_c.servoJ(self.joint_q, self.velocity, self.acceleration, 1.0/self.frequency_rtde_send, self.lookahead_time, self.gain)
2024-05-10 09:17:19 +00:00
def receiveJoints(self,looptime):
2024-05-10 09:17:19 +00:00
#Get robot joint angles
if looptime-self.last_receivertde<1.0/self.frequency_rtde_receive:
return
self.last_receivertde=looptime
if self.rtde_r is not None:
self.joint_q_received=self.rtde_r.getActualQ()
2024-05-10 15:18:08 +00:00
self.tcppose_received=self.rtde_r.getActualTCPPose()
else:
_offsets=[12.2,5.3,0,6,12.4,612.91]
_factors=[1.2,1.14,1.214,0.9921,1,1.092]
for _i,_offset in enumerate(_offsets): #generate mockup values
self.joint_q_received[_i]=(math.sin(time.time()*_factors[_i]+_offset)/2.0+0.5)*(self.joint_max[_i]-self.joint_min[_i])+self.joint_min[_i]
def receiveStatus(self,looptime):
if looptime-self.last_receivertde<1.0/self.frequency_rtde_receive:
return
self.last_receivertde=looptime
self.status_connected=self.rtde_r.isConnected()
self.status_timestamp=self.rtde_r.getTimestamp()
self.status_protectivestopped=self.rtde_r.isProtectiveStopped()
self.status_emergencystopped=self.rtde_r.isEmergencyStopped()
self.status_robotstatus=self.rtde_c.getRobotStatus()
'''
self.status_=self.rtde_r.()
self.status_=self.rtde_r.()
self.status_=self.rtde_r.()
self.status_=self.rtde_r.()
self.status_=self.rtde_r.()
self.status_=self.rtde_r.()
self.status_=self.rtde_r.()
'''
2024-05-10 09:17:19 +00:00
def getReceivedQ(self):
return self.joint_q_received
2024-05-10 15:18:08 +00:00
'''def getForwardKinematics(self):
if self.last_forwardkinematics_joint_q_received!=self.joint_q_received:
self.last_forwardkinematics_joint_q_received=self.joint_q_received
self.last_forwardkinematics=self.rtde_c.getForwardKinematics(self.joint_q_received)
return self.last_forwardkinematics
'''
def getReceivedTCPPose(self):
return self.tcppose_received
def getInverseKinematics(self,pos_q):
return self.rtde_c.getInverseKinematics(pos_q,self.joint_q)
2024-05-10 09:17:19 +00:00
def disconnect(self):
if self.rtde_c is not None:
self.rtde_c.servoStop()
self.rtde_c.stopScript()
2024-05-10 09:17:19 +00:00