mirror of
https://gitlab.com/lander-team/lander-cpp.git
synced 2025-06-16 15:17:23 +00:00
Merge branch 'main' of gitlab.com:lander-team/lander-cpp
This commit is contained in:
commit
14d68f852c
3
.vscode/extensions.json
vendored
3
.vscode/extensions.json
vendored
@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"recommendations": [
|
"recommendations": [
|
||||||
"ms-vscode.cpptools",
|
"ms-vscode.cpptools",
|
||||||
"wayou.vscode-todo-highlight"
|
"wayou.vscode-todo-highlight",
|
||||||
|
"usernamehw.errorlens"
|
||||||
]
|
]
|
||||||
}
|
}
|
@ -1,6 +1,8 @@
|
|||||||
#include "Vehicle.h"
|
#include "Vehicle.h"
|
||||||
#include "outVector.h"
|
#include "outVector.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
void burnStartTimeCalc(struct Vehicle &);
|
void burnStartTimeCalc(struct Vehicle &);
|
||||||
void thrustSelection(struct Vehicle &, int t);
|
void thrustSelection(struct Vehicle &, int t);
|
||||||
void pidController(struct Vehicle &, struct Vehicle &);
|
void pidController(struct Vehicle &, struct Vehicle &);
|
||||||
@ -38,19 +40,33 @@ bool sim(struct Vehicle &State, struct Vehicle &PrevState) {
|
|||||||
|
|
||||||
write2CSV(stateVector, State);
|
write2CSV(stateVector, State);
|
||||||
|
|
||||||
bool returnValue;
|
bool pass = 1;
|
||||||
|
|
||||||
if (abs(State.vz) < 5) {
|
double landing_angle =
|
||||||
if ((abs(State.yaw) < 5) && (abs(State.pitch) < 5)) {
|
pow(State.yaw * State.yaw + State.pitch * State.pitch, .5);
|
||||||
returnValue = 1;
|
|
||||||
} else {
|
double landing_velocity =
|
||||||
returnValue = 0;
|
pow(State.vx * State.vx + State.vy * State.vy + State.vz * State.vz, .5);
|
||||||
}
|
|
||||||
|
if (landing_angle < 5.0) {
|
||||||
|
std::cout << " Landing Angle < 5° | PASS | ";
|
||||||
} else {
|
} else {
|
||||||
returnValue = 0;
|
std::cout << " Landing Angle < 5° | FAIL | ";
|
||||||
|
pass = pass * 0;
|
||||||
}
|
}
|
||||||
|
std::cout << "Final Angles: [" << State.yaw << ", " << State.pitch << "]"
|
||||||
|
<< std::endl;
|
||||||
|
|
||||||
return returnValue;
|
if (landing_velocity < 5.0) {
|
||||||
|
std::cout << "Landing Velocity < 5 m/s | PASS | ";
|
||||||
|
} else {
|
||||||
|
std::cout << "Landing Velocity < 5 m/s | FAIL | ";
|
||||||
|
pass = pass * 0;
|
||||||
|
}
|
||||||
|
std::cout << "Final Velocity: [" << State.vx << ", " << State.vy << ", "
|
||||||
|
<< State.vz << "]" << std::endl;
|
||||||
|
|
||||||
|
return pass;
|
||||||
}
|
}
|
||||||
|
|
||||||
void burnStartTimeCalc(Vehicle &State) {
|
void burnStartTimeCalc(Vehicle &State) {
|
||||||
@ -340,8 +356,6 @@ void write2CSV(outVector &stateVector, Vehicle &State) {
|
|||||||
"thrust, deriv"
|
"thrust, deriv"
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
|
||||||
std::cout << "Writing to csv...\n";
|
|
||||||
|
|
||||||
// writing to output file
|
// writing to output file
|
||||||
for (int t = 0; t < State.simTime; t += State.stepSize) {
|
for (int t = 0; t < State.simTime; t += State.stepSize) {
|
||||||
outfile << t << ", ";
|
outfile << t << ", ";
|
||||||
@ -376,7 +390,7 @@ void write2CSV(outVector &stateVector, Vehicle &State) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
outfile.close();
|
outfile.close();
|
||||||
std::cout << "Output File Closed\n";
|
std::cout << "simOut.csv created successfully.\n" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
double derivative(double current, double previous, double step) {
|
double derivative(double current, double previous, double step) {
|
||||||
|
20
input.csv
20
input.csv
@ -1,20 +0,0 @@
|
|||||||
vx,0,m/s
|
|
||||||
vy,0,m/s
|
|
||||||
vz,0,m/s
|
|
||||||
yaw,75,degs
|
|
||||||
pitch,30,degs
|
|
||||||
roll,0,degs
|
|
||||||
yawdot,0,degs/s
|
|
||||||
pitchdot,0,degs/s
|
|
||||||
rolldot,0,degs/s
|
|
||||||
Max Servo Rotation,7,degs
|
|
||||||
Initial Mass,1.2,kg
|
|
||||||
Propellant Mass,0.06,kg
|
|
||||||
Burn Time,3.302,s
|
|
||||||
Vehicle Height,0.5318,m
|
|
||||||
Vehicle Radius,0.05105,m
|
|
||||||
Moment Arm,0.145,m
|
|
||||||
Sim Step Size,1,ms
|
|
||||||
Kp,-6.8699,x
|
|
||||||
Ki,0,x
|
|
||||||
Kd,-0.775,x
|
|
|
@ -51,7 +51,7 @@ title('Altitude vs Time')
|
|||||||
xlabel('Time (s)')
|
xlabel('Time (s)')
|
||||||
ylabel('Altitude (m)')
|
ylabel('Altitude (m)')
|
||||||
ylim([0 z(1)+5])
|
ylim([0 z(1)+5])
|
||||||
%saveas(gcf,'outputs/Accel-Vel-Alt vs Time.png')
|
saveas(gcf,'outputs/Accel-Vel-Alt vs Time.png')
|
||||||
|
|
||||||
figure(2)
|
figure(2)
|
||||||
|
|
||||||
@ -75,7 +75,7 @@ plot(t, rolldot)
|
|||||||
title('Angular Velocity vs Time')
|
title('Angular Velocity vs Time')
|
||||||
xlabel('Time (ms)')
|
xlabel('Time (ms)')
|
||||||
ylabel('Angular Velocity (deg/s)')
|
ylabel('Angular Velocity (deg/s)')
|
||||||
%saveas(gcf,'outputs/Euler Angles vs Time.png')
|
saveas(gcf,'outputs/Euler Angles vs Time.png')
|
||||||
legend("yawdot", "pitchdot", "rolldot")
|
legend("yawdot", "pitchdot", "rolldot")
|
||||||
|
|
||||||
figure(3)
|
figure(3)
|
||||||
@ -93,21 +93,4 @@ plot(t, Servo2)
|
|||||||
title('Servo 2 Position vs Time')
|
title('Servo 2 Position vs Time')
|
||||||
xlabel('Time (ms)')
|
xlabel('Time (ms)')
|
||||||
ylabel('Servo 2 Position (rad)')
|
ylabel('Servo 2 Position (rad)')
|
||||||
%saveas(gcf,'outputs/Servo Position vs Time.png')
|
saveas(gcf,'outputs/Servo Position vs Time.png')
|
||||||
|
|
||||||
figure(4)
|
|
||||||
|
|
||||||
% Servo 1 Position
|
|
||||||
subplot(2, 1, 1)
|
|
||||||
plot(t, PIDx)
|
|
||||||
title('PIDx vs Time')
|
|
||||||
xlabel('Time (ms)')
|
|
||||||
ylabel('PIDx')
|
|
||||||
|
|
||||||
% Servo 2 Position
|
|
||||||
subplot(2, 1, 2)
|
|
||||||
plot(t, PIDy)
|
|
||||||
title('PIDy vs Time')
|
|
||||||
xlabel('Time (ms)')
|
|
||||||
ylabel('PIDy')
|
|
||||||
%saveas(gcf,'outputs/Servo Position vs Time.png')
|
|
||||||
|
75
src/main.cpp
75
src/main.cpp
@ -17,79 +17,46 @@ int main() {
|
|||||||
Vehicle State;
|
Vehicle State;
|
||||||
Vehicle PrevState;
|
Vehicle PrevState;
|
||||||
|
|
||||||
// Create an input filestream
|
|
||||||
std::ifstream inFile("input.csv");
|
|
||||||
|
|
||||||
// Make sure the file is open
|
|
||||||
if (!inFile.is_open())
|
|
||||||
throw std::runtime_error("Could not open file");
|
|
||||||
|
|
||||||
std::vector<double> varValueVec = std::vector<double>(20, 0.0);
|
|
||||||
std::string varName, varValue, varUnits;
|
|
||||||
|
|
||||||
for (int i; i < 20; i++) {
|
|
||||||
std::getline(inFile, varName, ',');
|
|
||||||
std::getline(inFile, varValue, ',');
|
|
||||||
varValueVec[i] = stod(varValue);
|
|
||||||
std::getline(inFile, varUnits);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initial Velocity
|
// Initial Velocity
|
||||||
State.vx = varValueVec[0]; // [m/s]
|
State.vx = 0; // [m/s]
|
||||||
State.vy = varValueVec[1]; // [m/s]
|
State.vy = 0; // [m/s]
|
||||||
State.vz = varValueVec[2]; // [m/s]
|
State.vz = 0; // [m/s]
|
||||||
|
|
||||||
// Initial YPR
|
// Initial YPR
|
||||||
State.yaw = varValueVec[3] * M_PI / 180; // [rad]
|
State.yaw = 10 * M_PI / 180; // [rad]
|
||||||
State.pitch = varValueVec[4] * M_PI / 180; // [rad]
|
State.pitch = 5 * M_PI / 180; // [rad]
|
||||||
State.roll = varValueVec[5] * M_PI / 180; // [rad]
|
State.roll = 0 * M_PI / 180; // [rad]
|
||||||
|
|
||||||
// Initial YPRdot
|
// Initial YPRdot
|
||||||
State.yawdot = varValueVec[6] * M_PI / 180; // [rad/s]
|
State.yawdot = 1 * M_PI / 180; // [rad/s]
|
||||||
State.pitchdot = varValueVec[7] * M_PI / 180; // [rad/s]
|
State.pitchdot = -1 * M_PI / 180; // [rad/s]
|
||||||
State.rolldot = varValueVec[8] * M_PI / 180; // [rad/s]
|
State.rolldot = 0 * M_PI / 180; // [rad/s]
|
||||||
|
|
||||||
// Servo Limitation
|
// Servo Limitation
|
||||||
State.maxServo = varValueVec[9]; // [degs]
|
State.maxServo = 15; // [degs]
|
||||||
|
|
||||||
// Vehicle Properties
|
// Vehicle Properties
|
||||||
State.massInitial = varValueVec[10]; // [kg]
|
State.massInitial = 1.2; // [kg]
|
||||||
State.vehicleHeight = varValueVec[13]; // [m]
|
State.vehicleHeight = 0.5318; // [m]
|
||||||
State.vehicleRadius = varValueVec[14]; // [m]
|
State.vehicleRadius = 0.05105; // [m]
|
||||||
State.momentArm = varValueVec[15]; // [m]
|
State.momentArm = 0.145; // [m]
|
||||||
|
|
||||||
// Sim Step Size
|
// Sim Step Size
|
||||||
State.stepSize = varValueVec[16]; // [ms]
|
State.stepSize = 1; // [ms]
|
||||||
|
|
||||||
// PID Gains
|
|
||||||
State.Kp = varValueVec[17];
|
|
||||||
State.Ki = varValueVec[18];
|
|
||||||
State.Kd = varValueVec[19];
|
|
||||||
|
|
||||||
// Other Properties
|
// Other Properties
|
||||||
State.burntime = varValueVec[12]; // [s]
|
State.massPropellant = 0.06; // [kg]
|
||||||
State.massPropellant = varValueVec[11]; // [kg]
|
|
||||||
State.massBurnout = State.massInitial - State.massPropellant; // [kg]
|
State.massBurnout = State.massInitial - State.massPropellant; // [kg]
|
||||||
|
State.burntime = 3.45 - 0.148; // [s]
|
||||||
State.mdot = State.massPropellant / State.burntime; // [kg/s]
|
State.mdot = State.massPropellant / State.burntime; // [kg/s]
|
||||||
State.mass = State.massInitial; // [kg]
|
State.mass = State.massInitial; // [kg]
|
||||||
State.burnElapsed = 2000; // [s]
|
State.burnElapsed = 2000; // [s]
|
||||||
PrevState.thrust = 0; // [N]
|
PrevState.thrust = 0; // [N]
|
||||||
|
|
||||||
PrevState = State;
|
|
||||||
|
|
||||||
std::cout << "START\n";
|
|
||||||
bool outcome = sim(State, PrevState);
|
bool outcome = sim(State, PrevState);
|
||||||
|
|
||||||
std::cout << "Finished\n";
|
std::cout << std::endl << "Simulation Complete 🚀" << std::endl;
|
||||||
|
// ^^^
|
||||||
if (outcome == 1) {
|
// 50% chance this makes Mattys linux crash
|
||||||
std::cout << "Sim Result = Success!";
|
return 0;
|
||||||
return 0;
|
|
||||||
} else if (outcome == 0) {
|
|
||||||
std::cout << "Sim Result = Failed!";
|
|
||||||
|
|
||||||
// return 1; Until I figure out how to make CI/CD continue even when run
|
|
||||||
// fails.
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user