diff --git a/include/outVector.h b/include/outVector.h index 9b21427..f76d3ee 100644 --- a/include/outVector.h +++ b/include/outVector.h @@ -33,6 +33,8 @@ struct outVector { std::vector PIDx = std::vector(length, 0.0); std::vector PIDy = std::vector(length, 0.0); + + std::vector thrust = std::vector(length, 0.0); }; #endif \ No newline at end of file diff --git a/include/sim.h b/include/sim.h index a093f77..b0388b6 100644 --- a/include/sim.h +++ b/include/sim.h @@ -10,8 +10,8 @@ void TVC(struct Vehicle &, struct Vehicle &); void vehicleDynamics(struct Vehicle &, struct Vehicle &, int t); void state2vec(struct Vehicle &, struct Vehicle &, struct outVector &, int t); void write2CSV(struct outVector &, struct Vehicle &); -double derivative(double x2, double x1, double dt); -double integral(double x2, double x1, double dt); +double derivative(double current, double previous, double step); +double integral(double currentChange, double prevValue, double dt); double limit(double value, double upr, double lwr); // Any parameters that are constants should be declared here instead of @@ -334,6 +334,8 @@ void state2vec(Vehicle &State, Vehicle &PrevState, outVector &stateVector, stateVector.PIDx[t] = State.PIDx; stateVector.PIDy[t] = State.PIDy; + stateVector.thrust[t] = State.thrust; + // Set "prev" values for next timestep PrevState = State; } @@ -354,40 +356,42 @@ void write2CSV(outVector &stateVector, Vehicle &State) { // debugging outfile << "t, x, y, z, vx, vy, vz, ax, ay, az, yaw, pitch, roll, yawdot, " "pitchdot, rolldot, Servo1, Servo2, thrustFiring, PIDx, PIDy, " - "thrust, deriv" + "thrust" << std::endl; // writing to output file for (int t = 0; t < State.simTime; t += State.stepSize) { outfile << t << ", "; - outfile << stateVector.x[t] << ", "; - outfile << stateVector.y[t] << ", "; - outfile << stateVector.z[t] << ", "; + outfile << stateVector.x[t] << ","; + outfile << stateVector.y[t] << ","; + outfile << stateVector.z[t] << ","; - outfile << stateVector.vx[t] << ", "; - outfile << stateVector.vy[t] << ", "; - outfile << stateVector.vz[t] << ", "; + outfile << stateVector.vx[t] << ","; + outfile << stateVector.vy[t] << ","; + outfile << stateVector.vz[t] << ","; - outfile << stateVector.ax[t] << ", "; - outfile << stateVector.ay[t] << ", "; - outfile << stateVector.az[t] << ", "; + outfile << stateVector.ax[t] << ","; + outfile << stateVector.ay[t] << ","; + outfile << stateVector.az[t] << ","; - outfile << stateVector.yaw[t] * 180 / M_PI << ", "; - outfile << stateVector.pitch[t] * 180 / M_PI << ", "; - outfile << stateVector.roll[t] * 180 / M_PI << ", "; + outfile << stateVector.yaw[t] * 180 / M_PI << ","; + outfile << stateVector.pitch[t] * 180 / M_PI << ","; + outfile << stateVector.roll[t] * 180 / M_PI << ","; - outfile << stateVector.yawdot[t] * 180 / M_PI << ", "; - outfile << stateVector.pitchdot[t] * 180 / M_PI << ", "; - outfile << stateVector.rolldot[t] * 180 / M_PI << ", "; + outfile << stateVector.yawdot[t] * 180 / M_PI << ","; + outfile << stateVector.pitchdot[t] * 180 / M_PI << ","; + outfile << stateVector.rolldot[t] * 180 / M_PI << ","; - outfile << stateVector.servo1[t] << ", "; - outfile << stateVector.servo2[t] << ", "; + outfile << stateVector.servo1[t] << ","; + outfile << stateVector.servo2[t] << ","; - outfile << stateVector.thrustFiring[t] << ", "; + outfile << stateVector.thrustFiring[t] << ","; - outfile << stateVector.PIDx[t] << ", "; - outfile << stateVector.PIDy[t] << std::endl; + outfile << stateVector.PIDx[t] << ","; + outfile << stateVector.PIDy[t] << ","; + + outfile << stateVector.thrust[t] << std::endl; } outfile.close();