Skip to content

Commit e79cd9e

Browse files
committed
The discrepancy between serial and usb is now resolved. The bulk read in
USB was working, bu the single buyte read was breaking down.
1 parent 9d1b506 commit e79cd9e

File tree

3 files changed

+73
-111
lines changed

3 files changed

+73
-111
lines changed

javasdk/NRSDK/src/com/neuronrobotics/sdk/common/BowlerAbstractConnection.java

Lines changed: 43 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,11 @@ public synchronized BowlerDatagram sendSynchronusly(BowlerDatagram sendable){
176176
}
177177

178178
if (getLastSyncronousResponse() == null){
179-
try {
180-
//new RuntimeException().printStackTrace();
181-
Log.error("No response from device, no response in "+(System.currentTimeMillis()-startOfReciveTime)+" ms");
182-
183-
} catch (Exception e) {
184-
clearLastSyncronousResponse();
185-
executingLock.unlock();
186-
throw new RuntimeException(e);
179+
Log.error("No response from device, no response in "+(System.currentTimeMillis()-startOfReciveTime)+" ms");
180+
if( BowlerDatagram.isUseBowlerV4()){
181+
//If the ping fails to get a response, try the older bowler format
182+
Log.error("Switching to legacy parser");
183+
BowlerDatagram.setUseBowlerV4(false);
187184
}
188185
}
189186
BowlerDatagram b = getLastSyncronousResponse();
@@ -939,7 +936,8 @@ public ArrayList<RpcEncapsulation> getRpcList(String namespace,MACAddress addr)
939936
*/
940937
public BowlerDatagram send(BowlerAbstractCommand command,MACAddress addr, int retry) throws NoConnectionAvailableException, InvalidResponseException {
941938
for(int i=0;i<retry;i++){
942-
939+
if(i!=0)
940+
Log.error("Re-sending");
943941
BowlerDatagram ret;
944942
try{
945943
ret = send( command,addr);
@@ -949,10 +947,12 @@ public BowlerDatagram send(BowlerAbstractCommand command,MACAddress addr, int re
949947
//if(!ret.getRPC().contains("_err"))
950948

951949
return ret;
950+
}else{
951+
throw new InvalidResponseException("No response from device");
952952
}
953-
}catch(MalformattedDatagram | NullPointerException e){
953+
}catch(MalformattedDatagram |InvalidResponseException | NullPointerException e){
954954
Log.error("Sending Synchronus packet and there was a failure, will retry "+(retry-i-1)+" more times");
955-
ThreadUtil.wait(150*i);
955+
ThreadUtil.wait(150*i);
956956
}
957957

958958
}
@@ -980,13 +980,9 @@ public BowlerDatagram send(BowlerAbstractCommand command,MACAddress addr) throws
980980
if(back!=null){
981981
addr.setValues(back.getAddress());
982982
}
983-
try{
984-
return command.validate(back);
985-
}catch (InvalidResponseException ex){
986-
ex.printStackTrace();
987-
Log.error("Failed to send synchronusly: "+cmd+"\r\nGot>> "+back);
988-
return null;
989-
}
983+
984+
return command.validate(back);
985+
990986
//BowlerDatagramFactory.freePacket(cmd);
991987

992988
}
@@ -1010,7 +1006,7 @@ public PingCommand() {
10101006
public boolean ping(MACAddress mac) {
10111007
try {
10121008
//Log.warning("Ping device:");
1013-
BowlerDatagram bd = send(new PingCommand(),mac,2);
1009+
BowlerDatagram bd = send(new PingCommand(),mac,5);
10141010
if(bd !=null){
10151011
BowlerDatagramFactory.freePacket(bd);
10161012
return true;
@@ -1022,6 +1018,7 @@ public boolean ping(MACAddress mac) {
10221018
Log.error("No connection is available.");
10231019
e.printStackTrace();
10241020
}
1021+
10251022
return false;
10261023
}
10271024

@@ -1161,7 +1158,7 @@ public void run() {
11611158
private boolean runPacketUpdate() {
11621159
try {
11631160
if(loadPacketFromPhy(bytesToPacketBuffer))
1164-
bytesToPacketBuffer= new ByteList();
1161+
bytesToPacketBuffer.clear();
11651162
} catch (Exception e) {
11661163
if(isConnected()){
11671164
Log.error("Data read failed "+e.getMessage());
@@ -1198,50 +1195,34 @@ public void kill() {
11981195

11991196
public boolean loadPacketFromPhy(ByteList bytesToPacketBuffer) throws NullPointerException, IOException{
12001197
if(dataIns!=null){
1201-
while(getDataIns().available()>0){
1202-
long dataRead=System.currentTimeMillis();
1203-
//we want to run this until the buffer is clear or a packet is found
1204-
int b = getDataIns().read();
1205-
long dataReadEnd=System.currentTimeMillis();
1206-
if(b<0){
1207-
Log.error("Stream is broken - unexpected: claimed to have "+getDataIns().available()+" bytes, read in "+b);
1208-
//reconnect();
1209-
//something went wrong
1210-
new RuntimeException().printStackTrace();
1211-
return false;
1212-
}else{
1213-
1214-
bytesToPacketBuffer.add(b);
1215-
long dataAdd=System.currentTimeMillis();
1216-
BowlerDatagram bd = BowlerDatagramFactory.build(bytesToPacketBuffer);
1217-
long dataBuild=System.currentTimeMillis();
1218-
if (bd!=null) {
1219-
Log.info("\nR<<"+bd);
1220-
onDataReceived(bd);
1221-
1222-
long dataSet=System.currentTimeMillis();
1223-
//bytesToPacketBuffer.clear();
1224-
1225-
long bufferClear=System.currentTimeMillis();
1226-
1227-
if((System.currentTimeMillis()-getLastWrite())>(getSleepTime()*(getPercentagePrint() /100.0))&& bd.isSyncronous() && syncListen==null){
1228-
Log.info("Packet recive took more then "+getPercentagePrint()+"%. " +
1229-
//"\nRaw receive\t"+(start-getLastWrite() )+"" +
1230-
//"\nStart Section\t"+(dataRead- start)+"" +
1231-
"\nData Read\t"+(dataReadEnd-dataRead)+"" +
1232-
"\nAdd data\t"+(dataAdd-dataReadEnd)+
1233-
"\nBuild Packet\t"+(dataBuild-dataAdd)+
1234-
"\nSet Packet\t"+(dataSet-dataBuild)+
1235-
"\nClear Packet buffer\t"+(bufferClear-dataSet)
1236-
);
1237-
}
1238-
1239-
//Packet found, break the loop and deal with it
1240-
return true;
1241-
}
1198+
if(getDataIns().available()==0)
1199+
return false;
1200+
byte [] buff = new byte[getDataIns().available()];
1201+
//we want to run this until the buffer is clear or a packet is found
1202+
int b = getDataIns().read(buff);
1203+
1204+
if(b<0 || buff.length != b){
1205+
Log.error("Stream is broken - unexpected: claimed to have "+getDataIns().available()+" bytes, read in "+b);
1206+
//reconnect();
1207+
//something went wrong
1208+
new RuntimeException(" Buffer attempted to read "+buff.length+" got "+b).printStackTrace();
1209+
return false;
1210+
}else{
1211+
bytesToPacketBuffer.add(buff);
1212+
BowlerDatagram bd=null;
1213+
try{
1214+
bd = BowlerDatagramFactory.build(bytesToPacketBuffer);
1215+
}catch(Exception ex){
1216+
ex.printStackTrace();
1217+
}
1218+
if (bd!=null) {
1219+
Log.info("\nR<<"+bd);
1220+
onDataReceived(bd);
1221+
return true;
12421222
}
1243-
//Log.info("buffer: "+buffer);
12441223
}
1224+
//ThreadUtil.wait(1);
1225+
12451226
}else{
12461227
Log.error("Data In is null");
12471228
}

javasdk/NRSDK/src/com/neuronrobotics/sdk/common/BowlerDatagram.java

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -217,23 +217,13 @@ public void parse(ByteList raw) {
217217
int dataLength = raw.getUnsigned(9);
218218
//Either legacy parser or the v4 parser
219219
if((dataLength != raw.getBytes(11).length-1) && (dataLength != raw.getBytes(11).length) ) {
220-
throw new MalformattedDatagram("Datagram payload length is mismatched");
220+
throw new MalformattedDatagram("Datagram payload length is mismatched expected "+dataLength+" got "+raw.getBytes(11).length);
221221
}
222-
// Put the remaining data into the data payload
222+
// Put the remaining data into the data payload
223223
setData(raw.getBytes(HEADER_SIZE,dataLength));
224224

225225
// Validate the CRC
226226
if(!CheckCRC(raw,true) ) {
227-
Log.error("CRC failed check");
228-
if( BowlerDatagram.isUseBowlerV4()){
229-
//If the ping fails to get a response, try the older bowler format
230-
Log.error("Switching to legacy parser");
231-
BowlerDatagram.setUseBowlerV4(false);
232-
}else{
233-
Log.error("Switching to v4 parser");
234-
BowlerDatagram.setUseBowlerV4(true);
235-
}
236-
if(!CheckCRC(raw,true) )
237227
throw new MalformattedDatagram("CRC does not match");
238228
}else{
239229
setCrc(getCRC(raw));
@@ -365,16 +355,6 @@ private int getSessionID() {
365355
return getNamespaceResolutionID() >= 0 ? (int) getNamespaceResolutionID():(int) getNamespaceResolutionID()+256;
366356
}
367357

368-
/**
369-
* Get the datagram's current data payload after the RPC.
370-
* @Deprecated use getData() instead
371-
* @return the current data payload
372-
*/
373-
@Deprecated
374-
public ByteList getRPCData() {
375-
checkValidPacket();
376-
return getData();
377-
}
378358
/**
379359
* Get the datagram's current data payload after the RPC.
380360
*
@@ -402,7 +382,7 @@ public byte[] getBytes() {
402382
bl.add(getCRC());
403383
bl.add(data);
404384
if(isUseBowlerV4()){
405-
385+
//Log.warning("parsing v4 ");
406386
setDataCrc(genDataCrc(bl));
407387
bl.add(getDataCrc());
408388
}
@@ -459,24 +439,25 @@ public String toString(){
459439
*/
460440
static boolean CheckCRC(ByteList buffer, boolean checkData) {
461441
try{
462-
byte generated,inPacket;
463-
generated = genCrc(buffer);
464-
inPacket = getCRC(buffer);
465-
if(generated != inPacket){
466-
Log.error("CRC of packet is: "+generated+" Expected: "+inPacket);
467-
return false;
468-
}
469-
470-
if(checkData && isUseBowlerV4()){
471-
generated = genDataCrc(buffer);
472-
inPacket = getDataCrc(buffer);
442+
byte generated,inPacket;
443+
generated = genCrc(buffer);
444+
inPacket = getCRC(buffer);
473445
if(generated != inPacket){
474-
Log.error("Data CRC of packet is: "+generated+" Expected: "+inPacket);
446+
Log.error("CRC of packet is: "+generated+" Expected: "+inPacket);
475447
return false;
476448
}
477-
}
449+
450+
if(checkData && isUseBowlerV4()){
451+
generated = genDataCrc(buffer);
452+
inPacket = getDataCrc(buffer);
453+
if(generated != inPacket){
454+
Log.error("Data CRC of packet is: "+generated+" Expected: "+inPacket);
455+
return false;
456+
}
457+
}
478458
}catch(Exception ex){
479459
if(InterruptedException.class.isInstance(ex))throw ex;
460+
ex.printStackTrace();
480461
return false;
481462
}
482463
return true;
@@ -592,6 +573,8 @@ public static boolean isUseBowlerV4() {
592573
}
593574

594575
public static void setUseBowlerV4(boolean useBowlerV4) {
576+
Log.warning("Setting V4 mode = "+useBowlerV4);
577+
//new Exception().printStackTrace();
595578
BowlerDatagram.useBowlerV4 = useBowlerV4;
596579
}
597580
}

javasdk/NRSDK/src/com/neuronrobotics/sdk/common/BowlerDatagramFactory.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -206,23 +206,21 @@ private static BowlerDatagram build(ByteList buffer, BowlerDatagram staticMemory
206206
}
207207
int totalLen = len+BowlerDatagram.HEADER_SIZE;
208208

209+
if(BowlerDatagram.isUseBowlerV4())
210+
totalLen+=1;
211+
staticMemory.setFree(false,instance);
209212
// See if all the data has arrived for this packet
210-
if (buffer.size()>=(totalLen) || buffer.size()>=(totalLen+1)){
213+
if (buffer.size()>=(totalLen)){
211214
failed=0;
212-
ByteList rawContent = new ByteList(buffer.popList(totalLen));
213-
staticMemory.setFree(false,instance);
214-
try{
215-
staticMemory.parse(rawContent);
216-
if(BowlerDatagram.CheckCRC(rawContent,true)){
217-
return staticMemory;
218-
}else{
219-
Log.error("Data CRC check Fail "+staticMemory);
220-
failed = rawContent.size();
221-
}
222-
}catch(Exception E){
223-
E.printStackTrace();
215+
ByteList rawContent = new ByteList(buffer.popList(totalLen));
216+
staticMemory.parse(rawContent);
217+
if(BowlerDatagram.CheckCRC(rawContent,true)){
218+
return staticMemory;
219+
}else{
224220
Log.error("Data CRC check Fail "+staticMemory);
221+
failed = rawContent.size();
225222
}
223+
226224
}
227225
// if(failed>0)
228226
// Log.error("Failed out "+failed+" bytes");

0 commit comments

Comments
 (0)