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.
87 lines
2.2 KiB
87 lines
2.2 KiB
#include <include/a4988.h>
|
|
#include <pigpio.h>
|
|
#include <queue>
|
|
#include <cmath>
|
|
#include <thread>
|
|
|
|
#include <iostream>
|
|
|
|
namespace simon {
|
|
c_allegro_4988::c_allegro_4988(a4988_settings_t* pset)
|
|
: m_pset(pset), m_initialized(false), m_alive(true)
|
|
{
|
|
|
|
}
|
|
void c_allegro_4988::run(void)
|
|
{
|
|
if (!m_initialized) {
|
|
initialize();
|
|
}
|
|
while(m_alive)
|
|
{
|
|
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
|
|
}
|
|
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 {
|
|
m_alive = false;
|
|
}
|
|
}
|
|
}
|
|
void c_allegro_4988::post(move_t move)
|
|
{
|
|
m_queue.push(move);
|
|
}
|
|
void c_allegro_4988::initialize(void)
|
|
{
|
|
// TODO: proper error management throwing exceptions
|
|
if(m_pset == nullptr) {
|
|
return;
|
|
}
|
|
// Configure clock source for PWM
|
|
gpioCfgClock(CFG_SAMPLE_RATE_US, CFG_PERIPHERAL_SRC_PCM, CFG_DEPRECATED_ARG);
|
|
// Initialize pigpio library
|
|
gpioInitialise();
|
|
// Configure as outputs
|
|
gpioSetMode(m_pset->gpio_en, PI_OUTPUT);
|
|
gpioSetMode(m_pset->gpio_dir, PI_OUTPUT);
|
|
// Initialize outputs
|
|
enable();
|
|
gpioWrite(m_pset->gpio_dir, 0);
|
|
// Set PWM to 0
|
|
m_pwm_freq = 1000;
|
|
gpioHardwarePWM(m_pset->gpio_step, m_pwm_freq, 0);
|
|
m_initialized = true;
|
|
}
|
|
int c_allegro_4988::enable(void)
|
|
{
|
|
// TODO: proper error handling
|
|
gpioWrite(m_pset->gpio_en, 0);
|
|
return 0;
|
|
}
|
|
int c_allegro_4988::disable(void)
|
|
{
|
|
// TODO: proper error handling
|
|
gpioWrite(m_pset->gpio_en, 1);
|
|
return 0;
|
|
}
|
|
int c_allegro_4988::set_pwm_freq(const unsigned pwm_freq)
|
|
{
|
|
if (pwm_freq > PI_HW_PWM_MAX_FREQ) {
|
|
return 1;
|
|
}
|
|
m_pwm_freq = pwm_freq;
|
|
return 0;
|
|
}
|
|
|
|
}
|
|
|