前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >RoboMaster TT 无人机microPython编程.1

RoboMaster TT 无人机microPython编程.1

作者头像
云深无际
发布2021-04-14 10:30:20
1.9K0
发布2021-04-14 10:30:20
举报
文章被收录于专栏:云深之无迹

感谢小马的美图

这个Mind+中有支持TT扩展件MicroPython实现,今日做个整理以及有一些实验要做:

代码语言:javascript
复制
from machine import *
from RMTTLib import *

i2c = I2C(0, scl=Pin(26), sda=Pin(27), freq=400000)
# 控制小屏幕的显示,用I2C的接口,第一个参数我有点没有看懂
led = RMTTLedCtrl()
# 新建一个led的对象
matrix = RMTTMledCtrl(i2c)
# 新建一个矩阵对象

tof = RMTTToF(i2c)
# 读取TOF的距离

p34 = Pin(34, Pin.IN)
# 设置按下的对应的IO脚

uart1 = UART(1, baudrate=1000000, tx=18, rx=23)
# 飞行控制的按钮,就是摁下按钮开始进入command模式
# 使用的是esp32的serial1串口,波特率为100w,引脚在内部进行了重映射

protocol = RMTTProtocol(uart1)
# 新建一个控制对象,参数是串口

led.normal(0, 0, 255)
# 设置一个静态的灯颜色
led.normal(255, 255, 255)
led.start()
# 这个是开启的方法,设置好以后开启
led.breath(1, 0, 0, 255)
# 设置一个LED灯为呼吸模式,第一个参数为频率
led.breath(1, 255, 255, 255)
led.blink(1, 255, 0, 0, 0, 255, 0)
# 设置两个LED的闪烁
led.stop()
# 特效关闭的方法


matrix.normal(
    '0000000000rrrr0000r00r0000r00r0000r00r000rr0rr000rr0rr0000000000')
# 显示一个图像
matrix.static_char('A', 'r')
matrix.static_char('A', 'b')
matrix.static_char('A', 'p')
# 显示一个字母A,颜色是红色,然后看文档这个地方只有三个颜色可以显示,所以只有三个参数

matrix.static_graph(260, 'r')
matrix.static_graph(260, 'b')
matrix.static_graph(260, 'p')
# 显示图案和颜色

matrix.moveable_graph(
    'd', 1, '0000000000rrrr0000r00r0000r00r0000r00r000rr0rr000rr0rr0000000000')
# 滚动的显示图像
# 参数是方向,移动频率,图案

matrix.stop()
# 关闭特效的方法


print(tof.read())
# 打印这个距离

while not ((p34.value() == 0)):
    pass
# 这个代码是如何来判断TT的按键有没有按下,是用来开启机器的关键


def getTelloMsgString(self, cmd, timeout):
    while self.uart.any() > 0:
        self.uart.read()
    self.uart.write(cmd)
    back = ""
    oldtime = time.ticks_ms()
    while self.uart.any() == 0:
        newtime = time.ticks_ms()
        if (newtime-oldtime) > timeout:
            return "timeout"
    while self.uart.any() > 0:
        back += self.uart.read().decode()
    if back.find("\r\n"):
        back = back.replace("\r\n", "")
    return back
# 接受Tello的回传信息的函数放到这里,下面的函数要用


def startUntilControl(self):
    while True:
        if(self.getTelloMsgString("[TELLO] command", 1000) == "ETT ok"):
            break
    self.setColor(0, 255, 0)
# 这个方法我放到这里


protocol.startUntilControl()
# 阻塞方法直达收到飞机的ok信息

