2019-09-07 13:03:07 +01:00
|
|
|
|
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xdrv_25_a4988_stepper.ino - A4988 StepMotorDriverCircuit- support for Tasmota
|
2019-09-08 17:32:35 +01:00
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Tim Leuscher and Theo Arends
|
2019-09-07 13:03:07 +01:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-10-15 19:24:06 +01:00
|
|
|
#ifdef USE_A4988_STEPPER
|
2019-10-17 14:28:46 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* A4988 Stepper motor driver circuit
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-09-07 20:37:01 +01:00
|
|
|
#define XDRV_25 25
|
2019-09-07 13:03:07 +01:00
|
|
|
|
2019-10-17 14:28:46 +01:00
|
|
|
#include <A4988_Stepper.h>
|
|
|
|
|
2020-04-26 16:33:27 +01:00
|
|
|
short A4988_dir_pin = 0;
|
|
|
|
short A4988_stp_pin = 0;
|
|
|
|
short A4988_ms1_pin = 0;
|
|
|
|
short A4988_ms2_pin = 0;
|
|
|
|
short A4988_ms3_pin = 0;
|
|
|
|
short A4988_ena_pin = 0;
|
2019-09-07 13:03:07 +01:00
|
|
|
int A4988_spr = 0;
|
|
|
|
float A4988_rpm = 0;
|
2019-09-07 19:38:06 +01:00
|
|
|
short A4988_mis = 0;
|
2019-09-07 13:03:07 +01:00
|
|
|
|
|
|
|
A4988_Stepper* myA4988 = nullptr;
|
|
|
|
|
|
|
|
void A4988Init(void)
|
|
|
|
{
|
2020-04-26 16:33:27 +01:00
|
|
|
A4988_dir_pin = Pin(GPIO_A4988_DIR);
|
|
|
|
A4988_stp_pin = Pin(GPIO_A4988_STP);
|
|
|
|
A4988_ena_pin = Pin(GPIO_A4988_ENA);
|
|
|
|
A4988_ms1_pin = Pin(GPIO_A4988_MS1);
|
2020-10-02 16:09:09 +01:00
|
|
|
A4988_ms2_pin = Pin(GPIO_A4988_MS1, 1);
|
|
|
|
A4988_ms3_pin = Pin(GPIO_A4988_MS1, 2);
|
2019-09-07 13:03:07 +01:00
|
|
|
A4988_spr = 200;
|
|
|
|
A4988_rpm = 30;
|
2019-09-07 13:09:44 +01:00
|
|
|
A4988_mis = 1;
|
2019-09-07 13:03:07 +01:00
|
|
|
|
|
|
|
myA4988 = new A4988_Stepper( A4988_spr
|
|
|
|
, A4988_rpm
|
2019-09-07 13:09:44 +01:00
|
|
|
, A4988_mis
|
2019-09-07 13:03:07 +01:00
|
|
|
, A4988_dir_pin
|
|
|
|
, A4988_stp_pin
|
|
|
|
, A4988_ena_pin
|
|
|
|
, A4988_ms1_pin
|
|
|
|
, A4988_ms2_pin
|
|
|
|
, A4988_ms3_pin );
|
|
|
|
}
|
|
|
|
|
2019-09-10 15:29:19 +01:00
|
|
|
const char kA4988Commands[] PROGMEM = "Motor|" // prefix
|
|
|
|
"Move|Rotate|Turn|MIS|SPR|RPM";
|
2019-09-07 13:03:07 +01:00
|
|
|
|
2019-10-17 14:28:46 +01:00
|
|
|
void (* const A4988Command[])(void) PROGMEM = {
|
2019-09-10 15:29:19 +01:00
|
|
|
&CmndDoMove,&CmndDoRotate,&CmndDoTurn,&CmndSetMIS,&CmndSetSPR,&CmndSetRPM};
|
2019-09-07 13:03:07 +01:00
|
|
|
|
2019-09-10 15:19:23 +01:00
|
|
|
void CmndDoMove(void) {
|
2023-01-06 14:28:44 +00:00
|
|
|
// Move the motor the given number of steps (positive values: clockwise, negative values: counterclockwise)
|
2019-09-10 15:19:23 +01:00
|
|
|
if (XdrvMailbox.data_len > 0) {
|
2023-01-06 14:28:44 +00:00
|
|
|
long stepsPlease = strtol(XdrvMailbox.data, nullptr, 10);
|
2019-09-10 15:19:23 +01:00
|
|
|
myA4988->doMove(stepsPlease);
|
|
|
|
ResponseCmndDone();
|
|
|
|
}
|
2019-09-07 13:03:07 +01:00
|
|
|
}
|
|
|
|
|
2019-09-10 15:19:23 +01:00
|
|
|
void CmndDoRotate(void) {
|
2023-01-06 14:28:44 +00:00
|
|
|
// Rotate the motor the given number of degrees (positive values: clockwise, negative values: counterclockwise)
|
2019-09-10 15:19:23 +01:00
|
|
|
if (XdrvMailbox.data_len > 0) {
|
2023-01-06 14:28:44 +00:00
|
|
|
long degrsPlease = strtol(XdrvMailbox.data, nullptr, 10);
|
2019-09-10 15:19:23 +01:00
|
|
|
myA4988->doRotate(degrsPlease);
|
|
|
|
ResponseCmndDone();
|
2019-09-07 13:03:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 15:19:23 +01:00
|
|
|
void CmndDoTurn(void) {
|
2023-01-06 14:28:44 +00:00
|
|
|
// Spin the motor the given number of turns (positive values: clockwise, negative values: counterclockwise)
|
2019-09-10 15:19:23 +01:00
|
|
|
if (XdrvMailbox.data_len > 0) {
|
2023-01-06 14:28:44 +00:00
|
|
|
// float turnsPlease = strtod(XdrvMailbox.data,nullptr);
|
|
|
|
float turnsPlease = CharToFloat(XdrvMailbox.data); // Save 8k code size over strtod()
|
2019-09-10 15:19:23 +01:00
|
|
|
myA4988->doTurn(turnsPlease);
|
|
|
|
ResponseCmndDone();
|
2019-09-07 19:38:06 +01:00
|
|
|
}
|
2019-09-10 15:19:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndSetMIS(void) {
|
2023-01-06 14:28:44 +00:00
|
|
|
// 1,2,4,8,16 Set micro stepping increment - 1/1 (full steps) to 1/16 (default = 1)
|
|
|
|
if (PinUsed(GPIO_A4988_MS1) && PinUsed(GPIO_A4988_MS1, 1) && PinUsed(GPIO_A4988_MS1, 2) && (XdrvMailbox.payload > 0)) {
|
|
|
|
// short newMIS = strtoul(XdrvMailbox.data,nullptr,10);
|
|
|
|
myA4988->setMIS(XdrvMailbox.payload);
|
2019-09-10 15:19:23 +01:00
|
|
|
ResponseCmndDone();
|
|
|
|
}
|
|
|
|
}
|
2019-09-07 13:03:07 +01:00
|
|
|
|
2019-09-10 15:19:23 +01:00
|
|
|
void CmndSetSPR(void) {
|
2023-01-06 14:28:44 +00:00
|
|
|
// Set the number of steps the given motor needs for one revolution (default = 200)
|
|
|
|
if (XdrvMailbox.payload > 0) {
|
|
|
|
// int newSPR = strtoul(XdrvMailbox.data,nullptr,10);
|
|
|
|
myA4988->setSPR(XdrvMailbox.payload);
|
2019-09-10 15:19:23 +01:00
|
|
|
ResponseCmndDone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndSetRPM(void) {
|
2023-01-06 14:28:44 +00:00
|
|
|
// Set revolutions per minute (default = 30)
|
|
|
|
if (XdrvMailbox.payload > 0) {
|
|
|
|
// short newRPM = strtoul(XdrvMailbox.data,nullptr,10);
|
|
|
|
myA4988->setRPM(XdrvMailbox.payload);
|
2019-09-10 15:19:23 +01:00
|
|
|
ResponseCmndDone();
|
|
|
|
}
|
2019-09-07 13:03:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
2022-11-11 09:44:56 +00:00
|
|
|
bool Xdrv25(uint32_t function)
|
2019-09-07 13:03:07 +01:00
|
|
|
{
|
|
|
|
bool result = false;
|
2020-04-27 11:54:07 +01:00
|
|
|
if (PinUsed(GPIO_A4988_DIR) && PinUsed(GPIO_A4988_STP)) {
|
2019-09-07 13:03:07 +01:00
|
|
|
switch (function) {
|
|
|
|
case FUNC_INIT:
|
|
|
|
A4988Init();
|
|
|
|
break;
|
|
|
|
case FUNC_COMMAND:
|
|
|
|
result = DecodeCommand(kA4988Commands, A4988Command);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:28:46 +01:00
|
|
|
#endif // USE_A4988_STEPPER
|