Skip to content

Commit

Permalink
remote config
Browse files Browse the repository at this point in the history
  • Loading branch information
hanappe committed Apr 20, 2024
1 parent c8208f3 commit b18e781
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 56 deletions.
37 changes: 20 additions & 17 deletions applications/romi-cnc/OquamFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,47 @@ namespace romi {
{}

ICNCController& OquamFactory::create_controller(IOptions &options,
nlohmann::json &config)
nlohmann::json &cnc_config,
nlohmann::json &ports_config)
{
if (_controller == nullptr)
instantiate_controller(options, config);
instantiate_controller(options, cnc_config, ports_config);
return *_controller;
}

void OquamFactory::instantiate_controller(IOptions &options, nlohmann::json &config)
void OquamFactory::instantiate_controller(IOptions &options,
nlohmann::json &cnc_config,
nlohmann::json &ports_config)
{
std::string classname = get_controller_classname_in_config(config);
instantiate_controller(classname, options, config);
std::string classname = get_controller_classname_in_config(cnc_config);
instantiate_controller(classname, options, ports_config);
}

std::string OquamFactory::get_controller_classname_in_config(nlohmann::json &config)
{
std::string controller_classname ;
try {
controller_classname = config["oquam"]["controller-classname"];
controller_classname = config["controller-classname"];

} catch (nlohmann::json::exception& je) {
r_warn("Failed to get the value for "
"oquam.controller-classname: %s", je.what());
"cnc.controller-classname: %s", je.what());
throw std::runtime_error("No controller classname defined");
}
return controller_classname;
}

void OquamFactory::instantiate_controller(const std::string& classname,
IOptions &options,
nlohmann::json &config)
nlohmann::json &ports_config)
{
r_info("create_controller: Creating an instance of '%s'", classname.c_str());

if (classname == FakeCNCController::ClassName) {
instantiate_fake_controller();

} else if (classname == StepperController::ClassName) {
instantiate_stepper_controller(options, config);
instantiate_stepper_controller(options, ports_config);

} else {
r_err("Unknown controller class: '%s'", classname.c_str());
Expand All @@ -88,31 +91,31 @@ namespace romi {
}

void OquamFactory::instantiate_stepper_controller(IOptions &options,
nlohmann::json &config)
nlohmann::json &ports_config)
{
std::string device = get_stepper_controller_device(options, config);
std::string device = get_stepper_controller_device(options, ports_config);
std::shared_ptr<romiserial::ILog> log
= std::make_shared<romi::RomiSerialLog>();
auto romi_serial = romiserial::RomiSerialClient::create(device,
"oquam", log);
"cnc", log);
_controller = std::make_unique<StepperController>(romi_serial);
}

std::string OquamFactory::get_stepper_controller_device(IOptions &options,
nlohmann::json &config)
nlohmann::json &ports_config)
{
std::string device_name = options.get_value(RoverOptions::cnc_device);
std::string device_name = options.get_value(RoverOptions::kCncDevice);
if (device_name.empty()) {
device_name = get_stepper_controller_device_in_config(config);
device_name = get_stepper_controller_device_in_config(ports_config);
}
return device_name;
}

std::string OquamFactory::get_stepper_controller_device_in_config(nlohmann::json &config)
std::string OquamFactory::get_stepper_controller_device_in_config(nlohmann::json &ports_config)
{
std::string device_name;
try {
device_name = config["ports"]["oquam"]["port"];
device_name = ports_config["cnc"]["port"];

} catch (nlohmann::json::exception &je) {
r_warn("Failed to get the value for "
Expand Down
22 changes: 15 additions & 7 deletions applications/romi-cnc/OquamFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,30 @@ namespace romi {
protected:
std::unique_ptr<ICNCController> _controller;

void instantiate_controller(IOptions &options, nlohmann::json &config);
void instantiate_controller(IOptions &options,
nlohmann::json &cnc_config,
nlohmann::json &ports_config);

void instantiate_controller(const std::string& controller_classname,
IOptions &options, nlohmann::json &config);
std::string get_controller_classname_in_config(nlohmann::json &config);
IOptions &options,
nlohmann::json &ports_config);

std::string get_controller_classname_in_config(nlohmann::json &cnc_config);
void instantiate_fake_controller();
void instantiate_stepper_controller(IOptions &options, nlohmann::json &config);
void instantiate_stepper_controller(IOptions &options,
nlohmann::json &ports_config);
std::string get_stepper_controller_device(IOptions &options,
nlohmann::json &config);
std::string get_stepper_controller_device_in_config(nlohmann::json &config);
nlohmann::json &ports_config);
std::string get_stepper_controller_device_in_config(nlohmann::json &ports_config);

public:

OquamFactory();
virtual ~OquamFactory() = default;

ICNCController& create_controller(IOptions& options, nlohmann::json& config);
ICNCController& create_controller(IOptions& options,
nlohmann::json& cnc_config,
nlohmann::json& ports_config);

};
}
Expand Down
86 changes: 57 additions & 29 deletions applications/romi-cnc/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@
#include <rcom/Linux.h>
#include <rcom/RcomServer.h>
#include <rcom/RegistryServer.h>
#include <rcom/RcomClient.h>

