Changed setAccel() and setStepType() to return StepperControl*

Allows for daisy-chaining of configuration parameters.

Also added initialization of upLinkData in motorTask to prevent time-outs if setMove() is never called (by initializing msDelay=10)
This commit is contained in:
Gregg 2023-07-02 11:13:55 -05:00
parent fdfc4749d3
commit 195095c891
5 changed files with 37 additions and 35 deletions

View File

@ -39,19 +39,20 @@ StepperControl::StepperControl(uint32_t priority, uint32_t cpu){
////////////////////////// //////////////////////////
void StepperControl::setAccel(float accelSize, float accelSteps){ StepperControl *StepperControl::setAccel(float accelSize, float accelSteps){
if(accelSize<0.0){ if(accelSize<0.0){
ESP_LOGE(STEPPER_TAG,"accelSize cannot be less than 0.0"); ESP_LOGE(STEPPER_TAG,"accelSize cannot be less than 0.0");
return; return(this);
} }
if(accelSteps<1.0){ if(accelSteps<1.0){
ESP_LOGE(STEPPER_TAG,"accelSteps cannot be less than 1.0"); ESP_LOGE(STEPPER_TAG,"accelSteps cannot be less than 1.0");
return; return(this);
} }
this->accelSize=accelSize; this->accelSize=accelSize;
this->accelSteps=accelSteps; this->accelSteps=accelSteps;
return(this);
} }
////////////////////////// //////////////////////////
@ -62,7 +63,7 @@ void StepperControl::move(int nSteps, uint32_t msDelay, endAction_t endAction){
return; return;
} }
upLink_t upLinkData = { .nSteps=nSteps, .msDelay=msDelay, .action=MOVE, endAction=endAction }; upLink_t upLinkData = { .nSteps=nSteps, .msDelay=msDelay, .action=MOVE, .endAction=endAction };
xQueueOverwrite(upLinkQueue,&upLinkData); xQueueOverwrite(upLinkQueue,&upLinkData);
waitForAck(); waitForAck();
} }
@ -138,7 +139,7 @@ void StepperControl::enable(){
void StepperControl::motorTask(void *args){ void StepperControl::motorTask(void *args){
StepperControl *motor = (StepperControl *)args; StepperControl *motor = (StepperControl *)args;
upLink_t upLinkData; upLink_t upLinkData = { .nSteps=0, .msDelay=10, .action=MOVE, .endAction=NONE };
downLink_t downLinkData; downLink_t downLinkData;
boolean running=false; boolean running=false;

View File

@ -90,8 +90,8 @@ class StepperControl {
public: public:
StepperControl(uint32_t priority=1, uint32_t cpu=0); StepperControl(uint32_t priority=1, uint32_t cpu=0);
virtual void setStepType(int mode){}; virtual StepperControl *setStepType(int mode){return(this);};
void setAccel(float accelSize, float accelSteps); StepperControl *setAccel(float accelSize, float accelSteps);
void move(int nSteps, uint32_t msDelay, endAction_t endAction=NONE); void move(int nSteps, uint32_t msDelay, endAction_t endAction=NONE);
void moveTo(int nPosition, uint32_t msDelay, endAction_t endAction=NONE); void moveTo(int nPosition, uint32_t msDelay, endAction_t endAction=NONE);
int position(); int position();

View File

@ -91,7 +91,7 @@ struct Stepper_A3967 : StepperControl {
////////////////////////// //////////////////////////
void setStepType(int mode) override { StepperControl *setStepType(int mode) override {
switch(mode){ switch(mode){
case FULL_STEP_TWO_PHASE: case FULL_STEP_TWO_PHASE:
digitalWrite(m1Pin,LOW); digitalWrite(m1Pin,LOW);
@ -112,6 +112,7 @@ struct Stepper_A3967 : StepperControl {
default: default:
ESP_LOGE(STEPPER_TAG,"Unknown StepType=%d",mode); ESP_LOGE(STEPPER_TAG,"Unknown StepType=%d",mode);
} }
return(this);
} }
}; };

View File

@ -135,7 +135,7 @@ struct Stepper_TB6612 : StepperControl {
////////////////////////// //////////////////////////
void setStepType(int mode) override { StepperControl *setStepType(int mode) override {
switch(mode){ switch(mode){
case FULL_STEP_ONE_PHASE: case FULL_STEP_ONE_PHASE:
@ -156,7 +156,7 @@ struct Stepper_TB6612 : StepperControl {
case QUARTER_STEP: case QUARTER_STEP:
if(!pwmA){ if(!pwmA){
ESP_LOGE(STEPPER_TAG,"QUARTER_STEP requires PWM pins"); ESP_LOGE(STEPPER_TAG,"QUARTER_STEP requires PWM pins");
return; return(this);
} }
phase=0; phase=0;
nPhases=16; nPhases=16;
@ -165,7 +165,7 @@ struct Stepper_TB6612 : StepperControl {
case EIGHTH_STEP: case EIGHTH_STEP:
if(!pwmA){ if(!pwmA){
ESP_LOGE(STEPPER_TAG,"EIGHTH_STEP requires PWM pins"); ESP_LOGE(STEPPER_TAG,"EIGHTH_STEP requires PWM pins");
return; return(this);
} }
phase=0; phase=0;
nPhases=32; nPhases=32;
@ -174,6 +174,7 @@ struct Stepper_TB6612 : StepperControl {
default: default:
ESP_LOGE(STEPPER_TAG,"Unknown StepType=%d",mode); ESP_LOGE(STEPPER_TAG,"Unknown StepType=%d",mode);
} }
return(this);
} }
}; };

View File

@ -29,8 +29,13 @@
// as well as compile and test from this point. This file is ignored when the library is included in other sketches. // as well as compile and test from this point. This file is ignored when the library is included in other sketches.
#include "Stepper_TB6612.h" // include the driver for a TB6612 chip #include "Stepper_TB6612.h" // include the driver for a TB6612 chip
#include "Stepper_A3967.h"
StepperControl *motor; // create a global pointer to StepperControl so it can be accessed in both setup() and loop() StepperControl *bigMotor;
StepperControl *smallMotor;
#define BIG_MOTOR_POSITION 1600
#define SMALL_MOTOR_POSITION 2064
/////////////////// ///////////////////
@ -40,33 +45,27 @@ void setup() {
delay(1000); delay(1000);
Serial.printf("\nHomeSpan Stepper Control\n\n"); Serial.printf("\nHomeSpan Stepper Control\n\n");
motor=new Stepper_TB6612(23,32,22,14,33,27); // instantiate the motor object (with PWM pins 33 and 27 specified) bigMotor=(new Stepper_TB6612(23,32,22,14,33,27))->setStepType(StepperControl::HALF_STEP)->setAccel(10,20);;
smallMotor=new Stepper_A3967(17,16,19,18,21);
motor->setStepType(StepperControl::HALF_STEP); // set the mode to half-step, which means 400 steps are needed for a complete revolution for a 200-step motor // bigMotor->setStepType(StepperControl::HALF_STEP);
motor->setAccel(10,20); // add acceleration parameters of 10x spread of 20 steps // bigMotor->setAccel(10,20);
Serial.printf("Moving motor 400 steps and waiting until motor stops...\n");
motor->move(-400,5); // move the motor -400 steps (1 revolution), with 2ms between steps.
while(motor->stepsRemaining()); // wait until there no remaining steps
Serial.printf("Moving motor to absolute position of +1200 (reversing a total of 1600 steps, or 4 revolutions) without waiting...\n");
motor->moveTo(1200,2,StepperControl::BRAKE); // move the motor to an absolute position of 1200 steps with 3ms between steps; enter brake state when done
// Motor will continue moving in background even once setup() exits and loop() below starts
} }
/////////////////// ///////////////////
void loop(){ void loop(){
Serial.printf("Motor has %d remaining steps\n",motor->stepsRemaining()); if(smallMotor->position()==0)
smallMotor->moveTo(SMALL_MOTOR_POSITION,2);
else if(smallMotor->position()==SMALL_MOTOR_POSITION)
smallMotor->moveTo(0,2);
delay(1000); // motor is unaffected by delay() if(bigMotor->position()==0)
bigMotor->moveTo(BIG_MOTOR_POSITION,4);
else if(bigMotor->position()==BIG_MOTOR_POSITION)
bigMotor->moveTo(0,4);
if(motor->position()==1200){ delay(1000);
Serial.printf("Motor has reached final position and is now stopped.\n"); Serial.printf("Small Motor: %d Big Motor %d\n",smallMotor->stepsRemaining(),bigMotor->stepsRemaining());
while(1);
}
} }