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