Completed motor control setPosition()
Updated logic of setPosition(), move(), and moveTo() to use waitForAck() so that an initial acknowledgment is received from the motorTask() before proceeding.
This commit is contained in:
parent
480af6cf85
commit
fe4d83140f
|
|
@ -61,10 +61,9 @@ void StepperControl::move(int nSteps, uint32_t msDelay, endAction_t endAction){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
upLink_t upLinkData = { .nSteps=nSteps, .msDelay=msDelay, .absoluteStep=false, endAction=endAction };
|
upLink_t upLinkData = { .nSteps=nSteps, .msDelay=msDelay, .action=MOVE, endAction=endAction };
|
||||||
xQueueReceive(downLinkQueue,&downLinkData,0);
|
|
||||||
downLinkData.stepsRemaining=nSteps;
|
|
||||||
xQueueOverwrite(upLinkQueue,&upLinkData);
|
xQueueOverwrite(upLinkQueue,&upLinkData);
|
||||||
|
waitForAck();
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////
|
//////////////////////////
|
||||||
|
|
@ -75,10 +74,9 @@ void StepperControl::moveTo(int nPosition, uint32_t msDelay, endAction_t endActi
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
upLink_t upLinkData = { .nSteps=nPosition, .msDelay=msDelay, .absoluteStep=true, .endAction=endAction };
|
upLink_t upLinkData = { .nSteps=nPosition, .msDelay=msDelay, .action=MOVETO, .endAction=endAction };
|
||||||
xQueueReceive(downLinkQueue,&downLinkData,0);
|
|
||||||
downLinkData.stepsRemaining=nPosition-downLinkData.position;
|
|
||||||
xQueueOverwrite(upLinkQueue,&upLinkData);
|
xQueueOverwrite(upLinkQueue,&upLinkData);
|
||||||
|
waitForAck();
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////
|
//////////////////////////
|
||||||
|
|
@ -99,15 +97,24 @@ int StepperControl::position(){
|
||||||
|
|
||||||
void StepperControl::setPosition(int pos){
|
void StepperControl::setPosition(int pos){
|
||||||
if(!stepsRemaining()){
|
if(!stepsRemaining()){
|
||||||
downLinkData.position=pos;
|
upLink_t upLinkData = { .nSteps=pos, .msDelay=10, .action=SET_POSITION, .endAction=NONE };
|
||||||
xQueueOverwrite(downLinkQueue,&downLinkData);
|
xQueueOverwrite(upLinkQueue,&upLinkData);
|
||||||
|
waitForAck();
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGE(STEPPER_TAG,"can't set Position while motor is running");
|
ESP_LOGE(STEPPER_TAG,"can't set position while motor is running");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////
|
//////////////////////////
|
||||||
|
|
||||||
|
void StepperControl::waitForAck(){
|
||||||
|
downLinkData.ack=false;
|
||||||
|
while(downLinkData.ack==false)
|
||||||
|
xQueueReceive(downLinkQueue,&downLinkData,0);
|
||||||
|
};
|
||||||
|
|
||||||
|
//////////////////////////
|
||||||
|
|
||||||
void StepperControl::brake(){
|
void StepperControl::brake(){
|
||||||
move(0,10,BRAKE);
|
move(0,10,BRAKE);
|
||||||
while(stepsRemaining());
|
while(stepsRemaining());
|
||||||
|
|
@ -130,18 +137,27 @@ void StepperControl::enable(){
|
||||||
|
|
||||||
void StepperControl::motorTask(void *args){
|
void StepperControl::motorTask(void *args){
|
||||||
StepperControl *motor = (StepperControl *)args;
|
StepperControl *motor = (StepperControl *)args;
|
||||||
upLink_t upLinkData = { .nSteps=0, .msDelay=10, .absoluteStep=false, .endAction=NONE };
|
upLink_t upLinkData;
|
||||||
downLink_t downLinkData = { .stepsRemaining=0, .position=0 };
|
downLink_t downLinkData;
|
||||||
boolean running=false;
|
boolean running=false;
|
||||||
|
|
||||||
for(;;){
|
for(;;){
|
||||||
|
|
||||||
if(xQueueReceive(motor->upLinkQueue, &upLinkData, 0)){
|
if(xQueueReceive(motor->upLinkQueue, &upLinkData, 0)){
|
||||||
if(upLinkData.absoluteStep)
|
switch(upLinkData.action){
|
||||||
upLinkData.nSteps-=downLinkData.position;
|
case SET_POSITION:
|
||||||
downLinkData.stepsRemaining=upLinkData.nSteps;
|
downLinkData.position=upLinkData.nSteps;
|
||||||
motor->onEnable();
|
break;
|
||||||
|
case MOVETO:
|
||||||
|
upLinkData.nSteps-=downLinkData.position;
|
||||||
|
[[fallthrough]];
|
||||||
|
case MOVE:
|
||||||
|
downLinkData.stepsRemaining=upLinkData.nSteps;
|
||||||
|
motor->onEnable();
|
||||||
|
break;
|
||||||
|
}
|
||||||
running=true;
|
running=true;
|
||||||
|
downLinkData.ack=true;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t msDelay=upLinkData.msDelay;
|
uint32_t msDelay=upLinkData.msDelay;
|
||||||
|
|
@ -175,6 +191,7 @@ void StepperControl::motorTask(void *args){
|
||||||
xQueueOverwrite(motor->downLinkQueue,&downLinkData);
|
xQueueOverwrite(motor->downLinkQueue,&downLinkData);
|
||||||
downLinkData.stepsRemaining+=dStep;
|
downLinkData.stepsRemaining+=dStep;
|
||||||
downLinkData.position-=dStep;
|
downLinkData.position-=dStep;
|
||||||
|
downLinkData.ack=false;
|
||||||
vTaskDelay(msDelay);
|
vTaskDelay(msDelay);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,28 +51,36 @@ class StepperControl {
|
||||||
BRAKE
|
BRAKE
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum action_t {
|
||||||
|
MOVE,
|
||||||
|
MOVETO,
|
||||||
|
SET_POSITION
|
||||||
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
struct upLink_t {
|
struct upLink_t {
|
||||||
int nSteps;
|
int nSteps;
|
||||||
uint32_t msDelay;
|
uint32_t msDelay;
|
||||||
boolean absoluteStep;
|
action_t action;
|
||||||
endAction_t endAction;
|
endAction_t endAction;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct downLink_t {
|
struct downLink_t {
|
||||||
int stepsRemaining;
|
int stepsRemaining=0;
|
||||||
int position;
|
int position=0;
|
||||||
|
boolean ack=false;
|
||||||
};
|
};
|
||||||
|
|
||||||
float accelSteps=1;
|
float accelSteps=1;
|
||||||
float accelSize=0;
|
float accelSize=0;
|
||||||
downLink_t downLinkData = { .stepsRemaining=0, .position=0 };
|
downLink_t downLinkData;
|
||||||
|
|
||||||
TaskHandle_t motorTaskHandle;
|
TaskHandle_t motorTaskHandle;
|
||||||
QueueHandle_t upLinkQueue;
|
QueueHandle_t upLinkQueue;
|
||||||
QueueHandle_t downLinkQueue;
|
QueueHandle_t downLinkQueue;
|
||||||
|
|
||||||
|
void waitForAck();
|
||||||
virtual void onStep(boolean direction)=0;
|
virtual void onStep(boolean direction)=0;
|
||||||
virtual void onEnable(){};
|
virtual void onEnable(){};
|
||||||
virtual void onDisable(){};
|
virtual void onDisable(){};
|
||||||
|
|
|
||||||
|
|
@ -45,14 +45,14 @@ void setup() {
|
||||||
motor=new Stepper_TB6612(23,32,22,14);
|
motor=new Stepper_TB6612(23,32,22,14);
|
||||||
|
|
||||||
motor->setStepType(StepperControl::HALF_STEP);
|
motor->setStepType(StepperControl::HALF_STEP);
|
||||||
motor->setAccel(10,100);
|
motor->setAccel(10,10);
|
||||||
|
motor->setPosition(200);
|
||||||
motor->move(100,2);
|
motor->move(100,2);
|
||||||
motor->setPosition(200);
|
|
||||||
while(motor->stepsRemaining());
|
while(motor->stepsRemaining());
|
||||||
motor->setPosition(200);
|
motor->setPosition(-200);
|
||||||
delay(10);
|
|
||||||
Serial.printf("Position=%d\n",motor->position());
|
|
||||||
motor->moveTo(0,2,StepperControl::BRAKE);
|
motor->moveTo(0,2,StepperControl::BRAKE);
|
||||||
|
while(motor->position()<-11);
|
||||||
|
motor->disable();
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////
|
//////////////////////////////////////
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue