Browse Source

Stepper class structure halfway there

feature/program_options
Maximilian Stiefel 4 years ago
parent
commit
4e344e5f9b
  1. 47
      hal/stepper.cpp
  2. 4
      include/stepper.h

47
hal/stepper.cpp

@ -7,47 +7,42 @@
namespace simon { namespace simon {
namespace stepper { namespace stepper {
c_stepper::c_stepper(const std::string& axis) c_stepper::c_stepper(const std::string& axis)
/* : m_a4988
{ {
.gpio_en = 17, auto file_content = read_file("settings.json");
.gpio_step = 18, auto j_set = json::parse(file_content);
.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};
if (!j_set["axis_configurations"].is_array()) { if (!j_set["axis_configurations"].is_array()) {
std::cout << "Expected an array in the configuration below \"axis_configurations\"." << "\n"; std::cout << "Expected an array in the configuration below \"axis_configurations\"." << "\n";
// TODO: Throw exception // 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; int ind = 0;
for (auto& j_axis_conf : j_set["axis_configurations"]) { for (const auto& j_axis_conf : j_set["axis_configurations"]) {
ind++; ind++;
if (j_axis_conf["id"].is_string()) { if (j_axis_conf["id"].is_string()) {
if (j_axis_conf["id"] == axis) { if (j_axis_conf["id"] == axis) {
a4988::a4988_settings_t a = { auto a_ptr = std::make_unique<a4988::a4988_settings_t>(
// TODO: Do more sanizing before retrieving this data a4988::a4988_settings_t ({
// TODO: Do more sanitizing before retrieving this data
.gpio_en = j_axis_conf["a4988"]["gpio_en"], .gpio_en = j_axis_conf["a4988"]["gpio_en"],
.gpio_step = j_axis_conf["a4988"]["gpio_step"], .gpio_step = j_axis_conf["a4988"]["gpio_step"],
.gpio_dir = j_axis_conf["a4988"]["gpio_dir"], .gpio_dir = j_axis_conf["a4988"]["gpio_dir"],
.gpio_microsteps = j_axis_conf["a4988"]["microsteps"] .gpio_microsteps = j_axis_conf["a4988"]["microsteps"]
}; })
auto a_ptr = std::make_unique<a4988::a4988_settings_t>(a); );
m_ptr_a4988 = std::make_unique<a4988::c_allegro_4988>(std::move(a_ptr)); m_ptr_a4988 = std::make_unique<a4988::c_allegro_4988>(std::move(a_ptr));
settings_t s = { m_stepper = {
// TODO: Do more sanizing before retrieving this data // TODO: Do more sanitizing before retrieving this data
.gear_reduction = j_axis_conf["stepper"]["gear_reduction"], .gear_reduction = j_axis_conf["stepper"]["gear_reduction"],
.steps_per_revolution = j_axis_conf["stepper"]["steps_per_revolution"] .steps_per_revolution = j_axis_conf["stepper"]["steps_per_revolution"]
}; };
m_stepper = s;
break; break;
} }
} else { } else {
@ -55,10 +50,6 @@ namespace simon {
std::cerr << "Expected a string in the configuration below [\"axis_configurations\"][" << ind << "][\"id\"]\n"; 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 std::string c_stepper::read_file(const std::string& fname) const

4
include/stepper.h

@ -3,6 +3,7 @@
#include <include/a4988.h> #include <include/a4988.h>
#include <lib/json.hpp> #include <lib/json.hpp>
#include <thread>
namespace simon { namespace simon {
namespace stepper { namespace stepper {
@ -21,10 +22,11 @@ namespace simon {
private: private:
settings_t m_stepper; settings_t m_stepper;
std::unique_ptr<a4988::c_allegro_4988> m_ptr_a4988; std::unique_ptr<a4988::c_allegro_4988> m_ptr_a4988;
std::thread m_thread;
private: private:
std::string read_file(const std::string& fname) const; std::string read_file(const std::string& fname) const;
void extract_settings(const json& j_set, const std::string& axis);
}; };
} // namespace stepper } // namespace stepper
} // namespace simon } // namespace simon

Loading…
Cancel
Save