Skip to content

Commit

Permalink
[Polymetis] Fix modbus exception handling (#1384)
Browse files Browse the repository at this point in the history
* Fix exception handling

* Add error code to indicate a gripper read fail

* Pin pymodbus version

* Add warning
  • Loading branch information
exhaustin authored Oct 11, 2022
1 parent c27dd42 commit c1b8912
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion polymetis/polymetis/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ dependencies:
- pip
- protobuf
- pybullet ==3.17
- pymodbus
- pymodbus ==2.5.3
- pyserial
- pytest
- pytest-benchmark
Expand Down
1 change: 1 addition & 0 deletions polymetis/polymetis/protos/polymetis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ message GripperState {
float width = 2;
bool is_grasped = 3;
bool is_moving = 4;
int32 error_code = 5;
}

message LogInterval {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import time
import logging
from concurrent import futures

import grpc
Expand All @@ -17,6 +18,8 @@
Robotiq2FingerGripper,
)

log = logging.getLogger(__name__)


class RobotiqGripperClient:
"""gRPC client that exposes controls of a Robotiq gripper to a PolymetisGripperServer
Expand Down Expand Up @@ -66,9 +69,15 @@ def __init__(self, server_ip, server_port, comport="/dev/ttyUSB0", hz=60):
self.connection.InitRobotClient(metadata)

def get_gripper_state(self):
self.gripper.getStatus()

state = polymetis_pb2.GripperState()

if not self.gripper.getStatus():
# NOTE: getStatus returns False and does not update state if modbus read fails
state.error_code = 1
log.warning(
"Failed to read gripper state. Returning last observed state instead."
)

state.timestamp.GetCurrentTime()
state.width = self.gripper.get_pos()
state.is_grasped = self.gripper.object_detected()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"""

from pymodbus.client.sync import ModbusSerialClient
from pymodbus.exceptions import ModbusIOException
from math import ceil


Expand Down Expand Up @@ -113,6 +114,9 @@ def getStatus(self, numBytes):
if response is None:
# print("Failed to receive status")
return None
# Newer versions of pymodbus returns a ModbusIOException instead
elif type(response) is ModbusIOException:
return None

# Instantiate output as an empty list
output = []
Expand Down

0 comments on commit c1b8912

Please sign in to comment.