-
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* updated deps * updated generated pom * Test for UltrasonicSensor
- Loading branch information
Showing
7 changed files
with
90 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 52 additions & 107 deletions
159
src/test/java/org/myrobotlab/service/UltrasonicSensorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,130 +1,75 @@ | ||
package org.myrobotlab.service; | ||
|
||
import org.junit.After; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
import org.junit.runner.JUnitCore; | ||
import org.junit.runner.Result; | ||
import org.myrobotlab.logging.LoggerFactory; | ||
import org.myrobotlab.logging.Logging; | ||
import org.myrobotlab.service.interfaces.PinArrayControl; | ||
import org.myrobotlab.service.interfaces.SerialDevice; | ||
import org.myrobotlab.framework.Message; | ||
import org.myrobotlab.framework.Service; | ||
import org.myrobotlab.framework.TimeoutException; | ||
import org.myrobotlab.test.AbstractTest; | ||
import org.slf4j.Logger; | ||
|
||
//TODO: re-enable this test when we figure out why it fails from the | ||
// command line ant build... | ||
|
||
@Ignore | ||
public class UltrasonicSensorTest extends AbstractTest { | ||
|
||
static PinArrayControl arduino = null; | ||
static TestCatcher catcher = null; | ||
|
||
// VirtualArduino setup | ||
static int echoPin = 7; | ||
public final static Logger log = LoggerFactory.getLogger(UltrasonicSensor.class); | ||
static String port = "COM4"; | ||
|
||
static UltrasonicSensor sensor = null; | ||
static SerialDevice serial = null; | ||
static int trigPin = 8; | ||
static Serial uart = null; | ||
static boolean useVirtualHardware = true; // base class for this and | ||
|
||
static VirtualArduino virtual = null; | ||
|
||
// FIXME - test for re-entrant !!!! | ||
// FIXME - single switch for virtual versus "real" hardware | ||
|
||
static public void main(String[] args) { | ||
|
||
try { | ||
// // LoggingFactory.init(); | ||
// FIXME - base class static method .webGui() & .gui() | ||
// Runtime.start("webgui", "WebGui"); | ||
// Runtime.start("gui", "SwingGui"); | ||
|
||
// test a "real" arduino | ||
useVirtualHardware = true; | ||
|
||
UltrasonicSensorTest test = new UltrasonicSensorTest(); | ||
UltrasonicSensorTest.setUpBeforeClass(); | ||
|
||
// arduino.record(); | ||
|
||
if (virtual != null) { | ||
virtual.connect(port); | ||
} | ||
|
||
test.test(); | ||
|
||
// run junit as java app | ||
JUnitCore junit = new JUnitCore(); | ||
Result result = junit.run(UltrasonicSensorTest.class); | ||
log.info("Result was: {}", result); | ||
|
||
} catch (Exception e) { | ||
Logging.logError(e); | ||
} | ||
} | ||
static MockGateway gateway = null; | ||
static UltrasonicSensor ultra = null; | ||
static Arduino uno = null; | ||
|
||
@BeforeClass | ||
public static void setUpBeforeClass() throws Exception { | ||
// // LoggingFactory.init(Level.INFO); | ||
|
||
log.info("setUpBeforeClass"); | ||
sensor = (UltrasonicSensor) Runtime.start("arduino", "UltrasonicSensor"); | ||
virtual = (VirtualArduino) Runtime.start("virtual", "VirtualArduino"); | ||
if (useVirtualHardware) { | ||
virtual.connect(port); | ||
} | ||
|
||
catcher = (TestCatcher) Runtime.start("catcher", "TestCatcher"); | ||
uart = (Serial) virtual.getSerial(); | ||
// uart.setTimeout(100); // don't want to hang when decoding results... | ||
} | ||
|
||
@AfterClass | ||
public static void tearDownAfterClass() throws Exception { | ||
public static void setupBeforeClass() throws Exception { | ||
ultra = (UltrasonicSensor) Runtime.start("ultra", "UltrasonicSensor"); | ||
gateway = (MockGateway) Runtime.start("gateway", "MockGateway"); | ||
gateway.clear(); | ||
uno = (Arduino) Runtime.start("uno", "Arduino"); | ||
uno.setVirtual(true); | ||
uno.connect("COMX"); | ||
ultra.setTriggerPin(11); | ||
ultra.setEchoPin(10); | ||
ultra.attach("uno"); | ||
|
||
//Runtime.start("webgui", "WebGui"); | ||
} | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
public void setup() { | ||
gateway.clear(); | ||
} | ||
|
||
// TODO - Virtual Serial test - do a record of tx & rx on a real sensor | ||
// then send the data - IT MUST BE INTERLEAVED | ||
@Test | ||
public final void test() throws Exception { | ||
|
||
TestCatcher catcher = (TestCatcher) Runtime.start("catcher", "TestCatcher"); | ||
Arduino mega = (Arduino) Runtime.start("ultrasonic_arduino", "TestCatcher"); | ||
sensor.addRangeListener(catcher); | ||
sensor.attach(mega, trigPin, echoPin); | ||
sensor.startRanging(); | ||
log.info("here"); | ||
sensor.stopRanging(); | ||
|
||
uart.stopRecording(); | ||
|
||
sensor.startRanging(); | ||
log.info("here"); | ||
|
||
sensor.stopRanging(); | ||
|
||
sensor.startRanging(); | ||
public void testUltrasonicSensor() throws TimeoutException { | ||
assertTrue(ultra.isAttached()); | ||
Double range = ultra.ping(); | ||
assertNotNull(range); | ||
ultra.addListener("publishRange", "mocker@mockId"); | ||
ultra.startRanging(); | ||
gateway.waitForMsg("mocker@mockId", "onRange", 100); | ||
ultra.stopRanging(); | ||
Service.sleep(10); | ||
gateway.clear(); | ||
assertNull(gateway.getMsg("mocker@mockId", "onRange")); | ||
|
||
// 1 hz | ||
ultra.setRate(1); | ||
ultra.startRanging(); | ||
Message msg = gateway.waitForMsg("mocker@mockId", "onRange", 1000); | ||
Double r = (Double)msg.data[0]; | ||
assertNotNull(r); | ||
ultra.stopRanging(); | ||
Integer count = gateway.size("mocker@mockId", "onRange"); | ||
assertTrue(count == 0); | ||
} | ||
|
||
sensor.startRanging(); | ||
|
||
sensor.stopRanging(); | ||
@AfterClass | ||
public static void tearDownAfterClass() { | ||
Runtime.release("ultra"); | ||
Runtime.release("gateway"); | ||
Runtime.release("uno"); | ||
} | ||
|
||
} |