Support Stepper-motors connected to A4988-stepper-driver-circuit

This commit is contained in:
Tim Leuschner 2019-09-07 14:03:07 +02:00
parent 53235a3382
commit 5eac2dd939
34 changed files with 785 additions and 2 deletions

19
lib/A4988_Stepper/README.adoc Executable file
View File

@ -0,0 +1,19 @@
Stepper Library for Tasmota
This Class allows you to control bipolar stepper motors. To use it you will need an A4988-StepperDriverCircuit, connected at least with 2 GPIO's (direction and step) and of cause a stepper motor.
== License ==
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

24
lib/A4988_Stepper/keywords.txt Executable file
View File

@ -0,0 +1,24 @@
#######################################
# Syntax Coloring Map For Test
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
A4988_Stepper KEYWORD1 A4988_Stepper
#######################################
# Methods and Functions (KEYWORD2)
#######################################
doMove KEYWORD2
doRotate KEYWORD2
setRPM KEYWORD2
setSPR KEYWORD2
setMIC KEYWORD2
version KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################

View File

@ -0,0 +1,9 @@
name=A4988_Stepper
version=0.0.1
author=Tim Leuschner
maintainer=Tim Leuschner <tim@feuer.land>
sentence=Allows Tasmota to control stepper motors, connected to A4988-StepperDriverCircuit.
paragraph=This library allows you to control bipolar stepper motors, controlled by A4988-stepperDriverCircuit.
category=Device Control
url=
architectures=*

View File

@ -0,0 +1,174 @@
/* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Drives a bipolar motor, controlled by A4988 stepper driver circuit
*/
#include "Arduino.h"
#include "A4988_Stepper.h"
#include <stdlib.h>
A4988_Stepper::A4988_Stepper( int m_spr
, int m_rpm
, short m_mic
, short m_dir_pin
, short m_stp_pin
, short m_ena_pin
, short m_ms1_pin
, short m_ms2_pin
, short m_ms3_pin ) {
last_time = 0; // time stamp in us of the last step taken
motor_SPR = m_spr; // StepsPerRevolution
motor_RPM = m_rpm; // RoundsPerMinute
motor_MIC = m_mic; // Microsteps w/o effect if MS1-MS3 not connected - then full steps anyway
motor_dir_pin = m_dir_pin;
motor_stp_pin = m_stp_pin;
motor_ena_pin = m_ena_pin;
motor_ms1_pin = m_ms1_pin;
motor_ms2_pin = m_ms2_pin;
motor_ms3_pin = m_ms3_pin;
adjustDelay();
adjustPins();
adjustMicrosteps();
}
void A4988_Stepper::adjustPins(void) {
// setup the pins on the microcontroller:
pinMode(motor_dir_pin, OUTPUT);
pinMode(motor_stp_pin, OUTPUT);
if (motor_ena_pin <99) {
pinMode(motor_ena_pin, OUTPUT);
digitalWrite(motor_ena_pin, HIGH);
}
if ((motor_ms1_pin<99)&&(motor_ms2_pin<99)&&(motor_ms3_pin<99)) {
pinMode(motor_ms1_pin, OUTPUT);
pinMode(motor_ms2_pin, OUTPUT);
pinMode(motor_ms3_pin, OUTPUT);
}
}
void A4988_Stepper::adjustMicrosteps() {
if ((motor_ms1_pin<99)&&(motor_ms2_pin<99)&&(motor_ms3_pin<99)) {
switch (motor_MIC){
case 1:
digitalWrite(motor_ms1_pin, LOW);
digitalWrite(motor_ms2_pin, LOW);
digitalWrite(motor_ms3_pin, LOW);
break;
case 2:
digitalWrite(motor_ms1_pin, HIGH);
digitalWrite(motor_ms2_pin, LOW);
digitalWrite(motor_ms3_pin, LOW);
break;
digitalWrite(motor_ms1_pin, LOW);
digitalWrite(motor_ms2_pin, HIGH);
digitalWrite(motor_ms3_pin, LOW);
case 4:
digitalWrite(motor_ms1_pin, HIGH);
digitalWrite(motor_ms2_pin, HIGH);
digitalWrite(motor_ms3_pin, LOW);
break;
case 8:
digitalWrite(motor_ms1_pin, LOW);
digitalWrite(motor_ms2_pin, LOW);
digitalWrite(motor_ms3_pin, HIGH);
break;
case 16:
digitalWrite(motor_ms1_pin, HIGH);
digitalWrite(motor_ms2_pin, HIGH);
digitalWrite(motor_ms3_pin, HIGH);
break;
}
} else {
motor_MIC = 1;
}
}
void A4988_Stepper::adjustDelay(void) {
motor_delay = 60L * 1000L * 1000L / motor_SPR / motor_RPM / motor_MIC;
}
void A4988_Stepper::setMIC(short oneToSixteen) {
motor_MIC = oneToSixteen;
adjustMicrosteps();
}
short A4988_Stepper::getMIC(void) {
return motor_MIC;
}
void A4988_Stepper::setRPM(int howManyRounds) {
motor_RPM = howManyRounds;
adjustDelay();
}
int A4988_Stepper::getRPM(void) {
return motor_RPM;
}
void A4988_Stepper::setSPR(int howManySteps){
motor_SPR = howManySteps;
adjustDelay();
}
int A4988_Stepper::getSPR(void) {
return motor_SPR;
}
void A4988_Stepper::enable(){
if (motor_ena_pin < 99) {digitalWrite(motor_ena_pin, LOW);}
}
void A4988_Stepper::disable(){
if (motor_ena_pin < 99) {digitalWrite(motor_ena_pin, HIGH);}
}
void A4988_Stepper::doMove(long howManySteps)
{
long steps_togo = abs(howManySteps); // how many steps to take
bool lastStepWasHigh = false;
digitalWrite(motor_dir_pin, howManySteps>0?LOW:HIGH);
enable();
while (steps_togo > 0) {
delay(0); // don't get watchdoged in loop
unsigned long now = micros();
// move if delay has passed:
if (now - last_time >= motor_delay) {
digitalWrite(motor_stp_pin, lastStepWasHigh?LOW:HIGH);
lastStepWasHigh = !lastStepWasHigh;
// remeber step-time if last signal was HIGH we can pull low after 50ms as only HIGH actually moves the stepper
last_time = lastStepWasHigh?now-50:now;
if (!lastStepWasHigh) steps_togo--; // same here - only HIGH moves, if pulled LOW step is completed...
}
}
disable();
}
void A4988_Stepper::doRotate(long howManyDegrees)
{ long lSteps = 0;
lSteps = motor_SPR*motor_MIC*howManyDegrees/360;
doMove(lSteps);
}
void A4988_Stepper::doTurn(float howManyTimes)
{ long lSteps = 0;
lSteps = howManyTimes*motor_SPR;
doMove(lSteps);
}
int A4988_Stepper::version(void)
{
return 1;
}

