works fine...

This commit is contained in:
Tim Leuschner 2019-09-08 03:28:19 +02:00
parent e516262904
commit 4e1480d0f0
6 changed files with 119 additions and 88 deletions

View File

@ -1,31 +1,31 @@
/* 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
/*
This library 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 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 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/>.
Drives a bipolar motor, controlled by A4988 stepper driver circuit
*/
#include "Arduino.h"
#include "A4988_Stepper.h"
A4988_Stepper::A4988_Stepper( int m_spr
, int m_rpm
, short m_mis
, short m_dir_pin
, short m_stp_pin
, short m_ena_pin
, short m_ms1_pin
, short m_ms2_pin
, short m_ms3_pin ) {
, int m_rpm
, short m_mis
, 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
@ -96,7 +96,7 @@ void A4988_Stepper::adjustMicrosteps() {
}
void A4988_Stepper::adjustDelay(void) {
motor_delay = 60L * 1000L * 1000L / motor_SPR / motor_RPM / motor_MIS;
motor_delay = 60L * 1000L * 1000L / motor_SPR / motor_RPM / motor_MIS/2;
}
void A4988_Stepper::setMIS(short oneToSixteen) {
@ -147,8 +147,8 @@ void A4988_Stepper::doMove(long howManySteps)
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;
// remeber step-time
last_time = now;
if (!lastStepWasHigh) steps_togo--; // same here - only HIGH moves, if pulled LOW step is completed...
}
}

View File

@ -1,19 +1,18 @@
/* 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
*/
/*
This library 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 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 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/>.
*/
#ifndef A4988_Stepper_h
#define A4988_Stepper_h

View File

@ -218,7 +218,8 @@ upload_speed = 115200
upload_resetmethod = nodemcu
; *** Upload Serial reset method for Wemos and NodeMCU
upload_port = COM5
; upload_port = COM5
upload_port = /dev/cu.wchusbserial1410
extra_scripts = pio/strip-floats.py
; *** Upload file to OTA server using SCP

View File

@ -460,8 +460,6 @@
// Commands xdrv_25_A4988_Stepper.ino
#ifdef USE_A4988_Stepper
#define D_CMND_MOTOR "MOTOR"
#define D_JSON_MOTOR_COMMAND "Command"
#define D_JSON_MOTOR_VALUE "Value"
#define D_JSON_MOTOR_MOVE "doMove"
#define D_JSON_MOTOR_ROTATE "doRotate"
#define D_JSON_MOTOR_TURN "doTurn"

View File

