Пример #1
0
class DataTello:
    
    def __init__(self):
        # Inicia objeto de controle do Tello
        self.tello = Tello()
        
        # Array onde será armazenado a lista de dados coletado pelo Tello
        self.__data = []
        self.__array = []

        # Tempo de voo em mili segundos
        self.tempoVoo = 420000

        '''
        ___Padrão para nome dos arquivos das tabelas___
        Onde x é o nº da tabela e y a quantidade de tempo em segundos do voo
        
        1. Para a janela fechada e porta fechada: x_tudoFechado_y.csv
        2. Para a janela aberta e porta aberta: x_janelaPortaAberta_y.csv
        3. Para a janela e porta aberta, com ventilador ligado na direção do drone: x_janelaPortaAbertaVentilador_y.csv
        '''

        # Padrão de nome
        self.nomeArquivo = '2_tudoFechado_420'
        self.__df = pd.DataFrame(columns=['timestamp', 'pitch', 'roll', 
                                          'yaw', 'vgx', 'vgy', 'vgz', 
                                          'templ', 'temph', 'tof', 
                                          'height',  'battery',  'barometer', 
                                          'time', 'agx', 'agy',  'agz'])
    '''
        self.__startCollector = False
        self.__endProgram = False
        threadCollector = threading.Thread(target=self.dataCollector, args=())
        threadCollector.daemon = False
        threadCollector.start()

    def dataCollector(self):
        while True:
            if self.__startCollector:
                self.__data.append(self.tello.get_states())

            if  self.__endProgram:
                for item in self.__data:
                    timestamp = int(round(time.time() * 1000))         # Cria timestamp no momento que recebe os dados
                    self.__df.loc[len(self.__df)] = [timestamp, item[1],  item[3], item[5], item[7], 
                                    item[9],   item[11], item[13], item[15], item[17], item[19], 
                                    item[21],    item[23], item[25], item[27], item[29], item[31]] # Adiciona os novos valores em uma nova linha do DataFrame

                self.__df.to_csv('{}.csv'.format(self.nomeArquivo))

                break            
    '''           

    def fly(self):
        #
        self.tello.connect()
        self.tello.takeoff()
        timestampInicial = int(round(time.time() * 1000))
        timestampFinal = timestampInicial

        while ((timestampFinal - timestampInicial) < self.tempoVoo):
            try:
                timestampFinal = int(round(time.time() * 1000))         # Cria timestamp no momento que recebe os dados
                self.__data.append(self.tello.get_states())
                if (not len(self.__data) % 20 == 0):
                    self.tello.send_command_without_return('command')
            except KeyboardInterrupt:
                print ('\n . . .\n')
                self.tello.end()  
                break

        self.tello.land()
        self.tello.end()

        for item in self.__data:
            timestamp = int(round(time.time() * 1000))         # Cria timestamp no momento que recebe os dados
            self.__df.loc[len(self.__df)] = [timestamp, item[1],  item[3], item[5], item[7], 
                            item[9],   item[11], item[13], item[15], item[17], item[19], 
                            item[21],    item[23], item[25], item[27], item[29], item[31]] # Adiciona os novos valores em uma nova linha do DataFrame

        self.__df.to_csv('{}.csv'.format(self.nomeArquivo))

    def stop(self):
        self.tello.end()

        

    def run(self):
        self.tello.connect()
        self.tello.takeoff()
        tempo1 = self.tello.get_flight_time()
        tempo1 = tempo1[0:(len(tempo1)-1)]
        #time.sleep(3)
        bateria = self.tello.get_battery()
        tempo2 = self.tello.get_flight_time()
        tempo2 = tempo2[0:(len(tempo2)-1)]
        
        print('Nivel da bateria é: {}'.format(str(bateria)))
        
        print('Tempo de início foi {}'.format(str(tempo1)))
        print('Tempo de término foi de {}'.format(str(tempo2)))
        
        while ((int(tempo2) - int(tempo1)) < 10):
            print('Nivel da bateria é: ' + str(bateria))
            self.__array.append(self.tello.get_attitude())
            self.__data.append(self.tello.get_states())    
            tempo2 = self.tello.get_flight_time()
            tempo2 = tempo2[0:(len(tempo2)-1)]

        self.tello.land()
        self.tello.end()
        print(self.__array)
        print(self.__data)
