Browse Source

Integrated lazy settings reading from file

feature/build_system
Maximilian Stiefel 3 years ago
parent
commit
721ee1c680
  1. 22
      drivers/a4988.cpp
  2. 53
      hal/stepper.cpp
  3. 6
      include/a4988.h
  4. 15
      include/stepper.h
  5. 1710
      lib/json.hpp
  6. 18
      settings.json

22
drivers/a4988.cpp

@ -10,8 +10,8 @@
namespace simon { namespace simon {
namespace a4988 { namespace a4988 {
c_allegro_4988::c_allegro_4988(a4988_settings_t* pset) c_allegro_4988::c_allegro_4988(std::unique_ptr<a4988_settings_t> ptr_set)
: m_pset(pset), m_initialized(false), m_alive(true) : m_ptr_set(std::move(ptr_set)), m_initialized(false), m_alive(true)
{ {
} }
@ -31,16 +31,16 @@ namespace simon {
if (!set_pwm_freq(std::round(1e6/move->period_us))) { if (!set_pwm_freq(std::round(1e6/move->period_us))) {
// TODO: proper error handling // TODO: proper error handling
} }
gpioHardwarePWM(m_pset->gpio_step, m_pwm_freq, PI_HW_PWM_RANGE/2); gpioHardwarePWM(m_ptr_set->gpio_step, m_pwm_freq, PI_HW_PWM_RANGE/2);
gpioWrite(m_pset->gpio_dir, move->dir); gpioWrite(m_ptr_set->gpio_dir, move->dir);
std::cout << move->period_us << " - " << move->steps << std::endl; std::cout << move->period_us << " - " << move->steps << std::endl;
auto t_sleep = move->period_us*move->steps; auto t_sleep = move->period_us*move->steps;
std::this_thread::sleep_for(std::chrono::microseconds(t_sleep)); std::this_thread::sleep_for(std::chrono::microseconds(t_sleep));
gpioHardwarePWM(m_pset->gpio_step, m_pwm_freq, 0); gpioHardwarePWM(m_ptr_set->gpio_step, m_pwm_freq, 0);
} }
else if (*cmd == "enable") { else if (*cmd == "enable") {
auto enable = std::static_pointer_cast<c_enable>(cmd); auto enable = std::static_pointer_cast<c_enable>(cmd);
gpioWrite(m_pset->gpio_en, enable->enabled); gpioWrite(m_ptr_set->gpio_en, enable->enabled);
} }
else if (*cmd == "kill") { else if (*cmd == "kill") {
m_alive = false; m_alive = false;
@ -70,7 +70,7 @@ namespace simon {
void c_allegro_4988::initialize(void) void c_allegro_4988::initialize(void)
{ {
// TODO: proper error management throwing exceptions // TODO: proper error management throwing exceptions
if(m_pset == nullptr) { if(m_ptr_set == nullptr) {
return; return;
} }
// Configure clock source for PWM // Configure clock source for PWM
@ -78,14 +78,14 @@ namespace simon {
// Initialize pigpio library // Initialize pigpio library
gpioInitialise(); gpioInitialise();
// Configure as outputs // Configure as outputs
gpioSetMode(m_pset->gpio_en, PI_OUTPUT); gpioSetMode(m_ptr_set->gpio_en, PI_OUTPUT);
gpioSetMode(m_pset->gpio_dir, PI_OUTPUT); gpioSetMode(m_ptr_set->gpio_dir, PI_OUTPUT);
// Initialize outputs // Initialize outputs
enable(); enable();
gpioWrite(m_pset->gpio_dir, 0); gpioWrite(m_ptr_set->gpio_dir, 0);
// Set PWM to 0 // Set PWM to 0
m_pwm_freq = 1000; m_pwm_freq = 1000;
gpioHardwarePWM(m_pset->gpio_step, m_pwm_freq, 0); gpioHardwarePWM(m_ptr_set->gpio_step, m_pwm_freq, 0);
m_initialized = true; m_initialized = true;
} }
int c_allegro_4988::enable(void) int c_allegro_4988::enable(void)

53
hal/stepper.cpp

@ -1,11 +1,12 @@
#include<include/stepper.h> #include <include/stepper.h>
#include <thread> #include <thread>
#include <functional> #include <functional>
#include <iostream>
#include <fstream>
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(&m_a4988_set)
/* : m_a4988 /* : m_a4988
{ {
.gpio_en = 17, .gpio_en = 17,
@ -21,10 +22,52 @@ namespace simon {
} }
*/ */
{ {
std::thread t1(std::bind(&a4988::c_allegro_4988::run, &m_a4988)); auto j_set = json::parse(read_file("settings.json"));
m_a4988.post_move(32768, true, 100); a4988::a4988_settings_t a4988_set = {0};
m_a4988.kill(); if (!j_set["motors"].is_array()) {
std::cout << "Expected an array in the configuration below \"motors\"." << "\n";
// TODO: Throw exception
}
int ind = 0;
for (auto& motor : j_set["motors"]) {
ind++;
if (motor["id"].is_string()) {
if (motor["id"] == axis) {
a4988::a4988_settings_t a = {
// TODO: Do more sanizing before retrieving this data
.gpio_en = motor["a4988"]["gpio_en"],
.gpio_step = motor["a4988"]["gpio_step"],
.gpio_dir = motor["a4988"]["gpio_dir"],
.gpio_microsteps = motor["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));
break;
}
} else {
// TODO:Throw exception
std::cerr << "Expected a string in the configuration below [\"motors\"][" << 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(); t1.join();
} }
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;
}
} }
} }

6
include/a4988.h

@ -16,7 +16,7 @@ namespace simon {
const unsigned gpio_en; const unsigned gpio_en;
const unsigned gpio_step; const unsigned gpio_step;
const unsigned gpio_dir; const unsigned gpio_dir;
const bool gpio_microsteps[3]; const unsigned gpio_microsteps;
} a4988_settings_t; } a4988_settings_t;
class c_cmd class c_cmd
@ -60,7 +60,7 @@ namespace simon {
class c_allegro_4988 class c_allegro_4988
{ {
public: public:
explicit c_allegro_4988(a4988_settings_t* set); explicit c_allegro_4988(std::unique_ptr<a4988_settings_t> ptr_set);
int enable(void); int enable(void);
int disable(void); int disable(void);
void kill(void); void kill(void);
@ -69,7 +69,7 @@ namespace simon {
const unsigned period_us); const unsigned period_us);
void run(void); void run(void);
private: private:
a4988_settings_t* m_pset; std::unique_ptr<a4988_settings_t> m_ptr_set;
bool m_initialized; bool m_initialized;
std::queue<std::shared_ptr<c_cmd>> m_queue; std::queue<std::shared_ptr<c_cmd>> m_queue;
unsigned m_pwm_freq; unsigned m_pwm_freq;

15
include/stepper.h

@ -2,10 +2,13 @@
#define INCLUDED_HAL_STEPPER #define INCLUDED_HAL_STEPPER
#include <include/a4988.h> #include <include/a4988.h>
#include <lib/json.hpp>
namespace simon { namespace simon {
namespace stepper { namespace stepper {
using json = nlohmann::json;
typedef struct { typedef struct {
float gear_reduction; float gear_reduction;
float steps_per_revolution; float steps_per_revolution;
@ -17,15 +20,9 @@ namespace simon {
c_stepper(const std::string& axis); c_stepper(const std::string& axis);
private: private:
settings_t m_stepper; settings_t m_stepper;
a4988::c_allegro_4988 m_a4988; std::unique_ptr<a4988::c_allegro_4988> m_ptr_a4988;
a4988::a4988_settings_t m_a4988_set = { private:
.gpio_en = 17, std::string read_file(const std::string& fname) const;
.gpio_step = 18,
.gpio_dir = 22,
.gpio_microsteps{
true, true, true
}
};
}; };
} // namespace stepper } // namespace stepper

1710
lib/json.hpp

File diff suppressed because it is too large

18
settings.json

@ -2,16 +2,16 @@
"motors": [ "motors": [
{ {
"stepper": { "stepper": {
"gear_reduction": 64.0, "gear_reduction": 64.0,
"steps_per_revolution": 32 "steps_per_revolution": 32
}, },
"ad4988": { "a4988": {
"microsteps": 16, "microsteps": 16,
"gpio_dir": 18, "gpio_dir": 22,
"gpio_en": 17, "gpio_en": 17,
"gpio_step": 22 "gpio_step": 18
}, },
"id": "azimuth" "id": "azimuth"
} }
] ]
} }

Loading…
Cancel
Save