2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file stepper.h
|
|
|
|
*
|
|
|
|
* @date Dec 24, 2014
|
2020-01-13 18:57:43 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
2020-09-07 11:41:04 -07:00
|
|
|
|
|
|
|
#pragma once
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2018-09-16 19:26:57 -07:00
|
|
|
#include "global.h"
|
2019-03-29 06:11:13 -07:00
|
|
|
#include "efi_gpio.h"
|
2017-12-22 05:49:03 -08:00
|
|
|
#include "backup_ram.h"
|
2020-01-08 22:03:23 -08:00
|
|
|
#include "thread_controller.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-01-14 23:18:15 -08:00
|
|
|
class StepperHw {
|
|
|
|
public:
|
2020-09-07 11:41:04 -07:00
|
|
|
virtual bool step(bool positive) = 0;
|
2021-10-06 10:57:04 -07:00
|
|
|
// pause between steps
|
|
|
|
void pause(int divisor = 1) const;
|
|
|
|
// pause and enter the idle mode (less current consumption)
|
2021-10-30 17:31:09 -07:00
|
|
|
virtual void sleep();
|
2020-01-14 23:18:15 -08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void setReactionTime(float ms);
|
|
|
|
|
|
|
|
private:
|
|
|
|
float m_reactionTime = 5.0f;
|
|
|
|
};
|
|
|
|
|
|
|
|
class StepDirectionStepper final : public StepperHw {
|
|
|
|
public:
|
|
|
|
void initialize(brain_pin_e stepPin, brain_pin_e directionPin, pin_output_mode_e directionPinMode, float reactionTime, brain_pin_e enablePin, pin_output_mode_e enablePinMode);
|
|
|
|
|
2020-09-07 11:41:04 -07:00
|
|
|
bool step(bool positive) override;
|
2020-01-14 23:18:15 -08:00
|
|
|
|
|
|
|
private:
|
2020-09-07 11:41:04 -07:00
|
|
|
bool pulse();
|
2020-01-14 23:18:15 -08:00
|
|
|
void setDirection(bool isIncrementing);
|
|
|
|
|
|
|
|
bool m_currentDirection = false;
|
|
|
|
|
|
|
|
OutputPin directionPin, stepPin, enablePin;
|
|
|
|
pin_output_mode_e directionPinMode, stepPinMode, enablePinMode;
|
|
|
|
};
|
|
|
|
|
2020-09-07 11:41:04 -07:00
|
|
|
class DcMotor;
|
|
|
|
|
|
|
|
class DualHBridgeStepper final : public StepperHw {
|
|
|
|
public:
|
|
|
|
void initialize(DcMotor* motorPhaseA, DcMotor* motorPhaseB, float reactionTime);
|
|
|
|
|
|
|
|
bool step(bool positive) override;
|
|
|
|
|
2021-10-30 17:31:09 -07:00
|
|
|
void sleep() override;
|
2021-10-06 10:57:04 -07:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool update(float dutyMult);
|
|
|
|
|
2020-09-07 11:41:04 -07:00
|
|
|
private:
|
|
|
|
DcMotor* m_motorPhaseA = nullptr;
|
|
|
|
DcMotor* m_motorPhaseB = nullptr;
|
|
|
|
|
|
|
|
uint8_t m_phase = 0;
|
|
|
|
};
|
|
|
|
|
2021-08-02 04:03:57 -07:00
|
|
|
class StepperMotorBase {
|
2015-07-10 06:01:56 -07:00
|
|
|
public:
|
2021-08-02 04:03:57 -07:00
|
|
|
virtual void initialize(StepperHw* hardware, int totalSteps);
|
|
|
|
void doIteration();
|
2020-01-08 22:03:23 -08:00
|
|
|
|
2021-08-02 04:03:57 -07:00
|
|
|
void setTargetPosition(float targetPositionSteps);
|
|
|
|
float getTargetPosition() const;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-09-07 11:41:04 -07:00
|
|
|
bool isBusy() const;
|
|
|
|
|
2020-01-08 22:03:23 -08:00
|
|
|
int m_currentPosition = 0;
|
|
|
|
int m_totalSteps = 0;
|
|
|
|
|
|
|
|
protected:
|
2020-09-07 11:41:04 -07:00
|
|
|
void setInitialPosition(void);
|
|
|
|
|
|
|
|
void saveStepperPos(int pos);
|
|
|
|
int loadStepperPos();
|
|
|
|
|
|
|
|
void changeCurrentPosition(bool positive);
|
|
|
|
void postCurrentPosition(void);
|
2020-01-08 22:03:23 -08:00
|
|
|
|
2020-01-14 23:18:15 -08:00
|
|
|
StepperHw* m_hw = nullptr;
|
2019-04-12 14:28:19 -07:00
|
|
|
|
2021-08-02 04:03:57 -07:00
|
|
|
float m_targetPosition = 0;
|
2020-09-07 11:41:04 -07:00
|
|
|
bool initialPositionSet = false;
|
2021-08-02 04:03:57 -07:00
|
|
|
bool m_isBusy = false;
|
2015-07-10 06:01:56 -07:00
|
|
|
};
|
|
|
|
|
2021-08-02 04:03:57 -07:00
|
|
|
#if !EFI_UNIT_TEST
|
|
|
|
|
|
|
|
class StepperMotor final : public StepperMotorBase, private ThreadController<UTILITY_THREAD_STACK_SIZE> {
|
|
|
|
public:
|
|
|
|
StepperMotor() : ThreadController("stepper", PRIO_STEPPER) {}
|
|
|
|
|
|
|
|
void initialize(StepperHw* hardware, int totalSteps);
|
|
|
|
|
|
|
|
void ThreadTask() override {
|
|
|
|
// Require hardware to be set
|
|
|
|
if (!m_hw) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
doIteration();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|