Advertisement
Guest User

Olympe struggle

a guest
Oct 18th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.29 KB | None | 0 0
  1. # -*- coding: UTF-8 -*-
  2.  
  3. import olympe
  4. import threading
  5. from concurrent.futures import ThreadPoolExecutor
  6. import time
  7. import cv2
  8. import asyncio
  9. import logging
  10. from os import system
  11. from colorama import Fore, Style
  12. from olympe.messages.ardrone3.Piloting import TakeOff, Landing, Emergency, moveBy
  13. from olympe.messages.animation import start_flip
  14. from olympe.messages.ardrone3.PilotingSettingsState import *
  15. from olympe.messages.ardrone3.PilotingState import FlyingStateChanged
  16. from olympe.messages.drone_manager import discover_drones
  17. from olympe.messages.ardrone3.PilotingState import FlyingStateChanged
  18. from olympe.enums.ardrone3.PilotingState import FlyingStateChanged_State
  19. from olympe.messages.ardrone3.GPSSettingsState import GPSFixStateChanged
  20. from olympe.messages.ardrone3.PilotingSettings import MaxTilt
  21. from olympe.messages.wifi import *
  22. from olympe.messages.battery import health
  23. from olympe.messages.ardrone3.PilotingState import SpeedChanged, AltitudeChanged, GpsLocationChanged
  24. from olympe.messages import gimbal
  25. import olympe_deps as od
  26. import math
  27. import json
  28.  
  29.  
  30. class Drone():
  31.  
  32.     def __init__(self):
  33.         self.drone = olympe.Drone(
  34.             "10.202.0.1", loglevel=2, drone_type=od.ARSDK_DEVICE_TYPE_ANAFI4K)
  35.         self.file = "data.txt"
  36.         self.lock = threading.Lock()
  37.  
  38.     def main(self):
  39.         try:
  40.             self.drone.connection()
  41.             # self.drone.set_streaming_callbacks(raw_cb=self.yuv_frame_cb,)
  42.             self.drone.start_video_streaming()
  43.             if not self.lock.acquire(False):
  44.                 time.sleep(1)
  45.             else:
  46.                 print("TAkEOFFF : ACQUIRE")
  47.                 try:
  48.                     self.drone(
  49.                         FlyingStateChanged(state="hovering", _policy="check")
  50.                         | (
  51.                             GPSFixStateChanged(fixed=1, _timeout=10)
  52.                             >> (
  53.                                 TakeOff(_no_expect=True)
  54.                                 & FlyingStateChanged(state="hovering", _policy="wait", _timeout=10)
  55.                             )
  56.                         )
  57.                     ).wait()
  58.                 finally:
  59.                     self.lock.release()
  60.                     print("TAkEOFFF : RELEASE")
  61.             self.drone(MaxTilt(40)).wait().success()
  62.             self.drone(gimbal.set_target(
  63.                 gimbal_id=0,
  64.                 control_mode="position",
  65.                 yaw_frame_of_reference="none",   # None instead of absolute
  66.                 yaw=0.0,
  67.                 pitch_frame_of_reference="absolute",
  68.                 pitch=-45.0,
  69.                 roll_frame_of_reference="none",     # None instead of absolute
  70.                 roll=0.0,
  71.             )).wait()
  72.  
  73.             for i in range(3):
  74.                 if not self.lock.acquire(False):
  75.                     time.sleep(1)
  76.                     print("MOVING : NOPE")
  77.                 else:
  78.                     print("MOVING : ACQUIRE")
  79.                     try:
  80.                         print("Moving by ({}/3)...".format(i + 1))
  81.                         self.drone(moveBy(4, 0, 0, math.pi, _timeout=20)
  82.                                    ).wait().success()
  83.                     finally:
  84.                         self.lock.release()
  85.                         print("MOVING : RELEASE")
  86.                         time.sleep(2)
  87.         except Exception as e:
  88.             print(e)
  89.         self.drone.landing()
  90.         return 1
  91.  
  92.     def landing(self):
  93.         print("Landing...")
  94.         self.drone(
  95.             Landing()
  96.             >> FlyingStateChanged(state="landed", _timeout=5)
  97.         ).wait()
  98.         print("Landed\n")
  99.         self.drone.disconnection()
  100.  
  101.     def yuv_frame_cb(self, yuv_frame):
  102.         info = yuv_frame.info()
  103.         height, width = info["yuv"]["height"], info["yuv"]["width"]
  104.  
  105.         # convert pdraw YUV flag to OpenCV YUV flag
  106.         cv2_cvt_color_flag = {
  107.             olympe.PDRAW_YUV_FORMAT_I420: cv2.COLOR_YUV2BGR_I420,
  108.             olympe.PDRAW_YUV_FORMAT_NV12: cv2.COLOR_YUV2BGR_NV12,
  109.         }[info["yuv"]["format"]]
  110.  
  111.         cv2frame = cv2.cvtColor(yuv_frame.as_ndarray(), cv2_cvt_color_flag)
  112.  
  113.         cv2.imshow("live stream", cv2frame)
  114.         cv2.waitKey(1)
  115.  
  116.     def states(self):
  117.         final_dict = {}
  118.         inc = 0
  119.         dataSpeed = ["speedX", "speedY", "speedZ"]
  120.         dataAttidude = ["roll", "pitch", "yaw"]
  121.         dataGps = ["longitude", "latitude", "altidude"]
  122.         dict_test = {SpeedChanged: dataSpeed,
  123.                      AltitudeChanged: dataAttidude, GpsLocationChanged: dataGps, health: ["state_of_health"]}
  124.  
  125.         if not self.lock.acquire(False):
  126.             time.sleep(1)
  127.             print("STATES: NOPE")
  128.         else:
  129.             try:
  130.                 print("STATES : ACQUIRE")
  131.                 while True:
  132.                     try:
  133.                         if self.drone.get_state(SpeedChanged) and self.drone.get_state(AltitudeChanged) and self.drone.get_state(GpsLocationChanged):
  134.                             print("initalized!")
  135.                             break
  136.                     except Exception as e:
  137.                         time.sleep(1)
  138.                         continue
  139.  
  140.                 while self.drone.get_state(FlyingStateChanged)["state"] is not FlyingStateChanged_State.landing:
  141.                     inc += 1
  142.                     final_dict["ind"] = inc
  143.                     for k in dict_test:
  144.                         dictTotransform = self.drone.get_state(k)
  145.                         if dictTotransform is not None:
  146.                             for y, v in dictTotransform.items():
  147.                                 final_dict[y] = v
  148.                     time.sleep(1)
  149.                     # print(final_dict)
  150.                     with open(self.file) as f:
  151.                         file_str = f.read()
  152.                         file_str = json.dumps(final_dict)
  153.                     with open(self.file, 'w', encoding='utf-8') as f:
  154.                         f.write(file_str)
  155.                     print("STATES: WRITED")
  156.             finally:
  157.                 print("STATES : RELEASE")
  158.                 self.lock.release()
  159.  
  160. if __name__ == "__main__":
  161.     drone = Drone()
  162.     with ThreadPoolExecutor(max_workers=2) as executor:
  163.         task1 = executor.submit(drone.main())
  164.         task2 = executor.submit(drone.states())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement