diff --git a/CMakeLists.txt b/CMakeLists.txt index 05c3f0aa..153dcf28 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,69 +5,73 @@ project(RocketCode2020 C CXX) cmake_host_system_information(RESULT _memfree QUERY AVAILABLE_PHYSICAL_MEMORY) set_property(GLOBAL PROPERTY JOB_POOLS four_jobs=4) if (_memfree LESS 1000) - set(CMAKE_JOB_POOL_COMPILE four_jobs) -endif() + set(CMAKE_JOB_POOL_COMPILE four_jobs) +endif () add_subdirectory(./libraries) +if (DEFINED ENV{DESKTOP_COMPAT} OR DESKTOP_COMPAT) + add_compile_definitions(DESKTOP_COMPAT=$ENV{DESKTOP_COMPAT}) +endif () + if (DEFINED ENV{HOTFIRE_TEST} OR HOTFIRE_TEST) - add_compile_definitions(HOTFIRE_TEST=$ENV{HOTFIRE_TEST}) -endif() + add_compile_definitions(HOTFIRE_TEST=$ENV{HOTFIRE_TEST}) +endif () if (DEFINED ENV{USE_LOGGER} OR USE_LOGGER) - add_compile_definitions(USE_LOGGER=$ENV{USE_LOGGER}) -endif() + add_compile_definitions(USE_LOGGER=$ENV{USE_LOGGER}) +endif () if (DEFINED ENV{USE_SOCKET_CLIENT} OR USE_SOCKET_CLIENT) - add_compile_definitions(USE_SOCKET_CLIENT=$ENV{USE_SOCKET_CLIENT}) -endif() + add_compile_definitions(USE_SOCKET_CLIENT=$ENV{USE_SOCKET_CLIENT}) +endif () if (DEFINED ENV{USE_INPUT} OR USE_INPUT) - add_compile_definitions(USE_INPUT=$ENV{USE_INPUT}) -endif() + add_compile_definitions(USE_INPUT=$ENV{USE_INPUT}) +endif () if (DEFINED ENV{SKIP_INIT} OR SKIP_INIT) - add_compile_definitions(SKIP_INIT=$ENV{SKIP_INIT}) -endif() + add_compile_definitions(SKIP_INIT=$ENV{SKIP_INIT}) +endif () if (DEFINED ENV{TESTING} OR TESTING) - add_compile_definitions(TESTING=$ENV{TESTING}) -endif() + add_compile_definitions(TESTING=$ENV{TESTING}) +endif () # Sensors if (DEFINED ENV{USE_SBG} OR USE_SBG) - add_compile_definitions(USE_SBG=$ENV{USE_SBG}) -endif() + add_compile_definitions(USE_SBG=$ENV{USE_SBG}) +endif () if (DEFINED ENV{USE_RADIO} OR USE_RADIO) - add_compile_definitions(USE_RADIO=$ENV{USE_RADIO}) -endif() + add_compile_definitions(USE_RADIO=$ENV{USE_RADIO}) +endif () if (DEFINED ENV{USE_WIRING_Pi} OR USE_WIRING_Pi) - add_compile_definitions(USE_WIRING_Pi=$ENV{USE_WIRING_Pi}) -endif() + add_compile_definitions(USE_WIRING_Pi=$ENV{USE_WIRING_Pi}) +endif () if (DEFINED ENV{USE_GPIO} OR USE_GPIO) - add_compile_definitions(USE_GPIO=$ENV{USE_GPIO}) -endif() + add_compile_definitions(USE_GPIO=$ENV{USE_GPIO}) +endif () if (DEFINED ENV{USE_ARDUINO_PROXY} OR USE_ARDUINO_PROXY) - add_compile_definitions(USE_ARDUINO_PROXY=$ENV{USE_ARDUINO_PROXY}) -endif() + add_compile_definitions(USE_ARDUINO_PROXY=$ENV{USE_ARDUINO_PROXY}) +endif () # --------------- # Main executable # --------------- -find_package (Threads) +find_package(Threads) if (DEFINED ENV{SERVO_CONTROL} OR SERVO_CONTROL) - file(GLOB_RECURSE MAIN_CONFIG_SRC ${PROJECT_SOURCE_DIR}/projects/ServoControl/*.cpp) + file(GLOB_RECURSE MAIN_CONFIG_SRC ${PROJECT_SOURCE_DIR}/projects/ServoControl/*.cpp) elseif (DEFINED ENV{HOTFIRE_TEST} OR HOTFIRE_TEST) - file(GLOB_RECURSE MAIN_CONFIG_SRC ${PROJECT_SOURCE_DIR}/projects/HotFire/*.cpp) -else() - file(GLOB_RECURSE MAIN_CONFIG_SRC ${PROJECT_SOURCE_DIR}/projects/OctoberSky1/*.cpp) -endif() + file(GLOB_RECURSE MAIN_CONFIG_SRC ${PROJECT_SOURCE_DIR}/projects/HotFire/*.cpp) +else () + file(GLOB_RECURSE MAIN_CONFIG_SRC ${PROJECT_SOURCE_DIR}/projects/OctoberSky1/*.cpp) +endif () file(GLOB_RECURSE SRC_MAIN ${PROJECT_SOURCE_DIR}/src/*.cpp) @@ -78,21 +82,21 @@ target_include_directories(MainLoopLib PUBLIC ./src/) target_precompile_headers(MainLoopLib PUBLIC ${PROJECT_SOURCE_DIR}/src/common/pch.h) if (DEFINED ENV{SERVO_CONTROL} OR SERVO_CONTROL) - target_include_directories(MainLoopLib PUBLIC - ./projects/ServoControl/ - ) - target_include_directories(MainLoopLib PUBLIC - ./projects/HotFire/ - ) + target_include_directories(MainLoopLib PUBLIC + ./projects/ServoControl/ + ) + target_include_directories(MainLoopLib PUBLIC + ./projects/HotFire/ + ) elseif (DEFINED ENV{HOTFIRE_TEST} OR HOTFIRE_TEST) - target_include_directories(MainLoopLib PUBLIC - ./projects/HotFire/ - ) -else() - target_include_directories(MainLoopLib PUBLIC - ./projects/OctoberSky1/ - ) -endif() + target_include_directories(MainLoopLib PUBLIC + ./projects/HotFire/ + ) +else () + target_include_directories(MainLoopLib PUBLIC + ./projects/OctoberSky1/ + ) +endif () target_link_libraries(MainLoopLib sbgECom) add_dependencies(MainLoopLib sbgECom) @@ -100,21 +104,21 @@ add_dependencies(MainLoopLib sbgECom) target_link_libraries(MainLoopLib WiringPi) add_dependencies(MainLoopLib WiringPi) -if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm") - - target_include_directories(MainLoopLib PUBLIC "$ENV{BOOST_DIRECTORY}") - target_link_directories(MainLoopLib PUBLIC "$ENV{BOOST_DIRECTORY}/stage/lib") - target_link_libraries(MainLoopLib boost_filesystem boost_system) - - target_include_directories(MainLoopLib PUBLIC "$ENV{LIBI2C_DIRECTORY}/include") - target_link_directories(MainLoopLib PUBLIC "$ENV{LIBI2C_DIRECTORY}/lib") - -else() - target_link_libraries(MainLoopLib systemd) - find_package(Boost 1.65.1 COMPONENTS system filesystem REQUIRED) - target_include_directories(MainLoopLib PUBLIC ${Boost_INCLUDE_DIRS}) - target_link_libraries(MainLoopLib ${Boost_LIBRARIES}) -endif() +if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm") + + target_include_directories(MainLoopLib PUBLIC "$ENV{BOOST_DIRECTORY}") + target_link_directories(MainLoopLib PUBLIC "$ENV{BOOST_DIRECTORY}/stage/lib") + target_link_libraries(MainLoopLib boost_filesystem boost_system) + + target_include_directories(MainLoopLib PUBLIC "$ENV{LIBI2C_DIRECTORY}/include") + target_link_directories(MainLoopLib PUBLIC "$ENV{LIBI2C_DIRECTORY}/lib") + +else () + target_link_libraries(MainLoopLib systemd) + find_package(Boost 1.65.1 COMPONENTS system filesystem REQUIRED) + target_include_directories(MainLoopLib PUBLIC ${Boost_INCLUDE_DIRS}) + target_link_libraries(MainLoopLib ${Boost_LIBRARIES}) +endif () target_link_libraries(MainLoopLib i2c) @@ -124,9 +128,9 @@ target_link_libraries(MainLoopLib protobufDef) target_link_libraries(MainLoopLib cobs-c) -target_link_libraries (MainLoopLib ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(MainLoopLib ${CMAKE_THREAD_LIBS_INIT}) -target_link_libraries (MainLoopLib i2c) +target_link_libraries(MainLoopLib i2c) add_executable(MainLoop ./src/init/MainLoop.cpp) target_link_libraries(MainLoop MainLoopLib) diff --git a/projects/HotFire/config.h b/projects/HotFire/config.h index 9cbb68df..c7de4956 100644 --- a/projects/HotFire/config.h +++ b/projects/HotFire/config.h @@ -5,6 +5,7 @@ // clang-format off #define STATEMACHINE HotFireStateMachine +#define INTERFACE HotFireInterface #ifndef USE_SBG #define USE_SBG 0 @@ -57,4 +58,12 @@ #define USE_SENSOR_MAX_31865 1 #endif +// TODO: Remove this when USE_WIRING_Pi is eventually removed +#if DESKTOP_COMPAT == 1 + #ifdef USE_WIRING_Pi + #undef USE_WIRING_Pi + #endif + #define USE_WIRING_Pi 0 +#endif + // clang-format on diff --git a/projects/OctoberSky1/config.h b/projects/OctoberSky1/config.h index ec3654b0..155ec0be 100644 --- a/projects/OctoberSky1/config.h +++ b/projects/OctoberSky1/config.h @@ -3,6 +3,7 @@ // clang-format off #define STATEMACHINE OctoberSkyStateMachine +#define INTERFACE OctoberSkyInterface #ifndef TESTING #define TESTING 0 diff --git a/src/IO/ArduinoProxy.cpp b/src/IO/ArduinoProxy.cpp index 42bf1edc..e259be70 100644 --- a/src/IO/ArduinoProxy.cpp +++ b/src/IO/ArduinoProxy.cpp @@ -1,7 +1,5 @@ #include "ArduinoProxy.h" -#if USE_ARDUINO_PROXY == 1 - #include "IO/ArduinoEncoder.h" #include #include @@ -173,5 +171,3 @@ int ArduinoProxy::getServoState(int pin) return state.first; } } - -#endif \ No newline at end of file diff --git a/src/IO/ArduinoProxy.h b/src/IO/ArduinoProxy.h index df85babf..abda385c 100644 --- a/src/IO/ArduinoProxy.h +++ b/src/IO/ArduinoProxy.h @@ -1,8 +1,6 @@ #pragma once #include "config.h" -#if USE_ARDUINO_PROXY == 1 - #include "IO.h" #include #include @@ -39,5 +37,3 @@ class ArduinoProxy : IO void handleArduinoMessage(const RocketryProto::ArduinoOut &arduinoOut); }; - -#endif \ No newline at end of file diff --git a/src/IO/Input.h b/src/IO/Input.h index 424968d7..b37199d8 100644 --- a/src/IO/Input.h +++ b/src/IO/Input.h @@ -1,5 +1,4 @@ #pragma once -#if USE_INPUT == 1 #include "EventQueue.h" #include "IO.h" @@ -23,5 +22,3 @@ class Input : IO static bool isNumber(const std::string &s); }; - -#endif \ No newline at end of file diff --git a/src/IO/InterfaceImpl.cpp b/src/IO/InterfaceImpl.cpp deleted file mode 100644 index 10605bec..00000000 --- a/src/IO/InterfaceImpl.cpp +++ /dev/null @@ -1,188 +0,0 @@ -#include "config.h" -#if TESTING != 1 - -#include "IO/IO.h" -#include "IO/TestingSensors.h" -#include "InterfaceImpl.h" -#include "common/pch.h" -#include "data/UOSMData.h" -#include -#include -#include -#include - -InterfaceImpl::InterfaceImpl() - : eventQueue() -#if USE_INPUT == 1 - , - input(eventQueue) -#endif -#if USE_SOCKET_CLIENT == 1 - , - client(eventQueue) -#endif -#if USE_RADIO == 1 - , - radio(eventQueue) -#endif -{ - logger = spdlog::default_logger(); -} - -InterfaceImpl::~InterfaceImpl() = default; - -void InterfaceImpl::initialize() -{ - initializeInputs(); - initializeOutputs(); -} - -void InterfaceImpl::initializeInputs() -{ -#if USE_SBG == 1 - SPDLOG_LOGGER_INFO(logger, "Initializing SBG..."); - mySbgSensor.initialize(); -#endif -#if USE_INPUT == 1 - SPDLOG_LOGGER_INFO(logger, "Initializing INPUT..."); - input.initialize(); -#endif -#if USE_SOCKET_CLIENT == 1 - SPDLOG_LOGGER_INFO(logger, "Initializing SOCKET_CLIENT..."); - client.initialize(); -#endif -#if USE_ARDUINO_PROXY == 1 - arduinoProxy = ArduinoProxy::getInstance(); -#endif -#if USE_SENSORS - sensors.initialize(); -#endif -} - -void InterfaceImpl::initializeOutputs() -{ -#if USE_LOGGER == 1 - SPDLOG_LOGGER_INFO(logger, "Initializing SENSOR_LOGGER..."); - sensorLogger.initialize(); -#endif -#if USE_RADIO == 1 - SPDLOG_LOGGER_INFO(logger, "Initializing RADIO..."); - radio.initialize(); -#endif -#if USE_GPIO == 1 - SPDLOG_LOGGER_INFO(logger, "Initializing GPIO..."); - gpio.initialize(); -#endif -#if USE_ARDUINO_PROXY == 1 - SPDLOG_LOGGER_INFO(logger, "Initializing Arduino Proxy..."); - arduinoProxy->initialize(); -#endif -} - -bool InterfaceImpl::updateInputs() -{ - latestState = std::make_shared(); - - latestState->timeStamp = - std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count(); - -#if USE_SBG == 1 - latestState->sbg = mySbgSensor.getData(); -#endif - - latestState->eventNumber = eventQueue.pop(); - -#if USE_LOGGER == 1 - latestState->loggerIsInitialized = sensorLogger.isInitialized(); -#endif - -#if USE_SOCKET_CLIENT == 1 - latestState->clientIsInitialized = client.isInitialized(); - latestState->lastActiveClientTimestamp = client.getLastConnectionTimestamp(); -#endif - -#if USE_SBG == 1 - latestState->sbgIsInitialized = mySbgSensor.isInitialized(); -#endif - -#if USE_INPUT == 1 - latestState->inputIsInitialized = input.isInitialized(); -#endif - -#if USE_RADIO == 1 - latestState->radioIsInitialized = radio.isInitialized(); -#endif - -#if USE_GPIO == 1 - latestState->gpioIsInitialized = gpio.isInitialized(); - latestState->gpioState = gpio.getCurrentState(); -#endif - -#if USE_ARDUINO_PROXY == 1 - latestState->arduinoProxyIsInitialized = arduinoProxy->isInitialized(); -#endif - -#if USE_SENSORS - latestState->sensorState = sensors.getCurrentState(); -#endif - - return true; -} - -bool InterfaceImpl::updateOutputs(std::shared_ptr data) -{ - -#if USE_GPIO == 1 - data->gpioData = gpio.setOutputs(data->gpioData); -#endif - -#if USE_LOGGER == 1 - sensorLogger.enqueueSensorData(*data); -#endif - -#if USE_RADIO == 1 - radio.enqueueSensorData(*data); -#endif - -#if USE_SOCKET_CLIENT == 1 - client.enqueueSensorData(*data); -#endif - - return true; -} - -#if USE_GPIO == 1 -void InterfaceImpl::createNewGpioOutput(std::string name, int pinNbr) -{ - gpio.createNewGpioOutput(name, pinNbr); -} - -void InterfaceImpl::createNewGpioPwmOutput(std::string name, int pinNbr, int safePosition, bool softpwm) -{ - gpio.createNewGpioPwmOutput(name, pinNbr, safePosition, softpwm); -} -#endif - -void InterfaceImpl::calibrateTelemetry() -{ -#if USE_SBG == 1 - mySbgSensor.setZeroBarometricAltitude(); -#endif -} - -std::shared_ptr InterfaceImpl::getLatest() -{ - -#if USE_LOGGER == 1 - latestState->loggerWorking = SensorLogger::working; -#endif // USE_LOGGER - - return latestState; -} - -time_point InterfaceImpl::getCurrentTime() -{ - return std::chrono::steady_clock::now(); -} - -#endif \ No newline at end of file diff --git a/src/IO/Radio.cpp b/src/IO/Radio.cpp index 5dca2fcd..e6abda89 100644 --- a/src/IO/Radio.cpp +++ b/src/IO/Radio.cpp @@ -1,5 +1,4 @@ #include "config.h" -#if USE_RADIO == 1 #include "Radio.h" #include "data/StateData.h" @@ -96,5 +95,3 @@ void Radio::sendData(const StateData ¤tState) const serialPrintf(fd, data.c_str()); } - -#endif // USE_RADIO diff --git a/src/IO/Radio.h b/src/IO/Radio.h index a803e33d..f17bde2f 100644 --- a/src/IO/Radio.h +++ b/src/IO/Radio.h @@ -1,11 +1,9 @@ #pragma once -#include "config.h" -#if USE_RADIO == 1 - #include "../data/StateData.h" #include "EventQueue.h" #include "IO.h" +#include "config.h" #include #include #include @@ -58,5 +56,3 @@ class Radio : public IO InitStatus wiringPiStatus = INIT; } status; }; - -#endif \ No newline at end of file diff --git a/src/IO/SBGSensor.cpp b/src/IO/SBGSensor.cpp index 2c30ffa6..975c6ece 100644 --- a/src/IO/SBGSensor.cpp +++ b/src/IO/SBGSensor.cpp @@ -1,7 +1,5 @@ #include "config.h" -#if USE_SBG == 1 - #include "SBGSensor.h" #include @@ -328,6 +326,4 @@ sbgData SBGSensor::getData() { std::lock_guard lockGuard(mutex); return data; -} - -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/IO/SBGSensor.h b/src/IO/SBGSensor.h index 9d2b15ed..cd7dc4d9 100644 --- a/src/IO/SBGSensor.h +++ b/src/IO/SBGSensor.h @@ -1,15 +1,13 @@ #pragma once #include "config.h" -#if USE_SBG == 1 - -#include "../data/SBGData.h" -#include "IO.h" // sbgECom headers #include #include +#include "../data/SBGData.h" +#include "IO.h" #include class SBGSensor : public IO @@ -32,5 +30,3 @@ class SBGSensor : public IO { } status; }; - -#endif \ No newline at end of file diff --git a/src/IO/SPI.cpp b/src/IO/SPI.cpp index f5ba8c1d..b42e7563 100644 --- a/src/IO/SPI.cpp +++ b/src/IO/SPI.cpp @@ -16,6 +16,7 @@ SPI::SPI(const char *device, uint8_t mode, uint8_t bitsPerWord, uint32_t speed, bool lsbFirst) : device(device), bits(bitsPerWord), speed(speed) { +#if DESKTOP_COMPAT != 1 int ret = 0; fd = open(device, O_RDWR); @@ -83,11 +84,14 @@ SPI::SPI(const char *device, uint8_t mode, uint8_t bitsPerWord, uint32_t speed, { throw SPIError("Can't set max read speed hz to " + std::to_string(speed)); } +#endif } SPI::~SPI() { +#if DESKTOP_COMPAT != 1 close(fd); +#endif } /*! @@ -101,11 +105,13 @@ SPI::~SPI() */ void SPI::write(uint8_t *buffer, size_t len) const { +#if DESKTOP_COMPAT != 1 auto status = ::write(fd, buffer, len); if (status < 0) { throw SPIError("Error writing to SPI device"); } +#endif } /*! @@ -124,6 +130,7 @@ void SPI::write(uint8_t *buffer, size_t len) const void SPI::write_then_read(const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len, uint8_t sendvalue) const { +#if DESKTOP_COMPAT != 1 struct spi_ioc_transfer xfer[2]; memset(xfer, 0, sizeof xfer); @@ -140,4 +147,5 @@ void SPI::write_then_read(const uint8_t *write_buffer, size_t write_len, uint8_t { throw SPIError("Error reading to SPI device"); } +#endif } diff --git a/src/IO/TestingSensors.cpp b/src/IO/TestingSensors.cpp index e0fa8db6..bdb7a54d 100644 --- a/src/IO/TestingSensors.cpp +++ b/src/IO/TestingSensors.cpp @@ -1,5 +1,4 @@ #include "config.h" -#if TESTING == 1 #include "TestingSensors.h" #include "data/StateData.h" @@ -153,5 +152,3 @@ bool TestingSensors::isInitialized() // Unused when in testing mode return true; } - -#endif diff --git a/src/IO/TestingSensors.h b/src/IO/TestingSensors.h index 4825b4f4..145cd79a 100644 --- a/src/IO/TestingSensors.h +++ b/src/IO/TestingSensors.h @@ -1,7 +1,6 @@ #pragma once #include "config.h" -#if TESTING == 1 #include "../data/SBGData.h" #include "IO.h" @@ -21,5 +20,3 @@ class TestingSensors : public IO private: std::queue data; }; - -#endif \ No newline at end of file diff --git a/src/IO/gpio/DigitalOutput.h b/src/IO/gpio/DigitalOutput.h index 1fc8f6a1..91229002 100644 --- a/src/IO/gpio/DigitalOutput.h +++ b/src/IO/gpio/DigitalOutput.h @@ -1,13 +1,9 @@ #pragma once #include "config.h" -#if USE_GPIO == 1 #include "Output.h" -#if USE_ARDUINO_PROXY == 1 #include "IO/ArduinoProxy.h" -#endif - #include #include @@ -25,9 +21,5 @@ class DigitalOutput : public Output std::string name; const int pinNbr; -#if USE_ARDUINO_PROXY == 1 ArduinoProxy *arduinoProxy; -#endif }; - -#endif \ No newline at end of file diff --git a/src/IO/gpio/Gpio.h b/src/IO/gpio/Gpio.h index 950e2ff6..cba8f510 100644 --- a/src/IO/gpio/Gpio.h +++ b/src/IO/gpio/Gpio.h @@ -1,7 +1,6 @@ #pragma once #include "config.h" -#if USE_GPIO == 1 #include "IO/IO.h" #include "IO/gpio/DigitalOutput.h" @@ -50,5 +49,3 @@ class Gpio : public IO template ::value>::type * = nullptr> std::map toRawMap(std::map map); }; - -#endif \ No newline at end of file diff --git a/src/IO/gpio/Output.h b/src/IO/gpio/Output.h index e190ae2c..44a2506c 100644 --- a/src/IO/gpio/Output.h +++ b/src/IO/gpio/Output.h @@ -1,6 +1,5 @@ #pragma once #include "config.h" -#if USE_GPIO == 1 #include @@ -13,5 +12,3 @@ class Output protected: int currentState = -1; }; - -#endif diff --git a/src/IO/gpio/PwmOutput.h b/src/IO/gpio/PwmOutput.h index f2b81e78..01531859 100644 --- a/src/IO/gpio/PwmOutput.h +++ b/src/IO/gpio/PwmOutput.h @@ -2,14 +2,8 @@ #include "config.h" -#if USE_GPIO == 1 - -#include "Output.h" - -#if USE_ARDUINO_PROXY #include "IO/ArduinoProxy.h" -#endif - +#include "Output.h" #include #include @@ -29,14 +23,5 @@ class PwmOutput : public Output const int safePosition; const bool softPWM; -#if USE_ARDUINO_PROXY == 1 ArduinoProxy *arduinoProxy; - -// struct InitFlags -// { -// InitStatus wiringPiStatus = INIT; -// } status; -#endif }; - -#endif \ No newline at end of file diff --git a/src/IO/tcp/SocketServer.cpp b/src/IO/tcp/SocketServer.cpp index 35f4a0a0..9034f4bc 100644 --- a/src/IO/tcp/SocketServer.cpp +++ b/src/IO/tcp/SocketServer.cpp @@ -1,5 +1,4 @@ #include "config.h" -#if USE_SOCKET_CLIENT == 1 #include "IO/IO.h" #include "SocketServer.h" @@ -133,5 +132,3 @@ uint64_t SocketServer::getLastConnectionTimestamp() return lastConnectionTimestamp; } - -#endif diff --git a/src/IO/tcp/SocketServer.h b/src/IO/tcp/SocketServer.h index f6fea9de..5738b1b1 100644 --- a/src/IO/tcp/SocketServer.h +++ b/src/IO/tcp/SocketServer.h @@ -1,7 +1,6 @@ #pragma once #include "config.h" -#if USE_SOCKET_CLIENT == 1 #include "IO/EventQueue.h" #include "IO/IO.h" @@ -52,5 +51,3 @@ class SocketServer : public IO uint64_t lastConnectionTimestamp = 0; }; - -#endif diff --git a/src/data/StateData.h b/src/data/StateData.h index 2dd7382c..9329771a 100644 --- a/src/data/StateData.h +++ b/src/data/StateData.h @@ -17,47 +17,50 @@ struct StateData uint16_t currentStateNo = 0; eventType eventNumber = -1; -#if USE_SBG == 1 + /** + * SBG + */ sbgData sbg; - bool sbgIsInitialized = 0; -#endif + bool sbgIsInitialized = false; -#if USE_GPIO == 1 + /** + * GPIO + */ GpioData gpioData; GpioState gpioState; - bool gpioIsInitialized = 0; -#endif + bool gpioIsInitialized = false; -#if USE_LOGGER - bool loggerIsInitialized = 0; -#endif + /** + * Logger + */ + bool loggerIsInitialized = false; + bool loggerWorking = false; -#if USE_SOCKET_CLIENT - bool clientIsInitialized = 0; + /** + * Socket Client + */ + bool clientIsInitialized = false; uint64_t lastActiveClientTimestamp = 0; -#endif -#if USE_INPUT - bool inputIsInitialized = 0; -#endif + /** + * Input + */ + bool inputIsInitialized = false; -#if USE_RADIO - bool radioIsInitialized = 0; -#endif + /** + * Radio + */ + bool radioIsInitialized = false; -#if USE_ARDUINO_PROXY - bool arduinoProxyIsInitialized = 0; -#endif + /** + * Arduino Proxy + */ + bool arduinoProxyIsInitialized = false; -#if USE_SENSORS + /** + * Sensor + */ SensorState sensorState; -#endif -#if TESTING == 1 bool outOfData = false; -#endif - -#if USE_LOGGER == 1 - bool loggerWorking = false; -#endif // USE_LOGGER }; diff --git a/src/init/MainLoop.cpp b/src/init/MainLoop.cpp index 7670dfda..5b37f582 100644 --- a/src/init/MainLoop.cpp +++ b/src/init/MainLoop.cpp @@ -1,20 +1,21 @@ #include "common/pch.h" -#include "iostream" -#include -#include - -#include "helpers/Helper.h" - -#include "IO/InterfaceImpl.h" -#include "chrono" #include "data/UOSMData.h" -#include "spdlog/sinks/basic_file_sink.h" -#include "spdlog/sinks/dup_filter_sink.h" -#include "spdlog/sinks/stdout_color_sinks.h" -#include "spdlog/spdlog.h" +#include "helpers/Helper.h" +#include "interface/HotFire/HotFireInterface.h" +#include "interface/OctoberSky/OctoberSkyInterface.h" +#include "interface/TestingInterface.h" #include "stateMachine/HotFire/HotFireStateMachine.h" #include "stateMachine/OctoberSky/OctoberSkyStateMachine.h" +#include +#include +#include +#include +#include +#include +#include +#include + #define DEFAULT_TARGET_UPDATE_DURATION_NS (1000000000L / 30L) // in nanoseconds = 33 miliseconds = 30Hz // Specify the minimum logging level. The specified level and up will be logged. @@ -74,7 +75,7 @@ int main() SPDLOG_INFO("Using {}", TOSTRING(STATEMACHINE)); #if TESTING != 1 - InterfaceImpl interfaceImpl; + INTERFACE interfaceImpl; #else TestingInterface interfaceImpl; #endif diff --git a/src/interface/HotFire/HotFireInterface.cpp b/src/interface/HotFire/HotFireInterface.cpp new file mode 100644 index 00000000..6132d411 --- /dev/null +++ b/src/interface/HotFire/HotFireInterface.cpp @@ -0,0 +1,101 @@ +#include "config.h" + +#include "HotFireInterface.h" +#include "common/pch.h" +#include +#include +#include + +HotFireInterface::HotFireInterface() + : eventQueue(), input(eventQueue), client(eventQueue), arduinoProxy(ArduinoProxy::getInstance()) +{ + logger = spdlog::default_logger(); +} + +HotFireInterface::~HotFireInterface() = default; + +void HotFireInterface::initialize() +{ + initializeInputs(); + initializeOutputs(); +} + +void HotFireInterface::initializeInputs() +{ + SPDLOG_LOGGER_INFO(logger, "Initializing INPUT..."); + input.initialize(); + SPDLOG_LOGGER_INFO(logger, "Initializing SOCKET_CLIENT..."); + client.initialize(); + SPDLOG_LOGGER_INFO(logger, "Initializing SENSORS..."); + sensors.initialize(); +} + +void HotFireInterface::initializeOutputs() +{ + SPDLOG_LOGGER_INFO(logger, "Initializing SENSOR_LOGGER..."); + sensorLogger.initialize(); + SPDLOG_LOGGER_INFO(logger, "Initializing GPIO..."); + gpio.initialize(); + SPDLOG_LOGGER_INFO(logger, "Initializing Arduino Proxy..."); + arduinoProxy->initialize(); +} + +bool HotFireInterface::updateInputs() +{ + latestState = std::make_shared(); + + latestState->timeStamp = + std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count(); + + // EventQueue + latestState->eventNumber = eventQueue.pop(); + // Logger + latestState->loggerIsInitialized = sensorLogger.isInitialized(); + // Socket Client + latestState->clientIsInitialized = client.isInitialized(); + latestState->lastActiveClientTimestamp = client.getLastConnectionTimestamp(); + // Input + latestState->inputIsInitialized = input.isInitialized(); + // GPIO + latestState->gpioIsInitialized = gpio.isInitialized(); + latestState->gpioState = gpio.getCurrentState(); + // Arduino Proxy + latestState->arduinoProxyIsInitialized = arduinoProxy->isInitialized(); + // Sensors + latestState->sensorState = sensors.getCurrentState(); + + return true; +} + +bool HotFireInterface::updateOutputs(std::shared_ptr data) +{ + + data->gpioData = gpio.setOutputs(data->gpioData); + sensorLogger.enqueueSensorData(*data); + client.enqueueSensorData(*data); + + return true; +} + +void HotFireInterface::createNewGpioOutput(std::string name, int pinNbr) +{ + gpio.createNewGpioOutput(name, pinNbr); +} + +void HotFireInterface::createNewGpioPwmOutput(std::string name, int pinNbr, int safePosition, bool softpwm) +{ + gpio.createNewGpioPwmOutput(name, pinNbr, safePosition, softpwm); +} + +std::shared_ptr HotFireInterface::getLatest() +{ + + latestState->loggerWorking = SensorLogger::working; + + return latestState; +} + +time_point HotFireInterface::getCurrentTime() +{ + return std::chrono::steady_clock::now(); +} \ No newline at end of file diff --git a/src/IO/InterfaceImpl.h b/src/interface/HotFire/HotFireInterface.h similarity index 69% rename from src/IO/InterfaceImpl.h rename to src/interface/HotFire/HotFireInterface.h index f4a3ad7d..8f60453d 100644 --- a/src/IO/InterfaceImpl.h +++ b/src/interface/HotFire/HotFireInterface.h @@ -1,32 +1,28 @@ #pragma once #include "config.h" -#if TESTING != 1 -#include "EventQueue.h" #include "IO/ArduinoProxy.h" #include "IO/Input.h" -#include "IO/Interface.h" #include "IO/Radio.h" #include "IO/SBGSensor.h" #include "IO/SensorLogger.h" #include "IO/gpio/Gpio.h" #include "IO/tcp/SocketServer.h" -#include "Sensors.h" +#include +#include #include #include #include -class InterfaceImpl : public Interface +class HotFireInterface : public Interface { public: - InterfaceImpl(); - ~InterfaceImpl(); + HotFireInterface(); + ~HotFireInterface(); void initialize() override; - void calibrateTelemetry(); - // to get the latest rocket state. return a pointer to latestState std::shared_ptr getLatest() override; @@ -34,10 +30,8 @@ class InterfaceImpl : public Interface bool updateInputs() override; bool updateOutputs(std::shared_ptr data) override; -#if USE_GPIO == 1 void createNewGpioOutput(std::string name, int pinNbr) override; void createNewGpioPwmOutput(std::string name, int pinNbr, int safePosition, bool softpwm) override; -#endif time_point getCurrentTime() override; @@ -50,37 +44,13 @@ class InterfaceImpl : public Interface std::shared_ptr latestState; EventQueue eventQueue; -#if USE_SBG == 1 - SBGSensor mySbgSensor; -#endif - -#if USE_INPUT == 1 + /** + * IO instances + */ Input input; -#endif - -#if USE_SOCKET_CLIENT == 1 SocketServer client; -#endif - -#if USE_RADIO == 1 - Radio radio; -#endif - -#if USE_LOGGER == 1 SensorLogger sensorLogger; -#endif - -#if USE_GPIO == 1 Gpio gpio; -#endif - -#if USE_ARDUINO_PROXY == 1 ArduinoProxy *arduinoProxy; -#endif - -#if USE_SENSORS == 1 Sensors sensors; -#endif }; - -#endif \ No newline at end of file diff --git a/src/IO/Interface.h b/src/interface/Interface.h similarity index 84% rename from src/IO/Interface.h rename to src/interface/Interface.h index 60e43ac4..1c7d3aa2 100644 --- a/src/IO/Interface.h +++ b/src/interface/Interface.h @@ -11,7 +11,7 @@ class Interface public: virtual void initialize() = 0; - virtual void calibrateTelemetry() = 0; + virtual void calibrateTelemetry(){}; // to get the latest rocket state. return a pointer to latestState virtual std::shared_ptr getLatest() = 0; @@ -21,13 +21,9 @@ class Interface virtual bool updateOutputs(std::shared_ptr data) = 0; -#if USE_GPIO == 1 + virtual void createNewGpioOutput(std::string name, int pinNbr){}; - virtual void createNewGpioOutput(std::string name, int pinNbr) = 0; - - virtual void createNewGpioPwmOutput(std::string name, int pinNbr, int safePosition, bool softpwm) = 0; - -#endif + virtual void createNewGpioPwmOutput(std::string name, int pinNbr, int safePosition, bool softpwm){}; virtual time_point getCurrentTime() = 0; }; diff --git a/src/interface/OctoberSky/OctoberSkyInterface.cpp b/src/interface/OctoberSky/OctoberSkyInterface.cpp new file mode 100644 index 00000000..a1629f51 --- /dev/null +++ b/src/interface/OctoberSky/OctoberSkyInterface.cpp @@ -0,0 +1,83 @@ +#include "config.h" +#if TESTING != 1 + +#include "OctoberSkyInterface.h" +#include "common/pch.h" +#include +#include + +OctoberSkyInterface::OctoberSkyInterface() : eventQueue(), radio(eventQueue) +{ + logger = spdlog::default_logger(); +} + +OctoberSkyInterface::~OctoberSkyInterface() = default; + +void OctoberSkyInterface::initialize() +{ + initializeInputs(); + initializeOutputs(); +} + +void OctoberSkyInterface::initializeInputs() +{ + SPDLOG_LOGGER_INFO(logger, "Initializing SBG..."); + sbgSensor.initialize(); +} + +void OctoberSkyInterface::initializeOutputs() +{ + SPDLOG_LOGGER_INFO(logger, "Initializing SENSOR_LOGGER..."); + sensorLogger.initialize(); + + SPDLOG_LOGGER_INFO(logger, "Initializing RADIO..."); + radio.initialize(); +} + +bool OctoberSkyInterface::updateInputs() +{ + latestState = std::make_shared(); + + latestState->timeStamp = + std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count(); + + latestState->sbg = sbgSensor.getData(); + latestState->sbgIsInitialized = sbgSensor.isInitialized(); + + latestState->eventNumber = eventQueue.pop(); + + latestState->loggerIsInitialized = sensorLogger.isInitialized(); + + latestState->radioIsInitialized = radio.isInitialized(); + + return true; +} + +bool OctoberSkyInterface::updateOutputs(std::shared_ptr data) +{ + sensorLogger.enqueueSensorData(*data); + + radio.enqueueSensorData(*data); + + return true; +} + +void OctoberSkyInterface::calibrateTelemetry() +{ + sbgSensor.setZeroBarometricAltitude(); +} + +std::shared_ptr OctoberSkyInterface::getLatest() +{ + + latestState->loggerWorking = SensorLogger::working; + + return latestState; +} + +time_point OctoberSkyInterface::getCurrentTime() +{ + return std::chrono::steady_clock::now(); +} + +#endif \ No newline at end of file diff --git a/src/interface/OctoberSky/OctoberSkyInterface.h b/src/interface/OctoberSky/OctoberSkyInterface.h new file mode 100644 index 00000000..162fab38 --- /dev/null +++ b/src/interface/OctoberSky/OctoberSkyInterface.h @@ -0,0 +1,52 @@ +#pragma once + +#include "config.h" + +#include "IO/ArduinoProxy.h" +#include "IO/Input.h" +#include "IO/Radio.h" +#include "IO/SBGSensor.h" +#include "IO/SensorLogger.h" +#include "IO/gpio/Gpio.h" +#include "IO/tcp/SocketServer.h" +#include +#include +#include +#include +#include + +class OctoberSkyInterface : public Interface +{ + public: + OctoberSkyInterface(); + ~OctoberSkyInterface(); + + void initialize() override; + + void calibrateTelemetry() override; + + // to get the latest rocket state. return a pointer to latestState + std::shared_ptr getLatest() override; + + // loop over each sensor and update the latestState + bool updateInputs() override; + bool updateOutputs(std::shared_ptr data) override; + + time_point getCurrentTime() override; + + private: + void initializeInputs(); + void initializeOutputs(); + + std::shared_ptr logger; + + std::shared_ptr latestState; + EventQueue eventQueue; + + /** + * IO instances + */ + SBGSensor sbgSensor; + Radio radio; + SensorLogger sensorLogger; +}; diff --git a/src/IO/TestingInterface.cpp b/src/interface/TestingInterface.cpp similarity index 72% rename from src/IO/TestingInterface.cpp rename to src/interface/TestingInterface.cpp index 159f13da..55a6ec32 100644 --- a/src/IO/TestingInterface.cpp +++ b/src/interface/TestingInterface.cpp @@ -1,6 +1,5 @@ #include "config.h" #include -#if TESTING == 1 #include "TestingInterface.h" @@ -21,14 +20,8 @@ void TestingInterface::initialize() void TestingInterface::initializeOutputs() { -#if USE_LOGGER == 1 SPDLOG_LOGGER_INFO(logger, "Initializing LOGGER..."); sensorLogger.initialize(); -#endif -#if USE_RADIO == 1 - SPDLOG_LOGGER_INFO(logger, "Initializing RADIO..."); - radio.initialize(); -#endif } bool TestingInterface::updateInputs() @@ -40,7 +33,6 @@ bool TestingInterface::updateInputs() bool TestingInterface::updateOutputs(std::shared_ptr data) { -#if USE_LOGGER == 1 if (latestState->outOfData) { if (!sensorLogger.queueEmpty()) @@ -53,26 +45,10 @@ bool TestingInterface::updateOutputs(std::shared_ptr data) } sensorLogger.enqueueSensorData(*data); -#endif - -#if USE_RADIO == 1 - radio.enqueueSensorData(*data); -#endif return true; } -#if USE_GPIO == 1 -void TestingInterface::createNewGpioOutput(std::string name, int pinNbr) -{ - return; // don't do anything for now -} -void TestingInterface::createNewGpioPwmOutput(std::string name, int pinNbr, int safePosition, bool softpwm) -{ - return; // don't do anything for now -} -#endif - void TestingInterface::calibrateTelemetry() { } @@ -93,5 +69,3 @@ time_point TestingInterface::getCurrentTime() return time_point(std::chrono::duration_cast(duration_ns(0))); } } - -#endif \ No newline at end of file diff --git a/src/IO/TestingInterface.h b/src/interface/TestingInterface.h similarity index 72% rename from src/IO/TestingInterface.h rename to src/interface/TestingInterface.h index fb1aff5a..093c2a7f 100644 --- a/src/IO/TestingInterface.h +++ b/src/interface/TestingInterface.h @@ -1,12 +1,11 @@ #pragma once #include "config.h" -#if TESTING == 1 -#include "IO/Interface.h" #include "IO/Radio.h" #include "IO/SensorLogger.h" #include "IO/TestingSensors.h" +#include "Interface.h" #include "data/StateData.h" #include #include @@ -27,11 +26,6 @@ class TestingInterface : public Interface bool updateInputs() override; bool updateOutputs(std::shared_ptr data) override; -#if USE_GPIO == 1 - void createNewGpioOutput(std::string name, int pinNbr) override; - void createNewGpioPwmOutput(std::string name, int pinNbr, int safePosition, bool softpwm) override; -#endif - time_point getCurrentTime() override; private: @@ -45,13 +39,5 @@ class TestingInterface : public Interface time_point latestTime; -#if USE_LOGGER == 1 SensorLogger sensorLogger; -#endif - -#if USE_RADIO == 1 - Radio radio; -#endif }; - -#endif \ No newline at end of file diff --git a/src/stateMachine/InterfacingStateMachine.h b/src/stateMachine/InterfacingStateMachine.h index 00eed7e0..8fdc841b 100644 --- a/src/stateMachine/InterfacingStateMachine.h +++ b/src/stateMachine/InterfacingStateMachine.h @@ -1,9 +1,7 @@ #pragma once -#include "IO/Interface.h" -#include "IO/InterfaceImpl.h" -#include "IO/TestingInterface.h" #include "data/StateData.h" #include "stateMachineLib/StateMachine.h" +#include #include class InterfacingStateMachine : public StateMachine