找回密码
 马上注册

QQ登录

只需一步,快速开始

查看: 9881|回复: 15

有没有人知道怎么蓝牙读取I2C传感器?

[复制链接]
发表于 2013-8-3 17:44:43 | 显示全部楼层 |阅读模式
通过蓝牙使用Mindstorms Nxt Direct Commands可以直接读取传感器数据而不需要在NXT上跑程序,但是里面的读传感器的指令里只支持一些简单的传感器,像超声波传感器这些I2C协议的传感器好像要用LSWrite和LSRead指令,有没有谁知道这两个命令怎么用?
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
发表于 2013-8-4 09:01:27 | 显示全部楼层
可能要先通过nxt读出来,再通过蓝牙发送吧,我总感觉一步到位好像不行
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

 楼主| 发表于 2013-8-4 14:14:30 | 显示全部楼层
electrophile 发表于 2013-8-4 09:01
可能要先通过nxt读出来,再通过蓝牙发送吧,我总感觉一步到位好像不行

可以的。我看到好几个编程软件里有现成的模块,但就是不知道他具体的源代码是什么
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2013-8-4 14:46:38 | 显示全部楼层
还是仔细看看官方提供的那几本关于蓝牙的PDF吧
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

 楼主| 发表于 2013-8-4 16:41:32 | 显示全部楼层
ntwuhui 发表于 2013-8-4 14:46
还是仔细看看官方提供的那几本关于蓝牙的PDF吧

看了。但他完全没有例子。而且命令说明里只说了发送的数据包第几位到第几位是放data,没讲data格式应该怎样。截张图给你看
QQ截图20130804163946.jpg
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2013-8-4 16:52:54 | 显示全部楼层
本帖最后由  electrophile 于 2013-8-4 16:54 编辑
junf97 发表于 2013-8-4 14:14
可以的。我看到好几个编程软件里有现成的模块,但就是不知道他具体的源代码是什么

高手,加油,认真研究,这块我帮不上你的忙还有就是看看外国关于LEGO IIC和蓝牙的PDF书籍,虽然可能是英文,但是里面介绍到的可能性最大
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

 楼主| 发表于 2013-8-4 18:40:51 | 显示全部楼层
electrophile 发表于 2013-8-4 16:52
高手,加油,认真研究,这块我帮不上你的忙还有就是看看外国关于LEGO IIC和蓝牙的PDF书籍,虽然可能是英 ...

看了。研究了好几遍了。之前论坛里有个蓝牙控制NXT的程序就是我写的。现在就差这个和远程写程序一直研究不出来
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2013-8-5 00:00:34 | 显示全部楼层
google svn 上找到一段代码,貌似你要的,也是我需要的,贴一段看下
  1. public bool LsWrite( Sensor sensor, byte[] data, int expectedBytes )
  2.         {
  3.             if ( ( data.Length == 0 ) || ( data.Length > 16 ) )
  4.             {
  5.                 throw new ArgumentException( "Data length must be in the [1..16] range.", "data" );
  6.             }

  7.             byte[] command = new byte[5 + data.Length];
  8.             byte[] reply = new byte[3];

  9.             // prepare message
  10.             command[0] = (byte) NXTCommandType.DirectCommand;
  11.             command[1] = (byte) NXTDirectCommand.LsWrite;
  12.             command[2] = (byte) sensor;

  13.             command[3] = (byte) data.Length;
  14.             command[4] = (byte) expectedBytes;

  15.             Array.Copy( data, 0, command, 5, data.Length );

  16.             return ( SendCommand( command, reply ) );
  17.         }

  18.         /// <summary>
  19.         /// Read data from Low Speed bus.
  20.         /// </summary>
  21.         ///
  22.         /// <param name="sensor">Sensor to read data from.</param>
  23.         /// <param name="readValues">Array to read data to.</param>
  24.         /// <param name="bytesRead">Bytes actually read from I2C device.</param>
  25.         ///
  26.         /// <returns>Returns <b>true</b> if command was executed successfully or <b>false</b> otherwise.</returns>
  27.         ///
  28.         public bool LsRead( Sensor sensor, byte[] readValues, out int bytesRead )
  29.         {
  30.             byte[] command = new byte[3];
  31.             byte[] reply = new byte[20];

  32.             // prepare message
  33.             command[0] = (byte) NXTCommandType.DirectCommand;
  34.             command[1] = (byte) NXTDirectCommand.LsRead;
  35.             command[2] = (byte) sensor;

  36.             if ( SendCommand( command, reply ) )
  37.             {
  38.                 bytesRead = reply[3];
  39.                 Array.Copy( reply, 4, readValues, 0, Math.Min( readValues.Length, bytesRead ) );
  40.                 return true;
  41.             }

  42.             bytesRead = -1;
  43.             return false;
  44.         }
