Crazyflie入门教程-控制-检测周围障碍物
说明:
- 介绍将如何设置系统并进行首次自主交互式飞行
相关设备:
- crazyflie套件:采购地址
所需硬件:
- 1 x Crazyflie 2.X套件
- 1 x Crazyradio PA
- 1 x Flow V2平台
步骤:
安装Python和cflib
用于控制Crazyflie 2.X的后端库称为cflib,是用python 3编写的。要使用它,您必须在计算机上安装Pyhton 3,点击下载
使用标准设置安装python,为方便起见,勾选Add to PATH复选框
- 安装python 3后,打开命令提示符并使用pip安装cflib。
pip3 install cflib
当一切设置完成并安装完毕后,启动Python编辑器IDLE3
选择“文件”->“新建”,然后将以下脚本复制/粘贴到新脚本中。用合适的名称保存脚本。
"""
Example script that allows a user to "push" the Crazyflie 2.X around
using your hands while it's hovering.
This examples uses the Flow and Multi-ranger decks to measure distances
in all directions and tries to keep away from anything that comes closer
than 0.2m by setting a velocity in the opposite direction.
The demo is ended by either pressing Ctrl-C or by holding your hand above the
Crazyflie.
"""
import logging
import sys
import time
import cflib.crtp
from cflib.crazyflie import Crazyflie
from cflib.crazyflie.syncCrazyflie import SyncCrazyflie
from cflib.positioning.motion_commander import MotionCommander
from cflib.utils.multiranger import Multiranger
URI = 'radio://0/80/2M'
if len(sys.argv) > 1:
URI = sys.argv[1]
# Only output errors from the logging framework
logging.basicConfig(level=logging.ERROR)
def is_close(range):
MIN_DISTANCE = 0.2 # m
if range is None:
return False
else:
return range < MIN_DISTANCE
if __name__ == '__main__':
# Initialize the low-level drivers (don't list the debug drivers)
cflib.crtp.init_drivers(enable_debug_driver=False)
cf = Crazyflie(rw_cache='./cache')
with SyncCrazyflie(URI, cf=cf) as scf:
with MotionCommander(scf) as motion_commander:
with Multiranger(scf) as multi_ranger:
keep_flying = True
while keep_flying:
VELOCITY = 0.5
velocity_x = 0.0
velocity_y = 0.0
if is_close(multi_ranger.front):
velocity_x -= VELOCITY
if is_close(multi_ranger.back):
velocity_x += VELOCITY
if is_close(multi_ranger.left):
velocity_y -= VELOCITY
if is_close(multi_ranger.right):
velocity_y += VELOCITY
if is_close(multi_ranger.up):
keep_flying = False
motion_commander.start_linear_motion(
velocity_x, velocity_y, 0)
time.sleep(0.1)
print('Demo terminated!')
通过按F5运行脚本
注意:如果您打开了python客户端,请确保Crazyflie已从其断开连接。Crazyradio不支持同时来自多个程序的连接,如果Crazyflie仍连接到python客户端,则脚本将不起作用。
输出应与此类似:
Connecting to radio://0/110/2M
Connected to radio://0/110/2M
Demo terminated!
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号