#include <rpc/RcomLog.h>
#include <rover/RoverOptions.h>
#include <rpc/CNCAdaptor.h>
#include <oquam/Oquam.h>
#include <oquam/StepperSettings.h>
#include <data_provider/RomiDeviceData.h>
#include <data_provider/SoftwareVersion.h>
#include <session/Session.h>
#include <data_provider/Gps.h>
#include <data_provider/GpsLocationProvider.h>
#include <util/Clock.h>
#include <util/ClockAccessor.h>
#include <api/LocalConfig.h>
#include <rpc/RemoteConfig.h>
#include "OquamFactory.h"
#include "data_provider/RomiDeviceData.h"
#include "data_provider/SoftwareVersion.h"
#include "session/Session.h"
#include "data_provider/Gps.h"
#include "data_provider/GpsLocationProvider.h"
#include "util/Clock.h"
#include "util/ClockAccessor.h"

std::atomic<bool> quit(false);
static const char *kRegistry = "registry";
Expand Down Expand Up @@ -52,36 +56,57 @@ int main(int argc, char** argv)

int retval = 1;

romi::RoverOptions options;
romi::Option session_dir = {
"oquam-session", false, ".",
"Set the session directory"};
options.add_option(session_dir);
options.parse(argc, argv);
options.exit_if_help_requested();

log_init();
log_set_application("oquam");

std::signal(SIGSEGV, SignalHandler);
std::signal(SIGINT, SignalHandler);

try {
// Linux
rcom::Linux linux;
std::shared_ptr<rcom::ILog> rcomlog = std::make_shared<romi::RcomLog>();

romi::RoverOptions options;
romi::Option session_dir = {
"oquam-session", false, ".",
"Set the session directory"};
options.add_option(session_dir);
options.parse(argc, argv);
options.exit_if_help_requested();

log_init();
log_set_application("oquam");

if (options.is_set(kRegistry)) {
std::string ip = options.get_value(kRegistry);
r_info("Registry IP set to %s", ip.c_str());
rcom::RegistryServer::set_address(ip.c_str());
}

std::string config_file = options.get_config_file();
r_info("Oquam: Using configuration file: '%s'", config_file.c_str());
// TBD: USE FileUtils
std::ifstream ifs(config_file);
nlohmann::json config = nlohmann::json::parse(ifs);
std::shared_ptr<romi::IConfigManager> config;

if (options.is_set(romi::RoverOptions::kConfig)) {
std::string config_value = options.get_value(romi::RoverOptions::kConfig);

r_info("romi-camera: Using local configuration file: '%s'",
config_value.c_str());

std::filesystem::path config_path = config_value;
config = std::make_shared<romi::LocalConfig>(config_path);
} else {
r_info("romi-camera: Using remote configuration");
auto client = rcom::RcomClient::create("config", 10.0, rcomlog);
config = std::make_shared<romi::RemoteConfig>(client);
}

// std::string config_file = options.get_config_file();
// r_info("Oquam: Using configuration file: '%s'", config_file.c_str());
// // TBD: USE FileUtils
// std::ifstream ifs(config_file);
// nlohmann::json config = nlohmann::json::parse(ifs);

nlohmann::json cnc_config = config->get_section("cnc");

// Session
rcom::Linux linux;
romi::RomiDeviceData romiDeviceData("Oquam", "001"); // FIXME: from config
romi::SoftwareVersion softwareVersion;
romi::Gps gps;
Expand All @@ -97,23 +122,26 @@ int main(int argc, char** argv)
// Oquam cnc
romi::OquamFactory factory;

nlohmann::json range_data = config.at("oquam").at("cnc-range");
nlohmann::json range_data = cnc_config["cnc-range"];
romi::CNCRange range(range_data);

nlohmann::json stepper_data = config.at("oquam").at("stepper-settings");
nlohmann::json stepper_data = cnc_config["stepper-settings"];
romi::StepperSettings stepper_settings(stepper_data);

double slice_duration = (double) config["oquam"]["path-slice-duration"];
double maximum_deviation = (double) config["oquam"]["path-maximum-deviation"];
double slice_duration = (double) cnc_config["path-slice-duration"];
double maximum_deviation = (double) cnc_config["path-maximum-deviation"];

romi::ICNCController& controller = factory.create_controller(options, config);
nlohmann::json ports_config = config->get_section("ports");
romi::ICNCController& controller = factory.create_controller(options,
cnc_config,
ports_config);

double max_steps_per_block = 32000.0; // Should be less than 2^15/2-1
double max_slice_duration = stepper_settings.compute_minimum_duration(max_steps_per_block);

romi::AxisIndex homing_axes[3] = { romi::kAxisX, romi::kAxisY, romi::kAxisZ };

nlohmann::json homing_settings = config["oquam"]["homing"];
nlohmann::json homing_settings = cnc_config["homing"];
homing_axes[0] = homing_settings["axes"][0];
homing_axes[1] = homing_settings["axes"][1];
homing_axes[2] = homing_settings["axes"][2];
Expand Down
4 changes: 2 additions & 2 deletions config/cablebot-v2-dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"hardware-id": "001",
"type": "CablebotV3"
},
"oquam": {
"cnc": {
"-controller-classname": "stepper-controller",
"cnc-range": [
[
Expand Down Expand Up @@ -112,7 +112,7 @@
}
},
"ports": {
"oquam": {
"cnc": {
"port": "/dev/ttyACM0",
"type": "serial"
}
Expand Down
Loading

0 comments on commit b18e781

Please sign in to comment.