mirror of
https://github.com/anklimov/lighthub
synced 2025-12-07 12:19:49 +03:00
Motor driver fix and extension (auto switch PWM/NON PWM, Motor Quota)
This commit is contained in:
@@ -12,10 +12,10 @@ static int driverStatus = CST_UNKNOWN;
|
|||||||
void out_Motor::getConfig()
|
void out_Motor::getConfig()
|
||||||
{
|
{
|
||||||
pinUp=item->getArg(0);
|
pinUp=item->getArg(0);
|
||||||
if(pinUp<=0 || pinFeedback>=PINS_COUNT) pinUp=32;
|
if(pinUp<=0 || pinUp>=PINS_COUNT) pinUp=32;
|
||||||
|
|
||||||
pinDown=item->getArg(1);
|
pinDown=item->getArg(1);
|
||||||
if (pinDown<=0 || pinFeedback>=PINS_COUNT) pinDown=33;
|
if (pinDown<=0 || pinDown>=PINS_COUNT) pinDown=33;
|
||||||
|
|
||||||
pinFeedback=item->getArg(2);
|
pinFeedback=item->getArg(2);
|
||||||
if (pinFeedback<0 || pinFeedback>=PINS_COUNT) pinFeedback=0;
|
if (pinFeedback<0 || pinFeedback>=PINS_COUNT) pinFeedback=0;
|
||||||
@@ -35,12 +35,16 @@ int out_Motor::Setup()
|
|||||||
{
|
{
|
||||||
getConfig();
|
getConfig();
|
||||||
Serial.println("Motor Init");
|
Serial.println("Motor Init");
|
||||||
|
pinMode(pinUp,OUTPUT);
|
||||||
|
pinMode(pinDown,OUTPUT);
|
||||||
digitalWrite(pinUp,LOW);
|
digitalWrite(pinUp,LOW);
|
||||||
digitalWrite(pinDown,LOW);
|
digitalWrite(pinDown,LOW);
|
||||||
pinMode(pinFeedback, INPUT);
|
pinMode(pinFeedback, INPUT);
|
||||||
item->setExt(0);
|
item->setExt(0);
|
||||||
item->clearFlag(ACTION_NEEDED);
|
item->clearFlag(ACTION_NEEDED);
|
||||||
|
item->clearFlag(ACTION_IN_PROCESS);
|
||||||
driverStatus = CST_INITIALIZED;
|
driverStatus = CST_INITIALIZED;
|
||||||
|
motorQuote = MOTOR_QUOTE;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,6 +75,16 @@ int targetPos = -1;
|
|||||||
int dif;
|
int dif;
|
||||||
if (!item->getFlag(ACTION_NEEDED)) return 0;
|
if (!item->getFlag(ACTION_NEEDED)) return 0;
|
||||||
|
|
||||||
|
if (!item->getFlag(ACTION_IN_PROCESS))
|
||||||
|
{
|
||||||
|
if (motorQuote)
|
||||||
|
{
|
||||||
|
item->setFlag(ACTION_IN_PROCESS);
|
||||||
|
motorQuote--;
|
||||||
|
}
|
||||||
|
else return 0;
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t motorOfftime = item->getExt();
|
uint32_t motorOfftime = item->getExt();
|
||||||
|
|
||||||
switch (item->getCmd())
|
switch (item->getCmd())
|
||||||
@@ -116,13 +130,27 @@ if (dif<-POS_ERR)
|
|||||||
#ifndef ESP32
|
#ifndef ESP32
|
||||||
if (digitalPinHasPWM(pinUp))
|
if (digitalPinHasPWM(pinUp))
|
||||||
{
|
{
|
||||||
|
//Serial.println("pinUP PWM");
|
||||||
int velocity = map(-dif, 0, 10, 0, 255);
|
int velocity = map(-dif, 0, 10, 0, 255);
|
||||||
if (velocity>255) velocity=255;
|
if (velocity>255) velocity=255;
|
||||||
|
if (velocity<0) velocity=0;
|
||||||
analogWrite(pinUp,velocity);
|
analogWrite(pinUp,velocity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else if (digitalPinHasPWM(pinDown))
|
||||||
|
{
|
||||||
|
// Serial.println("pinDown PWM fallback");
|
||||||
|
digitalWrite(pinUp,HIGH);
|
||||||
|
int velocity = map(-dif, 0, 10, 255, 0);
|
||||||
|
if (velocity>255) velocity=255;
|
||||||
|
if (velocity<0) velocity=0;
|
||||||
|
analogWrite(pinDown,velocity);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
|
// Serial.print(pinUp);
|
||||||
|
// Serial.println(" pinUP noPWM");
|
||||||
digitalWrite(pinUp,HIGH);
|
digitalWrite(pinUp,HIGH);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -136,24 +164,39 @@ if (!item->getExt()) item->setExt(millis()+maxOnTime);
|
|||||||
#ifndef ESP32
|
#ifndef ESP32
|
||||||
if (digitalPinHasPWM(pinDown))
|
if (digitalPinHasPWM(pinDown))
|
||||||
{
|
{
|
||||||
|
//Serial.println("pinDown PWM");
|
||||||
int velocity = map(dif, 0, 10, 0, 255);
|
int velocity = map(dif, 0, 10, 0, 255);
|
||||||
if (velocity>255) velocity=255;
|
if (velocity>255) velocity=255;
|
||||||
|
if (velocity<0) velocity=0;
|
||||||
analogWrite(pinDown,velocity);
|
analogWrite(pinDown,velocity);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
if (digitalPinHasPWM(pinUp))
|
||||||
|
{
|
||||||
|
//Serial.println("pinUP PWM fallback");
|
||||||
|
digitalWrite(pinDown,HIGH);
|
||||||
|
int velocity = map(dif, 0, 10, 255, 0);
|
||||||
|
if (velocity>255) velocity=255;
|
||||||
|
if (velocity<0) velocity=0;
|
||||||
|
analogWrite(pinUp,velocity);
|
||||||
|
}
|
||||||
|
else
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
|
//Serial.print(pinDown);
|
||||||
|
//Serial.println(" pinDown noPWM");
|
||||||
digitalWrite(pinDown,HIGH);
|
digitalWrite(pinDown,HIGH);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else //Target zone
|
else //Target zone
|
||||||
{
|
{ Serial.println("Target");
|
||||||
digitalWrite(pinUp,LOW);
|
digitalWrite(pinUp,LOW);
|
||||||
digitalWrite(pinDown,LOW);
|
digitalWrite(pinDown,LOW);
|
||||||
item->setExt(0);
|
item->setExt(0);
|
||||||
item->clearFlag(ACTION_NEEDED);
|
item->clearFlag(ACTION_NEEDED);
|
||||||
|
item->clearFlag(ACTION_IN_PROCESS);
|
||||||
|
motorQuote++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -202,14 +245,6 @@ case S_CMD:
|
|||||||
switch (cmd)
|
switch (cmd)
|
||||||
{
|
{
|
||||||
case CMD_ON:
|
case CMD_ON:
|
||||||
|
|
||||||
/*
|
|
||||||
if (chActive>0 && send)
|
|
||||||
{
|
|
||||||
SendStatus(SEND_COMMAND);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
//retrive stored values
|
//retrive stored values
|
||||||
st = item->getVal();
|
st = item->getVal();
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,13 @@
|
|||||||
#define POS_ERR 2
|
#define POS_ERR 2
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// The number of simultaniusly working motors
|
||||||
|
#ifndef MOTOR_QUOTE
|
||||||
|
#define MOTOR_QUOTE 2
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static int8_t motorQuote = MOTOR_QUOTE;
|
||||||
|
|
||||||
class out_Motor : public abstractOut {
|
class out_Motor : public abstractOut {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user