Skip to content

Commit cfa8a42

Browse files
author
Dexter Industries
committed
Merge pull request 13eakers#1 from kenrikv/master
Added ColorSensor
2 parents 359073a + d32b164 commit cfa8a42

File tree

11 files changed

+238
-19
lines changed

11 files changed

+238
-19
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ Java implementation of Raspberry PI/Brick Pi interface
66

77
You can clone a complete Netbeans Project including the required libraries. It uses pi4j. The jar in the project has been modified to support he 500000 baud rate.
88

9-
Most sensors are missing. Except for I2C, color_full and ultrasonic other sensors should work as a RawSensor.
9+
Some sensors are still missing, the Light, Ultrasonic and Touch are there and (minimally) tested. The Color Sensors and RCX Light should all follow the Light Sensor pattern, but I don't have any to test. Color Full and I2C sensors are a little different - again, I don't have those sensors.
1010

11-
Motor is implemented but not yet tested.
11+
Motors are implemented. You can get the instantaneous speed of the motor, as calculated from the encoder values, by means of the "getCurrentSpeed" method of your Motor class. There are problem problems with this, such as if the encoder over/underflows, when changing direction, etc. - create an issue with a good description (and preferably a use-case) The speed also seems to be double the actual speed. No real idea why that is, unless the ticks per rev are 1440 and not 720.
1212

13-
The interfaces/APIs are subject to change at this point.
13+
Take a look at the BrickPiTests.java for usage examples.
14+
Conceptually, it's pretty simple. You create instances of one of the Sensor classes and/or Motors. Associate them with the correct port number on the BrickPi instance. You need to run "setupSensors", after that, it should all just work.
1415

16+
The interfaces/APIs may change, but should be pretty stable.
1517
You can integrate Netbeans so that it will automatically copy the code to the PI following the instructions here:
1618

1719
https://blogs.oracle.com/speakjava/entry/integrating_netbeans_for_raspberry_pi

pom.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.ergotech</groupId>
6+
<artifactId>brickpi</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>BrickPi</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>3.8.1</version>
22+
<scope>test</scope>
23+
</dependency>
24+
<dependency>
25+
<groupId>com.pi4j</groupId>
26+
<artifactId>pi4j-core</artifactId>
27+
<version>0.0.5</version>
28+
</dependency>
29+
</dependencies>
30+
</project>

