diff --git a/drivers/a4988.cpp b/drivers/a4988.cpp index 07ff348..2061c1b 100644 --- a/drivers/a4988.cpp +++ b/drivers/a4988.cpp @@ -119,5 +119,9 @@ namespace simon { m_pwm_freq = pwm_freq; return 0; } + unsigned c_allegro_4988::get_microsteps(void) + { + return m_ptr_set->gpio_microsteps; + } } // namespace a4988 } // namespace simon diff --git a/hal/stepper.cpp b/hal/stepper.cpp index fbd93b8..536d9d6 100644 --- a/hal/stepper.cpp +++ b/hal/stepper.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include namespace simon { namespace stepper { @@ -16,7 +18,8 @@ namespace simon { } 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); + rotate(180); + rotate(-180); m_ptr_a4988->kill(); m_thread.join(); } @@ -41,7 +44,8 @@ namespace simon { 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"] + .steps_per_revolution = j_axis_conf["stepper"]["steps_per_revolution"], + .clockwise = j_axis_conf["stepper"]["clockwise"] }; break; } @@ -66,5 +70,17 @@ namespace simon { } return jstring; } + + float c_stepper::rotate(const float& degrees) + { + auto steps_360 = m_stepper.gear_reduction + * m_stepper.steps_per_revolution + * static_cast(m_ptr_a4988->get_microsteps()); + auto steps = static_cast((std::abs(degrees)/360.0f) * steps_360); + bool direction = (degrees < 0) ? !m_stepper.clockwise : m_stepper.clockwise; + std::cout << "dir >" << direction << std::endl; + m_ptr_a4988->post_move(steps, direction, 100); + return steps; + } } } diff --git a/include/a4988.h b/include/a4988.h index e8cec1a..91ad5f2 100644 --- a/include/a4988.h +++ b/include/a4988.h @@ -68,6 +68,7 @@ namespace simon { void post_move(const unsigned steps, const bool dir, const unsigned period_us); void run(void); + unsigned get_microsteps(void); private: std::unique_ptr m_ptr_set; bool m_initialized; diff --git a/include/stepper.h b/include/stepper.h index 007705e..c627f2e 100644 --- a/include/stepper.h +++ b/include/stepper.h @@ -13,12 +13,14 @@ namespace simon { typedef struct { float gear_reduction; float steps_per_revolution; + bool clockwise; } settings_t; class c_stepper { public: c_stepper(const std::string& axis); + float rotate(const float& degrees); private: settings_t m_stepper; std::unique_ptr m_ptr_a4988; diff --git a/settings.json b/settings.json index 7efe52e..0a7e913 100644 --- a/settings.json +++ b/settings.json @@ -3,7 +3,8 @@ { "stepper": { "gear_reduction": 64.0, - "steps_per_revolution": 32 + "steps_per_revolution": 32, + "clockwise" : true }, "a4988": { "microsteps": 16,