Motion Profiling ve PID Control’ü ProfiledPIDController ile Birleştirme
Not
ProfiledPIDController
sınıfının command-based framework Çerçevesine uygulanmasına ilişkin bir kılavuz için, bkz: Komut Tabanlı Hareket Profilleme ve PID’yi Birleştirme.
Önceki makalede, trapezoidal hareket profili oluşturmak ve kullanmak için `` TrapezoidProfile`` sınıfının nasıl kullanılacağını gördük. Bu makaledeki örnek kod, bir “akıllı” motor kontrol cihazının harici PID kontrol özelliği ile TrapezoidProfile
sınıfını manuel olarak oluşturmayı gösterir.
This combination of functionality (a motion profile for generating setpoints combined with a PID controller for following them) is extremely common. To facilitate this, WPILib comes with a ProfiledPIDController
class (Java, C++) that does most of the work of combining these two functionalities. The API of the ProfiledPIDController
is very similar to that of the PIDController
, allowing users to add motion profiling to a PID-controlled mechanism with very few changes to their code.
ProfiledPIDController sınıfını kullanma
Not
C ++ ‘da, ``ProfiledPIDController``sınıfı, açısal veya doğrusal olabilen mesafe ölçümleri için kullanılan birim türüne göre şablonlanır. İletilen değerler * zorunlu * mesafe birimleriyle tutarlı birimlere sahip olmalıdır, aksi takdirde derleme zamanı hatası atılır. C ++ birimleri hakkında daha fazla bilgi için bkz C ++ Ünite Kitaplığı..
Not
ProfiledPIDController
işlevinin çoğu, `` PIDController`` ile etkin bir şekilde aynıdır. Buna göre, bu makale yalnızca hareket profili oluşturma işlevini barındırmak için büyük ölçüde değiştirilmiş özellikleri kapsayacaktır. Standart PIDController
özellikleri hakkında bilgi için bkz .: ref: docs / software / advanced-controls / controllers / pidcontroller: PID Control in WPILib.
Profilli PIDController Oluşturma
Not
C ++ genellikle iç sınıfların türünü çıkarabilir ve bu nedenle basit bir başlatıcı listesi (sınıf adı olmadan) parametre olarak gönderilebilir. Tam sınıf adı, netlik açısından aşağıdaki örnekte verilmiştir.
Bir ProfiledPIDController
oluşturmak, creating a PIDController oluşturmak ile neredeyse aynıdır. Tek fark, dahili olarak oluşturulan `` TrapezoidProfile ‘’ örneklerine otomatik olarak iletilecek olan bir dizi trapezoidal profile constraints, sağlama ihtiyacıdır:
// Creates a ProfiledPIDController
// Max velocity is 5 meters per second
// Max acceleration is 10 meters per second
ProfiledPIDController controller = new ProfiledPIDController(
kP, kI, kD,
new TrapezoidProfile.Constraints(5, 10));
// Creates a ProfiledPIDController
// Max velocity is 5 meters per second
// Max acceleration is 10 meters per second
frc::ProfiledPIDController<units::meters> controller(
kP, kI, kD,
frc::TrapezoidProfile<units::meters>::Constraints{5_mps, 10_mps_sq});
Hedef ve Ayar Noktası
Standart bir `` PIDController`` ile `` ProfiledPIDController`` arasındaki en büyük fark, kontrol döngüsünün gerçek* ayar noktasının * kullanıcı tarafından doğrudan belirtilmemesidir. Bunun yerine, kullanıcı bir *hedef * konumu veya durumu belirtir ve kontrolör için ayar noktası, mevcut durum ile hedef arasında oluşturulan hareket profilinden otomatik olarak hesaplanır. Dolayısıyla, kullanıcı tarafı arama çoğunlukla aynı görünürken:
// Calculates the output of the PID algorithm based on the sensor reading
// and sends it to a motor
motor.set(controller.calculate(encoder.getDistance(), goal));
// Calculates the output of the PID algorithm based on the sensor reading
// and sends it to a motor
motor.Set(controller.Calculate(encoder.GetDistance(), goal));
Belirtilen hedef
değeri (sıfırdan farklı bir hız isteniyorsa, bir konum değeri veya bir TrapezProfile.State
olabilir), döngünün zorunlu akım *ayar noktası değildir - daha ziyade, oluşturulan profil sona erdiğinde *nihai * ayar noktasıdır.
Ayar Noktasını Alma / Kullanma
ProfiledPIDController
hedefi ayar noktasından farklı olduğundan, genellikle kontrolörün mevcut ayar noktasını sorgulamak istenir (örneğin, kullanılacak değerleri almak için: ref: ileri besleme <docs/software/advanced-controls/controllers/combining-feedforward-feedback:Using Feedforward Components with PID>). Bu, getSetpoint ()
yöntemi ile yapılabilir.
Döndürülen ayar noktası daha sonra aşağıdaki örnekte olduğu gibi kullanılabilir:
double lastSpeed = 0;
double lastTime = Timer.getFPGATimestamp();
// Controls a simple motor's position using a SimpleMotorFeedforward
// and a ProfiledPIDController
public void goToPosition(double goalPosition) {
double pidVal = controller.calculate(encoder.getDistance(), goalPosition);
double acceleration = (controller.getSetpoint().velocity - lastSpeed) / (Timer.getFPGATimestamp() - lastTime);
motor.setVoltage(
pidVal
+ feedforward.calculate(controller.getSetpoint().velocity, acceleration));
lastSpeed = controller.getSetpoint().velocity;
lastTime = Timer.getFPGATimestamp();
}
units::meters_per_second_t lastSpeed = 0_mps;
units::second_t lastTime = frc2::Timer::GetFPGATimestamp();
// Controls a simple motor's position using a SimpleMotorFeedforward
// and a ProfiledPIDController
void GoToPosition(units::meter_t goalPosition) {
auto pidVal = controller.Calculate(units::meter_t{encoder.GetDistance()}, goalPosition);
auto acceleration = (controller.GetSetpoint().velocity - lastSpeed) /
(frc2::Timer::GetFPGATimestamp() - lastTime);
motor.SetVoltage(
pidVal +
feedforward.Calculate(controller.GetSetpoint().velocity, acceleration));
lastSpeed = controller.GetSetpoint().velocity;
lastTime = frc2::Timer::GetFPGATimestamp();
}
Tam Kullanım Örneği
ElevatorProfilePID örnek projesinde ProfiledPIDController
kullanımının daha eksiksiz bir örneği verilmiştir (Java <https://github.com/wpilibsuite/allwpilib/tree/main/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/elevatorprofiledpid> __, C ++ <https://github.com/wpilibsuite/allwpilib/tree/main/wpilibcExamples/src/main/cpp/examples/ElevatorProfiledPID/cpp> __):
5package edu.wpi.first.wpilibj.examples.elevatorprofiledpid;
6
7import edu.wpi.first.math.controller.ProfiledPIDController;
8import edu.wpi.first.math.trajectory.TrapezoidProfile;
9import edu.wpi.first.wpilibj.Encoder;
10import edu.wpi.first.wpilibj.Joystick;
11import edu.wpi.first.wpilibj.TimedRobot;
12import edu.wpi.first.wpilibj.motorcontrol.MotorController;
13import edu.wpi.first.wpilibj.motorcontrol.PWMSparkMax;
14
15public class Robot extends TimedRobot {
16 private static double kDt = 0.02;
17
18 private final Joystick m_joystick = new Joystick(1);
19 private final Encoder m_encoder = new Encoder(1, 2);
20 private final MotorController m_motor = new PWMSparkMax(1);
21
22 // Create a PID controller whose setpoint's change is subject to maximum
23 // velocity and acceleration constraints.
24 private final TrapezoidProfile.Constraints m_constraints =
25 new TrapezoidProfile.Constraints(1.75, 0.75);
26 private final ProfiledPIDController m_controller =
27 new ProfiledPIDController(1.3, 0.0, 0.7, m_constraints, kDt);
28
29 @Override
30 public void robotInit() {
31 m_encoder.setDistancePerPulse(1.0 / 360.0 * 2.0 * Math.PI * 1.5);
32 }
33
34 @Override
35 public void teleopPeriodic() {
36 if (m_joystick.getRawButtonPressed(2)) {
37 m_controller.setGoal(5);
38 } else if (m_joystick.getRawButtonPressed(3)) {
39 m_controller.setGoal(0);
40 }
41
42 // Run controller and update motor output
43 m_motor.set(m_controller.calculate(m_encoder.getDistance()));
44 }
45}
5#include <numbers>
6
7#include <frc/Encoder.h>
8#include <frc/Joystick.h>
9#include <frc/TimedRobot.h>
10#include <frc/controller/ProfiledPIDController.h>
11#include <frc/motorcontrol/PWMSparkMax.h>
12#include <frc/trajectory/TrapezoidProfile.h>
13#include <units/acceleration.h>
14#include <units/length.h>
15#include <units/time.h>
16#include <units/velocity.h>
17
18class Robot : public frc::TimedRobot {
19 public:
20 static constexpr units::second_t kDt = 20_ms;
21
22 Robot() {
23 m_encoder.SetDistancePerPulse(1.0 / 360.0 * 2.0 * std::numbers::pi * 1.5);
24 }
25
26 void TeleopPeriodic() override {
27 if (m_joystick.GetRawButtonPressed(2)) {
28 m_controller.SetGoal(5_m);
29 } else if (m_joystick.GetRawButtonPressed(3)) {
30 m_controller.SetGoal(0_m);
31 }
32
33 // Run controller and update motor output
34 m_motor.Set(
35 m_controller.Calculate(units::meter_t{m_encoder.GetDistance()}));
36 }
37
38 private:
39 frc::Joystick m_joystick{1};
40 frc::Encoder m_encoder{1, 2};
41 frc::PWMSparkMax m_motor{1};
42
43 // Create a PID controller whose setpoint's change is subject to maximum
44 // velocity and acceleration constraints.
45 frc::TrapezoidProfile<units::meters>::Constraints m_constraints{1.75_mps,
46 0.75_mps_sq};
47 frc::ProfiledPIDController<units::meters> m_controller{1.3, 0.0, 0.7,
48 m_constraints, kDt};
49};
50
51#ifndef RUNNING_FRC_TESTS
52int main() {
53 return frc::StartRobot<Robot>();
54}
55#endif