Skip to content

Commit

Permalink
Feat/config json (#152)
Browse files Browse the repository at this point in the history
* Loaded in user specified JSON, Got Values

Additionally:
-Did fix Anthony said for Magick++.h no such file or directory for Playground and MavlinkClient to get it to make
-Some modification in protos which I don't remember touching

* Generate default config can't read from one

* Generate default config when can't read from one

* Put config values into OBC constructor

- Changed parameters for OBC constuctor and connectMavlink
- Made it so connectMavlink will use config value

* pass mavlink_url into connectMavThread

account for changes to obc

* Made OBCConfig struct

Generate struct based on command line input
    - throw error if file not found
    - generate default if no filename passed

Pass struct into OBC instead of unwrapped configs

* fix lint & mavlink url not working

* add logging dir to config

---------

Co-authored-by: Tyler Lentz <[email protected]>
  • Loading branch information
mobslayer202 and Tyler-Lentz authored Apr 17, 2024
1 parent 1f2a558 commit 2c0b92a
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 17 deletions.
13 changes: 13 additions & 0 deletions configs/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"logging": {
"dir": "/workspaces/obcpp/logs"
},
"network": {
"mavlink": {
"connect": "serial:///dev/ttyACM0"
},
"gcs": {
"port": 5010
}
}
}
13 changes: 13 additions & 0 deletions configs/default-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"logging": {
"dir": "/workspaces/obcpp/logs"
},
"network": {
"gcs": {
"port": 5010
},
"mavlink": {
"connect": "tcp://172.17.0.1:5760"
}
}
}
13 changes: 13 additions & 0 deletions configs/dev-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"logging": {
"dir": "/workspaces/obcpp/logs"
},
"network": {
"mavlink": {
"connect": "tcp://172.17.0.1:5760"
},
"gcs": {
"port": 5010
}
}
}
5 changes: 3 additions & 2 deletions include/core/obc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
#include "core/mission_state.hpp"
#include "network/gcs.hpp"
#include "network/mavlink.hpp"
#include "utilities/obc_config.hpp"

