1
0
mirror of https://gitlab.com/lander-team/lander-cpp.git synced 2025-06-16 15:17:23 +00:00
Lander-Embedded/src/main.cpp
2021-11-23 11:59:58 -07:00

132 lines
2.5 KiB
C++

#if defined(_WIN32) || defined(NATIVE)
#include <cmath>
#include <fstream>
#include <iostream>
#include <stdexcept> // std::runtime_error
#include <stdio.h>
#include <string>
#include <vector>
#elif defined(TEENSY)
#include <Arduino.h>
#endif
#define M_PI 3.14159265359
#include "Vehicle.h"
#include "sim.h"
Vehicle State;
Vehicle PrevState;
#if defined(TEENSY)
int BUILTIN_LED = 13;
unsigned long last, initTime;
#include "teensy.h"
#include <HX711.h>
#include <PWMServo.h>
PWMServo yaw;
PWMServo pitch;
void setup() {
Serial.begin(9600);
delay(5000);
Serial.println("Simulation Countdown:");
for (int i = 0; i < 10; i++) {
Serial.println(10 - i);
delay(1000);
}
initLoadCells(State);
init_Vehicle(State);
yaw.attach(29);
pitch.attach(33);
testGimbal(yaw, pitch);
Serial.println("Servos Tested");
delay(3000);
Serial.println("Simulated Vehicle Initalized");
// Determine when to burn
analogWrite(pin_igniter, 0);
burnStartTimeCalc(State);
Serial.println("Starting Height Calculated");
initFile();
initTime = micros();
}
void loop() {
last = micros();
vehicleDynamics(State, PrevState);
thrustInfo(State);
pidController(State, PrevState);
TVC(State, PrevState);
processTVC(State, yaw, pitch);
write2CSV(State);
// Set previous values for next timestep
PrevState = State;
State.time += State.stepSize;
if ((State.z < 0.0) ) {
analogWrite(pin_igniter, 0);
Serial.println("Run duration:" + String((micros() - initTime) / 1e6) +
" seconds");
printSimResults(State);
closeFile();
delay(3000);
Serial.println("SUCCESS");
Serial.println("Exiting Sim");
teensyAbort();
}
State.stepDuration = micros() - last;
delayMicroseconds((1000.0 * State.stepSize) - State.stepDuration);
}
#endif
#if defined(_WIN32) || defined(NATIVE)
#include "native.h"
outVector stateVector;
int main() {
init_Vehicle(State);
// Determine when to burn
burnStartTimeCalc(State);
do {
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);
}
} while ((State.z > 0.0) || (State.thrustFiring != 2));
return 0;
}
#endif