Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stepper motor control w/ TMC2209 #69

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ build_src_filter = +<*> -<./angular/>
board_build.filesystem = littlefs
check_tool = cppcheck, clangtidy
build_flags =
-D OLED_ENABLED=false
-D OLED_ENABLED=true
-D PWM_MOTOR_CONTROL=false
-D STEPPER_MOTOR_CONTROL=true
-D HOME_ASSISTANT_ENABLED=false
Comment on lines +24 to 27
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set all to false before merging

check_flags =
clangtidy: -fix-errors,--format-style=google
Expand All @@ -36,3 +37,4 @@ lib_deps =
adafruit/Adafruit SSD1306@^2.5.9
electromagus/ESPMX1508@^1.0.5
dawidchyrzynski/home-assistant-integration@^2.1.0
janelia-arduino/TMC2209@^9.4.2
20 changes: 17 additions & 3 deletions src/platformio/osww-server/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,19 @@ AsyncWebServer server(80);
HTTPClient http;
WiFiClient client;
ESP32Time rtc;
String winderooVersion = "3.0.0";
String winderooVersion = "3.1.0";

#if PWM_MOTOR_CONTROL
#if PWM_MOTOR_CONTROL && STEPPER_MOTOR_CONTROL
#error "You can only have one optional motor control method enabled at a time. You must disable either PWM_MOTOR_CONTROL or STEPPER_MOTOR_CONTROL in the platformio.ini file."
#endif

// PWM control
#if PWM_MOTOR_CONTROL && !STEPPER_MOTOR_CONTROL
MotorControl motor(directionalPinA, directionalPinB, true);
#else
#endif

// Stepper motor control || Regular DC motor control
#if STEPPER_MOTOR_CONTROL && !PWM_MOTOR_CONTROL || !STEPPER_MOTOR_CONTROL && !WM_MOTOR_CONTROL
MotorControl motor(directionalPinA, directionalPinB);
#endif

Expand Down Expand Up @@ -1184,6 +1192,11 @@ void setup()
display.invertDisplay(OLED_INVERT_SCREEN);
display.setRotation(rotate);
drawNotification("Winderoo");


String savedNetworkMessage[2] = {"Winderoo build", "v" + winderooVersion};
drawMultiLineText(savedNetworkMessage);
delay(1200);
}

String savedNetworkMessage[2] = {"Connecting to", "saved network..."};
Expand Down Expand Up @@ -1302,6 +1315,7 @@ void setup()

drawNotification("Starting webserver...");
startWebserver();
delay(750); // delay to show notification

if (strcmp(userDefinedSettings.status.c_str(), "Winding") == 0)
{
Expand Down
53 changes: 48 additions & 5 deletions src/platformio/osww-server/src/utils/MotorControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,53 @@
int motorSpeed = 145;
#endif

#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;

const long SERIAL_BAUD_RATE = 115200;
const int32_t RUN_VELOCITY = 20000;
const int32_t STOP_VELOCITY = 0;


// 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;

#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.setRunCurrent(RUN_CURRENT_PERCENT);
stepper_driver.enableCoolStep();
stepper_driver.enable();

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

void MotorControl::clockwise()
{
#if PWM_MOTOR_CONTROL
MX1508 pwmControl(_pinA, _pinB, CH1, CH2);
pwmControl.motorGo(motorSpeed);
#elif STEPPER_MOTOR_CONTROL
stepper_driver.moveAtVelocity(RUN_VELOCITY);
#else
digitalWrite(_pinA, HIGH);
digitalWrite(_pinB, LOW);
Expand All @@ -32,6 +66,8 @@ void MotorControl::countClockwise()
#if PWM_MOTOR_CONTROL
MX1508 pwmControl(_pinA, _pinB, CH1, CH2);
pwmControl.motorRev(motorSpeed);
#elif STEPPER_MOTOR_CONTROL
stepper_driver.moveAtVelocity(RUN_VELOCITY);
#else
digitalWrite(_pinA, LOW);
digitalWrite(_pinB, HIGH);
Expand All @@ -44,6 +80,8 @@ void MotorControl::stop()
#if PWM_MOTOR_CONTROL
MX1508 pwmControl(_pinA, _pinB, CH1, CH2);
pwmControl.motorBrake();
#elif STEPPER_MOTOR_CONTROL
stepper_driver.moveAtVelocity(STOP_VELOCITY);
#else
digitalWrite(_pinA, LOW);
digitalWrite(_pinB, LOW);
Expand All @@ -53,16 +91,21 @@ void MotorControl::stop()

void MotorControl::determineMotorDirectionAndBegin()
{
// @todo - investigate if this is still needed
// stop();

if (_motorDirection)
{
clockwise();
#if STEPPER_MOTOR_CONTROL
stepper_driver.disableInverseMotorDirection();
#else
clockwise();
#endif
}
else
{
countClockwise();
#if STEPPER_MOTOR_CONTROL
stepper_driver.enableInverseMotorDirection();
#else
countClockwise();
#endif
}
}

Expand Down
Loading