Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arduino 如何使用MSP从CleanFlight读取陀螺信息?_Arduino - Fatal编程技术网

Arduino 如何使用MSP从CleanFlight读取陀螺信息?

Arduino 如何使用MSP从CleanFlight读取陀螺信息?,arduino,Arduino,使用Arduino,我将如何使用MultiWii串行协议从飞行控制器中的陀螺仪获取姿态?以下仅基于获取陀螺仪姿态信息,尽管它包括一些关于一般使用MSP的信息。可以找到引用的示例代码。其中最重要的部分是下面的数据部分,我在其他任何地方都找不到(一位朋友发现了这个秘密并让我知道了) 多Wii串行协议 首先,让我们看看MSP是如何工作的。我发现(替代链接)在理解这一点上非常有用,但我将在这里进行总结。 可以发送三种类型的消息 命令——发送给飞行控制器的信息,其中包含一些要发送的信息 请求——发送给飞

使用Arduino,我将如何使用MultiWii串行协议从飞行控制器中的陀螺仪获取姿态?

以下仅基于获取陀螺仪姿态信息,尽管它包括一些关于一般使用MSP的信息。可以找到引用的示例代码。其中最重要的部分是下面的数据部分,我在其他任何地方都找不到(一位朋友发现了这个秘密并让我知道了)


