cps_microros_imu/firmware/lib/pid/pid.cpp

55 lines
1.4 KiB
C++
Raw Normal View History

2023-07-10 09:54:53 +00:00
// Copyright (c) 2021 Juan Miguel Jimeno
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "Arduino.h"
#include "pid.h"
PID::PID(float min_val, float max_val, float kp, float ki, float kd):
min_val_(min_val),
max_val_(max_val),
kp_(kp),
ki_(ki),
kd_(kd)
{
}
double PID::compute(float setpoint, float measured_value)
{
double error;
double pid;
//setpoint is constrained between min and max to prevent pid from having too much error
error = setpoint - measured_value;
integral_ += error;
derivative_ = error - prev_error_;
if(setpoint == 0 && error == 0)
{
integral_ = 0;
derivative_ = 0;
}
pid = (kp_ * error) + (ki_ * integral_) + (kd_ * derivative_);
prev_error_ = error;
return constrain(pid, min_val_, max_val_);
}
void PID::updateConstants(float kp, float ki, float kd)
{
kp_ = kp;
ki_ = ki;
kd_ = kd;
}