Browse Source

Introduced polymorphic interface for A4988 driver

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

2
.gitignore

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

33
drivers/a4988.cpp

@ -3,10 +3,13 @@
#include <queue>
#include <cmath>
#include <thread>
#include <string>
#include <memory>
#include <iostream>
namespace simon {
namespace a4988 {
c_allegro_4988::c_allegro_4988(a4988_settings_t* pset)
: m_pset(pset), m_initialized(false), m_alive(true)
{
@ -21,16 +24,20 @@ namespace simon {
{
std::cout << "In thread" << std::endl;
if (!m_queue.empty()) {
auto& move = m_queue.front();
if (!set_pwm_freq(std::round(1e6/move.period_us))) {
// TODO: proper error handling
auto cmd = m_queue.front();
// 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
}
gpioHardwarePWM(m_pset->gpio_step, m_pwm_freq, PI_HW_PWM_RANGE/2);
gpioWrite(m_pset->gpio_dir, move->dir);
std::cout << move->period_us << " - " << move->steps << std::endl;
auto t_sleep = move->period_us*move->steps;
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, PI_HW_PWM_RANGE/2);
gpioWrite(m_pset->gpio_dir, move.dir);
std::cout << move.period_us << " - " << move.steps << std::endl;
auto t_sleep = move.period_us*move.steps;
std::this_thread::sleep_for(std::chrono::microseconds(t_sleep));
gpioHardwarePWM(m_pset->gpio_step, m_pwm_freq, 0);
m_queue.pop();
}
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)
{
@ -83,5 +90,5 @@ namespace simon {
m_pwm_freq = pwm_freq;
return 0;
}
}
} // namespace a4988
} // namespace simon

90
include/a4988.h

@ -6,44 +6,66 @@
#define CFG_DEPRECATED_ARG 0
#include <queue>
#include <string>
#include <memory>
namespace simon {
namespace a4988 {
typedef struct
{
const unsigned gpio_en;
const unsigned gpio_step;
const unsigned gpio_dir;
const bool gpio_microsteps[3];
} a4988_settings_t;
typedef struct
{
const unsigned gpio_en;
const unsigned gpio_step;
const unsigned gpio_dir;
const bool gpio_microsteps[3];
} a4988_settings_t;
typedef struct
{
unsigned steps;
bool dir;
unsigned period_us;
} move_t;
class c_allegro_4988
{
public:
c_allegro_4988(a4988_settings_t* set);
int enable(void);
int disable(void);
void post(move_t move);
void run(void);
private:
a4988_settings_t* m_pset;
bool m_initialized;
std::queue<move_t> m_queue;
unsigned m_pwm_freq;
bool m_alive;
private:
void initialize(void);
int set_pwm_freq(const unsigned pwm_freq);
};
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;
bool dir;
unsigned period_us;
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
{
public:
c_allegro_4988(a4988_settings_t* set);
int enable(void);
int disable(void);
void post(std::shared_ptr<c_cmd> cmd);
void run(void);
private:
a4988_settings_t* m_pset;
bool m_initialized;
std::queue<std::shared_ptr<c_cmd>> m_queue;
unsigned m_pwm_freq;
bool m_alive;
private:
void initialize(void);
int set_pwm_freq(const unsigned pwm_freq);
};
} // namespace a4988
} // namespace simon
#endif /*INCLUDED_DRIVER_ALLEGRO_4988*/

11
main.cpp

@ -2,10 +2,11 @@
#include <include/a4988.h>
#include <thread>
#include <functional>
#include <memory>
int main()
{
using namespace simon;
using namespace simon::a4988;
a4988_settings_t a4988_set{
.gpio_en = 17,
.gpio_step = 18,
@ -15,9 +16,13 @@ int main()
}
};
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));
a4988.post(move);
a4988.post(cmd);
t1.join();
std::cout << "Hello camera" << std::endl;
return 0;

Loading…
Cancel
Save