/*
* The highest level class for the entire OBC
* This contains all of the mission state, mission config, etc...
*/
class OBC {
public:
explicit OBC(uint16_t gcs_port);
explicit OBC(OBCConfig config);

void run();

Expand All @@ -28,7 +29,7 @@ class OBC {
std::thread connectMavThread;
std::thread connectAirdropThread;

void connectMavlink();
void connectMavlink(std::string mavlink_url);
void connectAirdrop();
};

Expand Down
2 changes: 1 addition & 1 deletion include/network/mavlink.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MavlinkClient {
* example:
* MavlinkClient("tcp://192.168.65.254:5762")
*/
explicit MavlinkClient(const char* link);
explicit MavlinkClient(std::string link);

/*
* BLOCKING. Continues to try to upload the mission based on the passed through MissionConfig
Expand Down
21 changes: 21 additions & 0 deletions include/utilities/obc_config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef INCLUDE_UTILITIES_OBC_CONFIG_HPP_
#define INCLUDE_UTILITIES_OBC_CONFIG_HPP_

#include <string>

struct OBCConfig {
std::string logging_dir;

std::string network_mavlink_connect;
int network_gcs_port;

// Load user specified config json, or make a new one
OBCConfig(int argc, char* argv[]);

private:
const std::string configsPath = "/workspaces/obcpp/configs/";

void makeDefault();
};

#endif // INCLUDE_UTILITIES_OBC_CONFIG_HPP_
16 changes: 11 additions & 5 deletions src/core/obc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,25 @@
#include "network/mavlink.hpp"
#include "network/airdrop_client.hpp"
#include "utilities/logging.hpp"
#include "utilities/obc_config.hpp"
extern "C" {
#include "network/airdrop_sockets.h"
}

// TODO: allow specifying config filename
OBC::OBC(uint16_t gcs_port) {
OBC::OBC(OBCConfig config) {
int gcs_port = config.network_gcs_port;

this->state = std::make_shared<MissionState>();
this->state->setTick(new MissionPrepTick(this->state));

this->gcs_server = std::make_unique<GCSServer>(gcs_port, this->state);

this->connectMavThread = std::thread([this]{this->connectMavlink();});
// Don't need to look at these futures at all because the connect functions
// will set the global mission state themselves when connected, which everything
// else can check.
this->connectMavThread = std::thread([this, config]
{this->connectMavlink(config.network_mavlink_connect);});
this->connectAirdropThread = std::thread([this]{this->connectAirdrop();});
}

Expand All @@ -34,12 +41,11 @@ void OBC::run() {
}
}

void OBC::connectMavlink() {
void OBC::connectMavlink(std::string mavlink_url) {
loguru::set_thread_name("mav connect");

// TODO: pull mav ip from config file
std::shared_ptr<MavlinkClient> mav(new MavlinkClient("serial:///dev/ttyACM0:57600"));
// std::shared_ptr<MavlinkClient> mav(new MavlinkClient("tcp://172.17.0.1:5760"));
std::shared_ptr<MavlinkClient> mav(new MavlinkClient(mavlink_url));
this->state->setMav(mav);
}

Expand Down
10 changes: 5 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <chrono>
#include <string>

extern "C" {
#include "network/airdrop_sockets.h"
Expand All @@ -8,14 +7,15 @@ extern "C" {
#include "core/obc.hpp"
#include "utilities/constants.hpp"
#include "utilities/logging.hpp"
#include "utilities/obc_config.hpp"


int main(int argc, char* argv[]) {
OBCConfig config(argc, argv);

// TODO: pull logging folder from config
initLogging("/workspaces/obcpp/logs", true, argc, argv);
initLogging(config.logging_dir, true, argc, argv);

// In future, load configs, perhaps command line parameters, and pass
// into the obc object
OBC obc(DEFAULT_GCS_PORT);
OBC obc(config);
obc.run();
}
6 changes: 3 additions & 3 deletions src/network/mavlink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
#include "utilities/logging.hpp"
#include "core/mission_state.hpp"

MavlinkClient::MavlinkClient(const char* link) {
LOG_F(INFO, "Connecting to Mav at %s", link);
MavlinkClient::MavlinkClient(std::string link) {
LOG_F(INFO, "Connecting to Mav at %s", link.c_str());

while (true) {
LOG_F(INFO, "Attempting to add mav connection...");
const auto conn_result = this->mavsdk.add_any_connection(link);
if (conn_result == mavsdk::ConnectionResult::Success) {
LOG_F(INFO, "Mavlink connection successfully established at %s", link);
LOG_F(INFO, "Mavlink connection successfully established at %s", link.c_str());
break;
}

Expand Down
3 changes: 2 additions & 1 deletion src/pathing/tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "pathing/environment.hpp"
#include "utilities/datatypes.hpp"
#include "utilities/rng.hpp"
#include "utilities/logging.hpp"

RRTNode::RRTNode(const RRTPoint& point, double cost, double path_length,
const std::vector<XYZCoord> path)
Expand Down Expand Up @@ -363,7 +364,7 @@ void RRTTree::RRTStar(RRTNode* sample, double rewire_radius) {

void RRTTree::setCurrentHead(RRTNode* goal) {
if (goal == nullptr) {
std::cout << "FAILURE: Goal is not in the tree\n" << std::endl;
LOG_F(ERROR, "FAILURE: Goal is not in the tree");
return;
}

Expand Down
1 change: 1 addition & 0 deletions src/utilities/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ SET(FILES
common.cpp
datatypes.cpp
logging.cpp
obc_config.cpp
rng.cpp
)

Expand Down
44 changes: 44 additions & 0 deletions src/utilities/obc_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "utilities/obc_config.hpp"

#include <fstream>
#include <stdexcept>
#include <string>

#include "nlohmann/json.hpp"

using json = nlohmann::json;


OBCConfig::OBCConfig(int argc, char* argv[]) {
// If config-json name is passed in
if (argc > 1) {
// Load in json file
std::ifstream configStream(configsPath + std::string(argv[1]));
if (!configStream.is_open()) {
throw std::invalid_argument("Invalid config-json name");
}
json configs = json::parse(configStream);

// Set configs
this->logging_dir = configs["logging"]["dir"];
this->network_mavlink_connect = configs["network"]["mavlink"]["connect"];
this->network_gcs_port = configs["network"]["gcs"]["port"];
} else {
makeDefault(); // Detect if there is already a default-config.json?
}
}

void OBCConfig::makeDefault() {
// Set configs
this->logging_dir = "/workspaces/obcpp/logs";
this->network_mavlink_connect = "tcp://172.17.0.1:5760";
this->network_gcs_port = 5010;

// Create default configs
json configs;
configs["logging"]["dir"] = this->logging_dir;
configs["network"]["mavlink"]["connect"] = this->network_mavlink_connect;
configs["network"]["gcs"]["port"] = this->network_gcs_port;
std::ofstream configFile(configsPath + "default-config.json");
configFile << configs.dump(4); // Dump to file with 4 space indents
}

0 comments on commit 2c0b92a

Please sign in to comment.