protocol.sendTelloCtrlMsg("motoron")
# 进入起浆模式,文档说是可以散热。emmmm,我觉得这个命令测试飞机是不是接收到了命令还不错
protocol.sendTelloCtrlMsg("motoroff")
# 对偶命令,停桨
protocol.sendTelloCtrlMsg("takeoff")
# 起飞,这个地方我明天测量起飞的默认距离
protocol.sendTelloCtrlMsg("throwfly")
# ε=ε=ε=(~ ̄▽ ̄)~,装逼命令。平行抛出
protocol.sendTelloCtrlMsg("land")
# 降落命令
protocol.sendTelloCtrlMsg("emergency")
# 紧急停机,做测试
protocol.sendTelloCtrlMsg("up "+str(int(50)))
# 向上飞行,以及下。后是一个cm的数值参数。str函数
protocol.sendTelloCtrlMsg("right "+str(int(50)))
# 向左右前后,就是空间平面飞行
protocol.sendTelloCtrlMsg("cw "+str(int(90)))
# 旋转,两个方向,很有用的命令
protocol.sendTelloCtrlMsg("flip f")
# 翻滚
protocol.sendTelloCtrlMsg("go "+str(int(50))+" " +
                          str(int(50))+" "+str(int(0))+" "+str(int(100)))
# 就是以当前飞机为中心点,然后飞行xyz这个地点,cm是单位。最后是一个速度
protocol.sendTelloCtrlMsg("mon")
# 打开 探测的功能
protocol.sendTelloCtrlMsg("mdirection 2")
# 下,前,所有->0,1,2


protocol.sendTelloCtrlMsg(
    "go "+str(int(50))+" "+str(int(50))+" "+str(int(80))+" "+str(int(50))+" "+"m-1")
# 飞往挑战卡:以当前挑战卡为中心点,然后飞行xyz这个地点,cm是单位。速度。后面的参数比较多,我需要测试
protocol.sendTelloCtrlMsg("curve "+str(int(20))+" "+str(int(20))+" "+str(int(80)) +
                          " "+str(int(40))+" "+str(int(60))+" "+str(int(80))+" "+str(int(60))+" "+"m-1")
# protocol.sendTelloCtrlMsg("curve "+str(int(20))+" "+str(int(20))+" "+str(int(80))+" "+str(int(40))+" "+str(int(60))+" "+str(int(80))+" "+str(int(60))+" "+"m-1")
protocol.sendTelloCtrlMsg("jump "+str(int(100))+" "+str(int(0))+" " +
                          str(int(80))+" "+str(int(50))+" "+str(int(0))+" "+"m-1"+" "+"m-1")
# 跳跃飞行,参数xyz,一个是偏航:偏航是飞机绕机体坐标系竖轴的短时旋转运动。
# 竖轴通过飞机重心在飞机对称平面内并垂直于纵轴。
# 后面的两个参数是跳跃点
protocol.sendTelloCtrlMsg("setyaw "+str(0)+" "+"m-1")
# 就是飞机在挑战卡上面旋转多少度
protocol.getTelloStatus(1000)
# 状态码,我这个不懂
# 我又懂了,就是飞机时时刻刻的发送很多的数据。这个数据是不停的更新的,上面的命令相当于一个读取频率
# 然后用print语句来打印相关的information

while True:
    protocol.getTelloStatus(1000)
    print(protocol.getTelloStatusWithName("x"))
    # 相对挑战卡的x坐标
    print(protocol.getTelloStatusWithName("pitch"))
    # 俯仰角
    print(protocol.getTelloStatusWithName("temph"))
    # 最高温度,tof距离啥的

while True:
    pass

上面得代码,是分为三个大部分:

  1. RGB灯得控制方法
  2. 显示屏幕得方法
  3. 控制无人机得一些命令

按照写法又分为,初始化,具体方法罗列:

代码语言:javascript
复制
i2c = I2C(0, scl=Pin(26), sda=Pin(27), freq=400000)
# 控制小屏幕的显示,用I2C的接口,第一个参数我有点没有看懂
led = RMTTLedCtrl()
# 新建一个led的对象
matrix = RMTTMledCtrl(i2c)
# 新建一个矩阵对象

tof = RMTTToF(i2c)
# 读取TOF的距离

p34 = Pin(34, Pin.IN)
# 设置按下的对应的IO脚

uart1 = UART(1, baudrate=1000000, tx=18, rx=23)
# 飞行控制的按钮,就是摁下按钮开始进入command模式
# 使用的是esp32的serial1串口,波特率为100w,引脚在内部进行了重映射