Пример #2
0
class FrontEnd(object):
    def __init__(self):
        # initiate pygame
        pygame.init()

        self.screen = pygame.display.set_mode([960, 720])

        # initiate Tello methods to interact with the drone
        self.tello = Tello()

        # drone velocities between -100~100
        self.for_back_velocity = 0
        self.left_right_velocity = 0
        self.up_down_velocity = 0
        self.yaw_velocity = 0
        self.speed = 10
        self.send_rc_control = False

        # create timer
        pygame.time.set_timer(USEREVENT + 1, 50)

    def run(self):

        if not self.tello.connect():
            print("Tello not connected")
            return

        if not self.tello.set_speed(self.speed):
            print("Not set speed to lowest possible")
            return

        if not self.tello.streamoff():
            print("Could not stop video stream")
            return

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

            # reads video stream from Tello
        frame_read = self.tello.get_frame_read()
        time.sleep(2.0)
        should_stop = False
        override = True
        # what happens when the stream is on
        while not should_stop:
            for event in pygame.event.get():
                # updating
                if event.type == USEREVENT + 1:
                    self.update()
                # stream turns off when pygame window is closed
                elif event.type == QUIT:
                    should_stop = True
                # if a key is physically pressed
                elif event.type == KEYDOWN:
                    # if the escape is pressed, stream turns off
                    if event.key == K_ESCAPE:
                        should_stop = True
                    elif event.key == K_SPACE:
                        override = True
                    elif event.key == K_RETURN:
                        override = False
                    if override == True:
                        self.keydown(event.key)
                elif event.type == KEYUP:
                    self.keyup(event.key)
            # if can't read video feed (e.g. tello stops transmitting)
            if frame_read.stopped:
                frame_read.stop()
                break
            # fill up colors/window
            self.screen.fill([0, 0, 0])
            frame = cv2.cvtColor(frame_read.frame, cv2.COLOR_BGR2RGB)
            frame = np.rot90(frame)
            frame = np.flipud(frame)
            frame = pygame.surfarray.make_surface(frame)
            self.screen.blit(frame, (0, 0))

            # telemtry values
            batt = self.tello.get_battery()
            flytime = self.tello.get_flight_time()

            # adding facial recognition bounding box
            cam = frame_read.frame
            (h, w) = cam.shape[:2]
            blob = cv2.dnn.blobFromImage(cv2.resize(cam, (300, 300)), 1.0,
                                         (300, 300), (104.0, 177.0, 123.0))
            # initiate facial recognition model
            net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])
            # passing blob through neural network to detect faces
            net.setInput(blob)
            detections = net.forward()

            for i in range(0, detections.shape[2]):
                confidence = detections[0, 0, i, 2]
                if confidence < args["confidence"]:
                    continue
                box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
                (Xi, Yi, Xf, Yf) = box.astype("int")
                cv2.rectangle(cam, (Xi, Yi), (Xf, Yf), (255, 255, 255), 2)

                # center of screen
                screen_center_x = 480
                screen_center_y = 360
                # draw center of screen
                cv2.circle(cam, (screen_center_x, screen_center_y), 6,
                           (0, 0, 255), 2, 8, 0)

                # target area
                TXi = screen_center_x - 150
                TXf = screen_center_x + 150
                TYi = screen_center_y - 150
                TYf = screen_center_y + 150
                # draw target area
                cv2.rectangle(cam, (TXi, TYi), (TXf, TYf), (255, 255, 0), 2)

                # center of bounding box
                box_center_x = (Xf + Xi) / 2
                box_center_y = (Yf + Yi) / 2
                # draw the center of bounding box
                cv2.circle(cam, (int(box_center_x), int(box_center_y)), 6,
                           (255, 0, 0), 2, 8, 0)

                # area of bounding box
                box_area = (Xf - Xi) * (Yf - Yi)
                # 65000 ~ 75000

                if override == False:
                    if box_center_x > TXf:
                        self.yaw_velocity = P
                        self.left_right_velocity = W
                    elif box_center_x < TXi:
                        self.yaw_velocity = -P
                        self.left_right_velocity = -W
                    else:
                        self.yaw_velocity = 0
                        self.left_right_velocity = 0

                    if box_center_y > TYf:
                        self.up_down_velocity = -S
                    elif box_center_y < TYi:
                        self.up_down_velocity = S
                    else:
                        self.up_down_velocity = 0

                    if box_area > 40000:
                        self.for_back_velocity = -M
                    elif box_area > 30000 and box_area < 40000:
                        self.for_back_velocity = -Q
                    elif box_area > 10000 and box_area < 20000:
                        self.for_back_velocity = Q
                    elif box_area < 10000:
                        self.for_back_velocity = M
                    else:
                        self.for_back_velocity = 0

            # show telementry data
            cv2.putText(cam, "Battery %: " + str(batt), (10, 30),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 255, 255), 2)
            cv2.putText(cam, "Flight time: " + str(flytime), (10, 70),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 255, 255), 2)
            cv2.putText(cam, "Manual mode? " + str(override), (10, 110),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 255, 255), 2)
            cv2.imshow("Display Window", cam)

            # frequency of update
            time.sleep(1 / FPS)
        # return (allocated memory) to the store of available RAM
        self.tello.end()

    def keydown(self, key):
        """ Update velocities based on key pressed
        Arguments:
            key: pygame key
        """
        if key == pygame.K_UP:  # set forward velocity
            self.for_back_velocity = N
        elif key == pygame.K_DOWN:  # set backward velocity
            self.for_back_velocity = -N
        elif key == pygame.K_LEFT:  # set left velocity
            self.left_right_velocity = -N
        elif key == pygame.K_RIGHT:  # set right velocity
            self.left_right_velocity = N
        elif key == pygame.K_w:  # set up velocity
            self.up_down_velocity = N
        elif key == pygame.K_s:  # set down velocity
            self.up_down_velocity = -N
        elif key == pygame.K_a:  # set yaw clockwise velocity
            self.yaw_velocity = -N
        elif key == pygame.K_d:  # set yaw counter clockwise velocity
            self.yaw_velocity = N

    def keyup(self, key):
        """ Update velocities based on key released
        Arguments:
            key: pygame key
        """
        if key == pygame.K_UP or key == pygame.K_DOWN:  # set zero forward/backward velocity
            self.for_back_velocity = 0
        elif key == pygame.K_LEFT or key == pygame.K_RIGHT:  # set zero left/right velocity
            self.left_right_velocity = 0
        elif key == pygame.K_w or key == pygame.K_s:  # set zero up/down velocity
            self.up_down_velocity = 0
        elif key == pygame.K_a or key == pygame.K_d:  # set zero yaw velocity
            self.yaw_velocity = 0
        elif key == pygame.K_t:  # takeoff
            self.tello.takeoff()
            self.send_rc_control = True
        elif key == pygame.K_l:  # land
            self.tello.land()
            self.send_rc_control = False

    def update(self):
        """ Update routine. Send velocities to Tello."""
        if self.send_rc_control:
            self.tello.send_rc_control(self.left_right_velocity,
                                       self.for_back_velocity,
                                       self.up_down_velocity,
                                       self.yaw_velocity)