View File

@ -0,0 +1,73 @@
/* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Drives a bipolar motor, controlled by A4988 stepper driver circuit
*/
#ifndef A4988_Stepper_h
#define A4988_Stepper_h
class A4988_Stepper {
public:
// constructor:
A4988_Stepper( int motor_spr
, int motor_rpm
, short motor_mic
, short motor_dir_pin
, short motor_stp_pin
, short motor_ena_pin
, short motor_ms1_pin
, short motor_ms2_pin
, short motor_ms3_pin
);
void setRPM (int whatRPM );
int getRPM (void );
void setMIC (short OneToSixteen);
short getMIC (void );
void setSPR (int howMany );
int getSPR (void );
void doMove (long steps_to_move);
void doRotate(long degrs_to_turn);
void doTurn (float howManyTimes);
void enable (void );
void disable (void );
int version (void );
private:
void adjustDelay(void);
void adjustPins(void);
void adjustMicrosteps(void);
unsigned long motor_delay; // delay between steps, in ms
int motor_SPR; // Steps Per Revolution
int motor_RPM; // Rounds Per Minute
short motor_MIC; // Micro Steps
// motor pins:
short motor_dir_pin;
short motor_stp_pin;
short motor_ena_pin;
short motor_ms1_pin;
short motor_ms2_pin;
short motor_ms3_pin;
unsigned long last_time; // timestamp of last pincycle of last step
};
#endif

View File

@ -456,6 +456,14 @@
#define D_JSON_ZIGBEEZNPSENT "ZigbeeZNPSent"
#define D_JSON_ZIGBEEZCLRECEIVED "ZigbeeZCLReceived"
#define D_JSON_ZIGBEEZCLSENT "ZigbeeZCLSent"
// Commands xdrv_98_A4988.ino
#ifdef USE_A4988_Stepper
#define D_CMND_MOTOR "MOTOR"
#define D_JSON_MOTOR_COMMAND "Command"
#define D_JSON_MOTOR_VALUE "Value"
#endif
/********************************************************************************************/
#define D_ASTERISK_PWD "****"

View File

@ -597,6 +597,13 @@
#define D_SENSOR_RDM6300_RX "RDM6300 RX"
#define D_SENSOR_CC1101_CS "CC1101 CS"
#define D_SENSOR_A4988_DIR "A4988 DIR"
#define D_SENSOR_A4988_STP "A4988 STP"
#define D_SENSOR_A4988_ENA "A4988 ENA"
#define D_SENSOR_A4988_MS1 "A4988 MS1"
#define D_SENSOR_A4988_MS2 "A4988 MS2"
#define D_SENSOR_A4988_MS3 "A4988 MS3"
// Units
#define D_UNIT_AMPERE "A"
#define D_UNIT_CENTIMETER "cm"

View File

@ -596,6 +596,12 @@
#define D_SENSOR_IBEACON_RX "iBeacon RX"
#define D_SENSOR_RDM6300_RX "RDM6300 RX"
#define D_SENSOR_CC1101_CS "CC1101 CS"
#define D_SENSOR_A4988_DIR "A4988 DIR"
#define D_SENSOR_A4988_STP "A4988 STP"
#define D_SENSOR_A4988_ENA "A4988 ENA"
#define D_SENSOR_A4988_MS1 "A4988 MS1"
#define D_SENSOR_A4988_MS2 "A4988 MS2"
#define D_SENSOR_A4988_MS3 "A4988 MS3"
// Units
#define D_UNIT_AMPERE "A"

View File

@ -596,6 +596,12 @@
#define D_SENSOR_IBEACON_RX "iBeacon RX"
#define D_SENSOR_RDM6300_RX "RDM6300 RX"
#define D_SENSOR_CC1101_CS "CC1101 CS"
#define D_SENSOR_A4988_DIR "A4988 DIR"
#define D_SENSOR_A4988_STP "A4988 STP"
#define D_SENSOR_A4988_ENA "A4988 ENA"
#define D_SENSOR_A4988_MS1 "A4988 MS1"
#define D_SENSOR_A4988_MS2 "A4988 MS2"
#define D_SENSOR_A4988_MS3 "A4988 MS3"
// Units
#define D_UNIT_AMPERE "A"

View File

@ -596,6 +596,12 @@
#define D_SENSOR_IBEACON_RX "iBeacon RX"
#define D_SENSOR_RDM6300_RX "RDM6300 RX"
#define D_SENSOR_CC1101_CS "CC1101 CS"
#define D_SENSOR_A4988_DIR "A4988 DIR"
#define D_SENSOR_A4988_STP "A4988 STP"
#define D_SENSOR_A4988_ENA "A4988 ENA"
#define D_SENSOR_A4988_MS1 "A4988 MS1"
#define D_SENSOR_A4988_MS2 "A4988 MS2"
#define D_SENSOR_A4988_MS3 "A4988 MS3"
// Units
#define D_UNIT_AMPERE "A"

View File

@ -596,6 +596,12 @@
#define D_SENSOR_IBEACON_RX "iBeacon RX"
#define D_SENSOR_RDM6300_RX "RDM6300 RX"
#define D_SENSOR_CC1101_CS "CC1101 CS"
#define D_SENSOR_A4988_DIR "A4988 DIR"
#define D_SENSOR_A4988_STP "A4988 STP"
#define D_SENSOR_A4988_ENA "A4988 ENA"
#define D_SENSOR_A4988_MS1 "A4988 MS1"
#define D_SENSOR_A4988_MS2 "A4988 MS2"
#define D_SENSOR_A4988_MS3 "A4988 MS3"
// Units
#define D_UNIT_AMPERE "A"

View File

@ -596,6 +596,12 @@
#define D_SENSOR_IBEACON_RX "iBeacon RX"
#define D_SENSOR_RDM6300_RX "RDM6300 RX"
#define D_SENSOR_CC1101_CS "CC1101 CS"
#define D_SENSOR_A4988_DIR "A4988 DIR"
#define D_SENSOR_A4988_STP "A4988 STP"
#define D_SENSOR_A4988_ENA "A4988 ENA"
#define D_SENSOR_A4988_MS1 "A4988 MS1"
#define D_SENSOR_A4988_MS2 "A4988 MS2"
#define D_SENSOR_A4988_MS3 "A4988 MS3"
// Units
#define D_UNIT_AMPERE "A"

View File

@ -596,6 +596,12 @@
#define D_SENSOR_IBEACON_RX "iBeacon RX"
#define D_SENSOR_RDM6300_RX "RDM6300 RX"
#define D_SENSOR_CC1101_CS "CC1101 CS"
#define D_SENSOR_A4988_DIR "A4988 DIR"
#define D_SENSOR_A4988_STP "A4988 STP"
#define D_SENSOR_A4988_ENA "A4988 ENA"
#define D_SENSOR_A4988_MS1 "A4988 MS1"
#define D_SENSOR_A4988_MS2 "A4988 MS2"
#define D_SENSOR_A4988_MS3 "A4988 MS3"
// Units
#define D_UNIT_AMPERE "A"

View File

@ -596,6 +596,12 @@
#define D_SENSOR_IBEACON_RX "iBeacon RX"
#define D_SENSOR_RDM6300_RX "RDM6300 RX"
#define D_SENSOR_CC1101_CS "CC1101 CS"
#define D_SENSOR_A4988_DIR "A4988 DIR"
#define D_SENSOR_A4988_STP "A4988 STP"
#define D_SENSOR_A4988_ENA "A4988 ENA"
#define D_SENSOR_A4988_MS1 "A4988 MS1"
#define D_SENSOR_A4988_MS2 "A4988 MS2"
#define D_SENSOR_A4988_MS3 "A4988 MS3"
// Units
#define D_UNIT_AMPERE "A"

View File

@ -596,6 +596,12 @@
#define D_SENSOR_IBEACON_RX "iBeacon RX"
#define D_SENSOR_RDM6300_RX "RDM6300 RX"
#define D_SENSOR_CC1101_CS "CC1101 CS"
#define D_SENSOR_A4988_DIR "A4988 DIR"
#define D_SENSOR_A4988_STP "A4988 STP"
#define D_SENSOR_A4988_ENA "A4988 ENA"
#define D_SENSOR_A4988_MS1 "A4988 MS1"
#define D_SENSOR_A4988_MS2 "A4988 MS2"
#define D_SENSOR_A4988_MS3 "A4988 MS3"
// Units
#define D_UNIT_AMPERE "A"

View File

@ -596,6 +596,12 @@
#define D_SENSOR_IBEACON_RX "iBeacon RX"
#define D_SENSOR_RDM6300_RX "RDM6300 RX"
#define D_SENSOR_CC1101_CS "CC1101 CS"
#define D_SENSOR_A4988_DIR "A4988 DIR"
#define D_SENSOR_A4988_STP "A4988 STP"
#define D_SENSOR_A4988_ENA "A4988 ENA"
#define D_SENSOR_A4988_MS1 "A4988 MS1"
#define D_SENSOR_A4988_MS2 "A4988 MS2"
#define D_SENSOR_A4988_MS3 "A4988 MS3"
// Units
#define D_UNIT_AMPERE "A"

View File

@ -596,6 +596,12 @@
#define D_SENSOR_IBEACON_RX "iBeacon RX"
#define D_SENSOR_RDM6300_RX "RDM6300 RX"
#define D_SENSOR_CC1101_CS "CC1101 CS"
#define D_SENSOR_A4988_DIR "A4988 DIR"
#define D_SENSOR_A4988_STP "A4988 STP"
#define D_SENSOR_A4988_ENA "A4988 ENA"
#define D_SENSOR_A4988_MS1 "A4988 MS1"
#define D_SENSOR_A4988_MS2 "A4988 MS2"
#define D_SENSOR_A4988_MS3 "A4988 MS3"
// Units
#define D_UNIT_AMPERE "A"

View File

@ -596,6 +596,12 @@
#define D_SENSOR_IBEACON_RX "iBeacon RX"
#define D_SENSOR_RDM6300_RX "RDM6300 RX"
#define D_SENSOR_CC1101_CS "CC1101 CS"
#define D_SENSOR_A4988_DIR "A4988 DIR"
#define D_SENSOR_A4988_STP "A4988 STP"
#define D_SENSOR_A4988_ENA "A4988 ENA"
#define D_SENSOR_A4988_MS1 "A4988 MS1"
#define D_SENSOR_A4988_MS2 "A4988 MS2"
#define D_SENSOR_A4988_MS3 "A4988 MS3"
// Units
#define D_UNIT_AMPERE "A"

View File

@ -596,6 +596,12 @@
#define D_SENSOR_IBEACON_RX "iBeacon RX"
#define D_SENSOR_RDM6300_RX "RDM6300 RX"
#define D_SENSOR_CC1101_CS "CC1101 CS"
#define D_SENSOR_A4988_DIR "A4988 DIR"
#define D_SENSOR_A4988_STP "A4988 STP"
#define D_SENSOR_A4988_ENA "A4988 ENA"
#define D_SENSOR_A4988_MS1 "A4988 MS1"
#define D_SENSOR_A4988_MS2 "A4988 MS2"
#define D_SENSOR_A4988_MS3 "A4988 MS3"
// Units
#define D_UNIT_AMPERE "A"

View File

@ -596,6 +596,12 @@
#define D_SENSOR_IBEACON_RX "iBeacon RX"
#define D_SENSOR_RDM6300_RX "RDM6300 RX"
#define D_SENSOR_CC1101_CS "CC1101 CS"
#define D_SENSOR_A4988_DIR "A4988 DIR"
#define D_SENSOR_A4988_STP "A4988 STP"
#define D_SENSOR_A4988_ENA "A4988 ENA"
#define D_SENSOR_A4988_MS1 "A4988 MS1"
#define D_SENSOR_A4988_MS2 "A4988 MS2"
#define D_SENSOR_A4988_MS3 "A4988 MS3"
// Units
#define D_UNIT_AMPERE "A"

View File

@ -596,6 +596,12 @@
#define D_SENSOR_IBEACON_RX "iBeacon RX"
#define D_SENSOR_RDM6300_RX "RDM6300 RX"
#define D_SENSOR_CC1101_CS "CC1101 CS"
#define D_SENSOR_A4988_DIR "A4988 DIR"
#define D_SENSOR_A4988_STP "A4988 STP"
#define D_SENSOR_A4988_ENA "A4988 ENA"
#define D_SENSOR_A4988_MS1 "A4988 MS1"
#define D_SENSOR_A4988_MS2 "A4988 MS2"
#define D_SENSOR_A4988_MS3 "A4988 MS3"
// Units
#define D_UNIT_AMPERE "A"

View File

@ -596,6 +596,12 @@
#define D_SENSOR_IBEACON_RX "iBeacon RX"
#define D_SENSOR_RDM6300_RX "RDM6300 RX"
#define D_SENSOR_CC1101_CS "CC1101 CS"
#define D_SENSOR_A4988_DIR "A4988 DIR"
#define D_SENSOR_A4988_STP "A4988 STP"
#define D_SENSOR_A4988_ENA "A4988 ENA"
#define D_SENSOR_A4988_MS1 "A4988 MS1"
#define D_SENSOR_A4988_MS2 "A4988 MS2"
#define D_SENSOR_A4988_MS3 "A4988 MS3"
// Units
#define D_UNIT_AMPERE "А"

View File

@ -596,6 +596,12 @@
#define D_SENSOR_IBEACON_RX "iBeacon RX"
#define D_SENSOR_RDM6300_RX "RDM6300 RX"
#define D_SENSOR_CC1101_CS "CC1101 CS"
#define D_SENSOR_A4988_DIR "A4988 DIR"
#define D_SENSOR_A4988_STP "A4988 STP"
#define D_SENSOR_A4988_ENA "A4988 ENA"
#define D_SENSOR_A4988_MS1 "A4988 MS1"
#define D_SENSOR_A4988_MS2 "A4988 MS2"
#define D_SENSOR_A4988_MS3 "A4988 MS3"
// Units
#define D_UNIT_AMPERE "A"

View File

@ -596,6 +596,12 @@
#define D_SENSOR_IBEACON_RX "iBeacon RX"
#define D_SENSOR_RDM6300_RX "RDM6300 RX"
#define D_SENSOR_CC1101_CS "CC1101 CS"
#define D_SENSOR_A4988_DIR "A4988 DIR"
#define D_SENSOR_A4988_STP "A4988 STP"
#define D_SENSOR_A4988_ENA "A4988 ENA"
#define D_SENSOR_A4988_MS1 "A4988 MS1"
#define D_SENSOR_A4988_MS2 "A4988 MS2"
#define D_SENSOR_A4988_MS3 "A4988 MS3"
// Units
#define D_UNIT_AMPERE "A"

