Hello there,
I’ve got a problem with the Line Follower in Java!
First of all, there is no Line Follower Class on the GitHub Site, so i’ve went ahead and tried to write my own “LineFollower” Class, using the information I got from reading the Line Follower C and Python Methods.
public class LineFollower {
private final I2CDevice device;
public LineFollower() throws IOException, InterruptedException, I2CFactory.UnsupportedBusNumberException{
int busId;
String type = SystemInfo.getBoardType().name();
if (type.indexOf("ModelA") > 0) {
busId = I2CBus.BUS_0;
} else {
busId = I2CBus.BUS_1;
}
I2CBus bus = null;
try {
bus = I2CFactory.getInstance(busId);
}catch (Exception e){
e.printStackTrace();
System.exit(-1);
}
device = bus.getDevice(0x06);
}
/**
* Copy Pasted Method from the Board class
* @param bytes
* @return
* @throws IOException
*/
public int writeI2c(int... bytes) throws IOException {
if (Gopigo.getInstance().isHalt()) {
Gopigo.getInstance().onHalt();
}
// Convert array: int[] to byte[]
final ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length);
for (int i = 0, len = bytes.length; i < len; i++) {
byteBuffer.put((byte) bytes[i]);
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
device.write(byteBuffer.array());
return Statuses.OK;
}
/**
* Copy Pasted Method from the Board class
* @param numberOfBytes
* @return
* @throws IOException
*/
public byte[] readI2c(int numberOfBytes) throws IOException {
if (Gopigo.getInstance().isHalt()) {
Gopigo.getInstance().onHalt();
}
byte[] buffer = new byte[numberOfBytes];
device.read(buffer, 0, buffer.length);
return buffer;
}
public int[] readSensor(){
try {
writeI2c(new int[]{1,3,0,0,0}); //
int regSize = 10;
byte[] rBuf = readI2c(regSize);
int[] rintbuf = new int[rBuf.length];
for(int j= 0; j < rBuf.length; j++){ //byte to unsigned "byte"
rintbuf[j] = rBuf[j] & 0xFF;
}
int[] returnArray = new int[5];
for(int i = 0; i < 10; i = i+2){
returnArray[i/2] = rintbuf[i]*256+rintbuf[i+1];
}
return returnArray;
} catch (IOException e) {
e.printStackTrace();
return new int[]{-1,-1,-1,-1,-1};
}
}
}
For a darkblue underground I get the byte Array: 288, 216, 127, 201, 175
For a white underground I get the byte Array: 245, 175, 108, 192, 218
And for 2 sensors darkblue and 3 sensors white I get the byte Array: 294, 208, 166, 214, 209
As you can see, these values barely change.
I expect the problem to be in the readSensor() Method at the write method call with the int array: 1, 3, 0, 0, 0
and I need help, to find the problem as there are no documentations on the Line Follower I could read to get the necessary information i need.
Im lookin’ forward to hear from anyone
[Edit] Additionally I’ve got the Issue that the check_line_sensor.py just returns: Line sensor found, but without anything more, it stops right there.
Note, that I dont use the provides robot raspberry os, ill use a classic debian jessie destr.