From 4e344e5f9bd4521e20ef3723e6a13a42d8302afe Mon Sep 17 00:00:00 2001 From: Maximilian Stiefel Date: Wed, 17 Feb 2021 21:50:18 +0100 Subject: [PATCH] Stepper class structure halfway there --- hal/stepper.cpp | 55 ++++++++++++++++++++--------------------------- include/stepper.h | 4 +++- 2 files changed, 26 insertions(+), 33 deletions(-) diff --git a/hal/stepper.cpp b/hal/stepper.cpp index c717d35..fbd93b8 100644 --- a/hal/stepper.cpp +++ b/hal/stepper.cpp @@ -7,47 +7,42 @@ namespace simon { namespace stepper { c_stepper::c_stepper(const std::string& axis) -/* : m_a4988 - { - .gpio_en = 17, - .gpio_step = 18, - .gpio_dir = 22, - .gpio_microsteps{ - true, true, true - }, - m_stepper - { - .gear_reduction = 64.0f, - .steps_per_revolution = 32.0f - } -*/ { - auto j_set = json::parse(read_file("settings.json")); - a4988::a4988_settings_t a4988_set = {0}; + auto file_content = read_file("settings.json"); + auto j_set = json::parse(file_content); if (!j_set["axis_configurations"].is_array()) { std::cout << "Expected an array in the configuration below \"axis_configurations\"." << "\n"; // TODO: Throw exception } + extract_settings(j_set, axis); + m_thread = std::thread(std::bind(&a4988::c_allegro_4988::run, &(*m_ptr_a4988))); + m_ptr_a4988->post_move(32768, true, 100); + m_ptr_a4988->kill(); + m_thread.join(); + } + + void c_stepper::extract_settings(const json& j_set, const std::string& axis) + { int ind = 0; - for (auto& j_axis_conf : j_set["axis_configurations"]) { + for (const auto& j_axis_conf : j_set["axis_configurations"]) { ind++; if (j_axis_conf["id"].is_string()) { if (j_axis_conf["id"] == axis) { - a4988::a4988_settings_t a = { - // TODO: Do more sanizing before retrieving this data - .gpio_en = j_axis_conf["a4988"]["gpio_en"], - .gpio_step = j_axis_conf["a4988"]["gpio_step"], - .gpio_dir = j_axis_conf["a4988"]["gpio_dir"], - .gpio_microsteps = j_axis_conf["a4988"]["microsteps"] - }; - auto a_ptr = std::make_unique(a); + auto a_ptr = std::make_unique( + a4988::a4988_settings_t ({ + // TODO: Do more sanitizing before retrieving this data + .gpio_en = j_axis_conf["a4988"]["gpio_en"], + .gpio_step = j_axis_conf["a4988"]["gpio_step"], + .gpio_dir = j_axis_conf["a4988"]["gpio_dir"], + .gpio_microsteps = j_axis_conf["a4988"]["microsteps"] + }) + ); m_ptr_a4988 = std::make_unique(std::move(a_ptr)); - settings_t s = { - // TODO: Do more sanizing before retrieving this data + m_stepper = { + // TODO: Do more sanitizing before retrieving this data .gear_reduction = j_axis_conf["stepper"]["gear_reduction"], .steps_per_revolution = j_axis_conf["stepper"]["steps_per_revolution"] }; - m_stepper = s; break; } } else { @@ -55,10 +50,6 @@ namespace simon { std::cerr << "Expected a string in the configuration below [\"axis_configurations\"][" << ind << "][\"id\"]\n"; } } - std::thread t1(std::bind(&a4988::c_allegro_4988::run, &(*m_ptr_a4988))); - m_ptr_a4988->post_move(32768, true, 100); - m_ptr_a4988->kill(); - t1.join(); } std::string c_stepper::read_file(const std::string& fname) const diff --git a/include/stepper.h b/include/stepper.h index 3340d12..007705e 100644 --- a/include/stepper.h +++ b/include/stepper.h @@ -3,6 +3,7 @@ #include #include +#include namespace simon { namespace stepper { @@ -21,10 +22,11 @@ namespace simon { private: settings_t m_stepper; std::unique_ptr m_ptr_a4988; + std::thread m_thread; private: std::string read_file(const std::string& fname) const; + void extract_settings(const json& j_set, const std::string& axis); }; - } // namespace stepper } // namespace simon