protocol = RMTTProtocol(uart1)
# 新建一个控制对象,参数是串口

你现在看到得代码,是执行时得初始画代码

  1. 引脚得分配和定义(I2C,串口,IO)
  2. 以及相应得控制对象得建立
代码语言:javascript
复制
led.normal(0, 0, 255)
# 设置一个静态的灯颜色
led.normal(255, 255, 255)
led.start()
# 这个是开启的方法,设置好以后开启
led.breath(1, 0, 0, 255)
# 设置一个LED灯为呼吸模式,第一个参数为频率
led.breath(1, 255, 255, 255)
led.blink(1, 255, 0, 0, 0, 255, 0)
# 设置两个LED的闪烁
led.stop()
# 特效关闭的方法

这段是控制RGB得方法(应该是所有)

代码语言:javascript
复制
matrix.normal(
    '0000000000rrrr0000r00r0000r00r0000r00r000rr0rr000rr0rr0000000000')
# 显示一个图像
matrix.static_char('A', 'r')
matrix.static_char('A', 'b')
matrix.static_char('A', 'p')
# 显示一个字母A,颜色是红色,然后看文档这个地方只有三个颜色可以显示,所以只有三个参数

matrix.static_graph(260, 'r')
matrix.static_graph(260, 'b')
matrix.static_graph(260, 'p')
# 显示图案和颜色

matrix.moveable_graph(
    'd', 1, '0000000000rrrr0000r00r0000r00r0000r00r000rr0rr000rr0rr0000000000')
# 滚动的显示图像
# 参数是方向,移动频率,图案

matrix.stop()
# 关闭特效的方法

这个是小屏幕得方法

代码语言:javascript
复制
print(tof.read())
# 打印这个距离

这个tof是前置传感器,我写了一些内容

第一次得代码,只能打印一次

因为是不停的读入,所以放进死循环

代码语言:javascript
复制
from machine import *
from RMTTLib import *
i2c = I2C(0, scl=Pin(26), sda=Pin(27), freq=400000)
tof = RMTTToF(i2c)
print(tof.read())
while True:
  pass

结果是一个

接着是改进的代码

代码语言:javascript
复制
from machine import *
from RMTTLib import *
i2c = I2C(0, scl=Pin(26), sda=Pin(27), freq=400000)
tof = RMTTToF(i2c)
while True:
  print(tof.read())

输出结果了,但是太快了。不太实用,下面的代码加入了一些延时

具体在写的时候,需要写个limit

代码语言:javascript
复制
while True:
  time.sleep(2)
  print(tof.read())

这样的话会好一些,就是速度还是慢,

摁这个打开飞行控制

代码语言:javascript
复制
p34 = Pin(34, Pin.IN)
# 设置按下的对应的IO脚

uart1 = UART(1, baudrate=1000000, tx=18, rx=23)
# 飞行控制的按钮,就是摁下按钮开始进入command模式
# 使用的是esp32的serial1串口,波特率为100w,引脚在内部进行了重映射

protocol = RMTTProtocol(uart1)
# 新建一个控制对象,参数是串口
while not ((p34.value() == 0)):
    pass
# 这个代码是如何来判断TT的按键有没有按下,是用来开启机器的关键


def getTelloMsgString(self, cmd, timeout):
    while self.uart.any() > 0:
        self.uart.read()
    self.uart.write(cmd)
    back = ""
    oldtime = time.ticks_ms()
    while self.uart.any() == 0:
        newtime = time.ticks_ms()
        if (newtime-oldtime) > timeout:
            return "timeout"
    while self.uart.any() > 0:
        back += self.uart.read().decode()
    if back.find("\r\n"):
        back = back.replace("\r\n", "")
    return back
# 接受Tello的回传信息的函数放到这里,下面的函数要用


def startUntilControl(self):
    while True:
        if(self.getTelloMsgString("[TELLO] command", 1000) == "ETT ok"):
            break
    self.setColor(0, 255, 0)
# 这个方法我放到这里


