Skip to content

Commit

Permalink
modified sqlite logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Octavian-Zhang committed Mar 30, 2022
1 parent 31270a2 commit 2fe1f32
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 66 deletions.
3 changes: 0 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -Wall -ggdb3")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

# set build type
SET(CMAKE_BUILD_TYPE "Debug")

# Include sub-projects.
add_subdirectory (MissionAlg)
add_subdirectory(MissionApp)
64 changes: 64 additions & 0 deletions MissionApp/include/DataLogging.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#ifndef DataLog
#define DataLog

#include "sqlite_orm.h"
#include "codegenReal2Mission.h"

#define initStorage(path) make_storage(path, \
make_table<RealUAVStateBus>("RealUAVState", \
make_column("Latitude_deg", &RealUAVStateBus::Latitude_deg), \
make_column("Longitude_deg", &RealUAVStateBus::Longitude_deg), \
make_column("Height_meter", &RealUAVStateBus::Height_meter), \
make_column("AirSpeed_mps", &RealUAVStateBus::AirSpeed_mps), \
make_column("HeadingAngle_deg", &RealUAVStateBus::HeadingAngle_deg), \
make_column("FlightPathAngle_deg", &RealUAVStateBus::FlightPathAngle_deg), \
make_column("RollAngle_deg", &RealUAVStateBus::RollAngle_deg)), \
make_table<FixedWingGuidanceStateBus>("SimUAVState", \
make_column("North", &FixedWingGuidanceStateBus::North), \
make_column("East", &FixedWingGuidanceStateBus::East), \
make_column("Height", &FixedWingGuidanceStateBus::Height), \
make_column("AirSpeed", &FixedWingGuidanceStateBus::AirSpeed), \
make_column("HeadingAngle", &FixedWingGuidanceStateBus::HeadingAngle), \
make_column("FlightPathAngle", &FixedWingGuidanceStateBus::FlightPathAngle), \
make_column("RollAngle", &FixedWingGuidanceStateBus::RollAngle), \
make_column("RollAngleRate", &FixedWingGuidanceStateBus::RollAngleRate)), \
make_table<FCUCMD>("FlightCMD", \
make_column("Latitude_deg", &FCUCMD::Latitude_deg), \
make_column("Longitude_deg", &FCUCMD::Longitude_deg), \
make_column("Height_meter", &FCUCMD::Height_meter), \
make_column("RefAirSpd_mps", &FCUCMD::RefAirSpd_mps)), \
make_table<InternalStatus>("InnerState", \
make_column("LagDistance", &InternalStatus::LagDistance), \
make_column("CrossTrackError", &InternalStatus::CrossTrackError), \
make_column("EngagedFlag", &InternalStatus::EngagedFlag), \
make_column("RealHeading", &InternalStatus::RealHeading), \
make_column("TargetHeading", &InternalStatus::TargetHeading), \
make_column("HeadingDiff", &InternalStatus::HeadingDiff), \
make_column("ADRC_U", &InternalStatus::ADRC_U), \
make_column("biasH", &InternalStatus::biasH), \
make_column("HdgStatus", &InternalStatus::HdgStatus)), \
make_table<MiscellaneousFlightStatus>("MiscStatus", \
make_column("GroundSpeed", &MiscellaneousFlightStatus::GroundSpeed), \
make_column("FlightMode", &MiscellaneousFlightStatus::FlightMode), \
make_column("Altitude", &MiscellaneousFlightStatus::Altitude), \
make_column("FlightPath", &MiscellaneousFlightStatus::FlightPath)), \
make_table<VectorSpeed>("VectorSpd", \
make_column("eastSpeed", &VectorSpeed::eastSpeed), \
make_column("northSpeed", &VectorSpeed::northSpeed), \
make_column("skySpeed", &VectorSpeed::skySpeed)))

using namespace sqlite_orm;
using Storage = decltype(initStorage(""));

