前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >TT无人机扩展模块库分析(操控函数)1

TT无人机扩展模块库分析(操控函数)1

作者头像
云深无际
发布2020-11-19 14:31:50
5780
发布2020-11-19 14:31:50
举报
文章被收录于专栏:云深之无迹
代码语言:javascript
复制

/**
 * Gamesir joystick control handling
 *  - Receive command from the joystick
 *  - Control the drone by received command
 *
 * @param arg Parameter about task control
 */
void gamesir_task(void *arg)
{
    uint8_t command_init = 0;
    uint8_t mac_init = 0;

    uint8_t stop_cnt = 5;

    for (;;)
    {
        if (mac_init == 0)
        {
            CommonSerial.print("[TELLO] getmac?");
            delay(100);
#ifdef __DEFAULT_LOG__
            Serial.println("gamesir_task(): mac is ok?");
#endif
            if (rmtt_joystick_mac_is_valid())
            {
#ifdef __DEFAULT_LOG__
                Serial.println("gamesir_task(): ble mac init");
#endif
                p_tt_gamesir->Init(get_rmtt_joystick_mac());
                mac_init = 1;
            }
        }
        else if ((command_init == 0) && (p_tt_gamesir->GetConnectedStatus()))
        {
            tt_sdk.SDKOn();
            delay(100);
            if (rmtt_bool_is_valid())
            {
                command_init = 1;
            }
        }
        else if (p_tt_gamesir->DataIsValid())
        {
            // Serial.println("data is update");
            PlainData data = p_tt_gamesir->GetData();

            int lx = ((float)data.left_x_3d - 512) / 512.0 * 100;
            int ly = ((float)data.left_y_3d - 512) / 512.0 * 100;
            int rx = ((float)data.right_x_3d - 512) / 512.0 * 100;
            int ry = ((float)data.right_y_3d - 512) / 512.0 * 100;

            if ((data.btn3 == 0x01) && (data.L2))
            {
                tt_sdk.Flip('f');
            }
            else if ((data.btn3 == 0x03) && (data.L2))
            {
                tt_sdk.Flip('r');
            }
            else if ((data.btn3 == 0x05) && (data.L2))
            {
                tt_sdk.Flip('b');
            }
            else if ((data.btn3 == 0x07) && (data.L2))
            {
                tt_sdk.Flip('l');
            }
            else if ((data.Y) && (data.R2))
            {
                if (takeoff_status == 0)
                {
                    tt_sdk.TakeOff();
                    takeoff_status = 1;
                }
                else
                {
                    tt_sdk.Land();
                    takeoff_status = 0;
                }
            }
            else
            {
#ifdef BLE_JAPAN_CTRL
                tt_sdk.SetRC(lx, -ly, -ry, rx);
#else
                tt_sdk.SetRC(rx, -ry, -ly, lx);
#endif
            }
        }

        /* 避免rc指令粘包 */
        delay(10);

        /* Regularly send data packet to ensure the drone
        floating steadily after the controller was offline*/
        if ((now_time - last_clean_time > 300) && command_init)
        {
            if (p_tt_gamesir->GetDataOffline())
            {
                if (stop_cnt)
                {
                    tt_sdk.SetRC(0, 0, 0, 0);
                    stop_cnt--;
                }
            }
            else
            {
                /* keepactive */
                CommonSerial.print("[TELLO] keepalive");
                stop_cnt = 5;
            }
            last_clean_time = millis();
        }
        else
        {
        }

        now_time = millis();
    }
}

* Gamesir游戏杆控制处理

*-从操纵杆接收命令

*-通过收到的命令控制无人机

*

* @param arg有关任务控制的参数

*

unsigned char这个东西我上面解读过

cnt变量多次出现,我去查一下

可以看到计数变量

for(;;)死循环

开始一个判断,如果mac的指令未初始化继续往下运行.

下面又是许多的判断语句,符合各种情况去处理

这个现象叫做粘包,就是指两次结果粘到一起了.看这个地方解决很简单,就是延时了一下.

打印,得到mac的地址没有?

延时一下,就开始继续运行,此时调试的打印开关打开.

手柄任务:mac地址ok嘛~

如果地址有效为正~打印:手柄(蓝牙地址初始化)

这个是初始化的函数,参数是这个手柄的蓝牙Mac.

接着

这是这个函数

函数声明

第一个是参数的声明

第一次判断

任务的结尾,吧标志位更改

如果这个指令初始的值为0和和链接的状态信息与运算为真就开始执行.

我们看到这个函数的命名空间是Protocol里面定义

下文看

SDK控制开始

*获取是否收到布尔类型数据的有效状态

*来自无人机

普通数据有个类

接着这个地方判断成立的条件是数据有效

后面结合SDK解读

这段有的地方不懂,翻转?

降落

我们打开这个看看下面

日本手用左手控制油门

下期我们来对飞行指令解包

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

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

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

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

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