Skip to content

Commit

Permalink
refactor to step / dir pins
Browse files Browse the repository at this point in the history
  • Loading branch information
mwood77 committed Oct 24, 2024
1 parent 9be4153 commit d42f859
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions src/platformio/osww-server/src/utils/MotorControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,43 @@
#if STEPPER_MOTOR_CONTROL
#include <TMC2209.h>

// Instantiate TMC2209
TMC2209 stepper_driver;
HardwareSerial & serial_stream = Serial1;

const int RX_PIN = 5;
const int TX_PIN = 26;
const uint8_t HARDWARE_ENABLE_PIN = 4;
HardwareSerial & serial_stream = Serial2;

const long SERIAL_BAUD_RATE = 115200;
const uint8_t STEP_PIN = 17;
const uint8_t DIRECTION_PIN = 16;

const int32_t RUN_VELOCITY = 20000;
const int32_t STOP_VELOCITY = 0;

const uint16_t HALF_STEP_DURATION_MICROSECONDS = 10;

// current values may need to be reduced to prevent overheating depending on
// specific motor and power supply voltage
const uint8_t RUN_CURRENT_PERCENT = 100;

// Instantiate TMC2209
TMC2209 stepper_driver;

#endif

MotorControl::MotorControl(int pinA, int pinB, bool pwmMotorControl)
{
#if STEPPER_MOTOR_CONTROL
stepper_driver.setup(serial_stream, SERIAL_BAUD_RATE, TMC2209::SERIAL_ADDRESS_0, RX_PIN, TX_PIN);
stepper_driver.setHardwareEnablePin(HARDWARE_ENABLE_PIN);

stepper_driver.setup(serial_stream);

pinMode(STEP_PIN, OUTPUT);
pinMode(DIRECTION_PIN, OUTPUT);

stepper_driver.setRunCurrent(RUN_CURRENT_PERCENT);
stepper_driver.enableCoolStep();
stepper_driver.enable();

_motorDirection = 0;
#else
_pinA = pinA;
_pinB = pinB;
_motorDirection = 0;
_pwmMotorControl = pwmMotorControl;
_pinA = pinA;
_pinB = pinB;
_motorDirection = 0;
_pwmMotorControl = pwmMotorControl;
#endif
}

Expand All @@ -53,7 +56,8 @@ void MotorControl::clockwise()
MX1508 pwmControl(_pinA, _pinB, CH1, CH2);
pwmControl.motorGo(motorSpeed);
#elif STEPPER_MOTOR_CONTROL
stepper_driver.moveAtVelocity(RUN_VELOCITY);
// stepper_driver.moveAtVelocity(RUN_VELOCITY);
stepper_driver.moveUsingStepDirInterface();
#else
digitalWrite(_pinA, HIGH);
digitalWrite(_pinB, LOW);
Expand All @@ -67,7 +71,8 @@ void MotorControl::countClockwise()
MX1508 pwmControl(_pinA, _pinB, CH1, CH2);
pwmControl.motorRev(motorSpeed);
#elif STEPPER_MOTOR_CONTROL
stepper_driver.moveAtVelocity(RUN_VELOCITY);
// stepper_driver.moveAtVelocity(RUN_VELOCITY);
stepper_driver.moveUsingStepDirInterface();
#else
digitalWrite(_pinA, LOW);
digitalWrite(_pinB, HIGH);
Expand All @@ -82,6 +87,7 @@ void MotorControl::stop()
pwmControl.motorBrake();
#elif STEPPER_MOTOR_CONTROL
stepper_driver.moveAtVelocity(STOP_VELOCITY);
stepper_driver.moveUsingStepDirInterface();
#else
digitalWrite(_pinA, LOW);
digitalWrite(_pinB, LOW);
Expand Down

0 comments on commit d42f859

Please sign in to comment.