Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ultrasonic test #1377

Merged
merged 4 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1655,6 +1655,14 @@
<groupId>io.netty</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>logback-classic</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>logback-core</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/myrobotlab/service/Arduino.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,17 @@ public void attach(String listener, int address) {

@Override
public void attach(UltrasonicSensorControl sensor, Integer triggerPin, Integer echoPin) throws Exception {

if (triggerPin == null) {
error("%s please set trigger pin");
return;
}

if (echoPin == null) {
error("%s please set echo pin");
return;
}

// refer to
// http://myrobotlab.org/content/control-controller-manifesto
if (isAttached(sensor)) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/myrobotlab/service/MockGateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,14 @@ public Message getMsg(String name, String callback) {
}
return null;
}

public Integer size(String name, String callback) {
String fullName = getFullRemoteName(name);

String key = String.format("%s.%s", fullName, callback);
if (!sendQueues.containsKey(key)) {
return null;
}
return sendQueues.get(key).size();
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/myrobotlab/service/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -5165,7 +5165,7 @@ static public void savePlan(String configName) {
private void savePlanInternal(String configName) {

if (configName == null) {
error("cannot save plan config name is null");
info("cannot save plan config name is null");
return;
}

Expand Down
14 changes: 6 additions & 8 deletions src/main/java/org/myrobotlab/service/UltrasonicSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,13 @@ public class UltrasonicSensor extends Service<UltrasonicSensorConfig> implements

private static final long serialVersionUID = 1L;

// probably should do this in a util class
public static int byteArrayToInt(int[] b) {
return b[3] & 0xFF | (b[2] & 0xFF) << 8 | (b[1] & 0xFF) << 16 | (b[0] & 0xFF) << 24;
}

// currently not variable in NewPing.h
// Integer maxDistanceCm = 500;

transient protected UltrasonicSensorController controller;

protected String controllerName;

transient protected BlockingQueue<Double> data = new LinkedBlockingQueue<Double>();

@Deprecated /* use directly from config - should be String */
protected Integer echoPin = null;

protected boolean isAttached = false;
Expand All @@ -74,6 +67,7 @@ public static int byteArrayToInt(int[] b) {

protected long timeout = 500;

@Deprecated /* use directly from config - should be String */
protected Integer trigPin = null;

protected final Set<String> types = new HashSet<String>(Arrays.asList("SR04", "SR05"));
Expand Down Expand Up @@ -222,6 +216,10 @@ public long getPingCount() {
public int getTriggerPin() {
return trigPin;
}

public boolean isAttached() {
return isAttached;
}

@Override
public boolean isAttached(String name) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/myrobotlab/service/meta/WebGuiMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public WebGuiMeta() {
addDependency("org.jmdns", "jmdns", "3.5.5");
addDependency("org.atmosphere", "nettosphere", "3.2.2");
exclude("io.netty", "*"); // it brings in an old version of netty
exclude("logback-classic", "*");
exclude("logback-core", "*");

addDependency("javax.annotation", "javax.annotation-api", "1.3.2");
// force correct version of netty
Expand Down
159 changes: 52 additions & 107 deletions src/test/java/org/myrobotlab/service/UltrasonicSensorTest.java
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");
}

}
Loading