C++ を使用して LEGO Mindstorms EV3 ブロックと通信しようとしています。私はev3sources repoのクローンを作成しました。これにより、Bluetooth 経由でそれを行うことができます。たとえば、ポート A に接続されたモーターを開始するには、次のようにします。
#include <unistd.h>
#include <fcntl.h>
#include "ev3sources/lms2012/c_com/source/c_com.h"
int main()
{
// start motor on port A at speed 20
unsigned const char start_motor[] {12, 0, 0, 0,
DIRECT_COMMAND_NO_REPLY,
0, 0,
opOUTPUT_POWER, LC0(0), LC0(0x01), LC0(20),
opOUTPUT_START, LC0(0), LC0(0x01)};
// send above command to EV3 via Bluetooth
int bt = open("/dev/tty.EV3-SerialPort", O_WRONLY);
write(bt, start_motor, 14);
close(bt);
}
しかし、EV3 ブリックからデータを取り戻すにはどうすればよいでしょうか? たとえば、ポート 1 に接続されているセンサーによってキャプチャされたデータを読み取りたいとします。レポの例に基づいて、次のようなものが必要であることがわかります。
#include <unistd.h>
#include <fcntl.h>
#include "ev3sources/lms2012/c_com/source/c_com.h"
int main()
{
// read sensor on port 1
unsigned const char read_sensor[] {11, 0, 0, 0,
DIRECT_COMMAND_REPLY,
0, 0,
opINPUT_READ, LC0(0), LC0(0), LC0(0), LC0(0), GV0(0)};
// send above command to EV3 via Bluetooth
int bt = open("/dev/tty.EV3-SerialPort", O_WRONLY);
write(bt, read_sensor, 13);
close(bt);
}
しかし、何かが欠けています。上記のスニペットはエラーを返しませんが、センサー データがどこにあるのかわかりません。では、どのように取得すればよいでしょうか。Bluetooth経由でも送信されると思いますが、どうすればそれをキャプチャできますか?
(OS X 10.9.3、Xcode 5.1.1、EV3 [31313])