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:
parent
fdfc4749d3
commit
195095c891
|
|
@ -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){
|
||||
ESP_LOGE(STEPPER_TAG,"accelSize cannot be less than 0.0");
|
||||
return;
|
||||
return(this);
|
||||
}
|
||||
|
||||
if(accelSteps<1.0){
|
||||
ESP_LOGE(STEPPER_TAG,"accelSteps cannot be less than 1.0");
|
||||
return;
|
||||
return(this);
|
||||
}
|
||||
|
||||
this->accelSize=accelSize;
|
||||
this->accelSteps=accelSteps;
|
||||
return(this);
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
|
|
@ -62,7 +63,7 @@ void StepperControl::move(int nSteps, uint32_t msDelay, endAction_t endAction){
|
|||
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);
|
||||
waitForAck();
|
||||
}
|
||||
|
|
@ -138,7 +139,7 @@ void StepperControl::enable(){
|
|||
|
||||
void StepperControl::motorTask(void *args){
|
||||
StepperControl *motor = (StepperControl *)args;
|
||||
upLink_t upLinkData;
|
||||
upLink_t upLinkData = { .nSteps=0, .msDelay=10, .action=MOVE, .endAction=NONE };
|
||||
downLink_t downLinkData;
|
||||
boolean running=false;
|
||||
|
||||
|
|
|
|||
|
|
@ -90,8 +90,8 @@ class StepperControl {
|
|||
public:
|
||||
|
||||
StepperControl(uint32_t priority=1, uint32_t cpu=0);
|
||||
virtual void setStepType(int mode){};
|
||||
void setAccel(float accelSize, float accelSteps);
|
||||
virtual StepperControl *setStepType(int mode){return(this);};
|
||||
StepperControl *setAccel(float accelSize, float accelSteps);
|
||||
void move(int nSteps, uint32_t msDelay, endAction_t endAction=NONE);
|
||||
void moveTo(int nPosition, uint32_t msDelay, endAction_t endAction=NONE);
|
||||
int position();
|
||||
|
|
|
|||
|
|
@ -91,7 +91,7 @@ struct Stepper_A3967 : StepperControl {
|
|||
|
||||
//////////////////////////
|
||||
|
||||
void setStepType(int mode) override {
|
||||
StepperControl *setStepType(int mode) override {
|
||||
switch(mode){
|
||||
case FULL_STEP_TWO_PHASE:
|
||||
digitalWrite(m1Pin,LOW);
|
||||
|
|
@ -112,6 +112,7 @@ struct Stepper_A3967 : StepperControl {
|
|||
default:
|
||||
ESP_LOGE(STEPPER_TAG,"Unknown StepType=%d",mode);
|
||||
}
|
||||
return(this);
|
||||
}
|
||||
|
||||
};
|
||||
|
|
|
|||
|
|
@ -135,7 +135,7 @@ struct Stepper_TB6612 : StepperControl {
|
|||
|
||||
//////////////////////////
|
||||
|
||||
void setStepType(int mode) override {
|
||||
StepperControl *setStepType(int mode) override {
|
||||
|
||||
switch(mode){
|
||||
case FULL_STEP_ONE_PHASE:
|
||||
|
|
@ -156,7 +156,7 @@ struct Stepper_TB6612 : StepperControl {
|
|||
case QUARTER_STEP:
|
||||
if(!pwmA){
|
||||
ESP_LOGE(STEPPER_TAG,"QUARTER_STEP requires PWM pins");
|
||||
return;
|
||||
return(this);
|
||||
}
|
||||
phase=0;
|
||||
nPhases=16;
|
||||
|
|
@ -165,7 +165,7 @@ struct Stepper_TB6612 : StepperControl {
|
|||
case EIGHTH_STEP:
|
||||
if(!pwmA){
|
||||
ESP_LOGE(STEPPER_TAG,"EIGHTH_STEP requires PWM pins");
|
||||
return;
|
||||
return(this);
|
||||
}
|
||||
phase=0;
|
||||
nPhases=32;
|
||||
|
|
@ -174,6 +174,7 @@ struct Stepper_TB6612 : StepperControl {
|
|||
default:
|
||||
ESP_LOGE(STEPPER_TAG,"Unknown StepType=%d",mode);
|
||||
}
|
||||
return(this);
|
||||
}
|
||||
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
||||
#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);
|
||||
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
|
||||
motor->setAccel(10,20); // add acceleration parameters of 10x spread of 20 steps
|
||||
|
||||
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
|
||||
// bigMotor->setStepType(StepperControl::HALF_STEP);
|
||||
// bigMotor->setAccel(10,20);
|
||||
}
|
||||
|
||||
///////////////////
|
||||
|
||||
void loop(){
|
||||
|
||||
Serial.printf("Motor has %d remaining steps\n",motor->stepsRemaining());
|
||||
|
||||
delay(1000); // motor is unaffected by delay()
|
||||
|
||||
if(motor->position()==1200){
|
||||
Serial.printf("Motor has reached final position and is now stopped.\n");
|
||||
while(1);
|
||||
}
|
||||
|
||||
if(smallMotor->position()==0)
|
||||
smallMotor->moveTo(SMALL_MOTOR_POSITION,2);
|
||||
else if(smallMotor->position()==SMALL_MOTOR_POSITION)
|
||||
smallMotor->moveTo(0,2);
|
||||
|
||||
if(bigMotor->position()==0)
|
||||
bigMotor->moveTo(BIG_MOTOR_POSITION,4);
|
||||
else if(bigMotor->position()==BIG_MOTOR_POSITION)
|
||||
bigMotor->moveTo(0,4);
|
||||
|
||||
delay(1000);
|
||||
Serial.printf("Small Motor: %d Big Motor %d\n",smallMotor->stepsRemaining(),bigMotor->stepsRemaining());
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue