Max baudrate GoPiGo


#1

Hello,

I am trying to write a java program and GUI for the GoPiGo and a problem I encountered was reading the voltage. Every time I read the voltage I got the same error:
java.io.IOException: Error reading from /dev/i2c-1 at address 0x8 to address 0x1. Got -1.
at com.pi4j.io.i2c.impl.I2CDeviceImpl.read(I2CDeviceImpl.java:252)
at nl.macbee.Main.readI2c(Main.java:117)
at nl.macbee.Main.volt(Main.java:80)
at nl.macbee.Main.main(Main.java:46)

The original code was:
public double volt() throws IOException {
writeI2c(118, 0, 0, 0);
sleep(100);

    byte[] b1 = readI2c(1);
    byte[] b2 = readI2c(1);
    int val1 = (int) b1[0] & 0xFF;
    int val2 = (int) b2[0] & 0xFF;

    if (val1 != -1 && val2 != -1) {
        double v = val1 * 256 + val2;
        v = (5 * (float) v / 1024) / .4;
        v = Math.round(v * 100.0) / 100.0;
        return v;
    } else {
        return -1;
    }
}

I altered the code a bit to retry on exception and play around:
public double volt() {
for (int i = 0; i < 3; i++) {
try {
writeI2c(118, 0, 0, 0);
sleep(100);

            byte[] b1 = readI2c(1);
            byte[] b2 = readI2c(1);
            int val1 = (int) b1[0] &amp; 0xFF;
            int val2 = (int) b2[0] &amp; 0xFF;

            if (val1 != -1 &amp;&amp; val2 != -1) {
                double v = val1 * 256 + val2;
                v = (5 * (float) v / 1024) / .4;
                v = Math.round(v * 100.0) / 100.0;
                return v;
            } else {
                return -1;
            }
        } catch (IOException e) {
            System.out.println("Retrying...");
            sleep(100);
        }
    }
    return -1;
}

But every time I got the same exception.

Only when I decreased the baudrate in /boot/config.txt to a lower level:
dtparam=i2c_arm_baudrate=32000
the failure of 100 iterations (asking the voltage) was significant better.

What is the maximum baudrate of the gopigo? Or am I doing something wrong? Or have you encountered the same problem?

Greetings,
Edwin


#2

I’m new to pi and gopigo. I was just trying the java examples and when tried the Test script in repo got the following error:

java.io.IOException: Error reading from /dev/i2c-1 at address 0x8 to address 0x1. Got -1.
at com.pi4j.io.i2c.impl.I2CDeviceImpl.read(I2CDeviceImpl.java:252)
at com.dexterind.gopigo.components.Board.readI2c(Board.java:137)
at com.dexterind.gopigo.components.Board.volt(Board.java:269)
at com.dexterind.gopigo.Gopigo$1$1.run(Gopigo.java:253)
at java.lang.Thread.run(Thread.java:745)

Seems to related to voltage but I am not sure what’s going on. Can you please provide some insight?
Thanks


#3

I’m not an embedded engineer, but when I was trying to get the voltage for my GUI, I got the same exception as you did. When I was trying to locate the error and I noticed when I was debugging the voltage return was just fine (except some inconsistent values). Then I looked at the code and saw a write and a read in the same method with a sleep. I played around with the sleep (100 millisecond to 5000 millisecond), but I got the same IOExceptions in a for-loop (100x).
So that was not the problem.

With a little googling I find out that a I2C had a baud rate and I was wondering if de raspberry pi wasn’t too fast for the GoPiGo-board. I decreased the baud rate on the Raspberry Pi and found out that the readings where much better. Randomly out of a 100 loops there were only two IOExceptions (average).
For my program I introduced a retry pattern that in case of a IOException the writing and reading where done again (for 3 times).

But as I wrote in my first sentence, I’m not an embedded engineer (but a software engineer), so I don’t know if I’m on the right track. I am hoping that somebody can help me with this issue or that the described solution (decrease baud rate) is the right one.

My first project is to write the same program in python and see if python encounters the same issues.

P.S. Your exception is the voltage reading (4th row).


#4

So is everyone on this thread working in Java? If so, what framework are you using?


#5

Yep, I am working in Java (1.8) standard on Raspbian Jessie. I have two little programs to read the voltage of the GoPiGo 100x times with some sleep in it (see attach).

When I am running the programs with a baud rate of 32000:

  • VoltageWithTryCatch -> has some errors but because of the try-catch-retry the programs ends normally (with sometimes inconsistent values)
  • VoltageWithoutTryCatch -> crashes at a random read

But when I am running on default RaspberryPi baudrate:

  • VoltageWithTryCatch -> is retrying every time and has bad values
  • VoltageWithoutTryCatch -> crashes almost immediately

When I lower the baud rate further, the errors almost disappear.


#6

Sorry, I had to change the file extension.


#7

Hey macbee,
I think you’ll need the try catch in there. The I2C does throw IOErrors sometimes and the easiest way to handle that is with the try catch block. We’ll try to add that in the examples itself. Thanks for the examples.

-Karan


#8

Have you been using our Java libraries (https://github.com/DexterInd/GoPiGo/tree/master/Software/Java) or others?


#9

I followed the instructions https://github.com/DexterInd/GoPiGo/tree/master/Software/Java
But I got errors (see attach) with curl -s get.pi4j.com | sudo bash.

So I downloaded an Raspbian Wheezy image (java 1.7):
https://sourceforge.net/projects/dexterindustriesraspbianflavor/files/Archive/2016.04.26-Dexter_Industries_wheezy.zip/download
and put it on an other SD-card.

  • sudo apt-get update
  • sudo apt-get upgrade
  • sudo apt-get install pi4j

Run the same little programs and with the same result (default baudrate = 100000).
Lowered the baudrate to 32000 and de result are the same.

Then I followed the instructions:

  • ./scripts/compile.sh && ./scripts/Test.sh
    but I got a permission denied.

So did it in the terminal:

  • sudo mkdir -p ./bin
  • sudo javac -d ./bin -classpath .:classes:/opt/pi4j/lib/’’ ./src/com/dexterind/gopigo/.java ./src/com/dexterind/gopigo/behaviours/.java ./src/com/dexterind/gopigo/components/.java ./src/com/dexterind/gopigo/events/.java ./src/com/dexterind/gopigo/utils/.java ./test/tests/.java ./test/.java;

Run the test program (baud rate = 32000):

  • cd ./bin; sudo java $JAVA_OPTS -classpath .:classes:/opt/pi4j/lib/’*’ Test

Change baud rate to default (100000) and run the test program and the program crashes immediately… (see attach)

So yes, I have been using your libraries and also tested it with wheezy now.


#10

Hey macbee,
Can you try using the try catch in the posts mentioned above ot reduct the clock speed to 16000 as mentioned here: http://www.dexterindustries.com/topic/io-read-error/.

-Karan


#11

Hey Karan,
I changed the baudrate to 16000 and run my two programs a number of times and the error rate was less than with 32000. So I made a private method for the read and write with a try-catch and changed the baudrate to 32000.

The next thing I did was to write a python file:
from gopigo import *

for i in range(1, 100):
print volt()

and removed the baudrate from the /boot/config.txt.
This program was running fine.

So I think there is a difference between python and pi4j. But I don’t know what.