- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 C++ 与 LEGO Mindstorms EV3 积木进行通信。我克隆了 ev3sources repo ,这让我可以通过蓝牙来做到这一点——例如,要启动连接到端口 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 程序 block 取回数据?例如,假设我想读取连接到端口 1 的任何传感器捕获的数据。基于 the repo examples我知道我需要看起来有点像这样的东西:
#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);
}
但缺少一些东西 - 上面的代码片段没有返回任何错误,但我不知道传感器数据在哪里。那么,我该如何取回它呢?我想它也会通过蓝牙发回,但我该如何捕获它?
(OS X 10.9.3、Xcode 5.1.1、EV3 [31313])
最佳答案
写完需要从bt
读取。与您要发送的数据一样,前两个字节是大小,因此请先读取 2 个字节并使用它来计算您还需要读取多少字节。
#define MAX_READ_SIZE 255
unsigned char read_data[MAX_READ_SIZE];
int read_data_size;
// existing code
// ...
write(bt, read_sensor, 13);
read(bt, read_data, 2);
read_size = read_data[0] + read_data[1] << 8;
read(bt, read_data, read_size);
close(bt);
// decode the data
// ...
编辑:根据评论完成代码
#include <unistd.h>
#include <fcntl.h>
#include "ev3sources/lms2012/c_com/source/c_com.h"
#define MAX_READ_SIZE 255
int main()
{
unsigned char read_data[MAX_READ_SIZE];
int read_data_size;
// read sensor on port 1
unsigned const char read_sensor[] {11, 0, 0, 0,
DIRECT_COMMAND_REPLY,
1, 0, // 1 global variable
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_RDWR);
write(bt, read_sensor, 13);
read(bt, read_data, 2);
read_size = read_data[0] + read_data[1] << 8;
read(bt, read_data, read_size);
close(bt);
// TODO: check that command was successful and so something with data
}
关于c++ - 使用 C++ 从乐高 EV3 传感器获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24253509/
我想通过自己编写的C API来控制一些物理引擎,开关,灯光。 我已经对如何实现这一目标有了一个非常全面的概述: 使用内核抽象,在其上编写驱动程序并将其用作控制。驱动程序本身必须管理传入字节并解释它们(
过去做过一些 Android 开发,但已经有一段时间了 - 想构建一个使用方向和接近传感器的应用程序。所以... mSensorManager = (SensorManager) getSystemS
Android SDK 实际上提供了一个很好的接口(interface)来访问传感器。但是例如线性加速度传感器可以评估为 documentation从重力和加速度描述 - 所以这个传感器没有真正的物理
我是 android 开发的新手,我需要查看 android 3.1 设备上的传感器列表。 我有这个代码: package milos.exemplesensorlist; import java.u
我正在尝试为网络传感器实现虫洞攻击检测程序。我在互联网上四处寻找这方面的源代码,但一无所获。有谁知道在哪里可以找到虫洞传感器攻击(用于模拟目的)或虫洞检测的源代码?如果是这样,在哪里?它可以是任何编程
有没有办法使用内置传感器从 Android 手机获取二氧化碳。目前我通过环境传感器获得的是当前速度、气压、湿度和噪音幅度。 如果这是不可能的,有没有一种方法可以计算二氧化碳(该值可以是估计值)? 最佳
我正在尝试访问我的 Samsung Galaxy S3 上的传感器(特别是接近传感器和 RGBW 传感器),并且已获得可运行的代码来访问接近传感器。但是,我只得到值 1 或 8(近/远)。我知道有时硬
我想知道是否可以通过javascript获取诺基亚5800的传感器值。我知道在 HTML5 中有一个事件 DeviceOrientationEvent 可以提供一些值。但由于诺基亚 5800 没有 H
我想知道如何正确使用“旋转 vector 传感器”的输出。目前我想到了以下内容,并想根据 result[] 计算偏航和俯仰,以便了解设备指向的位置(处于横向模式)。但我对结果有疑问。偏航计算非常精确,
我正在编写一个方法,如果存在并启用了 GPS 传感器,该方法将返回 true,但如果不存在或已关闭,则返回 false。事实证明这很难,因为... hasSystemFeature("FEATURE_
我正在尝试从 Samsung Galaxy Tab GT-P1000 读取多个传感器,相对于我使用的应用程序,它们似乎非常占用 CPU。 作为测试,我创建了一个简短的程序,它为加速度计传感器实现了 S
我正在像这样从重新启动的接收步骤创建一个演示。 public class MainActivity extends AppCompatActivity implements SensorEventLi
我正在开发用于汽车加速跟踪的应用程序。我使用标准加速度计,事先在特定位置进行校准。 然后,假设手机的方向没有改变,我记录了指定时间的加速度计数据并计算了移动参数,其中之一是测试结束时汽车的速度。 在笔
下面是API中定义的几个代表sensor的常量。 Int TYPE_ACCELEROMETER A constant desc
我使用的 Arduino 具有多个 (3) 个传感器,使用正常模式(非寄生模式)连接到数字引脚 2。 其中两个传感器是使用库“OneWire”(Library Page)和“DallasTempera
我想让 android 传感器与 opengl 一起工作,以将 opengl 的相机旋转到手机指向的任何地方。 详细说明:如果玩家正在看东方,我希望 opengl 的相机在游戏中也指向东方;如果玩家指
我正在使用 Google Cardboard(它的 HeadTracker 类)来检测 AR 应用程序中有关设备旋转的某些事情。效果很好。 但是,在某些设备上,它不起作用(没有任何反应)。我认为这是因
是否有适用于 Apple Watch Kit 传感器(例如加速度计、心率监测器、触觉传感器)的 API? 我如何访问这些传感器? 最佳答案 WatchOS 2.0 的 Watchkit 中现已提供传感
我正在学习如何使用 Pyfst 创建换能器,并且我正在尝试可视化我创建的换能器。最终目标是能够将传感器写入点文件并在 Graphviz 中查看它们。 我拿了一个示例代码来看看如何可视化下面的接受器。
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我是一名优秀的程序员,十分优秀!