Runs DS enabled loop in process (#785)

Solves mutex issues, and other issues with the existing teststand
software. And simplifies the unit test structure.
This commit is contained in:
Thad House
2017-11-28 19:12:05 -08:00
committed by Peter Johnson
parent 26a36779a6
commit fa0b4428e9
11 changed files with 228 additions and 106 deletions

View File

@@ -0,0 +1,85 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class MockDS {
private Thread m_thread;
private void generateEnabledDsPacket(byte[] data, short sendCount) {
data[0] = (byte) (sendCount >> 8);
data[1] = (byte) sendCount;
data[2] = 0x01; // general data tag
data[3] = 0x04; // teleop enabled
data[4] = 0x10; // normal data request
data[5] = 0x00; // red 1 station
}
@SuppressWarnings("JavadocMethod")
public void start() {
m_thread = new Thread(() -> {
DatagramSocket socket;
try {
socket = new DatagramSocket();
} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return;
}
InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 1110);
byte[] sendData = new byte[6];
DatagramPacket packet = new DatagramPacket(sendData, 0, 6, addr);
short sendCount = 0;
int initCount = 0;
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(20);
generateEnabledDsPacket(sendData, sendCount++);
// ~50 disabled packets are required to make the robot actually enable
// 1 is definitely not enough.
if (initCount < 50) {
initCount++;
sendData[3] = 0;
}
packet.setData(sendData);
socket.send(packet);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
}
socket.close();
});
// Because of the test setup in Java, this thread will not be stopped
// So it must be a daemon thread
m_thread.setDaemon(true);
m_thread.start();
}
@SuppressWarnings("JavadocMethod")
public void stop() {
if (m_thread == null) {
return;
}
m_thread.interrupt();
try {
m_thread.join(1000);
} catch (InterruptedException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
}
}

View File

@@ -17,6 +17,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.MockDS;
import edu.wpi.first.wpilibj.RobotBase;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.hal.HAL;
@@ -34,6 +35,9 @@ public abstract class AbstractComsSetup {
*/
private static boolean initialized = false;
// We have no way to stop the MockDS, so its thread is daemon.
private static MockDS ds;
/**
* This sets up the network communications library to enable the driver
* station. After starting network coms, it will loop until the driver station
@@ -45,6 +49,10 @@ public abstract class AbstractComsSetup {
// Set some implementations so that the static methods work properly
RobotBase.initializeHardwareConfiguration();
HAL.observeUserProgramStarting();
DriverStation.getInstance().getAlliance();
ds = new MockDS();
ds.start();
LiveWindow.setEnabled(false);
TestBench.out().println("Started coms");
@@ -57,8 +65,7 @@ public abstract class AbstractComsSetup {
} catch (InterruptedException ex) {
ex.printStackTrace();
}
// Prints the message on one line overwriting itself each time
TestBench.out().print("\rWaiting for enable: " + enableCounter++);
TestBench.out().println("Waiting for enable: " + enableCounter++);
}
TestBench.out().println();