示例#1
0
def run(CommandsQueue):

    move_distance = 30
    idle_sleep = 2  #sec
    video_ready_sleep = 7  #sec
    is_airborn = False
    global is_active

    tello = Tello()
    tello.connect()
    tello.set_speed(10)

    droneVid = threading.Thread(target=drone_video, args=(tello, ))
    droneVid.start()
    time.sleep(video_ready_sleep)

    while is_active:

        #execute next command from the que
        if not CommandsQueue.empty():
            command = CommandsQueue.get()
            timeStamp = str(datetime.datetime.now())
            print('Command for Drone: ' + str(command[0]) + ' at time ' +
                  timeStamp)

            if command[0] == Commands.up:
                if is_airborn:
                    tello.move_up(move_distance)
                else:
                    tello.takeoff()
                    is_airborn = True
            # elif command[0] == Commands.idle:
            #     time.sleep(idle_sleep)
            elif command[0] == Commands.up and is_airborn == True:
                tello.move_up(move_distance)
            elif command[0] == Commands.down and is_airborn == True:
                tello.move_down(move_distance)
            elif command[0] == Commands.forward and is_airborn == True:
                tello.move_forward(move_distance)
            elif command[0] == Commands.back and is_airborn == True:
                tello.move_back(move_distance)
            elif command[0] == Commands.left and is_airborn == True:
                tello.move_left(move_distance)
            elif command[0] == Commands.right and is_airborn == True:
                tello.move_right(move_distance)
            elif command[0] == Commands.flip and is_airborn == True:
                tello.flip_back()
            elif command[0] == Commands.stop:
                is_active = False
                break
        # else:
        #     time.sleep(idle_sleep)

    tello.land()
    is_airborn = False
    droneVid.join()
    tello.end()
