mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
TrajectoryGenerator: Allow replacement of error reporting function (C++) (#2267)
C++ version of #2234.
This commit is contained in:
@@ -9,7 +9,8 @@
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "frc/DriverStation.h"
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
#include "frc/spline/SplineHelper.h"
|
||||
#include "frc/spline/SplineParameterizer.h"
|
||||
#include "frc/trajectory/TrajectoryParameterizer.h"
|
||||
@@ -18,6 +19,14 @@ using namespace frc;
|
||||
|
||||
const Trajectory TrajectoryGenerator::kDoNothingTrajectory(
|
||||
std::vector<Trajectory::State>{Trajectory::State()});
|
||||
std::function<void(const char*)> TrajectoryGenerator::s_errorFunc;
|
||||
|
||||
void TrajectoryGenerator::ReportError(const char* error) {
|
||||
if (s_errorFunc)
|
||||
s_errorFunc(error);
|
||||
else
|
||||
wpi::errs() << "TrajectoryGenerator error: " << error << "\n";
|
||||
}
|
||||
|
||||
Trajectory TrajectoryGenerator::GenerateTrajectory(
|
||||
Spline<3>::ControlVector initial,
|
||||
@@ -40,7 +49,7 @@ Trajectory TrajectoryGenerator::GenerateTrajectory(
|
||||
SplinePointsFromSplines(SplineHelper::CubicSplinesFromControlVectors(
|
||||
initial, interiorWaypoints, end));
|
||||
} catch (SplineParameterizer::MalformedSplineException& e) {
|
||||
DriverStation::ReportError(e.what());
|
||||
ReportError(e.what());
|
||||
return kDoNothingTrajectory;
|
||||
}
|
||||
|
||||
@@ -84,7 +93,7 @@ Trajectory TrajectoryGenerator::GenerateTrajectory(
|
||||
points = SplinePointsFromSplines(
|
||||
SplineHelper::QuinticSplinesFromControlVectors(controlVectors));
|
||||
} catch (SplineParameterizer::MalformedSplineException& e) {
|
||||
DriverStation::ReportError(e.what());
|
||||
ReportError(e.what());
|
||||
return kDoNothingTrajectory;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
|
||||
/* Copyright (c) 2008-2020 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. */
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "frc/WPIErrors.h"
|
||||
#include "frc/livewindow/LiveWindow.h"
|
||||
#include "frc/smartdashboard/SmartDashboard.h"
|
||||
#include "frc/trajectory/TrajectoryGenerator.h"
|
||||
|
||||
typedef void (*SetCameraServerSharedFP)(frc::CameraServerShared* shared);
|
||||
|
||||
@@ -121,6 +122,8 @@ RobotBase::RobotBase() : m_ds(DriverStation::GetInstance()) {
|
||||
m_threadId = std::this_thread::get_id();
|
||||
|
||||
SetupCameraServerShared();
|
||||
TrajectoryGenerator::SetErrorHandler(
|
||||
[](const char* error) { DriverStation::ReportError(error); });
|
||||
|
||||
auto inst = nt::NetworkTableInstance::GetDefault();
|
||||
inst.SetNetworkIdentity("Robot");
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -114,7 +115,19 @@ class TrajectoryGenerator {
|
||||
return splinePoints;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set error reporting function. By default, it is output to stderr.
|
||||
*
|
||||
* @param func Error reporting function.
|
||||
*/
|
||||
static void SetErrorHandler(std::function<void(const char*)> func) {
|
||||
s_errorFunc = std::move(func);
|
||||
}
|
||||
|
||||
private:
|
||||
static void ReportError(const char* error);
|
||||
|
||||
static const Trajectory kDoNothingTrajectory;
|
||||
static std::function<void(const char*)> s_errorFunc;
|
||||
};
|
||||
} // namespace frc
|
||||
|
||||
Reference in New Issue
Block a user