PX4 Teleop
px4_teleop_key.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import math
4 
5 import curses
6 import rospy
7 
8 from geometry_msgs.msg import PoseStamped
9 
10 from mavros_msgs.srv import CommandBool
11 from mavros_msgs.srv import CommandBoolRequest
12 
13 from mavros_msgs.srv import SetMode
14 from mavros_msgs.srv import SetModeRequest
15 
16 from mavros_msgs.msg import State
17 
18 current_state = State()
19 
20 def addstrln(scr, string, posx=0, posy=0):
21  scr.addstr(posx, posy, string)
22  scr.insertln()
23 
24 
25 def show_key_config(scr):
26  addstrln(scr, "ctrl-c: quit")
27  addstrln(scr, "right arrow: turn right")
28  addstrln(scr, "left arrow: turn left")
29  addstrln(scr, "up arrow: upward")
30  addstrln(scr, "down arrow: downward")
31  addstrln(scr, "d: right")
32  addstrln(scr, "s: backward")
33  addstrln(scr, "a: left")
34  scr.addstr(0, 0, "w: forward")
35 
36 
37 def publish_pos(pub, msg, key):
38  if key==119:
39  # w
40  msg.header.stamp = rospy.Time.now()
41  msg.pose.position.x += 0.1
42  pub.publish(msg)
43  elif key==97:
44  # a
45  msg.header.stamp = rospy.Time.now()
46  msg.pose.position.y += 0.1
47  pub.publish(msg)
48  elif key==115:
49  # s
50  msg.header.stamp = rospy.Time.now()
51  msg.pose.position.x -= 0.1
52  pub.publish(msg)
53  elif key==100:
54  # d
55  msg.header.stamp = rospy.Time.now()
56  msg.pose.position.y -= 0.1
57  pub.publish(msg)
58  elif key==260:
59  # <-
60  msg.header.stamp = rospy.Time.now()
61  msg.pose.orientation.z = math.sin(0.5)
62  msg.pose.orientation.w = math.cos(0.5)
63  pub.publish(msg)
64  elif key==259:
65  # up arrow
66  msg.header.stamp = rospy.Time.now()
67  msg.pose.position.z += 0.1
68  pub.publish(msg)
69  elif key==261:
70  # ->
71  msg.header.stamp = rospy.Time.now()
72  msg.pose.orientation.z = math.sin(-0.5)
73  msg.pose.orientation.w = math.cos(-0.5)
74  pub.publish(msg)
75  elif key==258:
76  # down arrow
77  msg.header.stamp = rospy.Time.now()
78  msg.pose.position.z -= 0.1
79  pub.publish(msg)
80  else:
81  pub.publish(msg)
82 
83 
84 def state_cb(msg):
85  current_state = msg
86 
87 
89  rospy.init_node("px4_teleop_key_pub", anonymous=True)
90  rospy.loginfo("Node Initialized")
91 
92  state_sub = rospy.Subscriber("mavros/state", State, state_cb, queue_size=10)
93  pos_teleop_pub = rospy.Publisher("mavros/setpoint_position/local", PoseStamped, queue_size=10)
94 
95  arming_client = rospy.ServiceProxy("mavros/cmd/arming", CommandBool)
96  set_mode_client = rospy.ServiceProxy("mavros/set_mode", SetMode)
97  rospy.loginfo("Subscriber, Publisher and Service Clients are initilized")
98 
99  rate = rospy.Rate(20.0)
100 
101  while not rospy.is_shutdown() and current_state.connected:
102  rate.sleep()
103 
104  pos_teleop_msg = PoseStamped()
105  pos_teleop_msg.header.stamp = rospy.Time.now()
106  pos_teleop_msg.pose.position.x = 0.
107  pos_teleop_msg.pose.position.y = 0.
108  pos_teleop_msg.pose.position.z = 2.0
109 
110  for i in range(100):
111  pos_teleop_pub.publish(pos_teleop_msg)
112  rate.sleep()
113 
114  set_mode_req = SetModeRequest()
115  set_mode_req.custom_mode = "OFFBOARD"
116 
117  arm_cmd_req = CommandBoolRequest()
118  arm_cmd_req.value = True
119 
120  while not set_mode_client(set_mode_req).success:
121  pass
122 
123  rospy.loginfo("Offboard enabled")
124 
125  while not arming_client(arm_cmd_req).success:
126  pass
127 
128  rospy.loginfo("Vehicle armed")
129 
130  pos_teleop_pub.publish(pos_teleop_msg)
131 
132  try:
133  stdscr = curses.initscr()
134  curses.noecho()
135  curses.cbreak()
136  stdscr.keypad(1)
137  show_key_config(stdscr)
138 
139  while not rospy.is_shutdown():
140  if not current_state.mode=="OFFBOARD":
141  set_mode_client(set_mode_req)
142  op = stdscr.getch()
143  publish_pos(pos_teleop_pub, pos_teleop_msg, op)
144  rate.sleep()
145 
146  finally:
147  curses.nocbreak()
148  stdscr.keypad(0)
149  curses.echo()
150  curses.endwin()
151 
152 if __name__=="__main__":
153  try:
155  except rospy.ROSInterruptException:
156  pass
def addstrln(scr, string, posx=0, posy=0)
def state_cb(msg)
def show_key_config(scr)
def publish_pos(pub, msg, key)