示例#2
0
                centered = True

            if 50 < gateHeigth < 500 and gateShape:
                speed = 2.5 / gateHeigth * 4500
                #print(speed)
                if speed < 20:
                    speed = 20
                drone.move_forward(int(speed))
                time.sleep(0.1)
                close = False
            elif gateHeigth < 50:
                close = False
            elif gateShape == False:
                tooManyWrong += 1
            elif gateHeigth > 560 or tooManyWrong > 3:
                drone.move_back(20)
                time.sleep(0.1)
                if tooManyWrong > 4:
                    tooManyWrong = 0
                close = False 
            else:
                close = True

        except:
            print("rotate exception")
            continue

        try:
            leftRigthDist = (coordinates[bot_left][0] - coordinates[top_left][0]) > (
                        coordinates[bot_right][0] - coordinates[top_right][0])
            RigthLeftDist = (coordinates[bot_left][0] - coordinates[top_left][0]) < (
示例#3
0
tello.takeoff()

while True:
    # In reality you want to display frames in a seperate thread. Otherwise
    #  they will freeze while the drone moves.
    img = frame_read.framew
    cv2.imshow("drone", img)

    key = cv2.waitKey(1) & 0xff
    if key == 27:  # ESC
        break
    elif key == ord('w'):
        tello.move_forward(30)
    elif key == ord('s'):
        tello.move_back(30)
    elif key == ord('a'):
        tello.move_left(30)
    elif key == ord('d'):
        tello.move_right(30)
    elif key == ord('e'):
        tello.rotate_clockwise(30)
    elif key == ord('q'):
        tello.rotate_counter_clockwise(30)
    elif key == ord('r'):
        tello.move_up(30)
    elif key == ord('f'):
        tello.move_down(30)

tello.land()
tello.streamoff()
示例#4
0
class TelloCV(object):
    """
    TelloTracker builds keyboard controls on top of TelloPy as well
    as generating images from the video stream and enabling opencv support
    """

    def __init__(self):
        self.prev_flight_data = None
        self.record = False
        self.tracking = False
        self.keydown = False
        self.date_fmt = '%Y-%m-%d_%H%M%S'
        self.speed = 50
        self.go_speed = 80
        self.drone = Tello()
        self.init_drone()
        self.init_controls()

        self.battery = self.drone.get_battery()
        self.frame_read = self.drone.get_frame_read()
        self.forward_time = 0
        self.forward_flag = True
        self.takeoff_time = 0
        self.command_time = 0
        self.command_flag = False

        # trackingimport libh264decoder a color
        # green_lower = (30, 50, 50)
        # green_upper = (80, 255, 255)
        # red_lower = (0, 50, 50)
        # red_upper = (20, 255, 255)
        blue_lower = np.array([0, 0, 0])
        upper_blue = np.array([255, 255, 180])
        bh_lower = (180, 30, 100)
        bh_upper = (275, 50, 100)
        self.track_cmd = ""
        self.tracker = Tracker(960,
                               720,
                               blue_lower, upper_blue)
        self.speed_list = [5, 10, 15, 20, 25]
        self.frame_center = 480, 360
        self.error = 60

    def init_drone(self):
        """Connect, uneable streaming and subscribe to events"""
        # self.drone.log.set_level(2)
        self.drone.connect()
        self.drone.streamon()

    def on_press(self, keyname):
        """handler for keyboard listener"""
        if self.keydown:
            return
        try:
            self.keydown = True
            keyname = str(keyname).strip('\'')
            print('+' + keyname)
            if keyname == 'Key.esc':
                self.drone.quit()
                exit(0)
            if keyname in self.controls:
                key_handler = self.controls[keyname]
                if isinstance(key_handler, str):
                    getattr(self.drone, key_handler)(self.speed)
                else:
                    key_handler(self.speed)
        except AttributeError:
            print('special key {0} pressed'.format(keyname))

    def on_release(self, keyname):
        """Reset on key up from keyboard listener"""
        self.keydown = False
        keyname = str(keyname).strip('\'')
        print('-' + keyname)
        if keyname in self.controls:
            key_handler = self.controls[keyname]
            if isinstance(key_handler, str):
                getattr(self.drone, key_handler)(0)
            else:
                key_handler(0)

    def init_controls(self):
        """Define keys and add listener"""
        self.controls = {
            'w': lambda speed: self.drone.move_forward(speed),
            's': lambda speed: self.drone.move_back(speed),
            'a': lambda speed: self.drone.move_left(speed),
            'd': lambda speed: self.drone.move_right(speed),
            'Key.space': 'up',
            'Key.shift': 'down',
            'Key.shift_r': 'down',
            'q': 'counter_clockwise',
            'e': 'clockwise',
            'i': lambda speed: self.drone.flip_forward(),
            'k': lambda speed: self.drone.flip_back(),
            'j': lambda speed: self.drone.flip_left(),
            'l': lambda speed: self.drone.flip_right(),
            # arrow keys for fast turns and altitude adjustments
            'Key.left': lambda speed: self.drone.rotate_counter_clockwise(speed),
            'Key.right': lambda speed: self.drone.rotate_clockwise(speed),
            'Key.up': lambda speed: self.drone.move_up(speed),
            'Key.down': lambda speed: self.drone.move_down(speed),
            'Key.tab': lambda speed: self.drone.takeoff(),
            # 'Key.tab': self.drone.takeoff(60),
            'Key.backspace': lambda speed: self.drone.land(),
            'p': lambda speed: self.palm_land(speed),
            't': lambda speed: self.toggle_tracking(speed),
            'r': lambda speed: self.toggle_recording(speed),
            'z': lambda speed: self.toggle_zoom(speed),
            'Key.enter': lambda speed: self.take_picture(speed)
        }
        self.key_listener = keyboard.Listener(on_press=self.on_press,
                                              on_release=self.on_release)
        self.key_listener.start()
        # self.key_listener.join()

    def process_frame(self):
        """convert frame to cv2 image and show"""
        # print("TRACKING START")
        frame = self.frame_read.frame
        # self.drone.move_up(self.speed)
        # image = self.write_hud(image)
        # if self.record:
        #    self.record_vid(frame)
        return frame

    def move_up(self):
        self.drone.move_up(self.speed)

    def take_off(self):
        self.drone.takeoff()

    def go(self):
        self.drone.move_forward(self.go_speed)

    def move_left(self):
        self.drone.move_left(270)  # speed 테스트해서 조절하기

    def go_window9(self):
        self.drone.move_forward()

    def rotate_right(self):
        self.drone.rotate_clockwise()

    def rotate_left(self):
        self.drone.rotate_counter_clockwise()

    def landing(self):
        self.drone.land()

    def write_hud(self, frame):
        """Draw drone info, tracking and record on frame"""
        stats = self.prev_flight_data.split('|')
        stats.append("Tracking:" + str(self.tracking))
        if self.drone.zoom:
            stats.append("VID")
        else:
            stats.append("PIC")
        if self.record:
            diff = int(time.time() - self.start_time)
            mins, secs = divmod(diff, 60)
            stats.append("REC {:02d}:{:02d}".format(mins, secs))

        for idx, stat in enumerate(stats):
            text = stat.lstrip()
            cv2.putText(frame, text, (0, 30 + (idx * 30)),
                        cv2.FONT_HERSHEY_SIMPLEX,
                        1.0, (255, 0, 0), lineType=30)
        return frame

    def toggle_recording(self, speed):
        """Handle recording keypress, creates output stream and file"""
        if speed == 0:
            return
        self.record = not self.record

        if self.record:
            datename = [os.getenv('HOME'), datetime.datetime.now().strftime(self.date_fmt)]
            self.out_name = '{}/Pictures/tello-{}.mp4'.format(*datename)
            print("Outputting video to:", self.out_name)
            self.out_file = av.open(self.out_name, 'w')
            self.start_time = time.time()
            self.out_stream = self.out_file.add_stream(
                'mpeg4', self.vid_stream.rate)
            self.out_stream.pix_fmt = 'yuv420p'
            self.out_stream.width = self.vid_stream.width
            self.out_stream.height = self.vid_stream.height

        if not self.record:
            print("Video saved to ", self.out_name)
            self.out_file.close()
            self.out_stream = None

    def record_vid(self, frame):
        """
        convert frames to packets and write to file
        """
        new_frame = av.VideoFrame(
            width=frame.width, height=frame.height, format=frame.format.name)
        for i in range(len(frame.planes)):
            new_frame.planes[i].update(frame.planes[i])
        pkt = None
        try:
            pkt = self.out_stream.encode(new_frame)
        except IOError as err:
            print("encoding failed: {0}".format(err))
        if pkt is not None:
            try:
                self.out_file.mux(pkt)
            except IOError:
                print('mux failed: ' + str(pkt))

    def take_picture(self, speed):
        """Tell drone to take picture, image sent to file handler"""
        if speed == 0:
            return
        self.drone.take_picture()

    def palm_land(self, speed):
        """Tell drone to land"""
        if speed == 0:
            return
        self.drone.palm_land()

    def toggle_tracking(self, speed):
        """ Handle tracking keypress"""
        if speed == 0:  # handle key up event
            return
        self.tracking = not self.tracking
        print("tracking:", self.tracking)
        return

    def toggle_zoom(self, speed):
        """
        In "video" mode the self.drone sends 1280x720 frames.
        In "photo" mode it sends 2592x1936 (952x720) frames.
        The video will always be centered in the window.
        In photo mode, if we keep the window at 1280x720 that gives us ~160px on
        each side for status information, which is ample.
        Video mode is harder because then we need to abandon the 16:9 display size
        if we want to put the HUD next to the video.
        """
        if speed == 0:
            return
        self.drone.set_video_mode(not self.drone.zoom)

    def flight_data_handler(self, event, sender, data):
        """Listener to flight data from the drone."""
        text = str(data)
        if self.prev_flight_data != text:
            self.prev_flight_data = text

    def handle_flight_received(self, event, sender, data):
        """Create a file in ~/Pictures/ to receive image from the drone"""
        path = '%s/Pictures/tello-%s.jpeg' % (
            os.getenv('HOME'),
            datetime.datetime.now().strftime(self.date_fmt))
        with open(path, 'wb') as out_file:
            out_file.write(data)
        print('Saved photo to %s' % path)

    def enable_mission_pads(self):
        self.drone.enable_mission_pads()

    def disable_mission_pads(self):
        self.drone.disable_mission_pads()

    def go_xyz_speed_mid(self, x, y, z, speed, mid):
        self.drone.go_xyz_speed_mid(x, y, z, speed, mid)

    #  if function return True, set drone center to object's center
    def track_mid(self, x, y):
        midx, midy = 480, 360
        distance_x = abs(midx - x)
        distance_y = abs(midy - y)
        print(x, y, distance_x, distance_y)
        move_done = True
        if y > midy + self.error + 15:
            self.drone.move_down(20)
            move_done = False
        elif y < midy - self.error + 5:
            self.drone.move_up(20)
            move_done = False
        elif x < midx - self.error:
            self.drone.move_left(20)
            move_done = False
        elif x > midx + self.error:
            self.drone.move_right(20)
            move_done = False
        return move_done

    def track_x(self, x, left_count, right_count):
        midx = 480
        move_done = True
        if x < midx - 100:
            self.drone.move_left(20)
            left_count += 1
            move_done = False
        elif x > midx + 100:
            self.drone.move_right(20)
            right_count += 1
            move_done = False
        return move_done


    def go_slow(self):
        self.drone.move_forward(30)

    def go_fast(self):
        self.drone.move_forward(200)
示例#5
0
文件: square1.py 项目: moshes7/tello
from djitellopy import Tello
import time

# parameters
step = 100
sleepTime = 8

tello = Tello()

tello.connect()

tello.takeoff()
time.sleep(sleepTime)

tello.move_forward(step)
time.sleep(sleepTime)

tello.move_right(step)
time.sleep(sleepTime)

tello.move_back(step)
time.sleep(sleepTime)

tello.move_left(step)
time.sleep(sleepTime)

tello.land()
time.sleep(sleepTime)

tello.end()
class DroneObject:

    #initialize the object
    #Set default state to IdleState()
    #Create Tello object from the API
    #set default values for coordinate, FPS, distance, and tilt
    def __init__(self):
        self.state = IdleState()
        self.tello = Tello()
        self.coordinate = (0, 0)
        self.FPS = 30
        self.distance = 30 #pre defined, to be changed later
        self.tilt = 0

    #Generates pass events to tellodrone class
    def on_event(self, event):

        self.state = self.state.on_event(event)
    
    #setter for member variables
    def set_parameter (self, x,y, dist, tilt):
        self.coordinate = (x,y)
        self.distance = dist
        self.tilt = tilt

    #for take off the drone, called when the drone is in takeoff state
    #when take off is complete, trigger event for track
    def take_off(self):
        self.tello.takeoff()
        for i in range (0,3):
            print("taking off %d /3" % (i+1))
            time.sleep(1)
        self.on_event("track")

        
    #for landing the drone, called when the drone is in landingstate
    #when landing is complete, trigger event for idle

    def land(self):
        self.tello.land()
        for i in range (0,3):
            print("landing %d / 3" % (i+1))
            time.sleep(1)
        self.on_event("idle")

    #for tracking the drone, called when the drone is in track state
    #when tilt is active, it will prioritize the turning motion over the other motions
    #controls shifting, moving up and down, forward backwards, and turning
    def track(self):
        if(self.tilt <= 0.95 and self.tilt != 0):
            self.tello.rotate_clockwise(int((1-self.tilt)*100))
            time.sleep(0.05)
        elif(self.tilt >= 1.05):
            self.tello.rotate_counter_clockwise(int((self.tilt-1)*100))
            time.sleep(0.05)
        else:
            if (self.distance > 60):
                forward = int((self.distance - 60))
                if ((forward < 20)):
                    self.tello.move_forward(20)
                else:
                    self.tello.move_forward(forward)
                time.sleep(0.05)
            elif (self.distance < 50):
                backward = int(abs(self.distance - 50))
                if ((backward < 20)):
                    self.tello.move_back(20)
                else:
                    self.tello.move_back(backward)
                time.sleep(0.05)

            if (self.coordinate[0] < 400 and self.coordinate[0] >= 0):
                self.tello.move_left(20)
                time.sleep(0.05)

            elif (self.coordinate[0] < 959 and self.coordinate[0] >= 559):
                self.tello.move_right(20)
                time.sleep(0.05)

            if (self.coordinate[1] > 0 and self.coordinate[1] <= 200):
                self.tello.move_up(20)
                time.sleep(0.05)
            elif (self.coordinate[1] >= 519 and self.coordinate[1] < 719):
                self.tello.move_down(20)
                time.sleep(0.05)
    
    #For setting up the drone
        #Establish connection
        #Initialize streamming
        #Display battery
    def setup(self):
        if not self.tello.connect():
            print("Tello not connected")
            sys.exit()

        # In case streaming is on. This happens when we quit this program without the escape key.
        if not self.tello.streamoff():
            print("Could not stop video stream")
            sys.exit()

        if not self.tello.streamon():
            print("Could not start video stream")
            sys.exit()

        print("Current battery is " + self.tello.get_battery())
        self.tello.streamon()
        frame_read = self.tello.get_frame_read()

        
    #For calling corresponding functions based on their state
    def action(self):
        print("current state is" , self.state)
        print(str(self.state))
        if (str(self.state) == "TakeOffState"):
            print("take off!")
            self.take_off()
        elif (str(self.state) == "LandState"):
            print("land")
            self.land()
        if (str(self.state)== "TrackState"):
            self.track()

        else:
            return #idle state or undefined state do nothing
        return
示例#7
0
from djitellopy import Tello
import cv2
import time

tello = Tello()

tello.connect()

tello.takeoff()

#tello.move_left(1)
tello.move_up(20)
time.sleep(10)
tello.move_forward(20)
time.sleep(10)
tello.move_back(20)
time.sleep(10)
tello.rotate_counter_clockwise(90)
time.sleep(10)
tello.move_forward(10)
time.sleep(10)
tello.move_back(10)
time.sleep(10)

tello.land()
示例#8
0
class hand_tello_control:
    def __init__(self):
        self.action = " "
        self.mp_drawing = mp.solutions.drawing_utils
        self.mp_drawing_styles = mp.solutions.drawing_styles
        self.mp_hands = mp.solutions.hands
        self.action_done = True
        self.ffoward = 1
        self.fback = 1
        self.fright = 1
        self.fleft = 1
        self.fsquare = 1
        self.fmiddle = 1
        self.fno = 1
        self.fland = 1

    def tello_startup(self):
        # For Tello input:
        self.tello = Tello()  # Starts the tello object
        self.tello.connect()  # Connects to the drone

    def define_orientation(self, results):

        if results.multi_hand_landmarks[0].landmark[
                4].x < results.multi_hand_landmarks[0].landmark[17].x:
            orientation = "right hand"
        else:
            orientation = "left hand"
        return orientation

    def follow_hand(self, results):
        normalizedLandmark = results.multi_hand_landmarks[0].landmark[
            9]  # Normalizes the lowest middle-finger coordinate for hand tracking
        pixelCoordinatesLandmark = self.mp_drawing._normalized_to_pixel_coordinates(
            normalizedLandmark.x, normalizedLandmark.y, 255, 255
        )  #Tracks the coordinates of the same landmark in a 255x255 grid
        print(pixelCoordinatesLandmark)

        if pixelCoordinatesLandmark == None:  # If hand goes out of frame, stop following
            self.tello.send_rc_control(0, 0, 0, 0)
            return

        centerRange = [12,
                       12]  #Range for detecting commands in the x and y axis.
        centerPoint = [128, 128]  #Theoretical center of the image
        xCorrection = pixelCoordinatesLandmark[0] - centerPoint[0]
        yCorrection = pixelCoordinatesLandmark[1] - centerPoint[1]
        xSpeed = 0
        ySpeed = 0

        if xCorrection > centerRange[0] or xCorrection < -centerRange[
                0]:  #If the hand is outside the acceptable range, changes the current speed to compensate.
            xSpeed = xCorrection // 3
        if yCorrection > centerRange[1] or yCorrection < -centerRange[1]:
            ySpeed = -yCorrection // 3

        self.tello.send_rc_control(xSpeed, 0, ySpeed, 0)
        time.sleep(0.5)
        self.tello.send_rc_control(0, 0, 0, 0)

    def action_to_do(
            self, fingers, orientation,
            results):  #use the variable results for the hand tracking control

        if self.action_done == True:
            self.ffoward = 1
            self.fback = 1
            self.fright = 1
            self.fleft = 1
            self.fsquare = 1
            self.fmiddle = 1
            self.fno = 1
            self.fland = 1
            self.action_done = False
        #Left hand controls tricks, right hand controls movement
        if orientation == "left hand":  #Thumb on the left = left hand!
            if fingers == [0, 1, 0, 0, 0]:
                if self.ffoward >= 15:
                    self.action = "flip forward"
                    self.tello.flip_forward()
                    self.action_done = True
                self.ffoward = self.ffoward + 1
            elif fingers == [0, 1, 1, 0, 0] and self.battery == True:
                if self.fback >= 15:
                    self.action = "flip back"
                    self.tello.flip_back()
                    self.action_done = True
                self.fback = self.fback + 1
            elif fingers == [1, 0, 0, 0, 0] and self.battery == True:
                if self.fright >= 15:
                    self.action = "flip right"
                    self.tello.flip_right()
                    self.action_done = True
                self.fright = self.fright + 1
            elif fingers == [0, 0, 0, 0, 1] and self.battery == True:
                if self.fleft >= 15:
                    self.action = "flip left"
                    self.tello.flip_left()
                    self.action_done = True
                self.fleft = self.fleft + 1
            elif fingers == [0, 1, 1, 1, 0]:
                if self.fsquare >= 15:
                    self.action = "Square"
                    self.tello.move_left(20)
                    self.tello.move_up(40)
                    self.tello.move_right(40)
                    self.tello.move_down(40)
                    self.tello.move_left(20)
                    self.action_done = True
                self.fsquare = self.fsquare + 1
            elif fingers == [0, 0, 1, 0, 0]:
                if self.fmiddle >= 15:
                    self.action = " :( "
                    self.tello.land()
                    self.action_done = True
                self.fmiddle = self.fmiddle + 1
            elif ((self.battery == False) and
                  (fingers == [1, 0, 0, 0, 0] or fingers == [0, 1, 0, 0, 0]
                   or fingers == [0, 0, 0, 0, 1])):  #not avaiable to do tricks
                if self.fno >= 15:
                    self.tello.rotate_clockwise(45)
                    self.tello.rotate_counter_clockwise(90)
                    self.tello.rotate_clockwise(45)
                    self.action_done = True
                self.fno = self.fno + 1
            else:
                self.action = " "

        elif orientation == "right hand":  #Thumb on the right = right hand!
            if fingers == [1, 1, 1, 1, 1]:
                self.action = "Follow"
                self.follow_hand(results)
            elif fingers == [1, 0, 0, 0, 0]:
                if self.fland >= 15:
                    self.action = "Land"
                    self.tello.land()
                    self.action_done = True
                self.fland = self.fland + 1
            else:
                self.action = " "

    def fingers_position(self, results, orientation):

        # [thumb, index, middle finger, ring finger, pinky]
        # 0 for closed, 1 for open
        fingers = [0, 0, 0, 0, 0]

        if (results.multi_hand_landmarks[0].landmark[4].x >
                results.multi_hand_landmarks[0].landmark[3].x
            ) and orientation == "right hand":
            fingers[0] = 0
        if (results.multi_hand_landmarks[0].landmark[4].x <
                results.multi_hand_landmarks[0].landmark[3].x
            ) and orientation == "right hand":
            fingers[0] = 1

        if (results.multi_hand_landmarks[0].landmark[4].x >
                results.multi_hand_landmarks[0].landmark[3].x
            ) and orientation == "left hand":
            fingers[0] = 1
        if (results.multi_hand_landmarks[0].landmark[4].x <
                results.multi_hand_landmarks[0].landmark[3].x
            ) and orientation == "left hand":
            fingers[0] = 0

        fingermarkList = [8, 12, 16, 20]
        i = 1
        for k in fingermarkList:
            if results.multi_hand_landmarks[0].landmark[
                    k].y > results.multi_hand_landmarks[0].landmark[k - 2].y:
                fingers[i] = 0
            else:
                fingers[i] = 1

            i = i + 1

        return fingers

    def detection_loop(self):
        with self.mp_hands.Hands(model_complexity=0,
                                 min_detection_confidence=0.75,
                                 min_tracking_confidence=0.5) as hands:
            self.tello.streamoff(
            )  # Ends the current stream, in case it's still opened
            self.tello.streamon()  # Starts a new stream
            while True:
                frame_read = self.tello.get_frame_read(
                )  # Stores the current streamed frame
                image = frame_read.frame
                self.battery = self.tello.get_battery()

                # To improve performance, optionally mark the image as not writeable to
                # pass by reference.
                image.flags.writeable = True
                image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                results = hands.process(image)

                # Draw the hand annotations on the image.
                image.flags.writeable = True
                image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

                if results.multi_hand_landmarks:
                    action = " "
                    for hand_landmarks in results.multi_hand_landmarks:
                        self.mp_drawing.draw_landmarks(
                            image, hand_landmarks,
                            self.mp_hands.HAND_CONNECTIONS,
                            self.mp_drawing_styles.
                            get_default_hand_landmarks_style(),
                            self.mp_drawing_styles.
                            get_default_hand_connections_style())

                    orientation = self.define_orientation(results)
                    fingers = self.fingers_position(results, orientation)
                    #print(fingers)
                    self.action_to_do(fingers, orientation, results)

                for event in pg.event.get():
                    if event.type == pg.KEYDOWN:
                        if event.key == pg.K_l:
                            self.tello.land()
                        if event.key == pg.K_t:
                            self.tello.takeoff()
                        if event.key == pg.K_b:
                            print("A bateria esta em ",
                                  self.tello.get_battery(), "%")
                        if event.key == pg.K_m:
                            return 0

                cv2.imshow("image", image)
                if cv2.waitKey(5) & 0xFF == 27:
                    break

    def key_control(self):

        self.tello.streamoff(
        )  # Ends the current stream, in case it's still opened
        self.tello.streamon()  # Starts a new stream
        while True:
            frame_read = self.tello.get_frame_read(
            )  # Stores the current streamed frame
            image = frame_read.frame

            # To improve performance, optionally mark the image as not writeable to
            # pass by reference.
            image.flags.writeable = True
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

            # Draw the hand annotations on the image.
            image.flags.writeable = True
            image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

            for event in pg.event.get():
                if event.type == pg.KEYDOWN:
                    if event.key == pg.K_w:
                        self.tello.move_forward(20)
                    if event.key == pg.K_a:
                        self.tello.move_left(20)
                    if event.key == pg.K_s:
                        self.tello.move_back(20)
                    if event.key == pg.K_d:
                        self.tello.move_right(20)
                    if event.key == pg.K_q:
                        self.tello.rotate_counter_clockwise(20)
                    if event.key == pg.K_e:
                        self.tello.rotate_clockwise(20)
                    if event.key == pg.K_SPACE:
                        self.tello.move_up(20)
                    if event.key == pg.K_LCTRL:
                        self.tello.move_down(20)
                    if event.key == pg.K_b:
                        print("A bateria esta em ", self.tello.get_battery(),
                              "%")
                    if event.key == pg.K_m:
                        return 0

            cv2.imshow("image", image)
            if cv2.waitKey(5) & 0xFF == 27:
                break

    def main_interface(self):
        telloMode = -1
        self.tello_startup()
        pg.init()
        win = pg.display.set_mode((500, 500))
        pg.display.set_caption("Test")
        #self.tello.takeoff()

        print("Para controlar pelo teclado, digite 1")
        print("Para controlar com a mao, digite 2")
        print("Para sair, digite 0")

        self.tello.streamoff(
        )  # Ends the current stream, in case it's still opened
        self.tello.streamon()  # Starts a new stream
        while telloMode != 0:
            frame_read = self.tello.get_frame_read(
            )  # Stores the current streamed frame
            image = frame_read.frame

            # To improve performance, optionally mark the image as not writeable to
            # pass by reference.
            image.flags.writeable = True
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

            # Draw the hand annotations on the image.
            image.flags.writeable = True
            image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

            cv2.imshow("image", image)
            if cv2.waitKey(5) & 0xFF == 27:
                break

            if telloMode == 1:
                self.key_control()
                telloMode = -1
                print("Para controlar pelo teclado, digite 1")
                print("Para controlar com a mao, digite 2")
                print("Para sair, digite 0")
            elif telloMode == 2:
                self.detection_loop()
                telloMode = -1
                print("Para controlar pelo teclado, digite 1")
                print("Para controlar com a mao, digite 2")
                print("Para sair, digite 0")
            elif telloMode == 0:
                self.tello.land()
                telloMode = -1
                print("Obrigado por voar hoje")
            elif telloMode != -1 and telloMode != 1 and telloMode != 2:
                print("valor invalido!")
            for event in pg.event.get():
                if event.type == pg.KEYDOWN:
                    if event.key == pg.K_1:
                        telloMode = 1
                    if event.key == pg.K_2:
                        telloMode = 2
                    if event.key == pg.K_0:
                        telloMode = 0
                    if event.key == pg.K_l:
                        self.tello.land()
                    if event.key == pg.K_t:
                        self.tello.takeoff()
                    if event.key == pg.K_b:
                        print("A bateria esta em ", self.tello.get_battery(),
                              "%")
示例#9
0
def main():
    mp_drawing = mp.solutions.drawing_utils
    mp_hands = mp.solutions.hands

    # Argument parsing
    in_flight = False

    # For webcam input:
    hands = mp_hands.Hands(static_image_mode=True,
                           max_num_hands=2,
                           min_detection_confidence=0.8,
                           min_tracking_confidence=0.8)

    # Camera preparation
    tello = Tello()
    tello.connect()
    tello.streamon()

    # FPS Measurement
    cv_fps_calc = CvFpsCalc(buffer_len=5)

    frame_read = tello.get_frame_read()

    while True:
        fps = cv_fps_calc.get()
        battery_status = tello.get_battery()

        # In reality you want to display frames in a seperate thread. Otherwise
        #  they will freeze while the drone moves.
        image = frame_read.frame

        # Flip the image horizontally for a later selfie-view display, and convert
        # the BGR image to RGB.
        image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)

        # To improve performance, optionally mark the image as not writeable to
        # pass by reference.
        image.flags.writeable = False
        results = hands.process(image)

        # Draw the hand annotations on the image.
        image.flags.writeable = True
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        if results.multi_hand_landmarks:
            for hand_landmarks in results.multi_hand_landmarks:
                mp_drawing.draw_landmarks(image, hand_landmarks,
                                          mp_hands.HAND_CONNECTIONS)

        # Process Key (ESC: end)
        key = cv2.waitKey(1) & 0xff
        if key == 27:  # ESCArgument parsing
            tello.land()
            break
        elif key == 32:
            if not in_flight:
                # Take-off drone
                tello.takeoff()
                in_flight = True
                tello.move_up(50)
            elif in_flight:
                # Land tello
                tello.land()
                in_flight = False
        elif key == ord('w'):
            tello.move_forward(30)
        elif key == ord('s'):
            tello.move_back(30)
        elif key == ord('a'):
            tello.move_left(30)
        elif key == ord('d'):
            tello.move_right(30)
        elif key == ord('e'):
            tello.rotate_clockwise(30)
        elif key == ord('q'):
            tello.rotate_counter_clockwise(30)
        elif key == ord('r'):
            tello.move_up(30)
        elif key == ord('f'):
            tello.move_down(30)

        # Battery status and image rendering
        cv2.putText(image, "Battery: {} / fps:{}".format(battery_status, fps),
                    (5, 720 - 5), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
        cv2.imshow('DK_Drone', image)

    tello.land()
    tello.end()
    cv2.destroyAllWindows()
示例#10
0
def main():
    tello = Tello()
    tello.connect()

    vk_session = vk_api.VkApi(token='TOKEN')
    vk = vk_session.get_api()

    letsgo = VkKeyboard(one_time=True)
    letsgo.add_button('Начать', color=VkKeyboardColor.DEFAULT)

    keyboard_takeoff = VkKeyboard(one_time=True)
    keyboard_takeoff.add_button('Взлёт', color=VkKeyboardColor.POSITIVE)

    keyboard = VkKeyboard(one_time=True)
    keyboard.add_button('Вверх', color=VkKeyboardColor.PRIMARY)
    keyboard.add_button('Вниз', color=VkKeyboardColor.PRIMARY)
    keyboard.add_line()
    keyboard.add_button('Вперёд', color=VkKeyboardColor.PRIMARY)
    keyboard.add_line()
    keyboard.add_button('Влево', color=VkKeyboardColor.PRIMARY)
    keyboard.add_button('Флип', color=VkKeyboardColor.NEGATIVE)
    keyboard.add_button('Вправо', color=VkKeyboardColor.PRIMARY)
    keyboard.add_line()
    keyboard.add_button('Назад', color=VkKeyboardColor.PRIMARY)
    keyboard.add_line()
    keyboard.add_button('Проверка', color=VkKeyboardColor.DEFAULT)
    keyboard.add_line()
    keyboard.add_button('Посадка', color=VkKeyboardColor.NEGATIVE)

    flip_keyboard = VkKeyboard(one_time=False)
    flip_keyboard.add_button('Флип вперёд', color=VkKeyboardColor.DEFAULT)
    flip_keyboard.add_line()
    flip_keyboard.add_button('Флип влево', color=VkKeyboardColor.DEFAULT)
    flip_keyboard.add_button('Флип вправо', color=VkKeyboardColor.DEFAULT)
    flip_keyboard.add_line()
    flip_keyboard.add_button('Флип назад', color=VkKeyboardColor.DEFAULT)
    flip_keyboard.add_line()
    flip_keyboard.add_button('Доступность флипа',
                             color=VkKeyboardColor.NEGATIVE)
    flip_keyboard.add_line()
    flip_keyboard.add_button('Вернуться в меню управления',
                             color=VkKeyboardColor.NEGATIVE)

    mainmenu_keyboard = VkKeyboard(one_time=True)
    mainmenu_keyboard.add_button('Начать', color=VkKeyboardColor.DEFAULT)
    mainmenu_keyboard.add_line()
    mainmenu_keyboard.add_button('Конец работы', color=VkKeyboardColor.DEFAULT)

    longpoll = VkLongPoll(vk_session)

    for event in longpoll.listen():
        if event.type == VkEventType.MESSAGE_NEW and event.to_me and event.text:
            if event.text.lower() == 'флип':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=flip_keyboard.get_keyboard(),
                    message='Переход в меню флипов')
            if event.text.lower() == 'начать':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=keyboard_takeoff.get_keyboard(),
                    message='Взлет?')
            if event.text.lower() == 'конец работы':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    message='Конец работы')
            if event.text.lower() == 'взлёт':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=keyboard.get_keyboard(),
                    message='Куда летим?')
                tello.takeoff()
                print('Взлёт')
            if event.text.lower() == 'посадка':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=mainmenu_keyboard.get_keyboard(),
                    message='Посадка')
                tello.land()
                print('Посадка')
            if event.text.lower() == 'вперёд':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=keyboard.get_keyboard(),
                    message='Полёт вперёд')
                tello.move_forward(100)
                print('Вперёд')
            if event.text.lower() == 'влево':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=keyboard.get_keyboard(),
                    message='Полёт влево')
                tello.move_left(100)
                print('Влево')
            if event.text.lower() == 'вправо':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=keyboard.get_keyboard(),
                    message='Полёт вправо')
                tello.move_right(100)
                print('Вправо')
            if event.text.lower() == 'назад':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=keyboard.get_keyboard(),
                    message='Полёт назад')
                tello.move_back(100)
                print('Назад')
            if event.text.lower() == 'проверка':
                x = tello.get_battery()
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=keyboard.get_keyboard(),
                    message='Заряд баттареи ' + x)
                print('Проверка')
            if event.text.lower() == 'вверх':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=keyboard.get_keyboard(),
                    message='Полёт вверх')
                tello.move_up(20)
                print('Вверх')
            if event.text.lower() == 'вниз':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=keyboard.get_keyboard(),
                    message='Полёт вниз')
                tello.move_down(20)
                print('Вниз')
            if event.text.lower() == 'флип назад':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=flip_keyboard.get_keyboard(),
                    message='Флип назад')
                tello.flip('b')
                print('Флип назад')
            if event.text.lower() == 'флип вперёд':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=flip_keyboard.get_keyboard(),
                    message='Флип вперёд')
                tello.flip('f')
                print('Флип вперёд')
            if event.text.lower() == 'флип влево':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=flip_keyboard.get_keyboard(),
                    message='Флип влево')
                tello.flip('l')
                print('Флип влево')
            if event.text.lower() == 'флип вправо':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=flip_keyboard.get_keyboard(),
                    message='Флип вправо')
                tello.flip('r')
                print('Флип вправо')
            if event.text.lower() == 'доступность флипа':
                x = tello.get_battery()
                if x == 'ok':
                    vk.messages.send(user_id=event.user_id,
                                     keyboard=flip_keyboard.get_keyboard(),
                                     message='Нажмите ещё раз')
                else:
                    if int(x) > 50:
                        vk.messages.send(user_id=event.user_id,
                                         keyboard=flip_keyboard.get_keyboard(),
                                         message='Заряд баттареи ' + x +
                                         'Флип доступен')
                    else:
                        vk.messages.send(user_id=event.user_id,
                                         keyboard=flip_keyboard.get_keyboard(),
                                         message='Заряд баттареи ' + x +
                                         'Флип недоступен')
            if event.text.lower() == 'вернуться в меню управления':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=keyboard.get_keyboard(),
                    message='Возврат в меню управления')
                print('Возврат в меню управления')
    tello.end()
