2021-06-14 20:08:11 -07:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
|
|
#include <fmt/core.h>
|
2023-08-28 15:13:34 -07:00
|
|
|
#include <gtest/gtest.h>
|
2021-06-14 20:08:11 -07:00
|
|
|
#include <hal/HAL.h>
|
|
|
|
|
|
|
|
|
|
#include "mockds/MockDS.h"
|
|
|
|
|
|
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
|
|
|
|
|
class TestEnvironment : public testing::Environment {
|
|
|
|
|
bool m_alreadySetUp = false;
|
|
|
|
|
MockDS m_mockDS;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
TestEnvironment() {
|
|
|
|
|
// Only set up once. This allows gtest_repeat to be used to automatically
|
|
|
|
|
// repeat tests.
|
|
|
|
|
if (m_alreadySetUp) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_alreadySetUp = true;
|
|
|
|
|
|
|
|
|
|
if (!HAL_Initialize(500, 0)) {
|
|
|
|
|
fmt::print(stderr, "FATAL ERROR: HAL could not be initialized\n");
|
|
|
|
|
std::exit(-1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_mockDS.Start();
|
|
|
|
|
|
|
|
|
|
// This sets up the network communications library to enable the driver
|
|
|
|
|
// station. After starting network coms, it will loop until the driver
|
|
|
|
|
// station returns that the robot is enabled, to ensure that tests will be
|
|
|
|
|
// able to run on the hardware.
|
|
|
|
|
HAL_ObserveUserProgramStarting();
|
|
|
|
|
|
|
|
|
|
fmt::print("Started coms\n");
|
|
|
|
|
|
|
|
|
|
int enableCounter = 0;
|
|
|
|
|
|
|
|
|
|
auto checkEnabled = []() {
|
|
|
|
|
HAL_ControlWord controlWord;
|
|
|
|
|
std::memset(&controlWord, 0, sizeof(controlWord));
|
|
|
|
|
HAL_GetControlWord(&controlWord);
|
|
|
|
|
return controlWord.enabled && controlWord.dsAttached;
|
|
|
|
|
};
|
[hal, wpilib] New DS thread model and implementation (#3787)
The current DS thread model has some pretty major issues. It makes it difficult to know if all data is from the same remote packet, and if the data changes while the robot loop is running. Additionally, the DS thread is used for a few other things (MotorSafety and State Tracking for EducationalRobot). This also makes sim difficult, as user code has to wait for the thread to know it has new data.
This change completely rethinks how threading works in the driver station model.
First, the DS HAL system receives a new data callback, either from Netcomm or DriverStationSim. Inside the context of this callback, all the low latency data is read and put into a cache. Doing some investigation on the robot side, this is perfectly safe to do, and also ensures a ds packet will not be parsed before we finish reading the current packet data.
After all data is read, the cache is swapped with a 2nd buffer. This buffer just stores the data, none of the HAL DS calls read from this buffer. An event is then fired, stating there is new data ready to go.
Robot code calls HAL_UpdateDSData(). This swaps the 2nd buffer with a 3rd buffer, which always contains the current data. This data will not be updated until HAL_UpdateDSData is called again. Which solves the state problem.
The high level driver station classes have. an updateData() call, which calls HAL_UpdateDSData, and then update button state variables, then data log and update the NT FMS data table (Java also caches across the JNI boundary here, but that could trivially be removed). An extra event provider is provided, allowing other threads to know when this call has been completed.
IterativeRobotBase calls DS.updateData() at the beginning of each loop, and only once per loop. This means all commands will always have the same state.
All of this means there is no longer a DS thread. Everything happens synchronously. This means Sim and testing is easier, as you can just call DriverStationSim.NotifyNewData(), and then DriverStation.UpdateData(), and you can guarantee that all the DriverStation.*** data is up to date.
As for Motor Safety and Educational Robot State Handling, those can all be handled by their own threads. The Educational Thread only needs to run under EducationalRobot, and MotorSafety will only be started if there is a motor safety object enabled.
2022-10-21 22:01:55 -07:00
|
|
|
HAL_RefreshDSData();
|
2021-06-14 20:08:11 -07:00
|
|
|
while (!checkEnabled()) {
|
|
|
|
|
if (enableCounter > 50) {
|
|
|
|
|
// Robot did not enable properly after 5 seconds.
|
|
|
|
|
// Force exit
|
|
|
|
|
fmt::print(stderr, " Failed to enable. Aborting\n");
|
|
|
|
|
std::terminate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(100ms);
|
|
|
|
|
|
|
|
|
|
fmt::print("Waiting for enable: {}\n", enableCounter++);
|
[hal, wpilib] New DS thread model and implementation (#3787)
The current DS thread model has some pretty major issues. It makes it difficult to know if all data is from the same remote packet, and if the data changes while the robot loop is running. Additionally, the DS thread is used for a few other things (MotorSafety and State Tracking for EducationalRobot). This also makes sim difficult, as user code has to wait for the thread to know it has new data.
This change completely rethinks how threading works in the driver station model.
First, the DS HAL system receives a new data callback, either from Netcomm or DriverStationSim. Inside the context of this callback, all the low latency data is read and put into a cache. Doing some investigation on the robot side, this is perfectly safe to do, and also ensures a ds packet will not be parsed before we finish reading the current packet data.
After all data is read, the cache is swapped with a 2nd buffer. This buffer just stores the data, none of the HAL DS calls read from this buffer. An event is then fired, stating there is new data ready to go.
Robot code calls HAL_UpdateDSData(). This swaps the 2nd buffer with a 3rd buffer, which always contains the current data. This data will not be updated until HAL_UpdateDSData is called again. Which solves the state problem.
The high level driver station classes have. an updateData() call, which calls HAL_UpdateDSData, and then update button state variables, then data log and update the NT FMS data table (Java also caches across the JNI boundary here, but that could trivially be removed). An extra event provider is provided, allowing other threads to know when this call has been completed.
IterativeRobotBase calls DS.updateData() at the beginning of each loop, and only once per loop. This means all commands will always have the same state.
All of this means there is no longer a DS thread. Everything happens synchronously. This means Sim and testing is easier, as you can just call DriverStationSim.NotifyNewData(), and then DriverStation.UpdateData(), and you can guarantee that all the DriverStation.*** data is up to date.
As for Motor Safety and Educational Robot State Handling, those can all be handled by their own threads. The Educational Thread only needs to run under EducationalRobot, and MotorSafety will only be started if there is a motor safety object enabled.
2022-10-21 22:01:55 -07:00
|
|
|
HAL_RefreshDSData();
|
2021-06-14 20:08:11 -07:00
|
|
|
}
|
2021-06-19 01:21:07 -07:00
|
|
|
std::this_thread::sleep_for(500ms);
|
2021-06-14 20:08:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~TestEnvironment() override { m_mockDS.Stop(); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
testing::Environment* const environment =
|
|
|
|
|
testing::AddGlobalTestEnvironment(new TestEnvironment);
|