Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added 2fg7 support #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
344 changes: 344 additions & 0 deletions onRobot/onRobot/gripper.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,350 @@ def rg_grip(self, target_width: float = 100, target_force: float= 10) -> bool:
# Close the cURL object
curl.close()


class TwoFG7():
def __init__(self, robot_ip: str, id: int):
self.robot_ip = robot_ip
self.id = id

self.max_force = self.twofg_get_max_force()
self.max_ext_width = self.twofg_get_max_external_width()
self.max_int_width = self.twofg_get_max_internal_width()
self.min_ext_width = self.twofg_get_min_external_width()
self.min_int_width = self.twofg_get_min_internal_width()

self.gripper_width = [self.twofg_get_external_width(), self.twofg_get_internal_width()]


def _send_xml_rpc_request(self, _req=None):

headers = ["Content-Type: application/x-www-form-urlencoded"]

data = _req.replace('\r\n','').encode()

# Create a new cURL object
curl = pycurl.Curl()

# Set the URL to fetch
curl.setopt(curl.URL, f'http://{self.robot_ip}:41414')
curl.setopt(curl.HTTPHEADER, headers)
curl.setopt(curl.POSTFIELDS, data)
# Create a BytesIO object to store the response
buffer = BytesIO()
curl.setopt(curl.WRITEDATA, buffer)

# Perform the request
curl.perform()

# Get the response body
response = buffer.getvalue()

# Print the response
print(response.decode('utf-8'))

# Close the cURL object
curl.close()
# Get response from xmlrpc server
xml_response = xmlrpc.client.loads(response.decode('utf-8'))

return xml_response[0][0]

def twofg_get_external_width(self) -> float:
xml_request = f"""<?xml version="1.0"?>
<methodCall>
<methodName>twofg_get_external_width</methodName>
<params>
<param>
<value><int>{self.id}</int></value>
</param>
</params>
</methodCall>"""

return float(self._send_xml_rpc_request(xml_request))

def twofg_get_internal_width(self) -> float:
xml_request = f"""<?xml version="1.0"?>
<methodCall>
<methodName>twofg_get_internal_width</methodName>
<params>
<param>
<value><int>{self.id}</int></value>
</param>
</params>
</methodCall>"""

return float(self._send_xml_rpc_request(xml_request))


def twofg_grip_external(self, target_width: float = 40.00, target_force: int= 20, speed: int=1) -> int:
"""
speed is the range from 1 to 100. It represents the percentage of the maximum speed.
"""
assert target_width <= self.max_ext_width and target_width >= self.min_ext_width, f'Target Width must be within the range [{self.min_ext_width},{self.max_ext_width}]'
assert target_force <= self.max_force or target_force >= 20, f'Target force must be within the range [20,{self.max_force}]'

# WARNING: params will be sent straight to electrical system with no error checking on robot!
if (target_width > self.max_ext_width):
target_width = self.max_ext_width
if(target_width < self.min_ext_width):
target_width = self.min_ext_width
if(target_force > self.max_force):
target_force = self.max_force
if(target_force < 20):
target_force = 20

xml_request = f"""<?xml version="1.0"?>
<methodCall>
<methodName>twofg_grip_external</methodName>
<params>
<param>
<value><int>{self.id}</int></value>
</param>
<param>
<value><double>{target_width}</double></value>
</param>
<param>
<value><int>{target_force}</int></value>
</param>
<param>
<value><int>{speed}</int></value>
</param>
</params>
</methodCall>"""

# if status != 0, then command not succesful. Perhaps there is no space to move the gripper
return int(self._send_xml_rpc_request(xml_request))

def twofg_ext_release(self, target_width: float = 40.00, speed: int=1) -> int:
"""
speed is the range from 1 to 100. It represents the percentage of the maximum speed.
"""
target_force: int= 80

assert target_width <= self.max_ext_width and target_width >= self.min_ext_width, f'Target Width must be within the range [{self.min_ext_width},{self.max_ext_width}]'
assert target_force <= self.max_force or target_force >= 20, f'Target force must be within the range [20,{self.max_force}]'