protocol.startUntilControl()
# 阻塞方法直达收到飞机的ok信息

以上代码注释了主要的部分,具体的后面写

就是进入SDK控制模式需要的代码

代码语言:javascript
复制
protocol.sendTelloCtrlMsg("motoron")
# 进入起浆模式,文档说是可以散热。emmmm,我觉得这个命令测试飞机是不是接收到了命令还不错
protocol.sendTelloCtrlMsg("motoroff")
# 对偶命令,停桨
protocol.sendTelloCtrlMsg("takeoff")
# 起飞,这个地方我明天测量起飞的默认距离
protocol.sendTelloCtrlMsg("throwfly")
# ε=ε=ε=(~ ̄▽ ̄)~,装逼命令。平行抛出
protocol.sendTelloCtrlMsg("land")
# 降落命令
protocol.sendTelloCtrlMsg("emergency")
# 紧急停机,做测试
protocol.sendTelloCtrlMsg("up "+str(int(50)))
# 向上飞行,以及下。后是一个cm的数值参数。str函数
protocol.sendTelloCtrlMsg("right "+str(int(50)))
# 向左右前后,就是空间平面飞行
protocol.sendTelloCtrlMsg("cw "+str(int(90)))
# 旋转,两个方向,很有用的命令
protocol.sendTelloCtrlMsg("flip f")
# 翻滚
protocol.sendTelloCtrlMsg("go "+str(int(50))+" " +
                          str(int(50))+" "+str(int(0))+" "+str(int(100)))
# 就是以当前飞机为中心点,然后飞行xyz这个地点,cm是单位。最后是一个速度
protocol.sendTelloCtrlMsg("mon")
# 打开 探测的功能
protocol.sendTelloCtrlMsg("mdirection 2")
# 下,前,所有->0,1,2


protocol.sendTelloCtrlMsg(
    "go "+str(int(50))+" "+str(int(50))+" "+str(int(80))+" "+str(int(50))+" "+"m-1")
# 飞往挑战卡:以当前挑战卡为中心点,然后飞行xyz这个地点,cm是单位。速度。后面的参数比较多,我需要测试
protocol.sendTelloCtrlMsg("curve "+str(int(20))+" "+str(int(20))+" "+str(int(80)) +
                          " "+str(int(40))+" "+str(int(60))+" "+str(int(80))+" "+str(int(60))+" "+"m-1")
# protocol.sendTelloCtrlMsg("curve "+str(int(20))+" "+str(int(20))+" "+str(int(80))+" "+str(int(40))+" "+str(int(60))+" "+str
protocol.sendTelloCtrlMsg("jump "+str(int(100))+" "+str(int(0))+" " +
                          str(int(80))+" "+str(int(50))+" "+str(int(0))+" "+"m-1"+" "+"m-1")
# 跳跃飞行,参数xyz,一个是偏航:偏航是飞机绕机体坐标系竖轴的短时旋转运动。
# 竖轴通过飞机重心在飞机对称平面内并垂直于纵轴。
# 后面的两个参数是跳跃点
protocol.sendTelloCtrlMsg("setyaw "+str(0)+" "+"m-1")
# 就是飞机在挑战卡上面旋转多少度

这些是具体的控制命令

这两个地方需要进行测试

代码语言:javascript
复制
protocol.getTelloStatus(1000)
# 状态码,我这个不懂
# 我又懂了,就是飞机时时刻刻的发送很多的数据。这个数据是不停的更新的,上面的命令相当于一个读取频率
# 然后用print语句来打印相关的information

while True:
    protocol.getTelloStatus(1000)
    print(protocol.getTelloStatusWithName("x"))
    # 相对挑战卡的x坐标
    print(protocol.getTelloStatusWithName("pitch"))
    # 俯仰角
    print(protocol.getTelloStatusWithName("temph"))

状态码的应用


就是飞机时时刻刻的发送很多的数据。这个数据是不停的更新的,上面的命令相当于一个读取频率# 然后用print语句来打印相关的information。读取这个信息来做一写动作

可以读取这些

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-04-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 云深之无迹 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档