class DroneControl():
    def __init__(self):
        self.drone = Tello()  # Instantiate a Tello Object
        self.drone.connect()  # Connect to the drone
        self.drone.streamoff()  # In case stream never exited before
        self.drone.streamon()  # Turn on drone camera stream
        self.timer = 0  # Timing for printing statements
        self.flying = False  # Keep track of flying state
        # How many video frames have been requested
        self.frame_count = 0
        self.init_frames = 50  # Begin flying only after init_frames
        self.myThread = threading.Thread(target=self.fly)

    def __del__(self):
        self.drone.streamoff()

    def get_state(self):
        self.drone.get_battery()
        self.drone.get_speed_x
        self.drone.get_speed_y
        self.drone.get_speed_z
        self.drone.get_height
        self.drone.get_flight_time
        self.drone.get_temperature
        self.drone.get_yaw
        self.drone.get_roll
        self.drone.get_pitch
        self.drone.get_barometer
        self.drone.get_acceleration_x
        self.drone.get_acceleration_y
        self.drone.get_acceleration_z
        self.drone.get_distance_tof
        self.drone.query_wifi_signal_noise_ratio
        ####################################################
        ############## ADD YOUR CODE HERE ##################
        ####################################################

    def fly(self):
        #time.sleep(5)
        #self.get_state()
        #time.sleep(5)
        #self.drone.takeoff()
        #time.sleep(5)
        #self.drone.move_left(50)
        #time.sleep(5)
        #self.drone.rotate_clockwise(90)
        #time.sleep(5)
        #self.drone.rotate_clockwise(180)
        #time.sleep(5)
        #self.drone.land()
        #################################################
        ##################Challenge 1####################
        #################################################
        time.sleep(5)
        self.get_state()
        time.sleep(5)
        self.drone.takeoff()
        time.sleep(5)
        self.drone.move_left(50)
        time.sleep(5)
        self.drone.move_back(50)
        time.sleep(5)
        self.drone.move_right(50)
        time.sleep(5)
        self.drone.move_forward(50)
        time.sleep(5)
        self.drone.land()
        #################################################
        ###################Challenge 2###################
        #################################################
        time.sleep(5)
        self.drone.takeoff()
        time.sleep(10)
        self.drone.move_up(40)
        time.sleep(5)
        self.drone.move_forward(50)
        time.sleep(5)
        self.drone.rotate_counter_clockwise(45)
        time.sleep(5)
        self.drone.move_forward(50)
        time.sleep(5)
        self.drone.rotate_counter_clockwise(70)
        time.sleep(5)
        self.drone.flip_left()
        time.sleep(5)
        self.drone.move_down(25)
        time.sleep(5)
        self.drone.flip_right()
        time.sleep(5)
        self.drone.flip_right()
        time.sleep(5)
        self.drone.move_back(35)
        time.sleep(5)
        self.drone.flip_back()
        time.sleep(5)
        self.get_state()
        time.sleep(5)
        curve_xyz_speed(-50, -50, 0, -200, -200, 0, 10)
        time.sleep(5)
        self.rotate_clockwise(115)
        time.sleep(5)
        self.drone.land()
        ####################################################
        ############## ADD YOUR CODE HERE ##################
        ####################################################

    def get_frame(self):
        # only begin flying once a video feed is established
        self.frame_count += 1
        if self.flying == False and self.frame_count > self.init_frames:
            self.flying = True
            # self.fly()
            self.myThread.start()
        # Grab a frame and resize it
        frame_read = self.drone.get_frame_read()
        if frame_read.stopped:
            return
        frame = cv2.resize(frame_read.frame, (360, 240))
        # encode OpenCV raw frame to jpeg
        ret, jpeg = cv2.imencode('.jpg', frame)
        return jpeg.tobytes()
