Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e580a10
added rudder mixing
JJKSweaty May 28, 2025
d9ce32b
software trim added
William-Lu-Jia-Rong Jun 1, 2025
a244319
Make sure the precent of motor do not go below 0
William-Lu-Jia-Rong Jun 1, 2025
7e54198
Add offset value to each motor
William-Lu-Jia-Rong Jun 1, 2025
615f11c
Added PID header & cpp files (framework)
EemanAleem Jun 1, 2025
ef37963
set to private member
William-Lu-Jia-Rong Jun 5, 2025
be21d7f
Update attitude_manager.cpp
William-Lu-Jia-Rong Jun 5, 2025
d301507
formating
JJKSweaty Jun 8, 2025
e86bbcf
Merge branch 'feature/am/rudder-mixing' of https://github.com/UWARG/e…
JJKSweaty Jun 8, 2025
b908ea9
Added pitch controls to runControls func in PID_mapping hpp & cpp
EemanAleem Jun 30, 2025
0247f5d
PID start
JJKSweaty Jul 5, 2025
dfc7ed4
Cleaned up code
EemanAleem Jul 6, 2025
9e46757
Fixed include
EemanAleem Jul 6, 2025
aa0e7c0
Added separate pid_mapping files
EemanAleem Jul 6, 2025
d718ecb
Updated PID
JJKSweaty Jul 6, 2025
aa1bfc5
Correct pid_control_effort calcs
EemanAleem Jul 7, 2025
ea2ad68
Update pid_mapping.cpp
EemanAleem Jul 10, 2025
3df16bc
Implemented lint check format stuff
EemanAleem Jul 10, 2025
c16d724
Update attitude_manager.cpp
EemanAleem Jul 10, 2025
47001d7
Lint format + rollPID & pitchPID object functionality fixed
EemanAleem Jul 10, 2025
9821b58
More rollPID & pitchPID adjustments
EemanAleem Jul 11, 2025
bd2ac49
remove rudder mixing branch changes
avancruy Jul 18, 2025
1f22ab0
Removed pidOut and pidControlEffort as class members
EemanAleem Jul 21, 2025
1202218
Merge branch 'feature/am/PID' of https://github.com/UWARG/efs-zeropil…
EemanAleem Jul 21, 2025
2b97a46
Review changes + sample time addition to pidIntegral calc.
EemanAleem Sep 20, 2025
2058aa0
Fixed noexcept placement in fbwa_mapping and pid cpp files
EemanAleem Sep 20, 2025
3e8cf21
removed incorrect perecnet logic
JJKSweaty Sep 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stm32l552xx/boardfiles/drivers/motor/motor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ MotorControl::MotorControl(TIM_HandleTypeDef *timer, uint32_t timerChannel, uint
}

void MotorControl::set(uint32_t percent) {
percent = percent > 100 ? 100 : percent;
percent =(percent > 100 ? 100 : percent);
uint32_t ticks = ((percent / 100.0) * (maxCCR - minCCR)) + minCCR;
__HAL_TIM_SET_COMPARE(timer, timerChannel, ticks);
}
Expand Down
39 changes: 39 additions & 0 deletions zeropilot3.5/include/attitude_manager/fbwa_mapping.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include "flightmode.hpp"
#include "attitude_manager.hpp"
#include "pid.hpp"

class FBWAMapping : public Flightmode {
public:
FBWAMapping() noexcept;

RCMotorControlMessage_t runControl(RCMotorControlMessage_t controlInput) override;

// Setter *roll* for PID consts
void setRollPIDConstants(float newKp, float newKi, float newKd, float newTau) noexcept;

// Setter for *pitch* PID consts
void setPitchPIDConstants(float newKp, float newKi, float newKd, float newTau) noexcept;

// Destructor
~FBWAMapping() noexcept override;

private:

// Roll integral limits ******
inline const static float ROLL_INTEGRAL_MIN_LIM = 0.0f;
inline const static float ROLL_INTEGRAL_MAX_LIM = 0.0f;

// Pitch integral limits ******
inline const static float PITCH_INTEGRAL_MIN_LIM = 0.0f;
inline const static float PITCH_INTEGRAL_MAX_LIM = 0.0f;

// Output limits (for control effort)
inline const static float OUTPUT_MIN = 0.0f;
inline const static float OUTPUT_MAX = 0.0f;

// Roll and Pitch PID class objects
PID rollPID;
PID pitchPID;
};
34 changes: 34 additions & 0 deletions zeropilot3.5/include/attitude_manager/pid.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once


