Hacking EV3系列之六:iOS使用Direct Command 控制EV3

本文深入探讨如何使用iOS设备结合btstack库,通过Direct Command方式实现对LEGO EV3智能机器人进行远程蓝牙控制,为iOS开发者提供了一个独特的EV3编程体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


 在上一篇文章中,我们已经实现了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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值