# WARNING: params will be sent straight to electrical system with no error checking on robot!
if (target_width > self.max_ext_width):
target_width = self.max_ext_width
if(target_width < self.min_ext_width):
target_width = self.min_ext_width
if(target_force > self.max_force):
target_force = self.max_force
if(target_force < 20):
target_force = 20

xml_request = f"""<?xml version="1.0"?>
<methodCall>
<methodName>twofg_grip_external</methodName>
<params>
<param>
<value><int>{self.id}</int></value>
</param>
<param>
<value><double>{target_width}</double></value>
</param>
<param>
<value><int>{target_force}</int></value>
</param>
<param>
<value><int>{speed}</int></value>
</param>
</params>
</methodCall>"""

# if status != 0, then command not succesful. Perhaps there is no space to move the gripper
return int(self._send_xml_rpc_request(xml_request))

def twofg_grip_internal(self, target_width: float = 40.00, target_force: int= 10, speed: int=1) -> int:
"""
speed is the range from 1 to 100. It represents the percentage of the maximum speed.
"""
assert target_width <= self.max_int_width and target_width >= self.min_int_width, f'Target Width must be within the range [{self.min_int_width},{self.max_int_width}]'
assert target_force <= self.max_force or target_force >= 20, f'Target force must be within the range [20,{self.max_force}]'

# WARNING: params will be sent straight to electrical system with no error checking on robot!
if (target_width > self.max_int_width):
target_width = self.max_int_width
if(target_width < self.min_int_width):
target_width = self.min_int_width
if(target_force > self.max_force):
target_force = self.max_force
if(target_force < 20):
target_force = 20

xml_request = f"""<?xml version="1.0"?>
<methodCall>
<methodName>twofg_grip_internal</methodName>
<params>
<param>
<value><int>{self.id}</int></value>
</param>
<param>
<value><double>{target_width}</double></value>
</param>
<param>
<value><int>{target_force}</int></value>
</param>
<param>
<value><int>{speed}</int></value>
</param>
</params>
</methodCall>"""

# if status != 0, then command not succesful. Perhaps there is no space to move the gripper
return int(self._send_xml_rpc_request(xml_request))

def twofg_int_release(self, target_width: float = 40.00, speed: int=1) -> int:
"""
speed is the range from 1 to 100. It represents the percentage of the maximum speed.
"""
target_force: int= 80

assert target_width <= self.max_int_width and target_width >= self.min_int_width, f'Target Width must be within the range [{self.min_int_width},{self.max_int_width}]'
assert target_force <= self.max_force or target_force >= 20, f'Target force must be within the range [20,{self.max_force}]'

# WARNING: params will be sent straight to electrical system with no error checking on robot!
if (target_width > self.max_int_width):
target_width = self.max_int_width
if(target_width < self.min_int_width):
target_width = self.min_int_width
if(target_force > self.max_force):
target_force = self.max_force
if(target_force < 20):
target_force = 20

xml_request = f"""<?xml version="1.0"?>
<methodCall>
<methodName>twofg_grip_internal</methodName>
<params>
<param>
<value><int>{self.id}</int></value>
</param>
<param>
<value><double>{target_width}</double></value>
</param>
<param>
<value><int>{target_force}</int></value>
</param>
<param>
<value><int>{speed}</int></value>
</param>
</params>
</methodCall>"""

# if status != 0, then command not succesful. Perhaps there is no space to move the gripper
return int(self._send_xml_rpc_request(xml_request))

def twofg_get_max_external_width(self) -> float:
xml_request = f"""<?xml version="1.0"?>
<methodCall>
<methodName>twofg_get_max_external_width</methodName>
<params>
<param>
<value><int>{self.id}</int></value>
</param>
</params>
</methodCall>"""

return float(self._send_xml_rpc_request(xml_request))

