SHOW:
|
|
- or go back to the newest paste.
1 | - | #!/usr/bin/env python |
1 | + | |
2 | - | # -*- coding: utf-8 -*- |
2 | + | |
3 | from std_srvs.srv import Trigger | |
4 | import math | |
5 | ||
6 | rospy.init_node('flight') | |
7 | ||
8 | - | import csv |
8 | + | |
9 | navigate = rospy.ServiceProxy('navigate', srv.Navigate) | |
10 | land = rospy.ServiceProxy('land', Trigger) | |
11 | ||
12 | - | # Define service proxies |
12 | + | def wait_arrival(tolerance=0.2): |
13 | while not rospy.is_shutdown(): | |
14 | telem = get_telemetry(frame_id='navigate_target') | |
15 | if math.sqrt(telem.x ** 2 + telem.y ** 2 + telem.z ** 2) < tolerance: | |
16 | break | |
17 | - | def wait_arrival(tolerance=0.2, debug=False, log_writer=None): |
17 | + | rospy.sleep(0.2) |
18 | - | """ |
18 | + | |
19 | - | Waits until the drone reaches the target position within a certain tolerance. |
19 | + | |
20 | - | Logs telemetry data to the provided CSV writer during the wait. |
20 | + | # Взлетаем на высоту 2 метра |
21 | - | """ |
21 | + | navigate(x=0, y=0, z=2, frame_id='body', auto_arm=True) |
22 | - | rate = rospy.Rate(5) # 5 Hz |
22 | + | wait_arrival() |
23 | ||
24 | # Пролет по квадрату (5 метров на сторону) | |
25 | - | distance = math.sqrt(telem.x ** 2 + telem.y ** 2 + telem.z ** 2) |
25 | + | navigate(x=5, y=0, z=0, frame_id='body') |
26 | - | |
26 | + | wait_arrival() |
27 | - | if debug: |
27 | + | |
28 | - | rospy.loginfo(f"Current distance to target: {distance}") |
28 | + | navigate(x=0, y=5, z=0, frame_id='body') |
29 | - | |
29 | + | wait_arrival() |
30 | - | if log_writer: |
30 | + | |
31 | - | global_telem = get_telemetry(frame_id='map') |
31 | + | navigate(x=-5, y=0, z=0, frame_id='body') |
32 | - | log_writer.writerow([rospy.get_time(), global_telem.x, global_telem.y, global_telem.z, global_telem.yaw]) |
32 | + | wait_arrival() |
33 | - | |
33 | + | |
34 | - | if distance < tolerance: |
34 | + | navigate(x=0, y=-5, z=0, frame_id='body') |
35 | wait_arrival() | |
36 | - | rate.sleep() |
36 | + | |
37 | # Возвращение в исходную точку (центральную метку) | |
38 | navigate(x=0, y=0, z=0, frame_id='body') | |
39 | - | def navigate_and_wait(x, y, z, frame_id='body', auto_arm=False, debug=False, log_writer=None): |
39 | + | wait_arrival() |
40 | - | """ |
40 | + | |
41 | - | Sends the drone to the specified coordinates and waits until arrival. |
41 | + | # Получаем телеметрию и выводим её в консоль |
42 | - | """ |
42 | + | telemetry = get_telemetry() |
43 | - | navigate(x=x, y=y, z=z, frame_id=frame_id, auto_arm=auto_arm) |
43 | + | print(f"Telemetry: x={telemetry.x}, y={telemetry.y}, z={telemetry.z}, yaw={telemetry.yaw}, vx={telemetry.vx}, vy={telemetry.vy}, vz={telemetry.vz}") |
44 | - | if debug: |
44 | + | |
45 | - | rospy.loginfo(f"Navigating to: x={x}, y={y}, z={z}, frame_id={frame_id}") |
45 | + | # Посадка |
46 | - | wait_arrival(debug=debug, log_writer=log_writer) |
46 | + | land() |
47 |