的 1.使用SETINPUTMODE(0x05)命令设置输入模式,参数为LOWSPEED__9V(0x0B)和RAWMODE(0x00)。 强>
所以 的 SETINPUTMODE 强> 命令将发送以下字节 的 {0x00,0x05,0x03,0x0B,0x00} 强> 哪里:
0x00: DIRECT REPLY or 0x80 DIRECT NO REPLY 0x05: SETINPUTMODE 0x03: Port where the sensor is connected. The documentation recommends port 4 for ultrasonic sensor. 0x0B: LOWSPEED__9V 0x00: RAWMODE
的 2.使用LSWRITE(0x0F)命令告诉NXT您希望答案的字节数。 强>
所以 的 LSWRITE 强> 命令将发送以下字节 的 {0x00,0x0F,0x03,0x02,0x01,0x02,0x42} 强> 哪里:
0x00: DIRECT REPLY or 0x80 DIRECT NO REPLY 0x0F: LSWRITE 0x03: Port where the sensor is connected. The documentation recommends port 4 for ultrasonic sensor. 0x02: Tx -> Transmitted Data Length 0x01: Rx -> Receive Data Length 0x02: Byte 5 is the i2c address of the device, for the NXT it is usually 0x02. 0x42: This is the register you are attempting to write to. It is device dependent. For the NXT Ultrasonic Sensor 0x41 is used for commands, and the read registers are in 0x42 to 0x49 so you could use any from 0x42 to 0x49.
的 3.告诉NXT你准备好用LSGETSTATUS(0x0E)命令接收答案。 强>
所以 的 LSGETSTATUS 强> 命令将发送以下字节 的 {0x00,0x0e,0x03} 强> 哪里:
0x00: DIRECT REPLY 0x0E: LSGETSTATUS 0x03: Port where the sensor is connected. The documentation recommends port 4 for ultrasonic sensor.
的 4.等待成功值= 0x00的回复。 强>
所以一旦达到这一点,你必须继续发送 的 LSGETSTATUS 强> 命令,直到你获得成功(0)你一直在获得,因为这意味着答案已准备好被阅读。如果 的 LSGETSTATUS 强> 回复返回一个不同于0的值,然后答案没有准备好。给它一些时间,也许300毫秒。
正确的回复数据将如下所示 的 {0x02,0x0e,0x00,0x01} 强> 哪里:
0x02: REPLY 0x0E: LSGETSTATUS 0x00: SUCCESS (different from 0x00 means a specific error. Take a look at the documentation if you need to) 0x01: Bytes ready to be read.
的 5.使用LSREAD(0x10)命令读取值。 强>
所以 的 LSREAD 强> 命令将发送以下字节 的 {0x00,0x10,0x03} 强> 哪里:
0x00: DIRECT REPLY 0x10: LSREAD 0x03: Port where the sensor is connected. The documentation recommends port 4 for ultrasonic sensor.
的 6.一旦NXT砖响应,您将获得以厘米为单位的距离。 强>
正确的回复数据将如下所示 的 {0x02,0x10,0x00,0x01,byte1,byte2,byte3, ,byte19} 强> 哪里:
0x02: REPLY 0x10: LSREAD 0x00: SUCCESS (different from 0x00 means a specific error. Take a look at the documentation if you need to) 0x01: Bytes that contains the answer. You asked for 1 byte. byte1...byte19: Zero-padded -> HERE IS THE ANSWER
读取对应于数组第5个字节的答案的第一个字节。它的值将是传感器读取的距离。范围从1到255厘米。
的 7.循环重复步骤2到6,不断向传感器询问距离。 强>