49 public static boolean PCTest =
false;
51 public final static int PA0 = 0;
52 public final static int PA1 = 1;
53 public final static int PA2 = 2;
54 public final static int PA3 = 3;
55 public final static int PA4 = 4;
56 public final static int PA5 = 5;
57 public final static int PA6 = 6;
58 public final static int PA7 = 7;
59 public final static int PA8 = 8;
60 public final static int PA9 = 9;
61 public final static int PA10 = 10;
62 public final static int PA11 = 11;
63 public final static int PA12 = 12;
64 public final static int PA13 = 13;
65 public final static int PA14 = 14;
66 public final static int PA15 = 15;
68 public final static int PB0 = 16;
69 public final static int PB1 = 17;
70 public final static int PB2 = 18;
71 public final static int PB3 = 19;
72 public final static int PB4 = 20;
73 public final static int PB5 = 21;
74 public final static int PB6 = 22;
75 public final static int PB7 = 23;
76 public final static int PB8 = 24;
77 public final static int PB9 = 25;
78 public final static int PB10 = 26;
79 public final static int PB11 = 27;
80 public final static int PB12 = 28;
81 public final static int PB13 = 29;
82 public final static int PB14 = 30;
83 public final static int PB15 = 31;
85 public final static int PC13 = 32;
86 public final static int PC14 = 33;
87 public final static int PC15 = 34;
89 private static class V1 {
97 public final static int Q4_INT = PB1;
98 public final static int Q4_DIR = PC14;
100 public final static int Q5_DIR = PC15;
102 public final static int M1_1_DIR = PA4;
104 public final static int M1_2_DIR = PA5;
112 public final static int M3_2_DIR = PB12;
115 public final static int IO_1 = PA8;
116 public final static int IO_2 = PA9;
117 public final static int IO_3 = PA10;
118 public final static int IO_4 = PA11;
120 public final static int A1_A = PA0;
121 public final static int A1_B = PA1;
122 public final static int A2_A = PA2;
123 public final static int A2_B = PA3;
126 public static class V3 {
127 public final static int PC0 = 32;
128 public final static int PC1 = 33;
129 public final static int PC2 = 34;
130 public final static int PC3 = 35;
131 public final static int PC4 = 36;
132 public final static int PC5 = 37;
133 public final static int PC6 = 38;
134 public final static int PC7 = 39;
135 public final static int PC8 = 40;
136 public final static int PC9 = 41;
137 public final static int PC10 = 42;
138 public final static int PC11 = 43;
139 public final static int PC12 = 44;
140 public final static int PC13 = 45;
141 public final static int PC14 = 46;
142 public final static int PC15 = 47;
144 public final static int PD2 = 48;
161 public final static int M4_1_PWM =
Device.PA10;
162 public final static int M4_1_DIR =
Device.PB5;
163 public final static int M4_2_PWM =
Device.PA11;
164 public final static int M4_2_DIR =
Device.PB4;
166 public final static int M5_1_PWM =
Device.PA15;
167 public final static int M5_1_DIR =
Device.
V3.PC12;
168 public final static int M5_2_PWM =
Device.PB3;
169 public final static int M5_2_DIR =
Device.PA12;
186 public final static int Q6_INT =
Device.PB14;
187 public final static int Q6_DIR =
Device.
V3.PC5;
195 public final static int A3_A =
Device.PA2;
196 public final static int A3_B =
Device.PA5;
198 public final static int A4_A =
Device.PA3;
199 public final static int A4_B =
Device.PA4;
201 public final static int IO1_INT =
Device.PB15;
204 public static class V4 {
205 public final static int PA0 = 000;
206 public final static int PA1 = 001;
208 public final static int PC0 = 020;
209 public final static int PC1 = 021;
210 public final static int PC2 = 022;
211 public final static int PC3 = 023;
212 public final static int PC4 = 024;
213 public final static int PC5 = 025;
214 public final static int PC6 = 026;
215 public final static int PC7 = 027;
217 public final static int PD0 = 030;
218 public final static int PD1 = 031;
219 public final static int PD2 = 032;
220 public final static int PD3 = 033;
221 public final static int PD4 = 034;
222 public final static int PD5 = 035;
223 public final static int PD6 = 036;
224 public final static int PD7 = 037;
226 public final static int M1_1_PWM = PC3;
227 public final static int M1_1_DIR = PC6;
228 public final static int M1_2_PWM = PC4;
229 public final static int M1_2_DIR = PC7;
231 public final static int Q1_INT = PD3;
232 public final static int Q1_DIR = PC5;
233 public final static int Q2_INT = PD4;
234 public final static int Q2_DIR = PC0;
236 public final static int IO = PD6;
373 private static void setVersion1() {
374 Logger.
log(
"Device", 1,
"setVersion1()");
411 public final static int k_i2cDefAddr = 5;
413 public enum ProcessorType {
430 public interface Callback {
431 public void callback(ByteBuffer buffer, Object
object);
434 public enum RequestType {
435 EncoderRelativePosition,
439 private class GroupRequest {
440 private final RequestType m_type;
441 private final int m_id;
442 private final GroupRequestCallback m_callback;
443 private final Object m_control;
445 public GroupRequest(RequestType type,
int id, GroupRequestCallback callback, Object control) {
448 m_callback = callback;
453 interface GroupRequestCallback {
454 void callback(ByteBuffer data, Object
object);
459 private int m_nextEncoder = 0;
460 private long m_validPins = 0;
461 private int m_validAnalogPins = 0;
463 Processor(
int addr) {
467 int getNextEncoderNo() {
468 return m_nextEncoder++;
471 private long getValidPins() {
472 if (m_validPins == 0) {
473 m_validPins = m_device.getLong(m_addr, TwoWire.k_getValidOutputPins);
476 return (m_validPins);
479 private int getValidAnalogPins() {
480 if (m_validAnalogPins == 0) {
481 m_validAnalogPins = m_device.getInt(m_addr, TwoWire.k_getValidAnalogPins);
484 System.out.println(String.format(
"validAnalogPins = %x", m_validAnalogPins));
486 return (m_validAnalogPins);
489 void verifyPin(
int pin,
boolean analog) {
490 long validPins = analog ? getValidAnalogPins() : getValidPins();
495 if ((pin < 0) || (pin >= 64) || (((1 << pin) & validPins) == 0)) {
496 throw new TwoWire.HardwareException(
497 String.format(
"Encoder: Invalid%s pin: %d", analog ?
" Analog" :
"", pin));
503 if (m_device.checkUsedPin(m_addr, pin)) {
504 throw new TwoWire.HardwareException(String.format(
"Encoder: Pin %d is in use", pin));
519 private List<GroupRequest> m_groupRequests = Collections.synchronizedList(
new ArrayList<GroupRequest>());
521 private boolean m_groupRequestsStarted =
false;
523 public void addGroupRequest(RequestType type,
int id, GroupRequestCallback callback, Object control) {
524 synchronized(m_groupRequests) {
525 m_groupRequests.add(
new GroupRequest(type,
id, callback, control));
529 private void groupRequestCallback(ByteBuffer data, Object
object) {
530 synchronized(m_groupRequests) {
531 for (GroupRequest request : m_groupRequests) {
532 switch (request.m_type) {
533 case EncoderRelativePosition:
534 if (data.remaining() < 2) {
535 throw new HardwareException(
"Invalid group request data for encoder");
537 request.m_callback.callback(data, request.m_control);
541 if (data.remaining() < 3) {
542 throw new HardwareException(
"Invalid group request data for gyro");
544 request.m_callback.callback(data, request.m_control);
551 private void runGroupRequests() {
552 synchronized (m_groupRequests) {
553 byte[] data =
new byte[m_groupRequests.size()];
556 int responseSize = 0;
557 for (GroupRequest request : m_groupRequests) {
558 switch (request.m_type) {
559 case EncoderRelativePosition:
568 data[i++] = (byte) (request.m_id | (request.m_type.ordinal() << 4));
571 m_device.getBytes(m_addr, TwoWire.k_getMultipleEncoderPos, data, responseSize,
572 (b, o) -> groupRequestCallback(b, o));
576 public void startGroupRequest() {
579 if (!m_groupRequestsStarted) {
580 m_groupRequestsStarted =
true;
581 m_twoWire.addRunCommand(() -> runGroupRequests());
587 static List<Processor> m_processors = Collections.synchronizedList(
new ArrayList<Processor>());
589 public static void startGroupRequests() {
592 for (Processor processor : m_processors) {
593 processor.startGroupRequest();
597 Processor getProcessor(
int addr) {
598 for (Processor processor : m_processors) {
599 if (processor.m_addr == addr) {
604 Processor processor =
new Processor(addr);
605 m_processors.add(processor);
647 public static int m_boardVersion = 0;
648 public static int m_boardSubVersion = 0;
650 private SerialConnection m_serialConnection;
651 private static Device m_device;
652 private String m_command =
"";
654 @SuppressWarnings(
"unused")
655 private
int m_maxAnalogInputs = 0;
663 @SuppressWarnings(
"unused")
664 private
int m_pins = 0;
674 public final static
int k_mainTwoWireId = 5;
677 private final TwoWire m_twoWire = new TwoWire();
685 public enum PinMode {
690 GPIOInterface m_gpio;
693 m_gpio =
new GPIOInterface();
711 public void addRunCommand(RunCommand command) {
712 m_twoWire.addRunCommand(command);
719 public int getBoardVersion(
int addr) {
720 int fullVersion = m_twoWire.sendRequestByte(addr, TwoWire.k_getBoardVersion);
721 int version = fullVersion & 0xf;
723 Logger.log(
"Device", 1, String.format(
"getBoardVersion=%d", version));
727 Device.setVersion1();
729 else if (version == 3) {
732 else if (version == 4) {
735 else if (version > 4) {
736 throw new HardwareException(String.format(
"Unknown board type: %d", version));
742 public int getBoardVersion() {
743 return (getBoardVersion(k_mainTwoWireId));
757 public void getBytes(
int addr,
int command,
byte args[],
int responseSize, Callback callback) {
758 m_twoWire.sendRequestBytes(addr, command, args, responseSize, callback);
761 public long getLong(
int addr,
int command) {
762 return (m_twoWire.sendRequestLong(addr, command));
765 public int getInt(
int addr,
int command) {
766 return (m_twoWire.sendRequestInt(addr, command));
769 public int getInt(
int addr,
int command,
byte byte1) {
770 return (m_twoWire.sendRequestInt(addr, command, byte1));
773 public void getInt(
int addr,
int command,
byte byte1, Callback callback, Object
object) {
774 m_twoWire.sendRequestInt(addr, command, byte1, callback,
object);
777 public int getShort(
int addr,
int command,
byte byte1) {
778 return (m_twoWire.sendRequestShort(addr, command, byte1));
781 public int getByte(
int addr,
int command) {
782 return (m_twoWire.sendRequestByte(addr, command));
785 public int getByte(
int addr,
int command,
byte byte1) {
786 return (m_twoWire.sendRequestByte(addr, command, byte1));
789 public ByteBuffer getData(
int addr,
int command,
int size) {
790 return (m_twoWire.sendRequest(addr, command, size));
793 public boolean checkUsedPin(
int addr,
int pin) {
794 return (m_twoWire.checkUsedPin(addr, pin));
797 public boolean isPinValid(
int addr,
int pin,
PinType type) {
798 return m_twoWire.isPinValid(addr, pin, type);
801 public void setUsedPin(
int addr,
int pin) {
802 m_twoWire.setUsedPin(addr, pin);
805 public int getEncoderRange(
int addr) {
806 return m_twoWire.getEncoderRange(addr);
809 private void waitForReset() {
810 Logger.log(
"RobotBase", 0,
"Waiting for response");
812 RobotCore.sleep(3000);
818 m_boardVersion = getBoardVersion();
819 m_boardSubVersion = m_boardVersion >> 4;
820 m_boardVersion &= 0xf;
821 Logger.log(
"RobotBase", 0, String.format(
"type=%d,version=%d", m_boardVersion, m_boardSubVersion));
824 private void resetDevice() {
825 Logger.log(
"RobotBase", 2,
"ResetArduino");
829 m_gpio.setReset(
false);
831 m_gpio.setReset(
true);
844 public static Device getInstance() {
845 if (m_device ==
null) {
846 m_device =
new Device();
851 }
catch (InterruptedException ex) {
859 private void dataReceived(String data) {
864 while ((idx = data.indexOf(
'\n')) >= 0) {
865 m_command += data.substring(0, idx);
867 Logger.log(
"Device", 1, String.format(
"STM:%s", m_command));
871 data = data.substring(idx + 1);
878 public void start() throws InterruptedException {
879 Logger.log(
"Device", 1,
"Start receiver");
883 if (m_serialConnection !=
null) {
884 m_serialConnection.open((d) -> {
889 }
catch (Exception ex) {
890 System.out.println(
" ==>> SERIAL SETUP FAILED : " + ex.getMessage());
899 new Thread(
new Runnable() {
904 sendCommandToAll(TwoWire.k_cmdKeepAlive);
908 }
catch (InterruptedException ex) {
918 public void sendCommandToAll(
int command) {
919 m_twoWire.sendCommandToAll(command);
922 public void sendCommand(
int addr,
int command,
byte[] bytes) {
923 m_twoWire.sendCommand(addr, command, bytes);
926 public void sendCommand(
int addr,
int command,
byte byte1,
byte byte2,
byte byte3,
byte byte4) {
927 m_twoWire.sendCommand(addr, command, byte1, byte2, byte3, byte4);
930 public void sendCommand(
int addr,
int command,
byte byte1,
short value) {
931 m_twoWire.sendCommand(addr, command, byte1, value);
934 public void sendCommand(
int addr,
int command,
byte byte1,
int value) {
935 m_twoWire.sendCommand(addr, command, byte1, value);
938 public void sendCommand(
int addr,
int command,
byte byte1,
float value) {
939 m_twoWire.sendCommand(addr, command, byte1, value);
942 public void sendCommand(
int addr,
int command,
byte byte1,
byte byte2) {
943 m_twoWire.sendCommand(addr, command, byte1, byte2);
946 public void sendCommand(
int addr,
int command,
int yaw,
int x,
int y) {
947 m_twoWire.sendCommand(addr, command, yaw, x, y);
950 public void sendCommand(
int addr,
int command,
byte byte1) {
951 m_twoWire.sendCommand(addr, command, byte1);
954 public void sendCommand(
int addr,
int command) {
955 m_twoWire.sendCommand(addr, command);