Skip to content

Commit

Permalink
added function to determine steps to target and updated example
Browse files Browse the repository at this point in the history
  • Loading branch information
pkerspe committed Jun 9, 2020
1 parent 2ad951e commit 9baeedd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,13 @@ void setup()
void loop()
{
// just move the stepper back and forth in an endless loop
if (stepper.getDirectionOfMotion() == 0)
if (stepper.getDistanceToTargetSigned() == 0)
{
delay(5000);
previousDirection *= -1;
long relativeTargetPosition = DISTANCE_TO_TRAVEL_IN_STEPS * previousDirection;
Serial.printf("Moving stepper by %ld steps\n", relativeTargetPosition);
stepper.setTargetPositionRelativeInSteps(relativeTargetPosition);
delay(10);
}

// Notice that you can now do whatever you want in the loop function without the need to call processMovement().
Expand Down
33 changes: 28 additions & 5 deletions src/ESP_FlexyStepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,20 @@ void ESP_FlexyStepper::startAsService(void)
disableCore0WDT(); // we have to disable the Watchdog timer to prevent it from rebooting the ESP all the time another option would be to add a vTaskDelay but it would slow down the stepper
xTaskCreate(
ESP_FlexyStepper::taskRunner, /* Task function. */
"FlexyStepper", /* String with name of task. */
10000, /* Stack size in bytes. */
"FlexyStepper", /* String with name of task (by default max 16 characters long) */
1000, /* Stack size in bytes. */
this, /* Parameter passed as input of the task */
1, /* Priority of the task. */
1, /* Priority of the task, 1 seems to work just fine for us */
&this->xHandle); /* Task handle. */
}

void ESP_FlexyStepper::taskRunner(void *parameter)
{
ESP_FlexyStepper *stepperRef = (ESP_FlexyStepper *)parameter;
while (true)
for( ;; )
{
stepperRef->processMovement();
yield();
//vTaskDelay(1); // This would be a working solution to prevent the WDT to fire (if not disabled, yet it will cause noticeably less smooth stepper movements / lower frequencies)
}
}

Expand All @@ -114,6 +114,29 @@ bool ESP_FlexyStepper::isStartedAsService()
return (this->xHandle != NULL);
}

/**
* get the overall max stack size since task creation (since the call to startAsService() )
* This function is used to determine if the stacksize is large enough and has more of a debugging purpose.
* Return the minimum amount of free bytes on the stack that has been measured so far.
*/
long ESP_FlexyStepper::getTaskStackHighWaterMark()
{
if (this->isStartedAsService())
{
return uxTaskGetStackHighWaterMark(this->xHandle);
}
return 0;
}

/**
* get the distance in steps to the currently set target position.
* 0 is returned if the stepper is already at the target position.
* The returned value is signed, depending on the direction to move to reach the target
*/
long ESP_FlexyStepper::getDistanceToTargetSigned(){
return (this->targetPosition_InSteps - this->currentPosition_InSteps);
}

/**
* perform an emergency stop, causing all movements to be canceled instantly
* the optional parameter 'holdUntilReleased' allows to define if the emergency stop shall only affect the current motion (if any)
Expand Down
4 changes: 3 additions & 1 deletion src/ESP_FlexyStepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ESP_FlexyStepper
void connectToPins(byte stepPinNumber, byte directionPinNumber);
static void taskRunner(void *parameter);
bool isStartedAsService(void);
long getTaskStackHighWaterMark(void);

void setStepsPerMillimeter(float motorStepPerMillimeter);
float getCurrentPositionInMillimeters();
Expand All @@ -61,7 +62,8 @@ class ESP_FlexyStepper
void setTargetPositionRelativeInMillimeters(float distanceToMoveInMillimeters);
void moveToPositionInMillimeters(float absolutePositionToMoveToInMillimeters);
void setTargetPositionInMillimeters(float absolutePositionToMoveToInMillimeters);
float getCurrentVelocityInMillimetersPerSecond();
float getCurrentVelocityInMillimetersPerSecond(void);
long getDistanceToTargetSigned(void);

void setStepsPerRevolution(float motorStepPerRevolution);
void setCurrentPositionInRevolutions(float currentPositionInRevolutions);
Expand Down

0 comments on commit 9baeedd

Please sign in to comment.