Meanings of the arrays.

Forums:

Dear PiBorg community,

I use the code for kernel level I2C communication with the UltraSoon sensor but wonder what some things are. I checked on the GUI what the millimetres were and some values from the readBuffer.

milimeters (GUI)
___________ (138-139) (74-75) (50-51)
readBuffer[1] (203) (107) (251)
readBuffer[1] << 8 (51968) (27392) (64256)
ReadBuffer[2] (217) (210) (214)
time_us (52185) (27602) (64470)

1) I wonder what the operation readBuffer[1] << 8 actually does?
2) Why in the time_us calculation we use readBuffer << 8 value but normal readBuffer[2]
3) I checked readBuffer[3] and readBuffer[4], what does these values mean?
4) What is the meaning of readBuffer[1] its value and readBuffer[2] its value.

I hope piBorg can answer these to make clear how the ultraSoon sensor works and the boards handle it.

piborg's picture

I am not sure which piece of example code you are using.

Can you post a link to the code you are talking about?

Link address: https://gist.github.com/Xhendos/2b63268090114d2930fbe579a13463fc
Hope you could explain some things that are unclair for me at the moment.
I am 99% sure I have the HC-SR04.
I tried to read http://www.micropik.com/PDF/HCSR04.pdf but its still not clear for me.

piborg's picture

No problem, this can be a bit confusing at first.

The read back array is 4 values long, readBuffer[0] to readBuffer[3].
The values are:

  • readBuffer[0] - This should be the same as the command sent, in this case UB_COMMAND_GET_FILTER_USM1.
    You do not really need to look at this value, it is mostly used by us when we test the boards.
  • readBuffer[1] - This is the upper byte of the measured time in microseconds.
  • readBuffer[2] - This is the lower byte of the measured time in microseconds.
  • readBuffer[3] - This value is not used by this particular command, it can be ignored.

This means the measured time from the ultrasonic sensor is in both readBuffer[1] and readBuffer[2].

To get the full measured time we need to combine the two bytes into a single value:

  • (unsigned int) readBuffer[2] takes the lower byte and puts it into a space which can hold a larger number.
  • (unsigned int) readBuffer[1] does the same for the upper byte.
  • We use << 8 to move the upper byte to the "next byte upwards".

This means that ((unsigned int) readBuffer[1] << 8) + (unsigned int) readBuffer[2] gives us the full value from both bytes as a single number.

This is not quite enough to get the measured distance yet.
To do that we have to take the measured time from the ultrasonic sensor and convert it.
This can be done using the UB_USM_US_TO_MM constant.
What we need to do is multiply the time_us value like this:
double distance_mm = (double)time_us * UB_USM_US_TO_MM;

Hopefully that makes using the UltraBorg a little bit clearer for you.

Dear piBorg,

I changed my code a little bit and added the muliplier aswell.
https://gist.github.com/Xhendos/dc217e3cd8610aacc76333e17df52c6b

But for some reason I still dont get what the GUI gives me. (look the attached images)
Left under you see the GUI about 58 milimeters and that is correct.
The distance I get from running the code is different.

Hope with a little help I could make it.

Greetings,
Zhendos

Images: 
piborg's picture

This should be very easy to fix.
It looks like you are not actually reading the data from the I2C bus.

I think the mistake here is that you are only reading 1 byte from the UltraBorg, not 4.
This is confusing because the array has "garbage" data in it already.

What you need to do is change the length to 4 before the read command:

length = 4;
if(read(file, readBuffer, length) == length)
{
Subscribe to Comments for &quot;Meanings of the arrays.&quot;