{"id":248,"date":"2022-04-19T12:52:15","date_gmt":"2022-04-19T12:52:15","guid":{"rendered":"https:\/\/med.upc.edu\/team3-2021\/?p=248"},"modified":"2022-04-19T12:52:16","modified_gmt":"2022-04-19T12:52:16","slug":"dynamic-flight-plans","status":"publish","type":"post","link":"https:\/\/med.upc.edu\/team3-2021\/2022\/04\/19\/dynamic-flight-plans\/","title":{"rendered":"Dynamic flight plans"},"content":{"rendered":"\n<p>As mentioned in Activity 11, another option for controlling unmanned aircraft is dynamic flight planning. In general, in the drone platform, we use a Raspberry py as an onboard computer. The programs for our onboard computer are written in Python 2.7 by importing the Dronekit library. In this phase of the project, we use a drone simulator of Mission Planner software to notice if our phyton program is working. On the other hand, we connect our program with the simulated drone by using the Dronekit library.<\/p>\n\n\n\n<p>The diagram shown below illustrates the main algorithm of the program.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"664\" src=\"https:\/\/med.upc.edu\/team3-2021\/wp-content\/uploads\/sites\/4\/2022\/04\/Screen-Shot-2022-04-19-at-13.01.52-1-1024x664.png\" alt=\"\" class=\"wp-image-251\" srcset=\"https:\/\/med.upc.edu\/team3-2021\/wp-content\/uploads\/sites\/4\/2022\/04\/Screen-Shot-2022-04-19-at-13.01.52-1-1024x664.png 1024w, https:\/\/med.upc.edu\/team3-2021\/wp-content\/uploads\/sites\/4\/2022\/04\/Screen-Shot-2022-04-19-at-13.01.52-1-300x194.png 300w, https:\/\/med.upc.edu\/team3-2021\/wp-content\/uploads\/sites\/4\/2022\/04\/Screen-Shot-2022-04-19-at-13.01.52-1-768x498.png 768w, https:\/\/med.upc.edu\/team3-2021\/wp-content\/uploads\/sites\/4\/2022\/04\/Screen-Shot-2022-04-19-at-13.01.52-1-1536x996.png 1536w, https:\/\/med.upc.edu\/team3-2021\/wp-content\/uploads\/sites\/4\/2022\/04\/Screen-Shot-2022-04-19-at-13.01.52-1.png 1706w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>At the beginning of the program, it connects to the simulated drone in the Mission Planner using the Dronekit library. Then it directly checks if there is a valid mission (which can be any mission. For example, delivery service, ground surveying, etc&#8230; Important thing is that it has to include at least one waypoint and the mission has to be written to the autopilot by hitting &#8220;write&#8221; in the Mision Planner) available on the autopilot. If there is, it goes to the part of the program where it downloads the mission, executes take-off commands, and changes the mode to &#8220;Auto&#8221;. If there is no such mission on the autopilot, it waits.<\/p>\n\n\n\n<p>Once the algorithm starts for the mission as auto mode, it goes through every element (waypoints, camera shots, servo motor, etc..) until the last waypoint. After reaching the last waypoint, it switched the autopilot to Return To Land mode and erases the current flight plan (So that it is ready for getting a new flight plan). Then it goes directly to the home location and lands. At the end of the program, it jumps to the beginning. Now, therefore, the program is ready for getting the new mission. <\/p>\n\n\n\n<p>Outputs of the program is shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/med.upc.edu\/team3-2021\/wp-content\/uploads\/sites\/4\/2022\/04\/Screenshot-from-2022-04-19-14-34-41-1024x576.png\" alt=\"\" class=\"wp-image-252\" srcset=\"https:\/\/med.upc.edu\/team3-2021\/wp-content\/uploads\/sites\/4\/2022\/04\/Screenshot-from-2022-04-19-14-34-41-1024x576.png 1024w, https:\/\/med.upc.edu\/team3-2021\/wp-content\/uploads\/sites\/4\/2022\/04\/Screenshot-from-2022-04-19-14-34-41-300x169.png 300w, https:\/\/med.upc.edu\/team3-2021\/wp-content\/uploads\/sites\/4\/2022\/04\/Screenshot-from-2022-04-19-14-34-41-768x432.png 768w, https:\/\/med.upc.edu\/team3-2021\/wp-content\/uploads\/sites\/4\/2022\/04\/Screenshot-from-2022-04-19-14-34-41.png 1366w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In our case, we used the same flight plan as we used in activity 11 to investigate the interaction between the simulator drone and the python program. As result, we include the complete mission (Ground serveying) in the following photo.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/med.upc.edu\/team3-2021\/wp-content\/uploads\/sites\/4\/2022\/04\/Screenshot-from-2022-04-19-13-19-01-1024x576.png\" alt=\"\" class=\"wp-image-253\" srcset=\"https:\/\/med.upc.edu\/team3-2021\/wp-content\/uploads\/sites\/4\/2022\/04\/Screenshot-from-2022-04-19-13-19-01-1024x576.png 1024w, https:\/\/med.upc.edu\/team3-2021\/wp-content\/uploads\/sites\/4\/2022\/04\/Screenshot-from-2022-04-19-13-19-01-300x169.png 300w, https:\/\/med.upc.edu\/team3-2021\/wp-content\/uploads\/sites\/4\/2022\/04\/Screenshot-from-2022-04-19-13-19-01-768x432.png 768w, https:\/\/med.upc.edu\/team3-2021\/wp-content\/uploads\/sites\/4\/2022\/04\/Screenshot-from-2022-04-19-13-19-01.png 1366w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Source code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from dronekit import connect, VehicleMode, LocationGlobalRelative, Command, LocationGlobal\nimport time\nfrom pymavlink import mavutil\n\ndef arm_and_takeoff(altitude):\n\n   while not vehicle.is_armable:\n      print(\"waiting to be armable\")\n      time.sleep(1)\n\n   print(\"Arming motors\")\n   vehicle.mode = VehicleMode(\"GUIDED\")\n   vehicle.armed = True\n\n   while not vehicle.armed: time.sleep(1)\n\n   print(\"Taking Off\")\n   vehicle.simple_takeoff(altitude)\n\n   while True:\n      v_alt = vehicle.location.global_relative_frame.alt\n      print(\"&gt;&gt; Altitude = %.1f m\"%v_alt)\n      if v_alt &gt;= altitude - 1.0:\n          print(\"Target altitude reached\")\n          break\n      time.sleep(1)\n\ndef remove_mission(vehicle):\n    cmds = vehicle.commands\n    vehicle.commands.clear()\n    vehicle.flush()\n\n    cmds = vehicle.commands\n    cmds.download()\n    cmds.wait_ready()\n    \n\ndef download_mission(vehicle):\n    print \"Downloading mission\"\n    cmds = vehicle.commands\n    cmds.download()\n    cmds.wait_ready()\n    \n    missionList = &#091;]\n    n_WP        = 0\n    for wp in vehicle.commands:\n        missionList.append(wp)\n        n_WP += 1 \n        \n    return n_WP, missionList\n    \n\ndef add_home_as_last_wp(vehicle, wp_Last_Latitude, wp_Last_Longitude, wp_Last_Altitude):\n   \n    cmds = vehicle.commands\n    cmds.download()\n    cmds.wait_ready()\n\n    missionlist=&#091;]\n    for cmd in cmds:\n        missionlist.append(cmd)\n\n    wpLastObject = Command( 0, 0, 0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_WAYPOINT, 0, 0, 0, 0, 0, 0, \n                           wp_Last_Latitude, wp_Last_Longitude, wp_Last_Altitude)\n    missionlist.append(wpLastObject)\n\n    cmds.clear()\n\n    for cmd in missionlist:\n        cmds.add(cmd)\n    cmds.upload()\n    \n    return (cmds.count)    \n\ndef change_mode(vehicle, mode):\n    while vehicle.mode != VehicleMode(mode):\n            vehicle.mode = VehicleMode(mode)\n            time.sleep(0.5)\n    return True\n\ndef GND_mode(mode):\n    if mode == 'GROUND':\n        n_WP, missionList = download_mission(vehicle)\n        time.sleep(2)\n        if n_WP &gt; 0:\n            print (\"A mission uploaded: takeoff!\")\n            mode = 'TAKEOFF'\n    return mode\n\ndef TAKEOFF_mode(mode):\n    if mode == 'TAKEOFF':\n       \n        add_home_as_last_wp(vehicle, vehicle.location.global_relative_frame.lat, vehicle.location.global_relative_frame.lon, vehicle.location.global_relative_frame.alt)\n        print(\"Home waypoint added to the mission\")\n        time.sleep(1)\n        arm_and_takeoff(10)\n        print(\"Changing to AUTO\")\n        change_mode(vehicle,\"AUTO\")\n        vehicle.groundspeed = gnd_speed\n        mode = 'MISSION'\n        print (\"MISSION mode (AUTO)\")\n    return mode\n\ndef MISSION_mode(mode):\n    if mode == 'MISSION':\n        if vehicle.commands.next == vehicle.commands.count:\n            print (\"Final waypoint reached: go back home\")\n            remove_mission(vehicle)\n            print (\"Current mission removed\")\n            \n            change_mode(vehicle,\"RTL\")\n            mode = \"BACK\"\n    return mode\ndef AT_HOME_mode(mode):\n    if mode == \"BACK\":\n        if vehicle.location.global_relative_frame.alt &lt; 1:\n            print (\"GROUND mode, waiting for new missions\")\n            mode = 'GROUND'\n    return mode\n'''\n#-------------- Connection\n''' \ngnd_speed = 10 # &#091;m\/s]\nmode      = 'GROUND'\n\nprint('Connecting...')\nvehicle = connect('tcp:127.0.0.1:5762', wait_ready=True)\n\n\n'''\n#-------------- MAIN PROGRAMM  \n'''   \nwhile True:\n    \n    mode = GND_mode(mode)\n    mode = TAKEOFF_mode(mode)      \n    mode = MISSION_mode(mode)\n    mode = AT_HOME_mode(mode)     \n\n         \n    \n    time.sleep(0.5)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>As mentioned in Activity 11, another option for controlling unmanned aircraft is dynamic flight planning. In general, in the drone platform, we use a Raspberry py as an onboard computer. The programs for our onboard computer are written in Python 2.7 by importing the Dronekit library. In this phase of [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34,19,18],"tags":[38],"class_list":["post-248","post","type-post","status-publish","format-standard","hentry","category-advanced-mission","category-missionplanner","category-pixhawk","tag-activity-12"],"_links":{"self":[{"href":"https:\/\/med.upc.edu\/team3-2021\/wp-json\/wp\/v2\/posts\/248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/med.upc.edu\/team3-2021\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/med.upc.edu\/team3-2021\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/med.upc.edu\/team3-2021\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/med.upc.edu\/team3-2021\/wp-json\/wp\/v2\/comments?post=248"}],"version-history":[{"count":3,"href":"https:\/\/med.upc.edu\/team3-2021\/wp-json\/wp\/v2\/posts\/248\/revisions"}],"predecessor-version":[{"id":255,"href":"https:\/\/med.upc.edu\/team3-2021\/wp-json\/wp\/v2\/posts\/248\/revisions\/255"}],"wp:attachment":[{"href":"https:\/\/med.upc.edu\/team3-2021\/wp-json\/wp\/v2\/media?parent=248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/med.upc.edu\/team3-2021\/wp-json\/wp\/v2\/categories?post=248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/med.upc.edu\/team3-2021\/wp-json\/wp\/v2\/tags?post=248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}