示例#12
0
    '''
    # mission pad detection
    # me.mon()
    me.enable_mission_pads()
    me.set_mission_pad_detection_direction(0)
    '''
    if me.get_mission_pad_id() > 0:
        me.flip_back()
        break
    '''

    while me.get_mission_pad_id() != 4:
        me.move_forward(100)
        d += 100
        if d == 500:
            me.move_back(d)
            me.end()
        print("Haven't found mission pad\n")

    print("Found mission pad\n")
    # me.flip_back()
    me.move_back(d)
    me.end()
    break
    #print("Beginning Move Back:", time.process_time())
    # me.move_back(100)
    #print("Done Move Back:", time.process_time())
    '''
    me.stop()
    if cv2.waitKey(10) & 0xFF == ord('q'):
        # cv2.destroyAllWindows()
示例#13
0
import sys
from djitellopy import Tello
from sys import argv
import time

tello = Tello()
tello.connect()
time.sleep(0.5)

print(tello.get_battery())

if argv[1] == "takeoff":
    tello.takeoff()
elif argv[1] == "land":
    tello.land()
elif argv[1] == "forward":
    tello.move_forward(70)
elif argv[1] == "left":
    tello.move_left(70)
elif argv[1] == "right":
    tello.move_right(70)
elif argv[1] == "back":
    tello.move_back(70)
