|
24 | 24 | import com.neuronrobotics.sdk.commands.bcs.io.setmode.SetChannelModeCommand; |
25 | 25 | import com.neuronrobotics.sdk.common.BowlerAbstractCommand; |
26 | 26 | import com.neuronrobotics.sdk.common.BowlerDatagram; |
| 27 | +import com.neuronrobotics.sdk.common.BowlerMethod; |
27 | 28 | import com.neuronrobotics.sdk.common.ByteList; |
28 | 29 | import com.neuronrobotics.sdk.common.InvalidResponseException; |
29 | 30 | import com.neuronrobotics.sdk.common.Log; |
@@ -381,20 +382,29 @@ public DyIOChannelMode getMode() { |
381 | 382 | public int getValue() { |
382 | 383 | BowlerDatagram response=null; |
383 | 384 | 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]; |
395 | 407 | } |
396 | | - |
397 | | - val = new DyIOChannelEvent(this,bl).getValue(); |
398 | 408 | setCachedValue(val); |
399 | 409 | setPreviousValue(val); |
400 | 410 | return val; |
@@ -489,54 +499,39 @@ public boolean flush() { |
489 | 499 | // throw new RuntimeException("In chached mode and flushing from channel"); |
490 | 500 | //Log.enableDebugPrint(true); |
491 | 501 | 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()); |
535 | 520 | } |
| 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; |
536 | 531 | } |
537 | | - |
538 | | - return false; |
539 | 532 | } |
| 533 | + |
| 534 | + |
540 | 535 |
|
541 | 536 |
|
542 | 537 | /** |
@@ -745,6 +740,36 @@ private void resyncIfNotSynced() { |
745 | 740 | resync(false); |
746 | 741 | } |
747 | 742 | } |
| 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 | + } |
748 | 773 | private boolean isOutputMode( DyIOChannelMode m) { |
749 | 774 | switch(m) { |
750 | 775 | case SERVO_OUT: |
@@ -775,4 +800,37 @@ private boolean isOutputMode( DyIOChannelMode m) { |
775 | 800 | } |
776 | 801 | return false; |
777 | 802 | } |
| 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 | + } |
778 | 836 | } |
0 commit comments