@ -57,13 +57,13 @@
#define BOOT_LOOP_OFFSET 1 // [SetOption36] Number of boot loops before starting restoring defaults (0 = disable, 1..200 = boot loops offset)
// -- Wifi ----------------------------------------
#define WIFI_IP_ADDRESS "0.0.0.0" // [IpAddress1] Set to 0.0.0.0 for using DHCP or enter a static IP address
#define WIFI_GATEWAY "192.168.1.1" // [IpAddress2] If not using DHCP set Gateway IP address
#define WIFI_SUBNETMASK "255.255.255.0" // [IpAddress3] If not using DHCP set Network mask
#define WIFI_DNS "192.168.1.1" // [IpAddress4] If not using DHCP set DNS IP address (might be equal to WIFI_GATEWAY)
#define WIFI_IP_ADDRESS "172.17." // [IpAddress1] Set to 0.0.0.0 for using DHCP or enter a static IP address
#define WIFI_GATEWAY "172.17.0.1" // [IpAddress2] If not using DHCP set Gateway IP address
#define WIFI_SUBNETMASK "255.255.192.0" // [IpAddress3] If not using DHCP set Network mask
#define WIFI_DNS "172.17.0.1" // [IpAddress4] If not using DHCP set DNS IP address (might be equal to WIFI_GATEWAY)
#define STA_SSID1 "" // [Ssid1] Wifi SSID
#define STA_PASS1 "" // [Password1] Wifi password
#define STA_SSID1 "Tim" // [Ssid1] Wifi SSID
#define STA_PASS1 "qwer1234!" // [Password1] Wifi password
#define STA_SSID2 "" // [Ssid2] Optional alternate AP Wifi SSID
#define STA_PASS2 "" // [Password2] Optional alternate AP Wifi password
#define WIFI_CONFIG_TOOL WIFI_RETRY // [WifiConfig] Default tool if wifi fails to connect
@ -527,7 +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 (+12k7 code)
#define USE_A4988_Stepper // Add support for A4988 Stepper-Motors-Driver-circuit (+10k4 code)
/*********************************************************************************************\
* Debug features

View File

@ -1,8 +1,6 @@
/*
xsns_22_sr04.ino - SR04 ultrasonic sensor support for Sonoff-Tasmota
Copyright (C) 2019 Nuno Ferreira and Theo Arends
xdrv_25_A4988_Stepper.ino - A4988-StepMotorDriverCircuit- support for Sonoff-Tasmota
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
@ -22,7 +20,7 @@
#include <A4988_Stepper.h>
#define XDRV_25 25
enum A4988Errors { A4988_NO_ERROR, A4988_NO_JSON_COMMAND, A4988_INVALID_JSON};
enum A4988Errors { A4988_NO_ERROR, A4988_NO_JSON_COMMAND, A4988_INVALID_JSON, A4988_MOVE, A4988_ROTATE, A4988_TURN};
short A4988_dir_pin = pin[GPIO_MAX];
short A4988_stp_pin = pin[GPIO_MAX];
@ -59,53 +57,79 @@ void A4988Init(void)
, A4988_ms3_pin );
}
const char kA4988Commands[] PROGMEM = "MOTOR|"
"doMove|doRotate|doTurn|setSPR|setRPM|setMIS";
const char kA4988Commands[] PROGMEM = "|"
"MOTOR";
void (* const A4988Command[])(void) PROGMEM = { &CmndMOTOR};
uint32_t MOTORCmndJson(void)
{
// MOTOR {"Command":"doMove","Value":200}
// MOTOR {"Command":"doRotate","Value":360}
// MOTOR {"Command":"doTurn","Value":1.0}
// MOTOR {"doMove":200}
// MOTOR {"doRotate":360}
// MOTOR {"doTurn":1.0}
uint32_t returnValue =A4988_NO_JSON_COMMAND;
char parm_uc[12];
char dataBufUc[XdrvMailbox.data_len];
UpperCase(dataBufUc, XdrvMailbox.data);
RemoveSpace(dataBufUc);
if (strlen(dataBufUc) < 8) { return A4988_INVALID_JSON; }
if (strlen(dataBufUc) < 8) { returnValue =A4988_INVALID_JSON; }
DynamicJsonBuffer jsonBuf;
JsonObject &json = jsonBuf.parseObject(dataBufUc);
if (!json.success()) { return A4988_INVALID_JSON; }
if (json.containsKey(D_JSON_MOTOR_MOVE )){
long stepsPlease = 50;
stepsPlease = strtoul(json[D_JSON_MOTOR_MOVE],nullptr,10);
myA4988->doMove(stepsPlease);
} else if (json.containsKey(D_JSON_MOTOR_ROTATE )){
long degrsPlease = 45;
degrsPlease = strtoul(json[D_JSON_MOTOR_ROTATE],nullptr,10);
myA4988->doRotate(degrsPlease);
} else if (json.containsKey(D_JSON_MOTOR_TURN )){
float turnsPlease = 0.25;
turnsPlease = strtod(json[D_JSON_MOTOR_TURN],nullptr);
myA4988->doTurn(turnsPlease);
} else if (json.containsKey(D_JSON_MOTOR_SPR )){
int howManySteps =strtoul(json[D_JSON_MOTOR_SPR],nullptr,10);
myA4988->setSPR(howManySteps);
} else if (json.containsKey(D_JSON_MOTOR_RPM )){
int howManyRounds =strtoul(json[D_JSON_MOTOR_RPM],nullptr,10);
myA4988->setRPM(howManyRounds);
} else if (json.containsKey(D_JSON_MOTOR_MIS )){
short oneToSixteen =strtoul(json[D_JSON_MOTOR_MIS],nullptr,10);
myA4988->setMIS(oneToSixteen);
} else return A4988_NO_JSON_COMMAND;
return A4988_NO_ERROR;
if (json.success()) {
while (json.count()>0) {
UpperCase_P(parm_uc, PSTR(D_JSON_MOTOR_SPR));
if (json.containsKey(parm_uc)){
int howManySteps =strtoul(json[parm_uc],nullptr,10);
myA4988->setSPR(howManySteps);
returnValue = A4988_NO_ERROR;
json.remove(parm_uc);
}
UpperCase_P(parm_uc, PSTR(D_JSON_MOTOR_RPM));
if (json.containsKey(parm_uc)){
int howManyRounds =strtoul(json[parm_uc],nullptr,10);
myA4988->setRPM(howManyRounds);
returnValue = A4988_NO_ERROR;
json.remove(parm_uc);
}
UpperCase_P(parm_uc, PSTR(D_JSON_MOTOR_MIS));
if (json.containsKey(parm_uc)){
short oneToSixteen =strtoul(json[parm_uc],nullptr,10);
myA4988->setMIS(oneToSixteen);
returnValue = A4988_NO_ERROR;
json.remove(parm_uc);
}
UpperCase_P(parm_uc, PSTR(D_JSON_MOTOR_MOVE));
if (json.containsKey(parm_uc)){
long stepsPlease = strtoul(json[parm_uc],nullptr,10);
myA4988->doMove(stepsPlease);
returnValue = A4988_MOVE;
json.remove(parm_uc);
}
UpperCase_P(parm_uc, PSTR(D_JSON_MOTOR_ROTATE));
if (json.containsKey(parm_uc)){
long degrsPlease = strtoul(json[parm_uc],nullptr,10);
myA4988->doRotate(degrsPlease);
returnValue = A4988_ROTATE;
json.remove(parm_uc);
}
UpperCase_P(parm_uc, PSTR(D_JSON_MOTOR_TURN));
if (json.containsKey(parm_uc)){
float turnsPlease = strtod(json[parm_uc],nullptr);
myA4988->doTurn(turnsPlease);
returnValue = A4988_TURN;
json.remove(parm_uc);
}
}
} else returnValue =A4988_INVALID_JSON;
return returnValue;
}
void CmndMOTOR(void){
uint32_t error;
if (XdrvMailbox.data_len) {
if (strstr(XdrvMailbox.data, "{") == nullptr) {
if (strstr(XdrvMailbox.data, "}") == nullptr) {
error = A4988_NO_JSON_COMMAND;
} else {
error = MOTORCmndJson();
@ -117,7 +141,16 @@ void CmndMOTOR(void){
void A4988CmndResponse(uint32_t error){
switch (error) {
case A4988_NO_JSON_COMMAND:
ResponseCmndChar(D_JSON_INVALID_JSON);
ResponseCmndChar(PSTR("Kein Commando!"));
break;
case A4988_MOVE:
ResponseCmndChar(PSTR("Stepping!"));
break;
case A4988_ROTATE:
ResponseCmndChar(PSTR("Rotating!"));
break;
case A4988_TURN:
ResponseCmndChar(PSTR("Turning!"));
break;
default: // A4988_NO_ERROR
ResponseCmndDone();