Пример #3
0
from djitellopy import Tello
import sys, time

if len(sys.argv) > 1:
    tello = Tello(sys.argv[1])
else:
    tello = Tello()

tello.connect()

speed = tello.query_speed()
snr = tello.query_wifi_signal_noise_ratio()
sdk = tello.query_sdk_version()
serial = tello.query_serial_number()

print("Speed = ", speed)
print("Battery = ", tello.get_battery())
print("Duration = ", tello.get_flight_time())
print("Height = ", tello.get_height())
print("Distance = ", tello.get_distance_tof())
print("Barometer = ", tello.get_barometer())
print("Attitude = ", tello.get_pitch(), tello.get_roll(), tello.get_yaw())
print("WiFi SNR = ", snr)
print("SDK Version = ", sdk)
print("Serial Number = ", serial)

print(tello.get_current_state())
Пример #4
0
drone.get_acceleration_x()
drone.get_acceleration_y()
drone.get_acceleration_z()
# get acceleration in int

drone.get_height()
# get height in cm

drone.get_distance_tof()
# current distance from tof in cm

drone.get_barometer()
# barometer measurement in cm

drone.get_flight_time()
# flight time in s

drone.get_battery()
# get battery percentage

##############################
####### control functions :
##############################

# drone.move_x(int dist)
# x : up, down, left, right, forawrd, back
# dist : 20-500

# drone.rotate_clockwise(int deg)
# drone.rotate_counter_clockwise(int deg)