Drone simple xy move

  1'''
  2Drone - Drone_simple_xy_move.py with synchronous mode.
  3
  4- Ensure the drones center of gravity is properly balanced.
  5- Verify that all mounted payloads are securely fastened.
  6- Confirm that all screws on the drone are tightened.
  7- Check that the drone battery is fully charged** (approximately 12.5V).
  8- Make sure the USB drive has sufficient storage** to record the flight data.
  9- Insert the USB drive into the flight control computer.
 10
 11For other examples please check:
 12    https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
 13See README.md file to get detailed usage of this example.
 14
 15Copyright (c) 2022-2025 WPC Systems Ltd. All rights reserved.
 16'''
 17
 18## WPC
 19from wpcsys import pywpc
 20
 21## Python
 22import time
 23
 24
 25def main():
 26    ## Get Python driver version
 27    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 28
 29    ## Parameters setting
 30    baudrate = 921600
 31    timeout = 3  ## [sec]
 32    x_move = 0.3  ## [m]
 33    y_move = 0.3  ## [m]
 34    velocity = 1  ## [m/s]
 35
 36    ## Create device handle
 37    dev = pywpc.Drone()
 38
 39    ## Connect to device
 40    try:
 41        dev.connect("COM5", baudrate)  ## Depend on your device
 42        # dev.connect("/dev/ttyTHS1", baudrate)  ## Depend on your device
 43    except Exception as err:
 44        pywpc.printGenericError(err)
 45        ## Release device handle
 46        dev.close()
 47        return
 48
 49    try:
 50        ## Get firmware model & version
 51        firmware_version = dev.Drone_getFirmwareVersion(timeout)
 52        print(f"Firmware version: {firmware_version}")
 53
 54        ## Get serial number
 55        serial_number = dev.Drone_getSerialNumber(timeout)
 56        print(f"Serial number: {serial_number}")
 57
 58        ## Read task control mode
 59        control_mode = dev.Drone_readTaskControlMode(timeout)
 60        if control_mode == 0:
 61            print("Please switch the remote controller to mission computer mode.")
 62            print("Terminate example code. Goodbye!")
 63            return
 64
 65        ## Set drone flight mode to position mode
 66        err = dev.Drone_setPositionMode(timeout)
 67        print(f"Drone_setPositionMode, status: {err}")
 68
 69        ## Get drone flight mode
 70        flight_mode = Drone_getFlightMode(timeout)
 71        print(f"Drone_getFlightMode, flight_mode: {flight_mode}")
 72
 73        ## Activate drone
 74        err = dev.Drone_activate(timeout)
 75        print(f"Drone_activate, status: {err}")
 76
 77        ## Start drone take-off
 78        err = dev.Drone_startTakeOff(timeout)
 79        print(f"Drone_startTakeOff, status: {err}")
 80
 81        ## Read drone take-off status
 82        takeoff_status = 0
 83        print("Taking off...")
 84        while takeoff_status == 0:
 85            takeoff_status = dev.Drone_getTakeOffStatus(timeout)
 86            if takeoff_status == 1:
 87                print("Completed the takeoff procedure")
 88
 89        ## Wait
 90        print("Wait a while")
 91        time.sleep(5)  ## delay [sec]
 92
 93        ## X move with vehicle frame
 94        err = dev.Drone_moveVehicleRelXY(x_move, y_move, velocity, timeout)
 95        print(f"Drone_moveVehicleRelXY, status: {err}")
 96
 97        ## Read inposition
 98        inposition = 0
 99        while inposition == 0:
100            posi_list = dev.Drone_readInposition(timeout)
101            x_ready = posi_list[3]
102            y_ready = posi_list[4]
103            inposition = int(bool(x_ready) and bool(y_ready))
104            if inposition == 0:
105                pass
106            else:
107                print("Reached XY!")
108
109    except Exception as err:
110        pywpc.printGenericError(err)
111
112    finally:
113        ## Wait
114        print("Wait a while")
115        time.sleep(5)  ## delay [sec]
116
117        ## Start landing
118        err = dev.Drone_startLanding(timeout)
119        print(f"Drone_startLanding, status: {err}")
120
121        ## Read landing status
122        landing_status = 0
123        print("Landing....")
124        while landing_status == 0:
125            landing_status = dev.Drone_getLandingStatus(timeout)
126            if landing_status == 1:
127                print("Landing successful!")
128
129        ## Disactivate drone
130        err = dev.Drone_deactivate(timeout)
131        print(f"Drone_deactivate, status: {err}")
132
133        ## Disconnect device
134        dev.disconnect()
135
136        ## Release device handle
137        dev.close()
138
139if __name__ == '__main__':
140    main()