mirror of
https://gitlab.com/lander-team/lander-cpp.git
synced 2025-06-16 07:06:51 +00:00
117 lines
2.2 KiB
C++
117 lines
2.2 KiB
C++
#define M_PI 3.14159265359
|
|
|
|
#if defined(NATIVE) || defined(_WIN32)
|
|
#include <cmath>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <stdexcept> // std::runtime_error
|
|
#include <stdio.h>
|
|
#include <string>
|
|
#include <vector>
|
|
#elif defined(TEENSY)
|
|
#include <Arduino.h>
|
|
|
|
int BUILTIN_LED = 13;
|
|
unsigned long last;
|
|
#endif
|
|
|
|
#include "Vehicle.h"
|
|
#include "sim.h"
|
|
|
|
#if defined(NATIVE) || defined(_WIN32)
|
|
#include "native.h"
|
|
outVector stateVector;
|
|
#elif defined(TEENSY)
|
|
#include "teensy.h"
|
|
#endif
|
|
|
|
Vehicle State;
|
|
Vehicle PrevState;
|
|
|
|
#if defined(NATIVE) || defined(_WIN32)
|
|
void setup() {
|
|
init_Vehicle(State);
|
|
|
|
// Determine when to burn
|
|
burnStartTimeCalc(State);
|
|
}
|
|
#elif defined(TEENSY)
|
|
void setup() {
|
|
delay(5000);
|
|
init_Vehicle(State);
|
|
Serial.println("Simulated Vehicle Initalized");
|
|
delay(1000);
|
|
|
|
// Determine when to burn
|
|
burnStartTimeCalc(State);
|
|
Serial.println("Starting Height Calculated");
|
|
delay(1000);
|
|
loadCellCalibrate();
|
|
Serial.println("Load Cells Calibrated");
|
|
delay(1000);
|
|
initFile();
|
|
delay(1000);
|
|
}
|
|
#endif
|
|
|
|
#if defined(NATIVE) || defined(_WIN32)
|
|
void loop() {
|
|
vehicleDynamics(State, PrevState);
|
|
thrustInfo(State);
|
|
pidController(State, PrevState);
|
|
TVC(State, PrevState);
|
|
processTVC(State);
|
|
state2vec(State, PrevState, stateVector);
|
|
|
|
// Set "prev" values for next timestep
|
|
PrevState = State;
|
|
State.time += State.stepSize;
|
|
|
|
if ((State.z < 0.0) && (State.thrustFiring == 2)) {
|
|
write2CSV(stateVector, State);
|
|
printSimResults(State);
|
|
init_Vehicle(State);
|
|
}
|
|
}
|
|
#elif defined(TEENSY)
|
|
void loop() {
|
|
|
|
last = millis();
|
|
vehicleDynamics(State, PrevState);
|
|
thrustInfo(State);
|
|
pidController(State, PrevState);
|
|
TVC(State, PrevState);
|
|
processTVC(State);
|
|
write2CSV(State);
|
|
|
|
// Set "prev" values for next timestep
|
|
PrevState = State;
|
|
|
|
State.time += State.stepSize;
|
|
|
|
if ((State.z < 0.0) && (State.thrustFiring == 2)) {
|
|
printSimResults(State);
|
|
Serial.println("Run duration:" + String(millis() - last) + " ms");
|
|
|
|
closeFile();
|
|
delay(20000);
|
|
|
|
Serial.println("SUCCESS");
|
|
Serial.println("Aborting Sim");
|
|
teensyAbort();
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#if defined(_WIN32) || defined(NATIVE)
|
|
int main() {
|
|
|
|
setup();
|
|
|
|
do {
|
|
loop();
|
|
} while ((State.z > 0.0) || (State.thrustFiring != 2));
|
|
|
|
return 0;
|
|
}
|
|
#endif |