Browse Source

Introduced polymorphic interface for A4988 driver

feature/build_system
Maximilian Stiefel 4 years ago
parent
commit
4b177d4c19
  1. 2
      .gitignore
  2. 25
      drivers/a4988.cpp
  3. 34
      include/a4988.h
  4. 11
      main.cpp

2
.gitignore

@ -1,4 +1,4 @@
*.swp
main main
# Created by https://www.toptal.com/developers/gitignore/api/c++ # Created by https://www.toptal.com/developers/gitignore/api/c++

25
drivers/a4988.cpp

@ -3,10 +3,13 @@
#include <queue> #include <queue>
#include <cmath> #include <cmath>
#include <thread> #include <thread>
#include <string>
#include <memory>
#include <iostream> #include <iostream>
namespace simon { namespace simon {
namespace a4988 {
c_allegro_4988::c_allegro_4988(a4988_settings_t* pset) c_allegro_4988::c_allegro_4988(a4988_settings_t* pset)
: m_pset(pset), m_initialized(false), m_alive(true) : m_pset(pset), m_initialized(false), m_alive(true)
{ {
@ -21,16 +24,20 @@ namespace simon {
{ {
std::cout << "In thread" << std::endl; std::cout << "In thread" << std::endl;
if (!m_queue.empty()) { if (!m_queue.empty()) {
auto& move = m_queue.front(); auto cmd = m_queue.front();
if (!set_pwm_freq(std::round(1e6/move.period_us))) { // Distinguish between different tytpes of commands and derive type
if (*cmd == "move") {
auto move = std::static_pointer_cast<c_move>(cmd);
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_pset->gpio_step, m_pwm_freq, PI_HW_PWM_RANGE/2);
gpioWrite(m_pset->gpio_dir, move.dir); gpioWrite(m_pset->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_pset->gpio_step, m_pwm_freq, 0);
}
m_queue.pop(); m_queue.pop();
} }
else { else {
@ -38,9 +45,9 @@ namespace simon {
} }
} }
} }
void c_allegro_4988::post(move_t move) void c_allegro_4988::post(std::shared_ptr<c_cmd> cmd)
{ {
m_queue.push(move); m_queue.push(cmd);
} }
void c_allegro_4988::initialize(void) void c_allegro_4988::initialize(void)
{ {
@ -83,5 +90,5 @@ namespace simon {
m_pwm_freq = pwm_freq; m_pwm_freq = pwm_freq;
return 0; return 0;
} }
} // namespace a4988
} } // namespace simon

34
include/a4988.h

@ -6,9 +6,11 @@
#define CFG_DEPRECATED_ARG 0 #define CFG_DEPRECATED_ARG 0
#include <queue> #include <queue>
#include <string>
#include <memory>
namespace simon { namespace simon {
namespace a4988 {
typedef struct typedef struct
{ {
const unsigned gpio_en; const unsigned gpio_en;
@ -17,13 +19,33 @@ namespace simon {
const bool gpio_microsteps[3]; const bool gpio_microsteps[3];
} a4988_settings_t; } a4988_settings_t;
typedef struct class c_cmd
{
public:
explicit c_cmd(const std::string cmd) : m_cmd(cmd) {}
bool operator==(const std::string& vgl) {
return (vgl==m_cmd ? true : false);
}
virtual ~c_cmd(void) noexcept {}
private:
const std::string m_cmd;
};
class c_move : public c_cmd
{ {
public:
unsigned steps; unsigned steps;
bool dir; bool dir;
unsigned period_us; unsigned period_us;
} move_t; explicit c_move(void) : c_cmd("move"){}
};
class c_enable : public c_cmd
{
public:
bool enabled;
explicit c_enable(void) : c_cmd("enable"){}
};
class c_allegro_4988 class c_allegro_4988
{ {
@ -31,19 +53,19 @@ namespace simon {
c_allegro_4988(a4988_settings_t* set); c_allegro_4988(a4988_settings_t* set);
int enable(void); int enable(void);
int disable(void); int disable(void);
void post(move_t move); void post(std::shared_ptr<c_cmd> cmd);
void run(void); void run(void);
private: private:
a4988_settings_t* m_pset; a4988_settings_t* m_pset;
bool m_initialized; bool m_initialized;
std::queue<move_t> m_queue; std::queue<std::shared_ptr<c_cmd>> m_queue;
unsigned m_pwm_freq; unsigned m_pwm_freq;
bool m_alive; bool m_alive;
private: private:
void initialize(void); void initialize(void);
int set_pwm_freq(const unsigned pwm_freq); int set_pwm_freq(const unsigned pwm_freq);
}; };
} // namespace a4988
} // namespace simon } // namespace simon
#endif /*INCLUDED_DRIVER_ALLEGRO_4988*/ #endif /*INCLUDED_DRIVER_ALLEGRO_4988*/

11
main.cpp

@ -2,10 +2,11 @@
#include <include/a4988.h> #include <include/a4988.h>
#include <thread> #include <thread>
#include <functional> #include <functional>
#include <memory>
int main() int main()
{ {
using namespace simon; using namespace simon::a4988;
a4988_settings_t a4988_set{ a4988_settings_t a4988_set{
.gpio_en = 17, .gpio_en = 17,
.gpio_step = 18, .gpio_step = 18,
@ -15,9 +16,13 @@ int main()
} }
}; };
c_allegro_4988 a4988(&a4988_set); c_allegro_4988 a4988(&a4988_set);
move_t move{32768, true, 100}; auto move(std::make_shared<c_move>());
move->steps = 32768;
move->dir = true;
move->period_us = 1000;
auto cmd = std::static_pointer_cast<c_cmd>(move);
std::thread t1(std::bind(&c_allegro_4988::run, &a4988)); std::thread t1(std::bind(&c_allegro_4988::run, &a4988));
a4988.post(move); a4988.post(cmd);
t1.join(); t1.join();
std::cout << "Hello camera" << std::endl; std::cout << "Hello camera" << std::endl;
return 0; return 0;

Loading…
Cancel
Save