Skip to content

Commit c1f257e

Browse files
committed
adding the new parser get/set system
1 parent 90263ca commit c1f257e

File tree

3 files changed

+152
-71
lines changed

3 files changed

+152
-71
lines changed

javasdk/NRSDK/src/com/neuronrobotics/sdk/dyio/DyIO.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public DyIOChannel getChannel(int channel) {
132132
return getInternalChannels().get(channel);
133133
}
134134

135-
private Object[] send(String NS,BowlerMethod method, String rpcString, Object[] arguments){
135+
public Object[] send(String NS,BowlerMethod method, String rpcString, Object[] arguments){
136136
return send(NS,method,rpcString,arguments,2);
137137
}
138138

@@ -326,7 +326,7 @@ public ArrayList<DyIOChannelMode> getAllChannelModes(){
326326
ArrayList<DyIOChannelMode> modes = new ArrayList<DyIOChannelMode>();
327327
BowlerDatagram response;
328328
ByteList bl;
329-
if(legacyParser){
329+
if(isLegacyParser()){
330330
try {
331331
response = send(new GetChannelModeCommand());
332332
} catch (Exception e) {
@@ -400,10 +400,10 @@ public boolean resync() {
400400
}
401401

402402
if(hasNamespace(NEURONROBOTICS_DYIO_1_0)){
403-
legacyParser=false;
403+
setLegacyParser(false);
404404
}
405405
if(hasNamespace("neuronrobotics.dyio.*;0.3;;")){
406-
legacyParser=true;
406+
setLegacyParser(true);
407407
}
408408
setResyncing(true);
409409
setMuteResyncOnModeChange(true);
@@ -596,7 +596,7 @@ public void flushCache(double seconds) {
596596
values[i++]=d.getCachedValue();
597597
//d.flush();
598598
}
599-
if(legacyParser){
599+
if(isLegacyParser()){
600600
for(int j=0;j<5;j++) {
601601
try {
602602
send(new SetAllChannelValuesCommand(seconds,values));
@@ -1096,7 +1096,7 @@ public String toString() {
10961096
*/
10971097
public int[] getAllChannelValues() {
10981098
int [] back = new int[getInternalChannels().size()];
1099-
if(legacyParser){
1099+
if(isLegacyParser()){
11001100
BowlerDatagram gacv = send(new GetAllChannelValuesCommand());
11011101
Log.info("GACV RX<<\n"+gacv);
11021102
ByteList bl = gacv.getData();
@@ -1156,7 +1156,7 @@ public int getPIDChannelCount() {
11561156
public Integer getDyIOChannelCount(){
11571157

11581158
if(dyioChanCount == null){
1159-
if(legacyParser){
1159+
if(isLegacyParser()){
11601160
try{
11611161
BowlerDatagram dg = send (new GetDyIOChannelCountCommand());
11621162
dyioChanCount = ByteList.convertToInt(dg.getData().getBytes(0, 4));
@@ -1178,7 +1178,7 @@ public ArrayList<DyIOChannelMode> getAvailibleChannelModes(int channel){
11781178
ArrayList<DyIOChannelMode> modes = new ArrayList<DyIOChannelMode>();
11791179
ByteList m;
11801180

1181-
if(legacyParser){
1181+
if(isLegacyParser()){
11821182
BowlerDatagram dg = send(new GetChannelModeListCommand(channel));
11831183

11841184
m = dg.getData();
@@ -1203,6 +1203,14 @@ public GenericPIDDevice getPid() {
12031203
return pid;
12041204
}
12051205

1206+
public boolean isLegacyParser() {
1207+
return legacyParser;
1208+
}
1209+
1210+
public void setLegacyParser(boolean legacyParser) {
1211+
this.legacyParser = legacyParser;
1212+
}
1213+
12061214

12071215

12081216

javasdk/NRSDK/src/com/neuronrobotics/sdk/dyio/DyIOChannel.java

Lines changed: 116 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.neuronrobotics.sdk.commands.bcs.io.setmode.SetChannelModeCommand;
2525
import com.neuronrobotics.sdk.common.BowlerAbstractCommand;
2626
import com.neuronrobotics.sdk.common.BowlerDatagram;
27+
import com.neuronrobotics.sdk.common.BowlerMethod;
2728
import com.neuronrobotics.sdk.common.ByteList;
2829
import com.neuronrobotics.sdk.common.InvalidResponseException;
2930
import com.neuronrobotics.sdk.common.Log;
@@ -381,20 +382,29 @@ public DyIOChannelMode getMode() {
381382
public int getValue() {
382383
BowlerDatagram response=null;
383384
int val=0;
384-
try {
385-
response = getDevice().send(new GetValueCommand(number));
386-
} catch (InvalidResponseException e) {
387-
response = getDevice().send(new GetValueCommand(number));
388-
}
389-
ByteList bl = response.getData();
390-
391-
Byte b = bl.pop();
392-
if(b==null || b.intValue()!=number){
393-
Log.error("Failed to get value "+response);
394-
return 0;
385+
if(getDevice().isLegacyParser()){
386+
387+
try {
388+
response = getDevice().send(new GetValueCommand(number));
389+
} catch (InvalidResponseException e) {
390+
response = getDevice().send(new GetValueCommand(number));
391+
}
392+
ByteList bl = response.getData();
393+
394+
Byte b = bl.pop();
395+
if(b==null || b.intValue()!=number){
396+
Log.error("Failed to get value "+response);
397+
return 0;
398+
}
399+
400+
val = new DyIOChannelEvent(this,bl).getValue();
401+
}else{
402+
Object [] args =getDevice().send("bcs.io.*;0.3;;",
403+
BowlerMethod.POST,
404+
"gchv",
405+
new Object[]{number});
406+
val=(Integer)args[0];
395407
}
396-
397-
val = new DyIOChannelEvent(this,bl).getValue();
398408
setCachedValue(val);
399409
setPreviousValue(val);
400410
return val;
@@ -489,54 +499,39 @@ public boolean flush() {
489499
// throw new RuntimeException("In chached mode and flushing from channel");
490500
//Log.enableDebugPrint(true);
491501
Log.debug("Flushing channel: "+number);
492-
493-
ByteList b = new ByteList();
494-
switch(getMode()){
495-
case COUNT_IN_INT:
496-
case COUNT_IN_DIR:
497-
case COUNT_IN_HOME:
498-
case COUNT_OUT_INT:
499-
case COUNT_OUT_DIR:
500-
case COUNT_OUT_HOME:
501-
b.addAs32(getCachedValue());
502-
b.addAs32((int)(getCachedTime()*1000));
503-
break;
504-
case SERVO_OUT:
505-
b.add(getCachedValue());
506-
b.addAs16((int)(getCachedTime()*1000));
507-
break;
508-
default:
509-
b.add(getCachedValue());
510-
}
511-
Log.info("Setting channel: "+getChannelNumber()+" to value: "+b);
512-
boolean back = setValue(b);
513-
//Log.enableDebugPrint(false);
514-
return back;
515-
}
516-
517-
/* (non-Javadoc)
518-
* @see com.neuronrobotics.sdk.dyio.IDyIOChannel#setValue(com.neuronrobotics.sdk.common.ISendable)
519-
*/
520-
521-
public boolean setValue(ByteList data) {
522-
int attempts = MAXATTEMPTS;
523-
if(getMode() == DyIOChannelMode.USART_RX ||getMode() == DyIOChannelMode.USART_TX )
524-
attempts=1;
525-
for(int i = 0; i < attempts; i++) {
526-
try {
527-
getDevice().send(new SetChannelValueCommand(number, data));
528-
return true;
529-
} catch (InvalidResponseException e) {
530-
try {
531-
Thread.sleep(100);
532-
} catch (InterruptedException e1) {
533-
return false;
534-
}
502+
if(getDevice().isLegacyParser()){
503+
ByteList b = new ByteList();
504+
switch(getMode()){
505+
case COUNT_IN_INT:
506+
case COUNT_IN_DIR:
507+
case COUNT_IN_HOME:
508+
case COUNT_OUT_INT:
509+
case COUNT_OUT_DIR:
510+
case COUNT_OUT_HOME:
511+
b.addAs32(getCachedValue());
512+
b.addAs32((int)(getCachedTime()*1000));
513+
break;
514+
case SERVO_OUT:
515+
b.add(getCachedValue());
516+
b.addAs16((int)(getCachedTime()*1000));
517+
break;
518+
default:
519+
b.add(getCachedValue());
535520
}
521+
Log.info("Setting channel: "+getChannelNumber()+" to value: "+b);
522+
boolean back = setValue(b);
523+
//Log.enableDebugPrint(false);
524+
return back;
525+
}else{
526+
getDevice().send( "bcs.io.*;0.3;;",
527+
BowlerMethod.POST,
528+
"schv",
529+
new Object[]{number,getCachedValue(),(int)(getCachedTime()*1000)});
530+
return true;
536531
}
537-
538-
return false;
539532
}
533+
534+
540535

541536

542537
/**
@@ -745,6 +740,36 @@ private void resyncIfNotSynced() {
745740
resync(false);
746741
}
747742
}
743+
public boolean isStreamtMode( DyIOChannelMode m) {
744+
switch(m) {
745+
case USART_RX:
746+
case PPM_IN:
747+
case SPI_CLOCK:
748+
case SPI_MISO:
749+
case SPI_MOSI:
750+
case USART_TX:
751+
return true;
752+
case SERVO_OUT:
753+
case ANALOG_OUT:
754+
case DC_MOTOR_DIR:
755+
case DC_MOTOR_VEL:
756+
case PWM_OUT:
757+
case SPI_SELECT:
758+
case DIGITAL_OUT:
759+
case COUNT_OUT_DIR:
760+
case COUNT_OUT_HOME:
761+
case COUNT_OUT_INT:
762+
case ANALOG_IN:
763+
case COUNT_IN_DIR:
764+
case COUNT_IN_HOME:
765+
case COUNT_IN_INT:
766+
case DIGITAL_IN:
767+
case NO_CHANGE:
768+
case OFF:
769+
return false;
770+
}
771+
return false;
772+
}
748773
private boolean isOutputMode( DyIOChannelMode m) {
749774
switch(m) {
750775
case SERVO_OUT:
@@ -775,4 +800,37 @@ private boolean isOutputMode( DyIOChannelMode m) {
775800
}
776801
return false;
777802
}
803+
804+
/* (non-Javadoc)
805+
* @see com.neuronrobotics.sdk.dyio.IDyIOChannel#setValue(com.neuronrobotics.sdk.common.ISendable)
806+
*/
807+
808+
public boolean setValue(ByteList data) {
809+
if(!isStreamChannel())
810+
throw new RuntimeException("Only stream channels should talk to this method");
811+
if(getDevice().isLegacyParser()){
812+
int attempts = MAXATTEMPTS;
813+
if(getMode() == DyIOChannelMode.USART_RX ||getMode() == DyIOChannelMode.USART_TX )
814+
attempts=1;
815+
for(int i = 0; i < attempts; i++) {
816+
try {
817+
getDevice().send(new SetChannelValueCommand(number, data));
818+
return true;
819+
} catch (InvalidResponseException e) {
820+
try {
821+
Thread.sleep(100);
822+
} catch (InterruptedException e1) {
823+
return false;
824+
}
825+
}
826+
}
827+
}else{
828+
getDevice().send("bcs.io.*;0.3;;",
829+
BowlerMethod.POST,
830+
"strm",
831+
new Object[]{number,data});
832+
}
833+
834+
return false;
835+
}
778836
}

javasdk/NRSDK/src/com/neuronrobotics/sdk/dyio/peripherals/SPIChannel.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.neuronrobotics.sdk.commands.bcs.io.SetChannelValueCommand;
44
import com.neuronrobotics.sdk.common.BowlerDatagram;
5+
import com.neuronrobotics.sdk.common.BowlerMethod;
56
import com.neuronrobotics.sdk.common.ByteList;
67
import com.neuronrobotics.sdk.dyio.DyIO;
78
import com.neuronrobotics.sdk.dyio.DyIOChannelMode;
@@ -33,11 +34,13 @@ public SPIChannel (DyIO d) {
3334
* @param stream the Bytes to be sent out
3435
* @return true if success
3536
*/
36-
public BowlerDatagram sendSPIStream(int ss, byte [] stream) {
37+
private BowlerDatagram sendSPIStream(int ss, byte [] stream) {
38+
3739
ByteList b = new ByteList();
3840
b.add(ss);
3941
b.add(stream);
4042
return dyio.send(new SetChannelValueCommand(0, b));
43+
4144
}
4245
/**
4346
* This performs a dumb read. The data sent out by the host is junk data.
@@ -59,9 +62,21 @@ public BowlerDatagram sendSPIStream(int ss, byte [] stream) {
5962
* @return the data received
6063
*/
6164
public byte [] write(int ss, byte [] stream) {
62-
BowlerDatagram b= sendSPIStream(ss,stream);
63-
if(b==null)
64-
return new byte[0];
65-
return b.getData().getBytes(2);
65+
if(dyio.isLegacyParser()){
66+
BowlerDatagram b= sendSPIStream(ss,stream);
67+
if(b==null)
68+
return new byte[0];
69+
return b.getData().getBytes(2);
70+
}else{
71+
dyio.send("bcs.io.*;0.3;;",
72+
BowlerMethod.POST,
73+
"strm",
74+
new Object[]{0,new ByteList(stream)});
75+
Object [] args = dyio.send("bcs.io.*;0.3;;",
76+
BowlerMethod.GET,
77+
"strm",
78+
new Object[]{0});
79+
return ((ByteList)args[0]).getBytes();
80+
}
6681
}
6782
}

0 commit comments

Comments
 (0)