复制代码
读取超声波如下:
  1. public bool GetUltrasonicSensorsValue( Sensor sensor, out int value )
  2.         {
  3.             value = -1;

  4.             // request distance value
  5.             if ( !LsWrite( sensor, new byte[] { 0x02, 0x42 }, 1 ) )
  6.                 return false;

  7.             int readyBytes = -1;

  8.             for ( int i = 0; i < 10; i++ )
  9.             {
  10.                 if ( !LsGetStatus( sensor, out readyBytes ) )
  11.                     return false;

  12.                 if ( readyBytes >= 1 )
  13.                 {
  14.                     // read from I2C device
  15.                     byte[] readValues = new byte[1];
  16.                     int bytesRead;

  17.                     if ( !LsRead( sensor, readValues, out bytesRead ) )
  18.                         return false;

  19.                     value = readValues[0];

  20.                     return true;
  21.                 }
  22.             }
  23.             return false;
  24.         }
复制代码
明天白天还得继续在大连的行程,如果你急的话可以直接到这里下:https://code.google.com/p/aforge/source/browse/trunk/Sources/Robotics.Lego/?r=1418,你如果先下到的话就传我一份吧
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2013-8-5 00:10:36 | 显示全部楼层
本帖最后由 ntwuhui 于 2013-8-5 00:18 编辑

另外还看到这个:http://hsrc.static.net/Research/NXT%20I2C%20Communication/
提供参考,我也先收着

原文如下: (e文不好的我要遭点罪了)

LSWRITE is used for both reading registers and writing commands from the NXT.

LSGETSTATUS is used basically to wait until the i2c bus is free.

LSREAD will read any bytes that the NXT has received from the i2c device.

LSWRITE format:

。。。
Byte 0:
0x00 or 0x80 (0x00 = response required, 0x80 = No response)
Personally I’d leave it at 0x00 until you are finished debugging your masterpiece.

Byte 1:
0x0f – LSWRITE command

Byte 2:
Port     0x00 = port 1
            0x01 = port 2
            0x02 = port 3
            0x03 = port 4

Byte 3:
TxLength – This is the length of the message you want the NXT to write to the i2c bus. (It is a count of all the bytes starting with Byte 5).

Byte 4:
RxLength – How many bytes do you wish to receive for a command this is usually zero.

Byte 5 – N
These bytes are the ONLY ones that will be sent to the i2c device.
Byte 5 is the i2c address of the device, for the NXT it is usually 0x02

Byte 6
This is the register you are attempting to write to. It is device dependent, so get your data-sheets out.
For the NXT Distance Sensor 0x41 is used for commands, and the read registers are in 0x42 to 0x49.

Putting it all together to read values with the Distance Sensor
1)         Set the port into LOWSPEED_9V mode and RAWDATA mode
Command: {0x00, 0x05, port, 0x0B, 0x00}
2)         The sensor automatically goes into constant measurement mode.
3)         To read Measurement 0:
Send: {0x00, 0x0F, port, 0x02, 0x01, 0x02, 0x42 }
This tells the sensor you want 1 byte from register 0x42 which according to the data sheet is ‘Measurement Byte 0’
4)         Now you need to wait for the NXT to send the command to the i2c device and receive the byte back.
You can either pause for ~500msec (slow but wasy way) or poll the NXT with the LSGETSTATUS command until the status goes to 0x00 (no error) and there is at least 1 byte avalible.
Send: {0x00, 0x0e, port}
You will receive:
{0x02, 0x0e, status, bytesReady}
When the device is ready it will look like this:
{0x02, 0x0e, 0x00, 0x01}

5)         You can now use LSREAD to get the 1 byte which is ready and waiting on the NXT
Your command should look like:
{0x00, 0x0f, port}
The NXT will return a 20-byte block containing up to 16 bytes from the i2c device.
Format:
{0x02, 0x10, statusByte, bytesRead, byte1, byte2, byte3, ……, byte19}
statusByte should be 0x00 to indicate no error, and only byte 1 should contain any data, the rest will be zero-padded.

This works for the NXT Distance sensor, and both my Mindsensors Compass and my Mindsensors Acceleration sensor, however When reading multiple bytes from the registers, I have had no luck recieveing more than 2 bytes at a time from the Mindsensors devices.
More specifically, only access 1 measurement each time you query the device.
Eg: Do not ask for both the X-Tilt and the Y-Tilt bytes, it will return garbages results.
You can however, and SHOULD read both the LSB and MSB of the acceleration registers, if you read 1 byte at a time, it’s possible the value will change inbetween reads and your result will get corrupted.

