在上一篇文章中,我们已经实现了iOS通过BTstack给EV3发送Message!
但仅仅发送Message显然不是我们的目标! 我们要控制EV3的动作! 这需要依靠EV3协议中的Direct Command! EV3 brick内部已经内置了代码,iOS这边只要发送特定的命令也就是Direct Command,那么EV3这边就可以在不启动任何程序的情况下实现对各种传感器设备的控制。 LEGO 官方的App Commander就是依靠Direct Command控制EV3的。只是它的不足是很明显的。就是: 这个Commander App只能输出动力,控制点击,至于逻辑判断,就都做不到! 而我们的目标是通过手机控制EV3,并且把手机端作为机器人的大脑! 这种要求显然远远高于Commander!我们必须能接收传感器的数据,然后经过程序判断,输出动力! 这篇文章将谈如何通过使用Direct Command来控制EV3的电机运动! 首先还是摘录一下Direct Command的协议说明。完全从c_com.h文件中复制。 Beside running user programs the VM is able to execute direct commands from the Communication Module. In fact direct commands are small programs that consists of regular byte codes and they are executed in parallel with a running user program.\n Special care MUST be taken when writing direct commands because the decision until now is NOT to restrict the use of "dangerous" codes and constructions (loops in a direct command are allowed). If a new direct command from the same source is going to be executed an actual running direct command is terminated. Because of a small header objects are limited to one VMTHREAD only - SUBCALLs and BLOCKs is of course not possible.\n This header contains information about number of global variables (for response), number of local variables and command size. Direct commands that has data response can place the data in the global variable space. The global variable space is equal to the communication response buffer. The composition of the direct command defines at which offset the result is placed (global variable 0 is placed at offset 0 in the buffer). Offset in the response buffer (global variables) must be aligned (float/32bits first and 8 bits last). Direct Command Bytes: ,------,------,------,------,------,------,------,------, |Byte 0|Byte 1|Byte 2|Byte 3|Byte 4|Byte 5| |Byte n| '------'------'------'------'------'------'------'------' Byte 0 – 1: Command size, Little Endian\n Byte 2 – 3: Message counter, Little Endian\n Byte 4: Command type. see following defines */ #define DIRECT_COMMAND_REPLY 0x00 // Direct command, reply required #define DIRECT_COMMAND_NO_REPLY 0x80 // Direct command, r |
Hacking EV3系列之六:iOS使用Direct Command 控制EV3
最新推荐文章于 2021-05-31 16:55:38 发布