多Wii串行协议 首先,让我们看看MSP是如何工作的。我发现(替代链接)在理解这一点上非常有用,但我将在这里进行总结。 可以发送三种类型的消息

  • 命令——发送给飞行控制器的信息,其中包含一些要发送的信息
  • 请求——发送给飞行控制员要求返回某些信息的消息
  • 响应——飞行控制器发送的消息,其中包含响应请求的信息
  • MSP消息具有特定的结构。它们有一个标题、大小、类型、数据和校验和

    我发现这个图表非常有用:

    标题 标头为三个字节,包含消息开始字符“$M”和一个显示消息前进方向的字符。“”表示来自飞行控制器(响应)

    大小 第四个字节是数据段的长度(以字节为单位)。例如,如果数据部分有三个INT 16变量,那么大小字节将是6

    类型 字节类型指定消息中发送的信息。您可以找到类型列表。这方面的一个例子是MSP_态度,其类型编号为108

    资料 数据是发送所有信息的地方。请求消息中没有数据。命令和响应确实如此,因为它们包含信息。可以再次找到返回的数据类型

    数据部分最困难的部分是字节按顺序颠倒,这方面的文档记录非常糟糕。例如,如果我按这个顺序得到以下两个字节:

    10011010
    01001111
    
    您可能会认为它应该变成
    10011010 01001111
    ,但事实并非如此。它实际上会变成
    01001111 10011010

    在示例代码中,此操作如下所示:

    int16_t roll;
    byte c;                       // The current byte we read in.
    c = mspSerial.read();         // The first sent byte of the number.
    roll = c;                     // Put the first sent byte into the second byte of the int 16.
    c = mspSerial.read();         // The second sent byte of the number.
    roll <<= 8;                   // Move the first sent byte into the first byte of the int16.
    roll += c;                    // Put the second sent byte into the second byte of the int 16.
    roll = (roll & 0xFF00) >> 8 | (roll & 0x00FF) << 8; // Reverse the order of bytes in the int 16.
    
    00100100 -- '$' - Byte 1 of the header.
    01001101 -- 'M' - Byte 2 of the header.
    00111110 -- '>' - Byte 3 of the header.
    00000110 -- '6' - The size byte.
    01101100 -- '108' - The type number corresponding to "MSP_ATTITUDE".
    11100010 -- The first sent byte of the roll INT16.
    11111111 -- The second sent byte of the roll INT16.
    00010010 -- The first sent byte of the pitch INT16.
    00000000 -- The second sent byte of the pitch INT16.
    11000010 -- The first sent byte of the yaw INT16.
    00000000 -- The second sent byte of the yaw INT16.
    10100111 -- The checksum byte.
    
    Roll = -3.0
    Pitch = 1.8
    Yaw = 194
    
    滚动将变成:
    11111111100010=-30
    。 音高将变为:
    00000000 00010010=18
    。 偏航将变为:
    110000100000000=194

    如文件所述,横摇和纵摇的单位为1/10度。因此,最终值如下所示:

    int16_t roll;
    byte c;                       // The current byte we read in.
    c = mspSerial.read();         // The first sent byte of the number.
    roll = c;                     // Put the first sent byte into the second byte of the int 16.
    c = mspSerial.read();         // The second sent byte of the number.
    roll <<= 8;                   // Move the first sent byte into the first byte of the int16.
    roll += c;                    // Put the second sent byte into the second byte of the int 16.
    roll = (roll & 0xFF00) >> 8 | (roll & 0x00FF) << 8; // Reverse the order of bytes in the int 16.
    
    00100100 -- '$' - Byte 1 of the header.
    01001101 -- 'M' - Byte 2 of the header.
    00111110 -- '>' - Byte 3 of the header.
    00000110 -- '6' - The size byte.
    01101100 -- '108' - The type number corresponding to "MSP_ATTITUDE".
    11100010 -- The first sent byte of the roll INT16.
    11111111 -- The second sent byte of the roll INT16.
    00010010 -- The first sent byte of the pitch INT16.
    00000000 -- The second sent byte of the pitch INT16.
    11000010 -- The first sent byte of the yaw INT16.
    00000000 -- The second sent byte of the yaw INT16.
    10100111 -- The checksum byte.
    
    Roll = -3.0
    Pitch = 1.8
    Yaw = 194
    

    建立清洁飞行 要获得这些值,飞行控制器必须正确配置为使用MSP。我假设您已经运行了CleanFlight配置程序

    在代码运行时,您可能希望将主串行连接用于其他用途,因此我们将使用软串行2端口(电路板左侧的引脚7和8)

    转到“配置”选项卡并向下滚动至“其他功能”。确保“SOFTSERIAL”和“TELEMETRY”已打开。保存并重新启动

    转到“端口”选项卡,将“SOFTSERIAL2”的“数据”列设置为活动状态并设置为9600(如果需要,也可以使用19200,更高的值在Arduino端可能不起作用)。保存并重新启动

    飞行控制器现在已正确配置


    建立Arduino 要设置Arduino,只需将文件上载到Arduino即可。将Arduino上的针脚12连接到Naze板左侧的针脚7,将Arduino上的针脚11连接到Naze板左侧的针脚8

    打开与Arduino的串行连接,现在应输出横摇、俯仰和偏航


    用于其他MSP通信
    虽然代码是使用MSP_姿态的示例,但相同的理论适用于任何MSP通信。主要区别在于命令消息要求正确设置数据(我没有考虑到这一目的测试代码),readData函数需要根据接收到的数据修改switch语句。

    以下内容仅基于获取陀螺仪姿态信息,虽然它包含了一些关于一般使用MSP的信息。可以找到引用的示例代码。其中最重要的部分是下面的数据部分,我在其他任何地方都找不到(一位朋友发现了这个秘密并让我知道了)


    多Wii串行协议 首先,让我们看看MSP是如何工作的。我发现(替代链接)在理解这一点上非常有用,但我将在这里进行总结。 可以发送三种类型的消息

  • 命令——发送给飞行控制器的信息,其中包含一些要发送的信息
  • 请求——发送给飞行控制员要求返回某些信息的消息
  • 响应——飞行控制器发送的消息,其中包含响应请求的信息
  • MSP消息具有特定的结构。它们有一个标题、大小、类型、数据和校验和

    我发现这个图表非常有用:

    标题 标头为三个字节,包含消息开始字符“$M”和一个显示消息前进方向的字符。“”表示来自飞行控制器(响应)

    大小 第四个字节是数据段的长度(以字节为单位)。例如,如果数据部分有三个INT 16变量,那么大小字节将是6

    类型 字节类型指定消息中发送的信息。您可以找到类型列表。这方面的一个例子是MSP_态度,其类型编号为108

    资料 数据是发送所有信息的地方。请求消息中没有数据。命令和响应确实如此,因为它们包含信息。可以再次找到返回的数据类型<