首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CoDrone is_ready_to_fly()失败

CoDrone is_ready_to_fly()失败
EN

Stack Overflow用户
提问于 2020-07-05 22:24:20
回答 1查看 52关注 0票数 0

我已经开始用python3 (基础和机器学习)编程。为了练习OOP,我创建了下面的python程序来控制无人机(从CoDrone到robolink.com)。这些类主要是为此目的创建的(实践OOP),因此我想保留它们。然而,使用类会带来一些复杂性,我怀疑这可能是程序无法工作的原因。

test.py文件:

代码语言:javascript
复制
import CoDrone


class Drone:
    """Parent class with general data."""

    def __init__(self):
        self.drone = CoDrone.CoDrone()
        self.state = ''
        self.flight_state = False

    def get_statuses(self):
        """Get statuses from drone."""
        self.state = 'placeholder'
        self.flight_state = self.drone.is_ready_to_fly()
        return print(f"[info]: General State:\t{self.state}\n" +
                     f"[info]: Flight state:\t{self.flight_state}")


class Connection(Drone):
    """Connection class."""

    def __init__(self):
        self.pair_status = False
        super().__init__()

    def connect_ble(self):
        """Connect drone with BLE board."""
        # For me the BLE board is connected to COM5 port (device management).
        # TODO: Print error when BLE board is not connected.
        print("\nConnecting to drone #7852 on port COM5...")
        self.pair_status = self.drone.pair('7852', 'COM5')

        if self.pair_status:
            print("Drone successfully paired")
        else:
            print("Try again for any drone and on any port")
            self.drone.pair()

    def disconnect_ble(self):
        """Disconnect drone from BLE board."""
        self.drone.disconnect()
        self.pair_status = False


class Flight(Drone):
    """Flight movements class."""

    def __init__(self):
        super().__init__()

    def drone_dance(self):
        """Perform dance with drone."""
        self.get_statuses()
        if self.flight_state:  # TODO: Why is the status not TRUE?
            print("Dance!, Dance!, Dance!")
            self.drone.takeoff()
            self.drone.hover(5)
            self.drone.land()
        else:
            print(f"\nDrone is not ready to fly.\n\nCheck:\n" +
                  f"1. Is the drone oriented right-side up?\n" +
                  f"2. Is the drone already flying?")
            while True:
                usri_continue = input("Do you want to try again? (y/n):\n")
                if usri_continue == 'y' or usri_continue == 'Y':
                    self.drone_dance()
                    break
                elif usri_continue == 'n' or usri_continue == 'N':
                    print(f"\n[!!!!]: Program terminated")
                    break
                else:
                    continue


if __name__ == '__main__':
    DRONE_CONNECT = Connection()
    DRONE_FLIGHT = Flight()

    DRONE_CONNECT.connect_ble()
    DRONE_FLIGHT.drone_dance()
    DRONE_CONNECT.disconnect_ble()
    DRONE_CONNECT.get_statuses()

在课堂上:"Flight(Drone)",方法:"drone_dance()“应该让无人机在self.flight_stateTrue时移动。但是,当变量被选中时,状态是False

代码语言:javascript
复制
def drone_dance(self):
        """Perform dance with drone."""
        self.get_statuses()
        if self.flight_state:  # TODO: Why is the status not TRUE?
            print("Dance!, Dance!, Dance!")
            self.drone.takeoff()
            self.drone.hover(5)
            self.drone.land()
        else:

关于飞()的文档

此函数通过返回布尔值来检查无人机是否已准备好飞行。无人机已经准备好了,如果它的方向是向右向上,而不是飞行。

如果你知道为什么self.flight_stateFalse,我希望它是True,因为无人机是成对的和直立的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-06 01:14:17

我已经更改为代码(见下面),现在它可以工作了。

代码语言:javascript
复制
import CoDrone

drone = CoDrone.CoDrone()


class Drone:
    """Parent class with general data."""

    def get_statuses(self):
        """Get statuses from drone."""
        self.state = drone.get_state()
        self.flight_state = drone.is_ready_to_fly()
        return print(f"[info]: General State:\t{self.state}\n" +
                     f"[info]: Flight state:\t{self.flight_state}")


class Connection(Drone):
    """Connection class."""

    def __init__(self):
        self.pair_status = False

    def connect_ble(self):
        """Connect drone with BLE board."""
        # For me the BLE board is connected to COM5 port (device management).
        # TODO: Print error when BLE board is not connected.
        print("\nConnecting to drone #7852 on port COM5...")
        self.pair_status = drone.pair('7852', 'COM5')

        if self.pair_status:
            print("Drone successfully paired")
            self.get_statuses()
        else:
            print("Try again for any drone and on any port")
            drone.pair()

    def disconnect_ble(self):
        """Disconnect drone from BLE board."""
        drone.disconnect()
        self.pair_status = False
        print("Drone is disconnected.")


class Flight(Drone):
    """Flight movements class."""

    def drone_dance(self):
        """Perform dance with drone."""
        Connection().connect_ble()
        if drone.is_ready_to_fly():
            print("Dance!, Dance!, Dance!")
            drone.takeoff()
            self.get_statuses()
            drone.hover(2)
            drone.land()
        else:
            print(f"\nDrone is not ready to fly.\n\nCheck:\n" +
                  f"1. Is the drone oriented right-side up?\n" +
                  f"2. Is the drone already flying?")
            while True:
                usri_continue = input("Do you want to try again? (y/n):\n")
                if usri_continue == 'y' or usri_continue == 'Y':
                    self.drone_dance()
                    break
                elif usri_continue == 'n' or usri_continue == 'N':
                    Connection().disconnect()
                    print(f"\n[!!!!]: Program terminated")
                    break
                else:
                    continue


def main():
    """Execute when program is run, not when imported."""
    if __name__ == '__main__':
        try:
            DRONE_FLIGHT = Flight()
            DRONE_FLIGHT.drone_dance()
        except:
            print("[error}: BLE board not connected.")
            while True:
                usri_continue = input("Do you want to try again? (y/n):\n")
                if usri_continue == 'y' or usri_continue == 'Y':
                    main()
                    break
                elif usri_continue == 'n' or usri_continue == 'N':
                    print(f"\n[!!!!]: yProgram terminated")
                    DRONE_CONNECT = Connection()
                    DRONE_CONNECT.disconnect_ble()
                    break
                else:
                    continue


main()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62746981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档