Skip to content

Commit f6e0fdb

Browse files
Added IR Receiver sensor + tweaks
1 parent 7545a68 commit f6e0fdb

9 files changed

Lines changed: 236 additions & 20 deletions

File tree

Software/Java/src/com/dexterind/gopigo/Gopigo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ public final class Gopigo {
8282
* The ultrasonic sensor.
8383
*/
8484
public UltraSonicSensor ultraSonicSensor;
85+
/**
86+
* The IR Receiver sensor.
87+
*/
88+
public IRReceiverSensor irReceiverSensor;
8589
/**
8690
* The left led.
8791
*/
@@ -123,6 +127,7 @@ public Gopigo() {
123127
encoders = Encoders.getInstance();
124128
servo = Servo.getInstance();
125129
ultraSonicSensor = UltraSonicSensor.getInstance();
130+
irReceiverSensor = IRReceiverSensor.getInstance();
126131
ledLeft = new Led(Led.LEFT);
127132
ledRight = new Led(Led.RIGHT);
128133
motorLeft = new Motor(Motor.LEFT);

Software/Java/src/com/dexterind/gopigo/components/Board.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.dexterind.gopigo.*;
2929
import com.dexterind.gopigo.events.StatusEvent;
3030
import com.dexterind.gopigo.utils.*;
31-
3231
import com.pi4j.io.i2c.I2CBus;
3332
import com.pi4j.io.i2c.I2CDevice;
3433
import com.pi4j.io.i2c.I2CFactory;
@@ -153,7 +152,12 @@ public int digitalWrite(int pin, int value) throws IOException {
153152
if (Gopigo.getInstance().isHalt()) {
154153
Gopigo.getInstance().onHalt();
155154
}
156-
return writeI2c(Commands.DIGITAL_WRITE, pin, value, Commands.UNUSED);
155+
// if (pin == 10 || pin == 0 || pin == 1 || pin == 5 || pin == 16 || pin == 17)
156+
if (value == 0 || value == 1) {
157+
return writeI2c(Commands.DIGITAL_WRITE, pin, value, Commands.UNUSED);
158+
} else {
159+
return Statuses.ERROR;
160+
}
157161
}
158162

159163
/**
@@ -166,18 +170,18 @@ public int analogRead(int pin) throws IOException {
166170
if (Gopigo.getInstance().isHalt()) {
167171
Gopigo.getInstance().onHalt();
168172
}
169-
if (pin == 1) {
173+
//if (pin == 1) {
170174
writeI2c(Commands.ANALOG_READ, pin, Commands.UNUSED, Commands.UNUSED);
171-
sleep(100);
175+
sleep(50);
172176
byte[] b1 = readI2c(1);
173-
byte[] b2 = readI2c(2);
177+
byte[] b2 = readI2c(1); // Note: before was 2
174178
int val1 = (int)b1[0] & 0xFF;
175179
int val2 = (int)b2[0] & 0xFF;
176180

177181
return val1 * 256 + val2;
178-
} else {
182+
/*} else {
179183
return -2;
180-
}
184+
}*/
181185
}
182186

183187
/**
@@ -211,6 +215,7 @@ public int setPinMode(int pin, int pinMode) throws IOException {
211215
if (Gopigo.getInstance().isHalt()) {
212216
Gopigo.getInstance().onHalt();
213217
}
218+
// if (pin == 10 || pin == 15 || pin == 0 || pin == 1)
214219
return writeI2c(Commands.PIN_MODE, pin, pinMode, Commands.UNUSED);
215220
}
216221

@@ -279,6 +284,28 @@ public float version() throws IOException {
279284

280285
return (float)ver[0] / 10;
281286
}
287+
288+
/**
289+
* Returns the board revision.
290+
* @return The board revision.
291+
* @throws IOException
292+
*/
293+
public int revision() throws IOException {
294+
writeI2c(Commands.ANALOG_READ, 7, Commands.UNUSED, Commands.UNUSED);
295+
sleep(100);
296+
297+
byte[] b1 = readI2c(1);
298+
byte[] b2 = readI2c(1);
299+
int val1 = (int)b1[0] & 0xFF;
300+
int val2 = (int)b2[0] & 0xFF;
301+
302+
if (val1 != -1 && val2 != -1) {
303+
int v = val1 * 256 + val2;
304+
return v;
305+
} else {
306+
return Statuses.ERROR;
307+
}
308+
}
282309

283310
/**
284311
* Reads the current status.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* #%L
3+
* **********************************************************************
4+
* ORGANIZATION : DexterIndustries
5+
* PROJECT : GoPiGo Java Library
6+
* FILENAME : Gopigo.java
7+
* AUTHOR : Marcello Barile <marcello.barile@gmail.com>
8+
*
9+
* This file is part of the GoPiGo Java Library project. More information about
10+
* this project can be found here: https://github.com/DexterInd/GoPiGo
11+
* **********************************************************************
12+
* %%
13+
* This project is open source. These files have been made available
14+
* online through a Creative Commons Attribution-ShareAlike 3.0 license.
15+
*
16+
* http://creativecommons.org/licenses/by-sa/3.0/
17+
*
18+
* #L%
19+
*/
20+
package com.dexterind.gopigo.components;
21+
22+
import java.io.IOException;
23+
import java.util.Arrays;
24+
25+
import com.dexterind.gopigo.utils.*;
26+
27+
/**
28+
* It get access to the IR receiver sensor and receive IR signals
29+
* @author marcello
30+
*
31+
*/
32+
public class IRReceiverSensor {
33+
/**
34+
* The main object which handles the methods to get access to the resources
35+
* connected to the board.
36+
*/
37+
private static Board board;
38+
/**
39+
* The instance of the current object.
40+
*/
41+
private static IRReceiverSensor instance = null;
42+
/**
43+
* The current pin. It can be configured through the setPin() method.
44+
*/
45+
private int pin = 0;
46+
/**
47+
* The debug object.
48+
*/
49+
private Debug debug;
50+
51+
public IRReceiverSensor() throws IOException, InterruptedException {
52+
board = Board.getInstance();
53+
}
54+
55+
/**
56+
* Provides a global point of access to the UltraSonicSensor instance.
57+
* @return the <code>UltraSonicSensor</code> instance.
58+
*/
59+
public static IRReceiverSensor getInstance() throws IOException, InterruptedException {
60+
if(instance == null) {
61+
instance = new IRReceiverSensor();
62+
}
63+
return instance;
64+
}
65+
66+
/**
67+
* Sets the pin to use for the sensor.
68+
* @param pin The number of the pin where the sensor is attached. The given value is increased by one.
69+
*/
70+
public void setPin(int pin) {
71+
this.pin = pin + 1;
72+
}
73+
74+
/**
75+
* Reads the IR signal.
76+
* @return The bytes coming from the sensor.
77+
* @throws IOException
78+
*/
79+
public byte[] read() throws IOException {
80+
write(Commands.UNUSED);
81+
board.writeI2c(Commands.IR_READ, Commands.UNUSED, Commands.UNUSED, Commands.UNUSED);
82+
board.sleep(100);
83+
byte[] b = board.readI2c(22);
84+
int b1 = (int)b[1] & 0xFF;
85+
if (b1 != 255) {
86+
return Arrays.copyOfRange(b, 1, b.length);
87+
} else {
88+
return null;
89+
}
90+
}
91+
/**
92+
* Prepare the IR receiver
93+
* @return True or False.
94+
* @throws IOException
95+
*/
96+
public int write(int value) throws IOException {
97+
return board.writeI2c(Commands.IR_RECV_PIN, value, Commands.UNUSED, Commands.UNUSED);
98+
}
99+
}

Software/Java/src/com/dexterind/gopigo/components/Led.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,19 @@ public class Led {
3232
/**
3333
* The left pin code.
3434
*/
35-
private static final int LEFT_PIN = 10;
35+
private static final int LEFT_PIN_A = 10;
3636
/**
3737
* The right pin code.
3838
*/
39-
private static final int RIGHT_PIN = 5;
39+
private static final int RIGHT_PIN_A = 5;
40+
/**
41+
* The left pin code.
42+
*/
43+
private static final int LEFT_PIN_B = 17;
44+
/**
45+
* The right pin code.
46+
*/
47+
private static final int RIGHT_PIN_B = 16;
4048
/**
4149
* The left pin ID.
4250
*/
@@ -60,8 +68,18 @@ public class Led {
6068
private Debug debug;
6169

6270
public Led(int id) throws IOException, InterruptedException {
63-
pin = id == LEFT ? LEFT_PIN : RIGHT_PIN;
6471
board = Board.getInstance();
72+
int vol = board.analogRead(7);
73+
int left_pin = 0;
74+
int right_pin = 0;
75+
if (vol > 700) {
76+
left_pin = LEFT_PIN_B;
77+
right_pin = RIGHT_PIN_B;
78+
} else {
79+
left_pin = LEFT_PIN_A;
80+
right_pin = RIGHT_PIN_A;
81+
}
82+
pin = id == LEFT ? left_pin : right_pin;
6583
}
6684

6785
/**
@@ -70,6 +88,7 @@ public Led(int id) throws IOException, InterruptedException {
7088
* @throws IOException
7189
*/
7290
public int on() throws IOException {
91+
board.setPinMode(pin, 1);
7392
return board.digitalWrite(pin, 1);
7493
}
7594

@@ -79,6 +98,7 @@ public int on() throws IOException {
7998
* @throws IOException
8099
*/
81100
public int off() throws IOException {
101+
board.setPinMode(pin, 1);
82102
return board.digitalWrite(pin, 0);
83103
}
84104
}

Software/Java/src/com/dexterind/gopigo/utils/Commands.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,14 @@ public class Commands {
172172
* Set up the pin mode on a port.
173173
*/
174174
public static final int PIN_MODE = 16;
175-
175+
/**
176+
* InfraRed receiver read.
177+
*/
178+
public static final int IR_READ = 21;
179+
/**
180+
* InfraRed set pin.
181+
*/
182+
public static final int IR_RECV_PIN = 22;
176183
/**
177184
* This allows us to be more specific about which commands contain unused ints
178185
*/

Software/Java/test/tests/GopigoCommanderTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@
2020
package tests;
2121

2222
import com.dexterind.gopigo.*;
23-
import com.dexterind.gopigo.components.Motor;
24-
import com.dexterind.gopigo.components.UltraSonicSensor;
23+
import com.dexterind.gopigo.components.*;
2524
import com.dexterind.gopigo.events.*;
2625
import com.dexterind.gopigo.utils.Statuses;
2726

2827
import java.io.BufferedReader;
2928
import java.io.IOException;
3029
import java.io.InputStreamReader;
30+
import java.util.Arrays;
3131

3232
public class GopigoCommanderTest implements GopigoListener {
3333
private static int ultrasonicPin = 15;
34+
//private static int irReceiverPin = 8;
3435

3536
private static Gopigo gopigo = null;
3637

@@ -45,6 +46,7 @@ public GopigoCommanderTest() throws IOException, InterruptedException {
4546
gopigo.addListener(this);
4647

4748
gopigo.ultraSonicSensor.setPin(ultrasonicPin);
49+
//gopigo.irReceiverSensor.setPin(irReceiverPin);
4850
gopigo.setMinVoltage(5.5);
4951
gopigo.init();
5052
}
@@ -117,7 +119,9 @@ private static void askForCommand() throws InterruptedException {
117119
System.out.println("rotate right => rotates the GoPiGo to the right");
118120
System.out.println("set encoder targeting => sets the encoder targeting");
119121
System.out.println("firmware version => returns the firmware version");
122+
System.out.println("board revision => returns the board reversion");
120123
System.out.println("exit => exits from this test");
124+
System.out.println("ir receive => returns the data from the IR receiver");
121125
System.out.println("");
122126
}
123127
if (command.equals("reset")) {
@@ -230,6 +234,14 @@ private static void askForCommand() throws InterruptedException {
230234
outputMessage = "Firmware version";
231235
outputValue = Float.toString(gopigo.board.version());
232236
}
237+
if (command.equals("board revision") || command.equals("f")) {
238+
outputMessage = "Board revision";
239+
outputValue = Integer.toString(gopigo.board.revision());
240+
}
241+
if (command.equals("ir receive") || command.equals("u")) {
242+
outputMessage = "IR Receiver data::";
243+
outputValue = Arrays.toString(IRReceiverSensor.getInstance().read());
244+
}
233245

234246
if (outputMessage != null && outputValue != null) {
235247
System.out.println(outputMessage + "::" + outputValue);

0 commit comments

Comments
 (0)