class PID {
public:
// PID object constructor
PID(float kp, float ki, float kd,
float tau, float outputMinLim, float outputMaxLim,
float integralMinLim, float integralMaxLim, float t) noexcept;

// PID object's state var initialized (or reset)
void pidInitState() noexcept;

// For the PID roll & pitch consts -> may choose these to be optimized real-time dep. on optimization alg. chosen
void setConstants(float newKp, float newKi, float newKd, float newTau) noexcept;

// Computes PID for a measurement with its desired setpoint passed in
float pidOutput(float setpoint, float measurement);


private:
// Gains
float kp, ki, kd; // PID constants
float tau; // Derivative low-pass filter constant
float t; // Sample time (set to AM_MAIN_DELAY)

// Output and Integral Limits
float outputMinLim, outputMaxLim; // Output limits
float integralMinLim, integralMaxLim; // integral limits

// State variables
float pidDerivative, pidIntegral;
float prevError, prevMeasurement;
};
2 changes: 1 addition & 1 deletion zeropilot3.5/src/attitude_manager/attitude_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,4 @@ void AttitudeManager::outputToMotor(ControlAxis_e axis, uint8_t percent) {
motor->motorInstance->set(percent);
}
}
}
}
44 changes: 44 additions & 0 deletions zeropilot3.5/src/attitude_manager/fbwa_mapping.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "fbwa_mapping.hpp"

FBWAMapping::FBWAMapping() noexcept :
rollPID(0.0f, 0.0f, 0.0f,
0.0f, OUTPUT_MIN, OUTPUT_MAX,
ROLL_INTEGRAL_MIN_LIM, ROLL_INTEGRAL_MAX_LIM, AM_MAIN_DELAY),
pitchPID(0.0f, 0.0f, 0.0f,
0.0f, OUTPUT_MIN, OUTPUT_MAX,
PITCH_INTEGRAL_MIN_LIM, PITCH_INTEGRAL_MAX_LIM, AM_MAIN_DELAY)
{
rollPID.pidInitState();
pitchPID.pidInitState();
}

FBWAMapping::~FBWAMapping() noexcept {}

// Setter *roll* for PID consts
void FBWAMapping::setRollPIDConstants(float newKp, float newKi, float newKd, float newTau) noexcept {
rollPID.setConstants(newKp, newKi, newKd, newTau);
}

// Setter for *pitch* PID consts
void FBWAMapping::setPitchPIDConstants(float newKp, float newKi, float newKd, float newTau) noexcept {
pitchPID.setConstants(newKp, newKi, newKd, newTau);
}

RCMotorControlMessage_t FBWAMapping::runControl(RCMotorControlMessage_t controlInputs){
float rollSetpoint = controlInputs.roll;
float pitchSetpoint = controlInputs.pitch;

// Get from sensor fusion -- make sure to update include files when we get this ********
float rollMeasured = 50.0;
float pitchMeasured = 50.0;

// Currently, roll & pitch outputs receive absolute roll & pitch angles, not relative to current position.
float rollOutput = rollPID.pidOutput(rollSetpoint, rollMeasured);
float pitchOutput = pitchPID.pidOutput(pitchSetpoint, pitchMeasured);

controlInputs.roll = rollOutput; // setting desired roll angle
controlInputs.pitch = pitchOutput; // setting desired pitch angle

return controlInputs;
}

60 changes: 60 additions & 0 deletions zeropilot3.5/src/attitude_manager/pid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "pid.hpp"

// Constructor
PID::PID(float kp, float ki, float kd,
float tau, float outputMinLim, float outputMaxLim,
float integralMinLim, float integralMaxLim, float t) noexcept :
kp(kp), ki(ki), kd(kd), tau(tau), t(t),
outputMinLim(outputMinLim), outputMaxLim(outputMaxLim),
integralMinLim(integralMinLim), integralMaxLim(integralMaxLim)
{}

// Initialization method - Can be used as resetter
void PID::pidInitState() noexcept {
pidIntegral = 0.0f;
prevError = 0.0f;
pidDerivative = 0.0f;
prevMeasurement = 0.0f;
}

void PID::setConstants(float newKp, float newKi, float newKd, float newTau) noexcept {
kp = newKp;
ki = newKi;
kd = newKd;
tau = newTau;
}

// Update method
float PID::pidOutput(float setpoint, float measurement) {
// Calculate error
float error = setpoint - measurement;

// PID Proportional
float pidProportional = kp * error;

// PID Integral
pidIntegral += (0.5f * ki * t) * (error + prevError);

// Anti-integral windup
if (pidIntegral > integralMaxLim) { pidIntegral = integralMaxLim; }
if (pidIntegral < integralMinLim) { pidIntegral = integralMinLim; }

// PID Derivative with low-pass filter
pidDerivative = ((-1 * 2.0f * kd * (measurement - prevMeasurement)) + ((2 * tau - t) * pidDerivative)) / ((2.0f * tau) + t);

// PID control effort
float pidControlEffort = pidProportional + pidIntegral + pidDerivative;

// Clamp control effort output
if (pidControlEffort > outputMaxLim) { pidControlEffort = outputMaxLim; }
if (pidControlEffort < outputMinLim) { pidControlEffort = outputMinLim; }

// Update previous values
prevError = error;
prevMeasurement = measurement;

// Absolute motor control position
float pidOut = measurement + pidControlEffort; // Directly into motor control

return pidOut; // Must go directly into motor control
}