I2c does not work propertly on Pistorms raspberry NXTCAM
 Raspberry Pi   Started by Angel Perez   2017-09-04 15:52:31 -04:00   Comments: 9    Viewed: 217

  1. Angel Perez
    Angel Perez Member
    Hi everyone, I go directly to the problem.

    I am creating a library to use NXTCAM or Pixy cam (with mindsensors adapter) and making use of I2C to comunicate with this cameras.

    I am using also bcm2835 library for communications.

    To help you to understand, I just want to write in the correct register of the camera to start tracking, and then reading number of objects detected(for example)

    To do it, I first use set slave addres (it works, so the problem is not here), in order to stablish the camera as slave.

    Then i just use bcm2835_i2c_write function to start tracking. And finally I try to read register (read_register_rs function of bc2835) of objects detected (number 66 in decimal). The problem comes in this last step. I always get the value of 66, exactly the number of the register I want to read.

    To clear out how read_register_rs works, it first write the register wanted to be read, and then it returns back the content of that register. I also have to say that when I read device id, or software version, it works propertly.

    To sum up, I think that the problem comes directly when I want to read after writing the start track command and i2c bus gets stuck.

    I am glad you all listen to me.

    Hope your soon answer!!! :)

    Best regards.
    Angel Perez, 2017-09-04 15:52:31 -04:00
  2. seth10
    seth10 Member Staff Member

    Thank you for clearly stating the problem and what you have tried so far. I'm glad reading the software version works, that's a good sign. Which version of the NXTCam are you using to test this?

    To be clear, commands are send to the command register. For example, to select object tracking mode, write 0x41 for the register, then 0x42 for the data.
    To retrieve the number of objects detected, just read from register 0x42. Be sure to read only a single byte value.

    Have you tried adding a brief pause between selecting object tracking mode and reading the number of objects detected? Are you checking the number of objects once or repeatedly? If anything I would expect you to read 0, not 0x42 (66). Have you send the command to enable tracking?

    seth10, 2017-09-05 09:26:52 -04:00
  3. Angel Perez
    Angel Perez Member
    Hi seth10!!

    First of all thanks for your answer.

    Talking about the camera, I ve been using it in the way you are explaining and it works perfectly, (for example on ev3-dev it works fine with this steps). And yes, I only read only one byte value from register 0x42.

    Also, as you say, I do the brief pause. To sum up I do all steps you are saying =S and I still get this value.
    I tryed to read number of objects repeatedly ( always prints 66) and just once ( also print 66).

    Don't you think that it can be something related with baudrate. Maybe the camera does not have enough time to answer the i2c query, and i2c bus only gets back this value. It is just an idea...


    Angel Perez, 2017-09-06 14:01:27 -04:00
  4. seth10
    seth10 Member Staff Member
    Oh! Hm, well thank you for making sure you're doing all those things. What I think is significant is that it does work with ev3-dev but not with bcm2835. To be clear, in ev3-dev you are able to read the number of objects correctly, and don't get 66?

    What is the bcm2835 library (Broadcom?) and why did you decide to use it?
    seth10, 2017-09-06 15:30:18 -04:00
  5. Angel Perez
    Angel Perez Member
    Yes, in ev3-dev it works perfectly.

    Now I am working with this library because I am using a library developed by a friend who uses bcm2835 to work with I2C. 
    We are using as Operating System, MaRTE OS, which was developed by our university, (CANTABRIA UNIVERSITY) in Spain.

    My dutty here, apart from creating a new library to use this cameras in ev3-dev, was also to add this functionality to the library my friend made. Using raspberry and pistorms as environment (with MaRTE OS running).

    I add MaRTE OS link here just in case is helpfully.

    https://marte.unican.es/
    Angel Perez, 2017-09-07 07:49:20 -04:00
  6. seth10
    seth10 Member Staff Member
    Wow, cool project! I find if fascinating you get to work with a hard real-time OS, personally I have not had experience there.

    If everything works fine in ev3-dev, I think it's safe to say the NXTCam is functioning properly. In this case I suggest you look into the specifics of bcm2835, as the camera itself is fine.
    seth10, 2017-09-07 09:41:31 -04:00
  7. Angel Perez
    Angel Perez Member
    Do you think it can be a pistorms problem?

    When I worked with ev3-dev, I did not need pistorms to connect the camera, I just put the camera direct to ev3-dev.

    Also I ve been doing more tests, and I found a new result. When I try to read number of objects I get the result NCK.
    That makes me to think that the camera is not answering the query correctly using pistorms.

    What do you think?
    Angel Perez, 2017-09-08 06:22:34 -04:00
  8. seth10
    seth10 Member Staff Member
    Right, you did not have to use the PiStorms when you were using ev3-dev on the EV3. I have no experience with the bcm2835 library so I could not say. 
    We use the SMBus library in the first-party NXTCAM/NXTCam5 classes. In fact, we use this throughough the PiStorms library. Here's a "stack trace" of sorts to point you in the right direction. In short just look at /usr/local/lib/python2.7/dist-packages/mindsensors_i2c.py.
    https://www.mindsensors.com/reference/PiStorms/html/classmindsensors_1_1_n_x_t_c_a_m.html
    https://www.mindsensors.com/reference/PiStorms/html/class_ms_devices_1_1_n_x_t_cam5.html
    https://github.com/mindsensors/PiStorms/blob/master/sys/mindsensors.py#L1519
    https://github.com/mindsensors/PiStorms/blob/master/sys/MsDevices.py#L779
    https://pypi.python.org/pypi/mindsensors_i2c
    https://pypkg.com/pypi/mindsensors-i2c/f/mindsensors_i2c.py
    seth10, 2017-09-08 09:20:41 -04:00
  9. Angel Perez
    Angel Perez Member
    Hi seth10!! I write just for the community to know this problem, and how to solve it (because I got it in the end!! :D )

    Well, the problem is about the Clock Stretching. If you take a look in the following link, you will know it better.
    http://www.hobbytronics.co.uk/raspberry-pi-i2c-clock-stretching

    To solve this problem, I tryed to change i2c speed in order to get a correct response from camera.

    In bcm2835, there is a function to do this. 
    bcm2835_set_clockDivider allows you to specify a value to change i2c speed. In my case, using PixyCam and NXTCAM worked with value 2500.

    I hope this info to be usefull for future projects.

    Thanks for paying attention and help me with this!!

    Angel Perez, 2017-09-17 05:27:41 -04:00
  10. (You must log in or sign up to post here)

Log in | Sign up
Tems & Conditions!
Help!