mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
Remove PIDControllerRunner and mutex from new PIDController (#1795)
Teams that wish to use it asynchronously may still do so - they simply need to handle the thread safety themselves (it is not that difficult, and can be done more cleanly in the calling code anyway).
This commit is contained in:
@@ -10,8 +10,6 @@
|
||||
#include <functional>
|
||||
#include <limits>
|
||||
|
||||
#include <wpi/mutex.h>
|
||||
|
||||
#include "frc/smartdashboard/SendableBase.h"
|
||||
|
||||
namespace frc2 {
|
||||
@@ -36,8 +34,10 @@ class PIDController : public frc::SendableBase {
|
||||
|
||||
~PIDController() override = default;
|
||||
|
||||
PIDController(PIDController&& rhs);
|
||||
PIDController& operator=(PIDController&& rhs);
|
||||
PIDController(const PIDController&) = default;
|
||||
PIDController& operator=(const PIDController&) = default;
|
||||
PIDController(PIDController&&) = default;
|
||||
PIDController& operator=(PIDController&&) = default;
|
||||
|
||||
/**
|
||||
* Sets the PID Controller gain parameters.
|
||||
@@ -233,11 +233,9 @@ class PIDController : public frc::SendableBase {
|
||||
void InitSendable(frc::SendableBuilder& builder) override;
|
||||
|
||||
protected:
|
||||
mutable wpi::mutex m_thisMutex;
|
||||
|
||||
/**
|
||||
* Wraps error around for continuous inputs. The original error is returned if
|
||||
* continuous mode is disabled. This is an unsynchronized function.
|
||||
* continuous mode is disabled.
|
||||
*
|
||||
* @param error The current error of the PID controller.
|
||||
* @return Error for continuous inputs.
|
||||
@@ -293,15 +291,6 @@ class PIDController : public frc::SendableBase {
|
||||
|
||||
double m_setpoint = 0;
|
||||
double m_output = 0;
|
||||
|
||||
/**
|
||||
* Returns the next output of the PID controller.
|
||||
*
|
||||
* Unlike the public functions above, this function doesn't lock the mutex.
|
||||
*
|
||||
* @param measurement The current measurement of the process variable.
|
||||
*/
|
||||
double CalculateUnsafe(double measurement);
|
||||
};
|
||||
|
||||
} // namespace frc2
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2018-2019 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include <wpi/mutex.h>
|
||||
|
||||
#include "frc/Notifier.h"
|
||||
#include "frc/controller/PIDController.h"
|
||||
#include "frc/smartdashboard/SendableBase.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
class PIDControllerRunner : SendableBase {
|
||||
public:
|
||||
/**
|
||||
* Allocates a PIDControllerRunner.
|
||||
*
|
||||
* @param controller The controller on which to call Update().
|
||||
* @param controllerOutput The function which updates the plant using the
|
||||
* controller output passed as the argument.
|
||||
*/
|
||||
PIDControllerRunner(frc2::PIDController& controller,
|
||||
std::function<double(void)> measurementSource,
|
||||
std::function<void(double)> controllerOutput);
|
||||
|
||||
~PIDControllerRunner();
|
||||
|
||||
PIDControllerRunner(PIDControllerRunner&&) = default;
|
||||
PIDControllerRunner& operator=(PIDControllerRunner&&) = default;
|
||||
|
||||
/**
|
||||
* Begins running the controller.
|
||||
*/
|
||||
void Enable();
|
||||
|
||||
/**
|
||||
* Stops running the controller.
|
||||
*
|
||||
* This sets the output to zero before stopping.
|
||||
*/
|
||||
void Disable();
|
||||
|
||||
/**
|
||||
* Returns whether controller is running.
|
||||
*/
|
||||
bool IsEnabled() const;
|
||||
|
||||
void InitSendable(SendableBuilder& builder) override;
|
||||
|
||||
private:
|
||||
frc2::PIDController& m_controller;
|
||||
std::function<double(void)> m_measurementSource;
|
||||
std::function<void(double)> m_controllerOutput;
|
||||
bool m_enabled = false;
|
||||
|
||||
mutable wpi::mutex m_thisMutex;
|
||||
|
||||
// Ensures when Disable() is called, m_controllerOutput() won't run if
|
||||
// Controller::Update() is already running at that time.
|
||||
mutable wpi::mutex m_outputMutex;
|
||||
|
||||
// This is declared after all other member variables so that during
|
||||
// PIDControllerRunner destruction, the Notifier is stopped before any member
|
||||
// variables its callable uses are destructed. This avoids use-after-free
|
||||
// bugs like crashes when locking is attempted on deallocated mutexes.
|
||||
Notifier m_notifier{&PIDControllerRunner::Run, this};
|
||||
|
||||
void Run();
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
Reference in New Issue
Block a user