demo.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import rospy
4 from ds4_driver.msg import Feedback, Status
5 
6 
7 class Handler(object):
8  def __init__(self, status_topic='status', feedback_topic='set_feedback'):
9  self._min_interval = 0.1
10  self._last_pub_time = rospy.Time()
11  self._prev = Status()
12  self._led = {
13  'r': 0,
14  'g': 0,
15  'b': 0,
16  'flashing': False,
17  }
18 
19  self._pub_feedback = rospy.Publisher(feedback_topic, Feedback, queue_size=1)
20  rospy.Subscriber(status_topic, Status, self.cb_status, queue_size=1)
21 
22  def cb_status(self, msg):
23  """
24  :type msg: Status
25  """
26  now = rospy.Time.now()
27  if (now - self._last_pub_time).to_sec() < self._min_interval:
28  return
29 
30  feedback = Feedback()
31 
32  feedback.set_rumble = True
33  feedback.rumble_small = abs(msg.axis_left_y)
34  feedback.rumble_big = abs(msg.axis_right_y)
35 
36  # Set LED using the touchpad
37  touch = msg.touch0
38  if touch.active and msg.button_circle:
39  feedback.set_led = True
40  self._led['r'] = touch.x
41  if touch.active and msg.button_triangle:
42  feedback.set_led = True
43  self._led['g'] = touch.x
44  if touch.active and msg.button_cross:
45  feedback.set_led = True
46  self._led['b'] = touch.x
47  feedback.led_r = self._led['r']
48  feedback.led_g = self._led['g']
49  feedback.led_b = self._led['b']
50 
51  # Turn on/off flash with PS button
52  if not self._prev.button_ps and msg.button_ps:
53  feedback.set_led_flash = True
54  if self._led['flashing']:
55  feedback.led_flash_off = 0
56  else:
57  feedback.led_flash_on = 0.2
58  feedback.led_flash_off = 0.2
59  self._led['flashing'] = not self._led['flashing']
60 
61  self._pub_feedback.publish(feedback)
62  self._prev = msg
63  self._last_pub_time = now
64 
65 
66 def main():
67  rospy.init_node('sample')
68 
69  Handler()
70 
71  rospy.spin()
72 
73 
74 if __name__ == '__main__':
75  main()
def __init__(self, status_topic='status', feedback_topic='set_feedback')
Definition: demo.py:8
_last_pub_time
Definition: demo.py:10
_min_interval
Definition: demo.py:9
def cb_status(self, msg)
Definition: demo.py:22
_pub_feedback
Definition: demo.py:19
def main()
Definition: demo.py:66


ds4_driver
Author(s): Naoki Mizuno
autogenerated on Fri May 1 2020 03:25:46