From 29abfa48ef1edbcaeecf5d22f899c09f3cde88d2 Mon Sep 17 00:00:00 2001 From: Rob Giseburt Date: Thu, 15 Mar 2018 14:33:28 -0500 Subject: [PATCH] Minor cleanup with enable and step polarity --- .../device/step_dir_driver/step_dir_driver.h | 39 +++++++++++++------ g2core/stepper.cpp | 6 +-- g2core/stepper.h | 8 ++-- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/g2core/device/step_dir_driver/step_dir_driver.h b/g2core/device/step_dir_driver/step_dir_driver.h index 0b1cb526..5433a62a 100644 --- a/g2core/device/step_dir_driver/step_dir_driver.h +++ b/g2core/device/step_dir_driver/step_dir_driver.h @@ -37,6 +37,7 @@ using Motate::pin_number; using Motate::OutputPin; using Motate::PWMOutputPin; using Motate::kStartHigh; +using Motate::kStartLow; using Motate::kNormal; using Motate::Timeout; @@ -61,13 +62,16 @@ struct StepDirStepper final : Stepper { OutputPin _ms2; PWMOutputPin _vref; - ioMode _enable_polarity; // 0=active HIGH, 1=active LOW + ioMode _step_polarity; // IO_ACTIVE_LOW or IO_ACTIVE_HIGH + ioMode _enable_polarity; // IO_ACTIVE_LOW or IO_ACTIVE_HIGH // sets default pwm freq for all motor vrefs (commented line below also sets HiZ) - StepDirStepper(ioMode enable_polarity = IO_ACTIVE_LOW, const uint32_t frequency = 250000) : - Stepper{enable_polarity}, + StepDirStepper(ioMode step_polarity, ioMode enable_polarity, const uint32_t frequency = 250000) : + Stepper{}, + _step{step_polarity==IO_ACTIVE_LOW?kStartHigh:kStartLow}, _enable{enable_polarity==IO_ACTIVE_LOW?kStartHigh:kStartLow}, _vref{kNormal, frequency}, + _step_polarity{step_polarity}, _enable_polarity{enable_polarity} {}; @@ -141,17 +145,17 @@ struct StepDirStepper final : Stepper { }; void stepStart() override { - if (_invert_step) - _step.clear(); - else - _step.set(); + if (_step_polarity == IO_ACTIVE_LOW) + _step.clear(); + else + _step.set(); }; void stepEnd() override { - if (_invert_step) - _step.set(); - else - _step.clear(); + if (_step_polarity == IO_ACTIVE_LOW) + _step.set(); + else + _step.clear(); }; void setDirection(uint8_t new_direction) override { @@ -170,7 +174,18 @@ struct StepDirStepper final : Stepper { } }; - ioMode getEnablePolarity() override + ioMode getStepPolarity() const override + { + return _step_polarity; + }; + + void setStepPolarity(ioMode new_sp) override + { + _step_polarity = new_sp; + stepEnd(); + }; + + ioMode getEnablePolarity() const override { return _enable_polarity; }; diff --git a/g2core/stepper.cpp b/g2core/stepper.cpp index 04fc9ab2..29f2e527 100644 --- a/g2core/stepper.cpp +++ b/g2core/stepper.cpp @@ -1050,7 +1050,7 @@ stat_t st_set_sp(nvObj_t *nv) // set motor step polarity uint8_t motor = _get_motor(nv->index); if (motor > MOTORS) { return STAT_INPUT_VALUE_RANGE_ERROR; }; - Motors[motor]->setStepPolarity((int)nv->value); + Motors[motor]->setStepPolarity((ioMode)nv->value); return (STAT_OK); } @@ -1148,7 +1148,7 @@ static const char fmt_0mi[] = "[%s%s] m%s microsteps%16d [1,2,4,8,16,32]\n"; static const char fmt_0su[] = "[%s%s] m%s steps per unit %17.5f steps per%s\n"; static const char fmt_0po[] = "[%s%s] m%s polarity%18d [0=normal,1=reverse]\n"; static const char fmt_0ep[] = "[%s%s] m%s enable polarity%11d [0=active HIGH,1=active LOW]\n"; -static const char fmt_0ps[] = "[%s%s] m%s step polarity%13d [0=active HIGH,1=active LOW]\n"; +static const char fmt_0sp[] = "[%s%s] m%s step polarity%13d [0=active HIGH,1=active LOW]\n"; static const char fmt_0pm[] = "[%s%s] m%s power management%10d [0=disabled,1=always on,2=in cycle,3=when moving]\n"; static const char fmt_0pl[] = "[%s%s] m%s motor power level%13.3f [0.000=minimum, 1.000=maximum]\n"; static const char fmt_pwr[] = "[%s%s] Motor %c power level:%12.3f\n"; @@ -1188,7 +1188,7 @@ void st_print_mi(nvObj_t *nv) { _print_motor_int(nv, fmt_0mi);} void st_print_su(nvObj_t *nv) { _print_motor_flt_units(nv, fmt_0su, cm_get_units_mode(MODEL));} void st_print_po(nvObj_t *nv) { _print_motor_int(nv, fmt_0po);} void st_print_ep(nvObj_t *nv) { _print_motor_int(nv, fmt_0ep);} -void st_print_ps(nvObj_t *nv) { _print_motor_int(nv, fmt_0ps);} +void st_print_sp(nvObj_t *nv) { _print_motor_int(nv, fmt_0sp);} void st_print_pm(nvObj_t *nv) { _print_motor_int(nv, fmt_0pm);} void st_print_pl(nvObj_t *nv) { _print_motor_flt(nv, fmt_0pl);} void st_print_pwr(nvObj_t *nv){ _print_motor_pwr(nv, fmt_pwr);} diff --git a/g2core/stepper.h b/g2core/stepper.h index 857b132b..f899ad60 100644 --- a/g2core/stepper.h +++ b/g2core/stepper.h @@ -458,7 +458,7 @@ public: } }; - virtual ioMode getEnablePolarity() + virtual ioMode getEnablePolarity() const { return IO_ACTIVE_LOW; // we have to say something here }; @@ -467,12 +467,12 @@ public: { // do nothing }; - - virtual ioMode getStepPolarity() + + virtual ioMode getStepPolarity() const { return IO_ACTIVE_LOW; // we have to say something here }; - + virtual void setStepPolarity(ioMode new_mp) { // do nothing