controller.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import rospy
4 from sensor_msgs.msg import Joy
5 from crazyflie_driver.srv import UpdateParams
6 from std_srvs.srv import Empty
7 
8 class Controller():
9  def __init__(self, use_controller, joy_topic):
10  rospy.wait_for_service('update_params')
11  rospy.loginfo("found update_params service")
12  self._update_params = rospy.ServiceProxy('update_params', UpdateParams)
13 
14  rospy.loginfo("waiting for emergency service")
15  rospy.wait_for_service('emergency')
16  rospy.loginfo("found emergency service")
17  self._emergency = rospy.ServiceProxy('emergency', Empty)
18 
19  if use_controller:
20  rospy.loginfo("waiting for land service")
21  rospy.wait_for_service('land')
22  rospy.loginfo("found land service")
23  self._land = rospy.ServiceProxy('land', Empty)
24 
25  rospy.loginfo("waiting for takeoff service")
26  rospy.wait_for_service('takeoff')
27  rospy.loginfo("found takeoff service")
28  self._takeoff = rospy.ServiceProxy('takeoff', Empty)
29  else:
30  self._land = None
31  self._takeoff = None
32 
33  # subscribe to the joystick at the end to make sure that all required
34  # services were found
35  self._buttons = None
36  rospy.Subscriber(joy_topic, Joy, self._joyChanged)
37 
38  def _joyChanged(self, data):
39  for i in range(0, len(data.buttons)):
40  if self._buttons == None or data.buttons[i] != self._buttons[i]:
41  if i == 0 and data.buttons[i] == 1 and self._land != None:
42  self._land()
43  if i == 1 and data.buttons[i] == 1:
44  self._emergency()
45  if i == 2 and data.buttons[i] == 1 and self._takeoff != None:
46  self._takeoff()
47  if i == 4 and data.buttons[i] == 1:
48  value = int(rospy.get_param("ring/headlightEnable"))
49  if value == 0:
50  rospy.set_param("ring/headlightEnable", 1)
51  else:
52  rospy.set_param("ring/headlightEnable", 0)
53  self._update_params(["ring/headlightEnable"])
54  print(not value)
55 
56  self._buttons = data.buttons
57 
58 if __name__ == '__main__':
59  rospy.init_node('crazyflie_demo_controller', anonymous=True)
60  use_controller = rospy.get_param("~use_crazyflie_controller", False)
61  joy_topic = rospy.get_param("~joy_topic", "joy")
62  controller = Controller(use_controller, joy_topic)
63  rospy.spin()
def __init__(self, use_controller, joy_topic)
Definition: controller.py:9
def _joyChanged(self, data)
Definition: controller.py:38


crazyflie_demo
Author(s): Wolfgang Hoenig
autogenerated on Mon Sep 28 2020 03:40:12