e_drive for python / Examples / Ping

Modified : 2019.4.12



Ping 테스트

조종기에 Ping을 보내고, Ack 응답을 받아서 화면에 출력하는 예제입니다.

sendPing() 함수를 호출하고 응답을 받을 때까지 기다리는 시간이 필요하기 때문에 sleep() 함수를 사용했습니다.

데이터 송수신 과정에서 문제가 생겨 Ack를 받지 못하는 경우가 있을 수 있으므로 1초 동안 응답이 없으면 수신처리를 종료하게 하였습니다.

from time import sleep

from e_drive.drone import *
from e_drive.protocol import *


if __name__ == '__main__':

    drone = Drone(False)
    drone.open()

    drone.sendPing(DeviceType.Controller)

    timeStart = time.time()

    while True:
        sleep(0.01)
        dataType = drone.check()

        if dataType == DataType.Ack:
            ack = drone.getData(DataType.Ack)
            print("{0} / {1} / {2:04X}".format(ack.dataType.name, ack.systemTime, ack.crc16))
            print("T: {0}".format(time.time() - timeStart))
            break;

        # 1초 동안 응답이 없을 경우 응답 확인을 빠져나감
        if time.time() > timeStart + 1:
            print("Time Over")
            break;

    drone.close()



Ping 테스트(이벤트 함수 등록)

조종기에 Ping을 보내고, Ack 응답을 받아서 화면에 출력하는 예제입니다.

수신 받을 데이터를 처리하는 함수를 만들어 setEventHandler(DataType, function)를 사용하여 등록하면 해당 데이터를 받았을 때 미리 연결한 함수를 호출합니다.

마지막 sleep(0.1)Ping 전송 후 Ack 응답이 들어올 때까지 기다리기 위해 사용하였습니다.

from time import sleep

from e_drive.drone import *
from e_drive.protocol import *


def eventAck(ack):
    print("eventAck()")
    print("{0} / {1} / {2:04X}".format(ack.dataType.name, ack.systemTime, ack.crc16))


if __name__ == '__main__':

    drone = Drone()
    drone.open()

    # 이벤트 핸들링 함수 등록
    drone.setEventHandler(DataType.Ack, eventAck)

    # Ping 전송
    drone.sendPing(DeviceType.Controller)
    
    sleep(0.1)

    drone.close()



e_drive for python

  1. Intro
  2. Command Line
  3. System
  4. Protocol
  5. Drone
  6. Examples - Ping
  7. Examples - Information
  8. Examples - Control
  9. Examples - Sensor
  10. Examples - Motor
  11. Examples - Setup
  12. Examples - Buzzer
  13. Examples - Light
  14. Examples - Error


Index