Skip to content

Commit e5a823e

Browse files
committed
Update pi4j and address api changes
1 parent 439d12d commit e5a823e

5 files changed

Lines changed: 35 additions & 9 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ dependencies {
2727
compile files("${System.properties['java.home']}/../lib/tools.jar")
2828
compile "com.fasterxml.jackson.module:jackson-module-kotlin:2.8.7"
2929
compile 'com.neuronrobotics:nrjavaserial:3.11.0'
30-
compile 'com.pi4j:pi4j-core:1.0'
30+
compile 'com.pi4j:pi4j-core:1.1'
3131
compile 'commons-cli:commons-cli:1.3.1'
3232
compile 'io.reactivex:rxjava:1.2.7'
3333
compile 'io.reactivex:rxjavafx:1.1.0'

src/main/java/com/easternedgerobotics/rov/io/pololu/MaestroChannel.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import com.easternedgerobotics.rov.math.Range;
66

77
import com.pi4j.io.serial.Serial;
8+
import org.pmw.tinylog.Logger;
89

10+
import java.io.IOException;
911
import java.nio.ByteBuffer;
1012
import java.nio.ByteOrder;
1113
import java.util.BitSet;
@@ -141,7 +143,11 @@ private void setTarget(final short target) {
141143
final byte lsb = (byte) (microseconds & 0x7F);
142144
final byte msb = (byte) ((microseconds >> 7) & 0x7F);
143145

144-
serial.write(new byte[] {START_BYTE, maestro, COMMAND_SET_TARGET, channel, lsb, msb});
146+
try {
147+
serial.write(new byte[] {START_BYTE, maestro, COMMAND_SET_TARGET, channel, lsb, msb});
148+
} catch (final IOException e) {
149+
Logger.error(e);
150+
}
145151
} finally {
146152
lock.unlock();
147153
}
@@ -156,9 +162,12 @@ private short getPosition() {
156162
lock.lock();
157163
try {
158164
serial.write(new byte[] {START_BYTE, maestro, COMMAND_GET_POSITION, channel});
159-
final ByteBuffer response = ByteBuffer.allocate(4).putChar(serial.read()).putChar(serial.read());
165+
final ByteBuffer response = ByteBuffer.allocate(4).put(serial.read(), 0, 4);
160166
return ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).put(
161167
new byte[]{response.get(1), response.get(3)}).getShort(0);
168+
} catch (final IOException e) {
169+
Logger.error(e);
170+
return 0;
162171
} finally {
163172
lock.unlock();
164173
}
@@ -175,9 +184,14 @@ private short getPosition() {
175184
private BitSet getErrors() {
176185
lock.lock();
177186
try {
178-
serial.write(new byte[] {START_BYTE, maestro, COMMAND_GET_ERRORS});
179-
final ByteBuffer response = ByteBuffer.allocate(2).putChar(serial.read());
180-
return BitSet.valueOf(new byte[]{response.get(1), response.get(0)});
187+
try {
188+
serial.write(new byte[] {START_BYTE, maestro, COMMAND_GET_ERRORS});
189+
final ByteBuffer response = ByteBuffer.allocate(2).put(serial.read(), 0, 2);
190+
return BitSet.valueOf(new byte[]{response.get(1), response.get(0)});
191+
} catch (final IOException e) {
192+
Logger.error(e);
193+
return BitSet.valueOf(new byte[]{0});
194+
}
181195
} finally {
182196
lock.unlock();
183197
}

src/main/java/com/easternedgerobotics/rov/io/rpi/RaspberryI2CBus.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import com.pi4j.io.i2c.I2CBus;
66
import com.pi4j.io.i2c.I2CFactory;
7+
import com.pi4j.io.i2c.I2CFactory.UnsupportedBusNumberException;
8+
79
import org.pmw.tinylog.Logger;
810

911
import java.io.IOException;
@@ -40,7 +42,7 @@ public RaspberryI2CBus(final int channel) {
4042
I2CBus bus;
4143
try {
4244
bus = I2CFactory.getInstance(channel);
43-
} catch (final IOException e) {
45+
} catch (final IOException | UnsupportedBusNumberException e) {
4446
bus = null;
4547
}
4648
this.bus = bus;

src/test/java/com/easternedgerobotics/rov/integration/IndividualLightTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import rx.broadcast.BasicOrder;
1616
import rx.broadcast.UdpBroadcast;
1717

18+
import java.io.IOException;
1819
import java.net.DatagramSocket;
1920
import java.net.InetAddress;
2021
import java.net.SocketException;
@@ -26,7 +27,11 @@ private IndividualLightTest() {
2627

2728
}
2829

29-
public static void main(final String[] args) throws InterruptedException, SocketException, UnknownHostException {
30+
public static void main(final String[] args) throws
31+
IOException,
32+
InterruptedException,
33+
SocketException,
34+
UnknownHostException {
3035
final LaunchConfig launchConfig = new MockLaunchConfig();
3136
final byte deviceNumber = Byte.parseByte(args[0]);
3237
final byte channel = Byte.parseByte(args[1]);

src/test/java/com/easternedgerobotics/rov/integration/IndividualMotorTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.pi4j.io.serial.SerialFactory;
1111
import rx.subjects.PublishSubject;
1212

13+
import java.io.IOException;
1314
import java.net.SocketException;
1415
import java.net.UnknownHostException;
1516

@@ -19,7 +20,11 @@ private IndividualMotorTest() {
1920

2021
}
2122

22-
public static void main(final String[] args) throws InterruptedException, SocketException, UnknownHostException {
23+
public static void main(final String[] args) throws
24+
IOException,
25+
InterruptedException,
26+
SocketException,
27+
UnknownHostException {
2328
final byte maestroAddress = 0x01;
2429
final int baud = 115200;
2530
final Serial serial = SerialFactory.createInstance();

0 commit comments

Comments
 (0)