View File

@ -596,6 +596,12 @@
#define D_SENSOR_IBEACON_RX "iBeacon RX"
#define D_SENSOR_RDM6300_RX "RDM6300 RX"
#define D_SENSOR_CC1101_CS "CC1101 CS"
#define D_SENSOR_A4988_DIR "A4988 DIR"
#define D_SENSOR_A4988_STP "A4988 STP"
#define D_SENSOR_A4988_ENA "A4988 ENA"
#define D_SENSOR_A4988_MS1 "A4988 MS1"
#define D_SENSOR_A4988_MS2 "A4988 MS2"
#define D_SENSOR_A4988_MS3 "A4988 MS3"
// Units
#define D_UNIT_AMPERE "A"

View File

@ -596,6 +596,12 @@
#define D_SENSOR_IBEACON_RX "iBeacon RX"
#define D_SENSOR_RDM6300_RX "RDM6300 RX"
#define D_SENSOR_CC1101_CS "CC1101 CS"
#define D_SENSOR_A4988_DIR "A4988 DIR"
#define D_SENSOR_A4988_STP "A4988 STP"
#define D_SENSOR_A4988_ENA "A4988 ENA"
#define D_SENSOR_A4988_MS1 "A4988 MS1"
#define D_SENSOR_A4988_MS2 "A4988 MS2"
#define D_SENSOR_A4988_MS3 "A4988 MS3"
// Units
#define D_UNIT_AMPERE "А"

View File

@ -596,6 +596,12 @@
#define D_SENSOR_IBEACON_RX "iBeacon RX"
#define D_SENSOR_RDM6300_RX "RDM6300 RX"
#define D_SENSOR_CC1101_CS "CC1101 CS"
#define D_SENSOR_A4988_DIR "A4988 DIR"
#define D_SENSOR_A4988_STP "A4988 STP"
#define D_SENSOR_A4988_ENA "A4988 ENA"
#define D_SENSOR_A4988_MS1 "A4988 MS1"
#define D_SENSOR_A4988_MS2 "A4988 MS2"
#define D_SENSOR_A4988_MS3 "A4988 MS3"
// Units
#define D_UNIT_AMPERE "安"

View File

@ -596,6 +596,12 @@
#define D_SENSOR_IBEACON_RX "iBeacon RX"
#define D_SENSOR_RDM6300_RX "RDM6300 RX"
#define D_SENSOR_CC1101_CS "CC1101 CS"
#define D_SENSOR_A4988_DIR "A4988 DIR"
#define D_SENSOR_A4988_STP "A4988 STP"
#define D_SENSOR_A4988_ENA "A4988 ENA"
#define D_SENSOR_A4988_MS1 "A4988 MS1"
#define D_SENSOR_A4988_MS2 "A4988 MS2"
#define D_SENSOR_A4988_MS3 "A4988 MS3"
// Units
#define D_UNIT_AMPERE "安"

View File

