Update Stepper.md
This commit is contained in:
parent
a52b4ad050
commit
314af57b28
|
|
@ -170,6 +170,99 @@ void loop(){
|
|||
|
||||
A fully worked example showing how to use the *StepperControl* class within a complete HomeSpan sketch to control a Motorize Window Shade can be found in the Arduino IDE under [*File → Examples → HomeSpan → Other Examples → StepperMotorControl*](../examples/Other%20Examples/StepperMotorControl).
|
||||
|
||||
## Creating your own **StepperControl** Driver
|
||||
|
||||
If neither of the above motor driver classes works for your specific chip or driver board, it is relatively straightfoward to create a new driver to use in your sketch. This is because all the logic to operate a stepper motor in the background is already embedded in the parent **StepperControl** class. To create your own driver, start by creating a child class derived from **StepperControl**. Next, add a constructor that defines the pins and performs any initializations if needed. Finally, define the following methods that **StepperControl** calls to operate the motor:
|
||||
|
||||
* `void onStep(boolean direction)` - contains the logic to advance the motor by a single step based on the *direction* parameter
|
||||
* `void onEnable()` - contains the logic that enables the motor driver
|
||||
* `void onDisable()` - contains the logic that disables the motor driver
|
||||
* `void onBrake()` - contains the logic that puts the motor into a short brake state
|
||||
* `void setStepType(int mode)` - contains the logic to set the step type mode based on the *mode* parameter
|
||||
|
||||
Only the first method, `onStep()`, is required to be defined. You can leave any of the other methods undefined if they are not applicable for your specific driver board. You can of course create additional methods to reflect any other features your driver board may support.
|
||||
|
||||
As an example, below is the complete code for the **Stepper_A3967** class:
|
||||
|
||||
```C++
|
||||
#include "StepperControl.h"
|
||||
|
||||
//////////////////////////
|
||||
|
||||
struct Stepper_A3967 : StepperControl {
|
||||
|
||||
int m1Pin;
|
||||
int m2Pin;
|
||||
int stepPin;
|
||||
int dirPin;
|
||||
int enablePin;
|
||||
|
||||
//////////////////////////
|
||||
|
||||
Stepper_A3967(int m1Pin, int m2Pin, int stepPin, int dirPin, int enablePin, std::pair<uint32_t, uint32_t> taskParams = {1,0}) : StepperControl(taskParams.first,taskParams.second){
|
||||
this->m1Pin=m1Pin;
|
||||
this->m2Pin=m2Pin;
|
||||
this->stepPin=stepPin;
|
||||
this->dirPin=dirPin;
|
||||
this->enablePin=enablePin;
|
||||
|
||||
pinMode(m1Pin,OUTPUT);
|
||||
pinMode(m2Pin,OUTPUT);
|
||||
pinMode(stepPin,OUTPUT);
|
||||
pinMode(dirPin,OUTPUT);
|
||||
pinMode(enablePin,OUTPUT);
|
||||
|
||||
setStepType(FULL_STEP_TWO_PHASE);
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
|
||||
void onStep(boolean direction){
|
||||
digitalWrite(dirPin,direction);
|
||||
digitalWrite(stepPin,HIGH);
|
||||
digitalWrite(stepPin,LOW);
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
|
||||
void onEnable() override {
|
||||
digitalWrite(enablePin,0);
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
|
||||
void onDisable() override {
|
||||
digitalWrite(enablePin,1);
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
|
||||
void setStepType(int mode) override {
|
||||
switch(mode){
|
||||
case FULL_STEP_TWO_PHASE:
|
||||
digitalWrite(m1Pin,LOW);
|
||||
digitalWrite(m2Pin,LOW);
|
||||
break;
|
||||
case HALF_STEP:
|
||||
digitalWrite(m1Pin,HIGH);
|
||||
digitalWrite(m2Pin,LOW);
|
||||
break;
|
||||
case QUARTER_STEP:
|
||||
digitalWrite(m1Pin,LOW);
|
||||
digitalWrite(m2Pin,HIGH);
|
||||
break;
|
||||
case EIGHTH_STEP:
|
||||
digitalWrite(m1Pin,HIGH);
|
||||
digitalWrite(m2Pin,HIGH);
|
||||
break;
|
||||
default:
|
||||
ESP_LOGE(STEPPER_TAG,"Unknown StepType=%d",mode);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
[↩️](../README.md) Back to the Welcome page
|
||||
|
|
|
|||
Loading…
Reference in New Issue