class DataLogging
{
private:
Storage storage_;
DataLogging(const std::string&);
public:
static void wrtieLog(const FlightLogging*);
DataLogging(DataLogging const&) = delete;
void operator=(DataLogging const&) = delete;
};

#endif
23 changes: 23 additions & 0 deletions MissionApp/src/DataLogging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <future>
#include "DataLogging.hpp"

DataLogging::DataLogging(const std::string& path) : storage_(initStorage(path))
{
storage_.sync_schema();
}

void DataLogging::wrtieLog(const FlightLogging* data)
{
static DataLogging instance{"FlightLogging.sqlite"};
auto sqlfuture = std::async(std::launch::async,
[&]()
{ instance.storage_.transaction([&]
{
instance.storage_.insert(data->FlightCMD);
instance.storage_.insert(data->InnerState);
instance.storage_.insert(data->MiscStatus);
instance.storage_.insert(data->RealUAVState);
instance.storage_.insert(data->SimUAVState);
instance.storage_.insert(data->VectorSpd);
return true; }); });
}
69 changes: 6 additions & 63 deletions MissionApp/src/ert_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,59 +30,12 @@
#include "codegenReal2Mission.h" // Model's header file

#include "msgQueue.hpp"
#include "sqlite_orm.h"
#include "DataLogging.hpp"

/* Simulink Model IO */
static msgQueue MQ_ExtU("/PosixMQ_ExtU", O_CREAT | O_RDONLY | O_NONBLOCK, 1, sizeof(codegenReal2MissionModelClass::ExtU_codegenReal2Mission_T));
static msgQueue MQ_ExtY("/PosixMQ_ExtY", O_CREAT | O_WRONLY | O_NONBLOCK, 1, sizeof(codegenReal2MissionModelClass::ExtY_codegenReal2Mission_T));

auto initStorage(const std::string& path) {
using namespace sqlite_orm;
return make_storage(path,
make_table<RealUAVStateBus>("RealUAVState",
make_column("Latitude_deg", &RealUAVStateBus::Latitude_deg),
make_column("Longitude_deg", &RealUAVStateBus::Longitude_deg),
make_column("Height_meter", &RealUAVStateBus::Height_meter),
make_column("AirSpeed_mps", &RealUAVStateBus::AirSpeed_mps),
make_column("HeadingAngle_deg", &RealUAVStateBus::HeadingAngle_deg),
make_column("FlightPathAngle_deg", &RealUAVStateBus::FlightPathAngle_deg),
make_column("RollAngle_deg", &RealUAVStateBus::RollAngle_deg)),
make_table<FixedWingGuidanceStateBus>("SimUAVState",
make_column("North", &FixedWingGuidanceStateBus::North),
make_column("East", &FixedWingGuidanceStateBus::East),
make_column("Height", &FixedWingGuidanceStateBus::Height),
make_column("AirSpeed", &FixedWingGuidanceStateBus::AirSpeed),
make_column("HeadingAngle", &FixedWingGuidanceStateBus::HeadingAngle),
make_column("FlightPathAngle", &FixedWingGuidanceStateBus::FlightPathAngle),
make_column("RollAngle", &FixedWingGuidanceStateBus::RollAngle),
make_column("RollAngleRate", &FixedWingGuidanceStateBus::RollAngleRate)),
make_table<FCUCMD>("FlightCMD",
make_column("Latitude_deg", &FCUCMD::Latitude_deg),
make_column("Longitude_deg", &FCUCMD::Longitude_deg),
make_column("Height_meter", &FCUCMD::Height_meter),
make_column("RefAirSpd_mps", &FCUCMD::RefAirSpd_mps)),
make_table<InternalStatus>("InnerState",
make_column("LagDistance", &InternalStatus::LagDistance),
make_column("CrossTrackError", &InternalStatus::CrossTrackError),
make_column("EngagedFlag", &InternalStatus::EngagedFlag),
make_column("RealHeading", &InternalStatus::RealHeading),
make_column("TargetHeading", &InternalStatus::TargetHeading),
make_column("HeadingDiff", &InternalStatus::HeadingDiff),
make_column("ADRC_U", &InternalStatus::ADRC_U),
make_column("biasH", &InternalStatus::biasH),
make_column("HdgStatus", &InternalStatus::HdgStatus)),
make_table<MiscellaneousFlightStatus>("MiscStatus",
make_column("GroundSpeed", &MiscellaneousFlightStatus::GroundSpeed),
make_column("FlightMode", &MiscellaneousFlightStatus::FlightMode),
make_column("Altitude", &MiscellaneousFlightStatus::Altitude),
make_column("FlightPath", &MiscellaneousFlightStatus::FlightPath)),
make_table<VectorSpeed>("VectorSpd",
make_column("eastSpeed", &VectorSpeed::eastSpeed),
make_column("northSpeed", &VectorSpeed::northSpeed),
make_column("skySpeed", &VectorSpeed::skySpeed)));
}
using Storage = decltype(initStorage(""));

