Skip to content

Commit

Permalink
Fix feedback and status message not being published after a while
Browse files Browse the repository at this point in the history
Fixes #34
  • Loading branch information
naoki-mizuno committed Feb 23, 2023
1 parent 4d2b82e commit bdade90
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
3 changes: 3 additions & 0 deletions ds4_driver/config/params.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ ds4_driver:
# Amount by which the joystick has to move before it is considered to be
# off-center.
deadzone: 0.1

# Maximum rate in Hz to publish Status messages.
max_status_rate: 100.0
14 changes: 13 additions & 1 deletion ds4_driver/ds4_driver/controller_ros.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@ def __init__(self, node):
self.node.declare_parameter("frame_id", "ds4")
self.node.declare_parameter("imu_frame_id", "ds4_imu")
self.node.declare_parameter("autorepeat_rate", 0.0)
self.node.declare_parameter("max_status_rate", 100.0)

self.use_standard_msgs = self.node.get_parameter("use_standard_msgs").value
self.deadzone = self.node.get_parameter("deadzone").value
self.frame_id = self.node.get_parameter("frame_id").value
self.imu_frame_id = self.node.get_parameter("imu_frame_id").value
# Only publish Joy messages on change
self._autorepeat_rate = self.node.get_parameter("autorepeat_rate").value
self._max_status_rate = self.node.get_parameter("max_status_rate").value
self._prev_joy = None

self.stop_rumble_timer = None

self._last_status_publish_time = None

# Use ROS-standard messages (like sensor_msgs/Joy)
if self.use_standard_msgs:
self.pub_report = self.node.create_publisher(Report, "raw_report", 0)
Expand All @@ -61,9 +65,15 @@ def cb_report(self, report):
:param report:
:return:
"""
now = self.node.get_clock().now()
if self._max_status_rate > 0 and self._last_status_publish_time is not None:
dt = (now - self._last_status_publish_time).nanoseconds / 1e9
if dt < (1 / self._max_status_rate):
return

report_msg = Report()
report_msg.header.frame_id = self.frame_id
report_msg.header.stamp = self.node.get_clock().now().to_msg()
report_msg.header.stamp = now.to_msg()
for attr in dir(report):
if attr.startswith("_"):
continue
Expand Down Expand Up @@ -100,6 +110,8 @@ def cb_report(self, report):
else:
self.pub_status.publish(status_msg)

self._last_status_publish_time = now

def cb_feedback(self, msg):
"""
Callback method for ds4_driver/Feedback
Expand Down
6 changes: 2 additions & 4 deletions ds4_driver/nodes/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ def cb_status(self, msg):
"""
now = self._node.get_clock().now()

# Commenting out this check for now because to_sec() does not seem to work for rclpy Duration objects
# This checks to see if the min interval for a callback has been violated.
# if (now - self._last_pub_time).to_sec() < self._min_interval:
# return
if (now - self._last_pub_time).nanoseconds / 1e9 < self._min_interval:
return

feedback = Feedback()

Expand Down

0 comments on commit bdade90

Please sign in to comment.