-
Notifications
You must be signed in to change notification settings - Fork 1
/
drone_commander.py
138 lines (109 loc) · 3.46 KB
/
drone_commander.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import rospy
from geometry_msgs.msg import Twist
from std_msgs.msg import String
from std_msgs.msg import Empty
import sys
import os
import rospy
last_command = ""
def abort(data):
print("Emergency!")
os._exit(1)
def commander(data):
global last_command
#rospy.Subscriber("/obstacle_finder/cmd", String, commander).unregister()
rate = rospy.Rate(5) # 5hz
#print("Received :", data)
# print(type(data))
command = data.data
print(command)
time = 0.6
final_time = 6
takeoff_publisher = rospy.Publisher(
'bebop/takeoff', Empty, queue_size=1)
landing_publisher = rospy.Publisher(
'bebop/land', Empty, queue_size=1)
movement_publisher = rospy.Publisher(
'bebop/cmd_vel', Twist, queue_size=1)
if command == "GO":
print("Forward")
counter = 0.0
movement_cmd = Twist()
while not rospy.is_shutdown():
movement_cmd.linear.x = 0.1
movement_cmd.linear.y = 0
movement_cmd.linear.z = 0
movement_cmd.angular.x = 0
movement_cmd.angular.y = 0
movement_cmd.angular.z = 0
counter += 0.2
movement_publisher.publish(movement_cmd)
rate.sleep()
print(counter)
if counter >= time:
#command = ""
break
if command == "STOP":
print("Stop")
counter = 0.0
movement_cmd = Twist()
while not rospy.is_shutdown():
movement_cmd.linear.x = 0
movement_cmd.linear.y = 0
movement_cmd.linear.z = 0
movement_cmd.angular.x = 0
movement_cmd.angular.y = 0
movement_cmd.angular.z = 0
counter += 0.2
movement_publisher.publish(movement_cmd)
rate.sleep()
if counter >= time:
#command = ""
break
if command == "APPROACH":
print("Close")
counter = 0.0
movement_cmd = Twist()
while not rospy.is_shutdown():
movement_cmd.linear.x = 0.1
movement_cmd.linear.y = 0
movement_cmd.linear.z = 0
movement_cmd.angular.x = 0
movement_cmd.angular.y = 0
movement_cmd.angular.z = 0
counter += 0.2
movement_publisher.publish(movement_cmd)
rate.sleep()
if counter >= final_time:
movement_cmd = Empty()
while not rospy.is_shutdown():
landing_publisher.publish(movement_cmd)
counter += 0.2
rate.sleep()
if counter >= time:
print("Landing!")
os._exit(1)
if command == 'TAKEOFF':
counter = 0.0
movement_cmd = Empty()
while not rospy.is_shutdown():
takeoff_publisher.publish(movement_cmd)
counter += 0.05
rate.sleep()
if counter >= time:
#command = ""
break
def setup():
print("Commander starting...")
rospy.init_node("bebop_commander")
rospy.Subscriber('chatter', String, abort)
rospy.Subscriber('obstacle_finder/cmd', String,
commander, queue_size=1, buff_size=2**12)
rate = rospy.Rate(5) # 5hz
print("Commander ready")
rospy.spin()
if __name__ == '__main__':
try:
setup()
except rospy.ROSInterruptException:
pass