def twofg_get_max_internal_width(self) -> float:
xml_request = f"""<?xml version="1.0"?>
<methodCall>
<methodName>twofg_get_max_internal_width</methodName>
<params>
<param>
<value><int>{self.id}</int></value>
</param>
</params>
</methodCall>"""

return float(self._send_xml_rpc_request(xml_request))

def twofg_get_min_external_width(self) -> float:
xml_request = f"""<?xml version="1.0"?>
<methodCall>
<methodName>twofg_get_min_external_width</methodName>
<params>
<param>
<value><int>{self.id}</int></value>
</param>
</params>
</methodCall>"""

return float(self._send_xml_rpc_request(xml_request))

def twofg_get_min_internal_width(self) -> float:
xml_request = f"""<?xml version="1.0"?>
<methodCall>
<methodName>twofg_get_min_internal_width</methodName>
<params>
<param>
<value><int>{self.id}</int></value>
</param>
</params>
</methodCall>"""

return float(self._send_xml_rpc_request(xml_request))

def twofg_get_max_force(self) -> int:
xml_request = f"""<?xml version="1.0"?>
<methodCall>
<methodName>twofg_get_max_force</methodName>
<params>
<param>
<value><int>{self.id}</int></value>
</param>
</params>
</methodCall>"""

return int(self._send_xml_rpc_request(xml_request))

def twofg_get_status(self) -> int:
# The status codes are not fully clear.
# sofar:
# 0: no grip
# 2: has gripped an object

xml_request = f"""<?xml version="1.0"?>
<methodCall>
<methodName>twofg_get_status</methodName>
<params>
<param>
<value><int>{self.id}</int></value>
</param>
</params>
</methodCall>"""

return int(self._send_xml_rpc_request(xml_request))

def twofg_get_busy(self) -> bool:
xml_request = f"""<?xml version="1.0"?>
<methodCall>
<methodName>twofg_get_busy</methodName>
<params>
<param>
<value><int>{self.id}</int></value>
</param>
</params>
</methodCall>"""

return bool(self._send_xml_rpc_request(xml_request))

def twofg_get_grip_detected(self) -> bool:
xml_request = f"""<?xml version="1.0"?>
<methodCall>
<methodName>twofg_get_grip_detected</methodName>
<params>
<param>
<value><int>{self.id}</int></value>
</param>
</params>
</methodCall>"""

return bool(self._send_xml_rpc_request(xml_request))

def main():

# Default id is zero, if you have multiple grippers,
Expand Down
41 changes: 41 additions & 0 deletions xml_rpc_on_robot/twofg/get_all_twofg_variables_response.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param><value><struct>
<member><name>busy</name>
<value><boolean>0</boolean></value></member>
<member><name>error_linear_sensor</name>
<value><boolean>0</boolean></value></member>
<member><name>error_not_calibrated</name>
<value><boolean>0</boolean></value></member>
<member><name>external_width</name>
<value><double>70.3000030517578</double></value></member>
<member><name>finger_height</name>
<value><double>45</double></value></member>
<member><name>finger_length</name>
<value><double>8.5</double></value></member>
<member><name>finger_orientation_outward</name>
<value><boolean>1</boolean></value></member>
<member><name>fingertip_offset</name>
<value><double>3.5</double></value></member>
<member><name>force</name>
<value><i4>2</i4></value></member>
<member><name>grip_detected</name>
<value><boolean>0</boolean></value></member>
<member><name>internal_width</name>
<value><double>84.3000030517578</double></value></member>
<member><name>max_external_width</name>
<value><double>71</double></value></member>
<member><name>max_force</name>
<value><i4>140</i4></value></member>
<member><name>max_internal_width</name>
<value><double>85</double></value></member>
<member><name>min_external_width</name>
<value><double>33</double></value></member>
<member><name>min_internal_width</name>
<value><double>47</double></value></member>
<member><name>status</name>
<value><i4>0</i4></value></member>
</struct></value></param>
</params>
</methodResponse>
Loading