@ -527,6 +527,7 @@
#define USE_SM16716 // Add support for SM16716 RGB LED controller (+0k7 code)
//#define USE_HRE // Add support for Badger HR-E Water Meter (+1k4 code)
#define USE_A4988_Stepper // Add support for A4988 Stepper-Motors-Driver-circuit
/*********************************************************************************************\
* Debug features

View File

@ -257,6 +257,7 @@ char* ToHex_P(const unsigned char * in, size_t insz, char * out, size_t outsz, c
#undef USE_RF_SENSOR // Disable support for RF sensor receiver (434MHz or 868MHz) (+0k8 code)
#undef USE_SM16716 // Disable support for SM16716 RGB LED controller (+0k7 code)
#undef USE_HRE // Disable support for Badger HR-E Water Meter (+1k4 code)
#undef USE_A4988_Stepper // Disable support for A4988_Stepper
#undef DEBUG_THEO // Disable debug code
#undef USE_DEBUG_DRIVER // Disable debug code
#endif // FIRMWARE_CLASSIC
@ -386,6 +387,7 @@ char* ToHex_P(const unsigned char * in, size_t insz, char * out, size_t outsz, c
#undef USE_RF_SENSOR // Disable support for RF sensor receiver (434MHz or 868MHz) (+0k8 code)
#undef USE_SM16716 // Disable support for SM16716 RGB LED controller (+0k7 code)
#undef USE_HRE // Disable support for Badger HR-E Water Meter (+1k4 code)
#undef USE_A4988_Stepper // Disable support for A4988_Stepper
#undef DEBUG_THEO // Disable debug code
#undef USE_DEBUG_DRIVER // Disable debug code
@ -480,6 +482,7 @@ char* ToHex_P(const unsigned char * in, size_t insz, char * out, size_t outsz, c
#undef USE_RF_SENSOR // Disable support for RF sensor receiver (434MHz or 868MHz) (+0k8 code)
#undef USE_SM16716 // Disable support for SM16716 RGB LED controller (+0k7 code)
#undef USE_HRE // Disable support for Badger HR-E Water Meter (+1k4 code)
#undef USE_A4988_Stepper // Disable support for A4988_Stepper
#undef DEBUG_THEO // Disable debug code
#undef USE_DEBUG_DRIVER // Disable debug code
#endif // FIRMWARE_BASIC
@ -561,6 +564,7 @@ char* ToHex_P(const unsigned char * in, size_t insz, char * out, size_t outsz, c
#undef USE_RF_SENSOR // Disable support for RF sensor receiver (434MHz or 868MHz) (+0k8 code)
#undef USE_SM16716 // Disable support for SM16716 RGB LED controller (+0k7 code)
#undef USE_HRE // Disable support for Badger HR-E Water Meter (+1k4 code)
#undef USE_A4988_Stepper // Disable support for A4988_Stepper
#undef DEBUG_THEO // Disable debug code
#undef USE_DEBUG_DRIVER // Disable debug code
#endif // FIRMWARE_MINIMAL

View File

@ -194,6 +194,12 @@ enum UserSelectablePins {
GPIO_RDM6300_RX, // RDM6300 RX
GPIO_IBEACON_TX, // HM17 IBEACON TX
GPIO_IBEACON_RX, // HM17 IBEACON RX
GPIO_A4988_DIR, // A4988 direction pin
GPIO_A4988_STP, // A4988 step pin
GPIO_A4988_ENA, // A4988 enabled pin
GPIO_A4988_MS1, // A4988 microstep pin1
GPIO_A4988_MS2, // A4988 microstep pin2
GPIO_A4988_MS3, // A4988 microstep pin3
GPIO_SENSOR_END };
// Programmer selectable GPIO functionality
@ -267,6 +273,7 @@ const char kSensorNames[] PROGMEM =
D_SENSOR_ZIGBEE_TXD "|" D_SENSOR_ZIGBEE_RXD "|"
D_SENSOR_RDM6300_RX "|"
D_SENSOR_IBEACON_TX "|" D_SENSOR_IBEACON_RX "|"
D_SENSOR_A4988_DIR "|" D_SENSOR_A4988_STP "|" D_SENSOR_A4988_ENA "|" D_SENSOR_A4988_MS1 "|" D_SENSOR_A4988_MS2 "|" D_SENSOR_A4988_MS3 "|"
;
// User selectable ADC0 functionality
@ -689,6 +696,15 @@ const uint8_t kGpioNiceList[] PROGMEM = {
GPIO_HRE_CLOCK,
GPIO_HRE_DATA,
#endif
#ifdef USE_A4988_Stepper
GPIO_A4988_DIR, // A4988 direction pin
GPIO_A4988_STP, // A4988 step pin
// folowing are not mandatory
GPIO_A4988_ENA, // A4988 enabled pin
GPIO_A4988_MS1, // A4988 microstep pin1
GPIO_A4988_MS2, // A4988 microstep pin2
GPIO_A4988_MS3, // A4988 microstep pin3
#endif
};
const uint8_t kModuleNiceList[] PROGMEM = {

View File

@ -438,7 +438,9 @@ void GetFeatures(void)
#ifdef USE_IBEACON
feature5 |= 0x00000004; // xsns_52_ibeacon.ino
#endif
// feature5 |= 0x00000008;
#ifdef USE_A4988_Stepper
feature5 |= 0x00000008; // xdrv_98_A4988.ino
#endif
// feature5 |= 0x00000010;
// feature5 |= 0x00000020;

View File

@ -0,0 +1,320 @@
/*
xsns_22_sr04.ino - SR04 ultrasonic sensor support for Sonoff-Tasmota
Copyright (C) 2019 Nuno Ferreira and Theo Arends
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/>.
*/
#ifdef USE_A4988_Stepper
#include <A4988_Stepper.h>
/*********************************************************************************************\
* Stepper mötör on driver A4988
*
* - https://www.dfrobot.com/wiki/index.php/Weather-proof_Ultrasonic_Sensor_SKU_:_SEN0207
\*********************************************************************************************/
#define XDRV_98 98
enum A4988Errors { A4988_NO_ERROR, A4988_NO_JSON_COMMAND, A4988_INVALID_JSON};
short A4988_dir_pin = pin[GPIO_MAX];
short A4988_stp_pin = pin[GPIO_MAX];
short A4988_ms1_pin = pin[GPIO_MAX];
short A4988_ms2_pin = pin[GPIO_MAX];
short A4988_ms3_pin = pin[GPIO_MAX];
short A4988_ena_pin = pin[GPIO_MAX];
int A4988_spr = 0;
float A4988_rpm = 0;
short A4988_mic = 0;
A4988_Stepper* myA4988 = nullptr;
void A4988Init(void)
{
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];
A4988_ms2_pin = pin[GPIO_A4988_MS2];
A4988_ms3_pin = pin[GPIO_A4988_MS3];
A4988_spr = 200;
A4988_rpm = 30;
A4988_mic = 1;
myA4988 = new A4988_Stepper( A4988_spr
, A4988_rpm
, A4988_mic
, A4988_dir_pin
, A4988_stp_pin
, A4988_ena_pin
, A4988_ms1_pin
, A4988_ms2_pin
, A4988_ms3_pin );
A4988_spr = myA4988->getSPR();
A4988_rpm = myA4988->getRPM();
A4988_mic = myA4988->getMIC();
if ((A4988_ms1_pin < 99)&&(A4988_ms2_pin < 99)&&(A4988_ms3_pin < 99)&&(A4988_ena_pin<99)) {
AddLog_P2(LOG_LEVEL_INFO, PSTR("STP: A4988-Driver initialized (%dSPR, %dRPM,%dMIC). Pins: Dir[%d] Stp[%d] Ena[%d] MS1[%d] MS2[%d] MS3[%d]"),A4988_spr, A4988_rpm, A4988_mic, A4988_dir_pin,A4988_stp_pin,A4988_ena_pin,A4988_ms1_pin,A4988_ms2_pin,A4988_ms3_pin);
} else {
if ((A4988_ena_pin<99)) {
AddLog_P2(LOG_LEVEL_INFO, PSTR("STP: A4988-Driver initialized (%dSPR, %dRPM,%dMIC). Pins: Dir[%d] Stp[%d] Ena[%d] MicroStepping hardwired"),A4988_spr, A4988_rpm, A4988_mic, A4988_dir_pin,A4988_stp_pin,A4988_ena_pin);
} else {
AddLog_P2(LOG_LEVEL_INFO, PSTR("STP: A4988-Driver initialized (%dSPR, %dRPM,%dMIC). Pins: Dir[%d] Stp[%d] motor permanently on, MicroStepping hardwired"),A4988_spr, A4988_rpm, A4988_mic, A4988_dir_pin,A4988_stp_pin,A4988_ena_pin);
}
}
}
const char kA4988Commands[] PROGMEM = "MOTOR|"
"doMove|doRotate|doTurn|setSPR|setRPM|setMIC|getSPR|getRPM|getMIC|doVader";
void (* const A4988Command[])(void) PROGMEM = { &CmndMOTOR, &CmndDoMove, &CmndDoRotate, &CmndDoTurn, &CmndSetSPR, &CmndSetRPM, &CmndSetMic , &CmndGetSPR, &CmndGetRPM, &CmndGetMIC, &CmndDoVader };
uint32_t MOTORCmndJson(void)
{
// ArduinoJSON entry used to calculate jsonBuf: JSON_OBJECT_SIZE(3) + 40 = 96
// MOTOR { "command": "doMove", "value": 200 }
// MOTOR { "command": "doRotate", "value": 200 }
// MOTOR { "command": "doTurn", "value": 200 }
char dataBufUc[XdrvMailbox.data_len];
UpperCase(dataBufUc, XdrvMailbox.data);
RemoveSpace(dataBufUc);
if (strlen(dataBufUc) < 8) {
return A4988_INVALID_JSON;
}
StaticJsonBuffer<40> jsonBuf;
JsonObject &root = jsonBuf.parseObject(dataBufUc);
if (!root.success()) {
return A4988_INVALID_JSON;
}
char parm_uc[10];
const char *command = root[PSTR(parm_uc, PSTR(D_JSON_MOTOR_COMMAND))];
const char *commandval = root[PSTR(parm_uc, PSTR(D_JSON_MOTOR_VALUE))];
if (!(command && commandval)) {
return A4988_INVALID_JSON;
}
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("MOTOR: command: '%s', value: '%s'"),command, commandval);
switch (command)
{ case "doMove": {
long stepsPlease = 1;
stepsPlease = strtoul(commandval,nullptr,10);
myA4988->doMove(stepsPlease); break;
}
case "doRotate" : {
long degrsPlease = 1;
degrsPlease = strtoul(commandval,nullptr,10);
AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: Rotating %d degrs"), degrsPlease);
myA4988->doRotate(degrsPlease); break;
}
case "doTurn" : {
float turnsPlease = 0;
turnsPlease = strtod(commandval,nullptr);
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: Turning %d times"), turnsPlease);
myA4988->doTurn(turnsPlease); break;
}
default:
ResponseCmndChar(D_JSON_PROTOCOL_NOT_SUPPORTED);
}
return A4988_NO_ERROR;
}
void CmndMOTOR(void){
if (XdrvMailbox.data_len) {
if (strstr(XdrvMailbox.data, "{") == nullptr) {
error = A4988_NO_JSON_COMMAND;
} else {
error = MOTORCmndJson();
}
}
A4988CmndResponse(error);
}
void A4988CmndResponse(uint32_t error){
}
void CmndGetSPR(void) {
AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: SPR = %d steps"), myA4988->getSPR());
}
void CmndGetRPM(void) {
AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: RPM = %d rounds"), myA4988->getRPM());
}
void CmndGetMIC(void) {
AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: MIC = %d steps"), myA4988->getMIC());
}
void CmndDoMove(void)
{
if (XdrvMailbox.data_len > 0) {
long stepsPlease = 1;
stepsPlease = strtoul(XdrvMailbox.data,nullptr,10);
AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: Moving %d steps"), stepsPlease);
myA4988->doMove(stepsPlease);
} else {
AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: Moving 25 steps"));
myA4988->doMove(25);
}
ResponseCmndDone();
}
void CmndDoRotate(void)
{
if (XdrvMailbox.data_len > 0) {
long degrsPlease = 1;
degrsPlease = strtoul(XdrvMailbox.data,nullptr,10);
AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: Rotating %d degrs"), degrsPlease);
myA4988->doRotate(degrsPlease);
} else {
AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: Moving 45 degrs"));
myA4988->doRotate(45);
}
ResponseCmndDone();
}
void CmndDoTurn(void)
{
if (XdrvMailbox.data_len > 0) {
float turnsPlease = 0;
turnsPlease = strtod(XdrvMailbox.data,nullptr);
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: Turning %d times"), turnsPlease);
myA4988->doTurn(turnsPlease);
} else {
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: Turning 0.25 times"));
myA4988->doRotate(0.25);
}
ResponseCmndDone();
}
void CmndSetRPM(void)
{
short rpmPlease = 60;
if (XdrvMailbox.data_len > 0) {
rpmPlease = strtoul(XdrvMailbox.data,nullptr,10);
}
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: RPM set to %d"), rpmPlease);
myA4988->setRPM(rpmPlease);
ResponseCmndDone();
}
void CmndSetSPR(void)
{
int sprPlease = 200;
if (XdrvMailbox.data_len > 0) {
sprPlease = strtoul(XdrvMailbox.data,nullptr,10);
}
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: SPR set to %d"), sprPlease);
myA4988->setSPR(sprPlease);
ResponseCmndDone();
}
void CmndSetMic(void)
{
if ((pin[GPIO_A4988_MS1] < 99) && (pin[GPIO_A4988_MS2] < 99) && (pin[GPIO_A4988_MS3] < 99)) {
short micPlease = 1;
if (XdrvMailbox.data_len > 0) {
micPlease = strtoul(XdrvMailbox.data,nullptr,10);
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: Microsteps set to %d"), micPlease);
myA4988->setMIC(micPlease);
}
} else {
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: Microsteps constant = 1. You'll have to define GPIO's for MS1-MS3 and connect them to A4988 or hardwire A4988 itself"));
}
ResponseCmndDone();
}
void CmndDoVader(void){
myA4988->enable();
myA4988->setRPM(25); myA4988->doMove( 30); delay(5);
myA4988->setRPM(25); myA4988->doMove(-30); delay(5);
myA4988->setRPM(25); myA4988->doMove( 30); delay(5);
myA4988->setRPM(20); myA4988->doMove(-25); delay(5);
myA4988->setRPM(30); myA4988->doMove( 20); delay(5);
myA4988->setRPM(25); myA4988->doMove( 50); delay(5);
myA4988->setRPM(20); myA4988->doMove(-25); delay(5);
myA4988->setRPM(30); myA4988->doMove( 20); delay(5);
myA4988->setRPM(25); myA4988->doMove(-50); delay(5);//
myA4988->setRPM(38); myA4988->doMove( 31); delay(5);
myA4988->setRPM(38); myA4988->doMove(-31); delay(5);
myA4988->setRPM(38); myA4988->doMove( 31); delay(5);
myA4988->setRPM(38); myA4988->doMove(-31); delay(5);
myA4988->setRPM(41); myA4988->doMove( 21); delay(5);
myA4988->setRPM(30); myA4988->doMove(-21); delay(5);
myA4988->setRPM(25); myA4988->doMove( 30); delay(5);
myA4988->setRPM(20); myA4988->doMove(-25); delay(5);
myA4988->setRPM(30); myA4988->doMove( 20); delay(5);
myA4988->setRPM(25); myA4988->doMove(-50); delay(5);//
myA4988->setRPM(50); myA4988->doMove( 40); delay(5);
myA4988->setRPM(25); myA4988->doMove(-20); delay(5);
myA4988->setRPM(25); myA4988->doMove( 15); delay(5);
myA4988->setRPM(50); myA4988->doMove(-40); delay(5);
myA4988->setRPM(48); myA4988->doMove( 35); delay(5);
myA4988->setRPM(45); myA4988->doMove(-25); delay(5);
myA4988->setRPM(41); myA4988->doMove( 25); delay(5);
myA4988->setRPM(38); myA4988->doMove(-26); delay(5);
myA4988->setRPM(41); myA4988->doMove( 25); delay(5);
myA4988->setRPM(25); myA4988->doMove( 25); delay(5);
myA4988->setRPM(41); myA4988->doMove(-35); delay(5);
myA4988->setRPM(38); myA4988->doMove( 25); delay(5);
myA4988->setRPM(36); myA4988->doMove(-25); delay(5);
myA4988->setRPM(33); myA4988->doMove( 25); delay(5);
myA4988->setRPM(30); myA4988->doMove(-27); delay(5);
myA4988->setRPM(33); myA4988->doMove( 25); delay(5);
myA4988->setRPM(33); myA4988->doMove( 25); delay(5);
myA4988->setRPM(38); myA4988->doMove(-30); delay(5);
myA4988->setRPM(18); myA4988->doMove( 10); delay(5);
myA4988->setRPM(18); myA4988->doMove(-10); delay(5);
myA4988->setRPM(25); myA4988->doMove( 20); delay(5);
myA4988->setRPM(20); myA4988->doMove(-25); delay(5);
myA4988->setRPM(30); myA4988->doMove( 20); delay(5);
myA4988->setRPM(25); myA4988->doMove(-50); delay(5);
myA4988->setRPM(20); myA4988->doMove( 25); delay(5);
myA4988->setRPM(30); myA4988->doMove(-20); delay(5);
myA4988->setRPM(25); myA4988->doMove( 50); delay(5);
myA4988->disable();
AddLog_P2(LOG_LEVEL_INFO, PSTR("Stepper: may the force be with you!"));
ResponseCmndDone();
}
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
bool Xdrv98(uint8_t function)
{
bool result = false;
if ((pin[GPIO_A4988_DIR] < 99) && (pin[GPIO_A4988_STP] < 99)) {
switch (function) {
case FUNC_INIT:
A4988Init();
break;
case FUNC_COMMAND:
result = DecodeCommand(kA4988Commands, A4988Command);
break;
}
}
return result;
}
#endif

View File

@ -160,7 +160,7 @@ a_features = [[
"USE_ADE7953","USE_SPS30","USE_VL53L0X","USE_MLX90614",
"USE_MAX31865","USE_CHIRP","USE_SOLAX_X1","USE_PAJ7620"
],[
"USE_BUZZER","USE_RDM6300","USE_IBEACON","",
"USE_BUZZER","USE_RDM6300","USE_IBEACON","USE_A4988_Stepper",
"","","","",
"","","","",
"","","","",