Sending Commands

To send commands to i2c devices you use LSWRITE
Each device has a register which you write commands to. On the NXT it is usually 0x41, for all the devices I have seen commands are 1 extra byte following the register.

Reset command for the NXT distance sensor:

{ 0x00, 0x0f, port, 0x03, 0x00, 0x02, 0x41, 0x04 }
This will perform a ‘warm reset’ on the distance sensor.

估计这段e文对理解上面提到的完整代码会有帮助!!
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

 楼主| 发表于 2013-8-6 12:24:12 | 显示全部楼层
ntwuhui 发表于 2013-8-5 00:10
另外还看到这个:http://hsrc.static.net/Research/NXT%20I2C%20Communication/
提供参考,我也先收着

真的非常感谢了。我当时找了很久都没找到这种资料
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

 楼主| 发表于 2013-8-6 14:12:09 | 显示全部楼层
ntwuhui 发表于 2013-8-5 00:10
另外还看到这个:http://hsrc.static.net/Research/NXT%20I2C%20Communication/
提供参考,我也先收着

补充一下。个人建议仔细研究你发的第二份英语资料。那个讲得比前一个源代码清晰
然后那份资料里是以Distance Sensor举例,里面有提到一个register需要查传感器data-sheet。我找了一下。那个可以在hitechnic官网传感器界面(http://www.hitechnic.com/sensors)选择传感器点View Detail后找到一个Sensor Register Layout的表格。这个就是对应传感器的I2C数据格式
还有除了传感器。如果你参加FTC或FRC的话。我这里还找到一些hitechnic的Motor Controller(Tetrix DC Controller)的资料。可以蓝牙控制Tetrix电机及encoder
http://www.legoeducation.us/etc/supportFiles/TETRIX/739413/HiTechnicMotorControllerBriefv1.3.pdf
http://stormingrobots.com/prod/tutorial/I2C%20RobotC%20Tutorial.pdf
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2013-8-6 20:26:42 | 显示全部楼层
客气了!第二份英语资料我的理解就是个思路或者通过nxt取得传感器信息的过程,我已经在程序的备注中看到相关说明,说的也正是这个地址,准备回去后有时间仔细研究研究,你所提到的Hitchnic的相关资料也是我想要的。有时间多交流!
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

 楼主| 发表于 2013-8-7 14:35:57 | 显示全部楼层
本帖最后由 junf97 于 2013-8-7 15:23 编辑
ntwuhui 发表于 2013-8-6 20:26
客气了!第二份英语资料我的理解就是个思路或者通过nxt取得传感器信息的过程,我已经在程序的备注中看到相 ...

你的第二份资料和我发的链接里给的都是是一些完整地数据的发送格式。具体用来发送蓝牙数据的程序其实我自己这里以前已经有写好的。Android和PC版的我都有。你想要的话留个邮箱我也可以发给你。只要按照资料里写得格式把数据输出去应该就可以读到数据了
PS 刚刚在看你的那份英语资料。里面最后一部分5)中说发送{0x00, 0x0f, port}写错了。实际应为{0x00, 0x10, port} ,0F是LSWrite,这里应该用LSRead,故为0x10

PPS 之前为了发给别人把这里的东西都整理了一下然后加了点东西,我放在附件里了。这份文档应该看起来会更加清楚一点。

蓝牙发送及接受数据格式(读取NXT主机I2C设备).pdf

53.85 KB, 下载次数: 17

如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2013-8-9 17:42:42 | 显示全部楼层
本帖最后由 ntwuhui 于 2013-8-9 17:50 编辑

一直没能有时间上坛子,刚看到!谢谢你的更正及资料。邮箱号码: ntwuhui@163.com,麻烦发个pc版的学习学习!
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

 楼主| 发表于 2013-8-9 18:30:12 | 显示全部楼层
ntwuhui 发表于 2013-8-9 17:42
一直没能有时间上坛子,刚看到!谢谢你的更正及资料。邮箱号码: ,麻烦发个pc版的学习学习!

已发。我是在Visual Studio2010上用C#写的。主要蓝牙通讯在Bluetooth_Process类下。因为这个是以前的程序所以I2C的没弄上去。我现在是在Android上在弄I2C。已经试过可行。按照我发的那份word里写就行了
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|中文乐高 ( 桂ICP备13001575号-7 )

GMT+8, 2024-5-24 06:59 , Processed in 0.092874 second(s), 26 queries .

Powered by Discuz! X3.5

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表