class codegenReal2MissionModelClassSendData_IndividualUAVCmdT : public
SendData_IndividualUAVCmdT{
mqd_t msgQueue;
Expand All @@ -108,21 +61,11 @@ static codegenReal2MissionModelClassSendData_IndividualUAVCmdT
CurrentMissionSendData_arg;
class codegenReal2MissionModelClassSendData_FlightLoggingT : public
SendData_FlightLoggingT{
Storage storage;
public:
codegenReal2MissionModelClassSendData_FlightLoggingT() : storage(initStorage("FlightLogging.sqlite"))
{
storage.sync_schema();
}
void SendData(const FlightLogging* data, int32_T length, int32_T* status)
{
// Add send data logic here
storage.insert(data->FlightCMD);
storage.insert(data->InnerState);
storage.insert(data->MiscStatus);
storage.insert(data->RealUAVState);
storage.insert(data->SimUAVState);
storage.insert(data->VectorSpd);
DataLogging::wrtieLog(data);
}
};

Expand Down Expand Up @@ -152,7 +95,7 @@ class codegenReal2MissionModelClassRecvData_IndividualUAVCmdT : public
static codegenReal2MissionModelClassRecvData_IndividualUAVCmdT
MissionCMDRecvData_arg;
static codegenReal2MissionModelClass codegenReal2Mission_Obj{
CurrentMissionSendData_arg, FlightLogSendData_arg, MissionCMDRecvData_arg };// Instance of model class
CurrentMissionSendData_arg, FlightLogSendData_arg, MissionCMDRecvData_arg };// Instance of model class

#define CHECK_STATUS(status, fcn) if (status != 0) {fprintf(stderr, "Call to %s returned error status (%d).\n", fcn, status); perror(fcn); fflush(stderr); exit(EXIT_FAILURE);}

Expand Down Expand Up @@ -391,9 +334,9 @@ int main(int argc, const char *argv[])

// Waiting for OS clock calibration
msgQueue mqStart("/ptrPosixMQ_Start", O_CREAT | O_RDONLY, 1, sizeof(bool));
// bool startflag{}; printf("Waiting for OS Clock Calibration\n");
// printf((mq_receive(mqStart.getMQ(), (char *)&startflag, sizeof(bool), nullptr) > 0)
// ? "OS Clock Calibrated: %c\n" : "MQ_Receive Error %c", startflag);
bool startflag{}; printf("Waiting for OS Clock Calibration\n");
printf((mq_receive(mqStart.getMQ(), (char *)&startflag, sizeof(bool), nullptr) > 0)
? "OS Clock Calibrated: %c\n" : "MQ_Receive Error %c", startflag);

{
// Connect and enable the signal handler for timers notification
Expand Down

0 comments on commit 2fe1f32

Please sign in to comment.