src/com/ergotech/brickpi/BrickPi.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,15 +312,16 @@ public void setSensor(Sensor sensor, int port) {
312312
* return null. If a sensor has not previously been attached to the port, a
313313
* RawSensor will be created, attached and returned.
314314
*
315+
* @param <T> the sensor associated with the port
315316
* @param port the port associated with the requested sensor.
316317
* @return a valid Sensor object. If no sensor is current associated with
317318
* the port a RawSensor will be returned.
318319
*/
319-
public Sensor getSensor(int port) {
320+
public <T extends Sensor> T getSensor(int port) {
320321
if (sensorType[port] == null) {
321322
sensorType[port] = new RawSensor();
322323
}
323-
return sensorType[port];
324+
return (T)sensorType[port];
324325
}
325326

326327
/**
@@ -499,7 +500,7 @@ public void updateValues() throws IOException {
499500
Sensor currentSensor = sensorType[counter * 2 + sensorCount];
500501
if (currentSensor != null) {
501502
// request that each sensor encode itself into the packet.
502-
currentSensor.decodeValues(values, startingBitLocation);
503+
startingBitLocation= currentSensor.decodeValues(values, startingBitLocation);
503504
} else {
504505
startingBitLocation += 10; // the default seems to be 10 bits....
505506
}

src/com/ergotech/brickpi/BrickPiTests.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
package com.ergotech.brickpi;
1616

1717
import com.ergotech.brickpi.motion.Motor;
18+
import com.ergotech.brickpi.sensors.RawSensor;
1819
import com.ergotech.brickpi.sensors.TouchSensor;
20+
import com.ergotech.brickpi.sensors.UltraSonicSensor;
21+
import com.ergotech.brickpi.sensors.UltraSonicSensorSS;
1922
import java.io.IOException;
2023
import java.util.logging.Level;
2124
import java.util.logging.Logger;
@@ -34,26 +37,38 @@ public static void main(String[] args) {
3437
Logger.getLogger(BrickPiTests.class.getName()).log(Level.SEVERE, null, ex);
3538
}
3639
// add touch sensors to all the ports.
37-
brickPi.setSensor(new TouchSensor(), 0);
38-
brickPi.setSensor(new TouchSensor(), 1);
39-
brickPi.setSensor(new TouchSensor(), 2);
40+
brickPi.setSensor(new RawSensor(), 0);
41+
brickPi.setSensor(new UltraSonicSensor(),1);
42+
brickPi.setSensor(new RawSensor(), 2);
4043
brickPi.setSensor(new TouchSensor(), 3);
4144
try {
4245
// configure the sensors
4346
brickPi.setupSensors();
4447
} catch (IOException ex) {
4548
Logger.getLogger(BrickPiTests.class.getName()).log(Level.SEVERE, null, ex);
4649
}
47-
System.out.println("Update Values");
50+
51+
for (int counter = 0; counter < 100; counter++) {
52+
System.out.println("Update Values");
53+
try {
54+
// get the updated values.
55+
Thread.sleep(200); // wait for the values to be read....
56+
} catch (InterruptedException ex) {
57+
Logger.getLogger(BrickPiTests.class.getName()).log(Level.SEVERE, null, ex);
58+
}
59+
// here're the values
60+
System.out.println("Sensors: " + brickPi.getSensor(0).getValue() + " " + brickPi.getSensor(1).getValue() + " " + brickPi.getSensor(2).getValue() + " " + brickPi.getSensor(3).getValue());
61+
}
62+
63+
brickPi.setSensor(new UltraSonicSensorSS(), 1);
4864
try {
49-
// get the updated values.
50-
Thread.sleep(200); // wait for the values to be read....
51-
} catch (InterruptedException ex) {
65+
// configure the sensors
66+
brickPi.setupSensors();
67+
} catch (IOException ex) {
5268
Logger.getLogger(BrickPiTests.class.getName()).log(Level.SEVERE, null, ex);
5369
}
54-
// here're the values
55-
System.out.println("Sensors: " + brickPi.getSensor(0).getValue() + " " + brickPi.getSensor(1).getValue() + " " + brickPi.getSensor(2).getValue() + " " + brickPi.getSensor(3).getValue());
5670

71+
System.exit(0);
5772
Motor motor = new Motor();
5873
motor.setCommandedOutput(0);
5974
motor.setEnabled(true);
@@ -62,7 +77,7 @@ public static void main(String[] args) {
6277
motor.setCommandedOutput(25);
6378
for (int counter = 0; counter < 50; counter++) {
6479
try {
65-
System.out.println("Forward Motors: Speed " + brickPi.getMotor(0).getCurrentSpeed() + " encoder " + brickPi.getMotor(0).getCurrentEncoderValue() + " Time " + System.currentTimeMillis() %10000);
80+
System.out.println("Forward Motors: Speed " + brickPi.getMotor(0).getCurrentSpeed() + " encoder " + brickPi.getMotor(0).getCurrentEncoderValue() + " Time " + System.currentTimeMillis() % 10000);
6681
Thread.sleep(200);
6782
} catch (InterruptedException ex) {
6883
// ignore
@@ -72,7 +87,7 @@ public static void main(String[] args) {
7287
for (int counter = 0; counter < 50; counter++) {
7388
try {
7489
Thread.sleep(200);
75-
System.out.println("Reverse Motors: Speed " + brickPi.getMotor(0).getCurrentSpeed() + " encoder " + brickPi.getMotor(0).getCurrentEncoderValue() );
90+
System.out.println("Reverse Motors: Speed " + brickPi.getMotor(0).getCurrentSpeed() + " encoder " + brickPi.getMotor(0).getCurrentEncoderValue());
7691
} catch (InterruptedException ex) {
7792
// ignore
7893
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.ergotech.brickpi.sensors;
7+
8+
import com.ergotech.brickpi.BrickPi;
9+
10+
/**
11+
*
12+
* @author kenrik
13+
*/
14+
public class ColorSensor extends Sensor {
15+
16+
public ColorSensor() {
17+
}
18+
19+
@Override
20+
public byte getSensorType() {
21+
return TYPE_SENSOR_COLOR_FULL;
22+
}
23+
24+
@Override
25+
public int decodeValues(byte[] message, int startLocation) {
26+
value = BrickPi.decodeInt(3, message, startLocation);
27+
return value;
28+
}
29+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright ErgoTech Systems, Inc 2014
3+
*
4+
* This file is made available online through a Creative Commons Attribution-ShareAlike 3.0 license.
5+
* (http://creativecommons.org/licenses/by-sa/3.0/)
6+
*
7+
* This is a library of functions for the RPi to communicate with the BrickPi.
8+
*/
9+
package com.ergotech.brickpi.sensors;
10+
11+
import com.ergotech.brickpi.BrickPi;
12+
13+
/**
14+
* Representation of a Touch Sensor.
15+
*/
16+
public class LightSensorOff extends Sensor {
17+
18+
/**
19+
* Returns an instance of this sensor.
20+
*/
21+
public LightSensorOff() {
22+
23+
}
24+
25+
@Override
26+
public int decodeValues(byte[] message, int startLocation) {
27+
value = BrickPi.decodeInt(10, message, startLocation);
28+
return startLocation + 10;
29+
}
30+
31+
@Override
32+
public byte getSensorType() {
33+
return TYPE_SENSOR_LIGHT_OFF;
34+
}
35+
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright ErgoTech Systems, Inc 2014
3+
*
4+
* This file is made available online through a Creative Commons Attribution-ShareAlike 3.0 license.
5+
* (http://creativecommons.org/licenses/by-sa/3.0/)
6+
*
7+
* This is a library of functions for the RPi to communicate with the BrickPi.
8+
*/
9+
package com.ergotech.brickpi.sensors;
10+
11+
import com.ergotech.brickpi.BrickPi;
12+
13+
/**
14+
* Representation of a Touch Sensor.
15+
*/
16+
public class LightSensorOn extends Sensor {
17+
18+
/**
19+
* Returns an instance of this sensor.
20+
*/
21+
public LightSensorOn() {
22+
23+
}
24+
25+
@Override
26+
public int decodeValues(byte[] message, int startLocation) {
27+
value = BrickPi.decodeInt(10, message, startLocation);
28+
return startLocation + 10;
29+
}
30+
31+
@Override
32+
public byte getSensorType() {
33+
return TYPE_SENSOR_LIGHT_ON;
34+
}
35+
36+
}

src/com/ergotech/brickpi/sensors/RawSensor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
package com.ergotech.brickpi.sensors;
1010

1111
import com.ergotech.brickpi.BrickPi;
12-
import java.util.BitSet;
1312

1413
/**
1514
* Representation of a Touch Sensor.

src/com/ergotech/brickpi/sensors/TouchSensor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
package com.ergotech.brickpi.sensors;
1010

1111
import com.ergotech.brickpi.BrickPi;
12-
import java.util.BitSet;
1312

1413
/**
1514
* Representation of a Touch Sensor.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright ErgoTech Systems, Inc 2014
3+
*
4+
* This file is made available online through a Creative Commons Attribution-ShareAlike 3.0 license.
5+
* (http://creativecommons.org/licenses/by-sa/3.0/)
6+
*
7+
* This is a library of functions for the RPi to communicate with the BrickPi.
8+
*/
9+
package com.ergotech.brickpi.sensors;
10+
11+
import com.ergotech.brickpi.BrickPi;
12+
13+
/**
14+
* Representation of a Touch Sensor.
15+
*/
16+
public class UltraSonicSensor extends Sensor {
17+
18+
/**
19+
* Returns an instance of this sensor.
20+
*/
21+
public UltraSonicSensor() {
22+
23+
}
24+
25+
@Override
26+
public int decodeValues(byte[] message, int startLocation) {
27+
value = BrickPi.decodeInt(8, message, startLocation);
28+
return startLocation + 8;
29+
}
30+
31+
@Override
32+
public byte getSensorType() {
33+
return TYPE_SENSOR_ULTRASONIC_CONT;
34+
}
35+
36+
}

0 commit comments

Comments
 (0)