Simon's and Max' camera steering software. https://stiefel.tech
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

70 lines
2.4 KiB

#include <include/stepper.h>
#include <thread>
#include <functional>
#include <iostream>
#include <fstream>
namespace simon {
namespace stepper {
c_stepper::c_stepper(const std::string& axis)
{
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 (const auto& j_axis_conf : j_set["axis_configurations"]) {
ind++;
if (j_axis_conf["id"].is_string()) {
if (j_axis_conf["id"] == axis) {
auto a_ptr = std::make_unique<a4988::a4988_settings_t>(
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<a4988::c_allegro_4988>(std::move(a_ptr));
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"]
};
break;
}
} else {
// TODO:Throw exception
std::cerr << "Expected a string in the configuration below [\"axis_configurations\"][" << ind << "][\"id\"]\n";
}
}
}
std::string c_stepper::read_file(const std::string& fname) const
{
std::ifstream input_stream(fname);
if (!input_stream) {
// TODO: Throw exception here
std::cerr << "Can't open input file!";
}
std::string line;
std::string jstring;
while (getline(input_stream, line)) {
jstring += line;
}
return jstring;
}
}
}