elif argv[1] == "flip":
    tello.flip_forward()
elif argv[1] == "up":
    tello.move_up()
elif argv[1] == "down":
    tello.move_down
示例#14
0
while True:

    # GET THE IMGAE FROM TELLO
    #frame_read = me.get_frame_read()
    #myFrame = frame_read.frame
    #img = cv2.resize(myFrame, (width, height))

    # DISPLAY IMAGE
    #cv2.imshow("MyResult", img)

    # TO GO UP IN THE BEGINNING
    if startCounter == 0:
        me.takeoff()
        time.sleep(5)
        me.move_back(160)
        time.sleep(5)
        me.rotate_counter_clockwise(90)
        time.sleep(5)
        me.flip_forward()
        time.sleep(5)
        me.flip_forward()
        time.sleep(5)
        me.flip_right()
        time.sleep(5)
        me.flip_left()
        time.sleep(5)
        me.flip_back()
        time.sleep(5)
        me.flip_back()
        #me.move_forward(195)
示例#15
0
print(tello.get_battery())
recorder.start()
tello.takeoff()
time.sleep(5)
# tof = tello.get_distance_tof()
# time.sleep(5)
# print(tof)
tello.move_up(100)
time.sleep(5)
for command in route:
    time.sleep(5)
    if command[0] == "v":
        if command[1] > 0:
            tello.move_forward(command[1])
        else:
            tello.move_back(-command[1])
    elif command[0] == "h":
        if command[1] > 0:
            tello.move_right(command[1])
        else:
            tello.move_left(-command[1])
    elif command[0] == "r":
        command[1] == command[1] % 360
        while command[1] > 180:
            command[1] = -360 + command[1]
        if command[1] > 0:
            tello.rotate_clockwise(command[1])
        else:
            tello.rotate_counter_clockwise(-command[1])
    elif command[0] == "c":
        tello.move_down(50)