rusefi-1/firmware/hw_layer/stepper.cpp

111 lines
2.9 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file stepper.cpp
*
* http://rusefi.com/wiki/index.php?title=Hardware:Stepper_motor
*
* @date Dec 24, 2014
2017-01-03 03:05:22 -08:00
* @author Andrey Belomutskiy, (c) 2012-2017
2015-07-10 06:01:56 -07:00
*/
#include "stepper.h"
#include "pin_repository.h"
2017-06-12 15:31:55 -07:00
#include "engine.h"
2015-07-10 06:01:56 -07:00
2017-06-12 15:31:55 -07:00
EXTERN_ENGINE;
2015-07-10 06:01:56 -07:00
static msg_t stThread(StepperMotor *motor) {
chRegSetThreadName("stepper");
2017-06-13 06:28:05 -07:00
motor->directionPin.setValue(false);
2015-07-10 06:01:56 -07:00
2017-02-11 23:02:24 -08:00
/**
* let's park the motor in a known position to begin with
*
* I believe it's safer to retract the valve for parking - at least on a bench I've seen valves
* disassembling themselves while pushing too far out.
*/
2015-07-10 06:01:56 -07:00
for (int i = 0; i < motor->totalSteps; i++) {
motor->pulse();
}
while (true) {
int targetPosition = motor->getTargetPosition();
int currentPosition = motor->currentPosition;
if (targetPosition == currentPosition) {
2017-06-12 15:31:55 -07:00
chThdSleepMilliseconds(boardConfiguration->idleStepperPulseDuration);
2015-07-10 06:01:56 -07:00
continue;
}
2016-01-11 16:02:19 -08:00
bool isIncrementing = targetPosition > currentPosition;
2017-06-13 06:28:05 -07:00
motor->directionPin.setValue(isIncrementing);
2015-07-10 06:01:56 -07:00
if (isIncrementing) {
motor->currentPosition++;
} else {
motor->currentPosition--;
}
motor->pulse();
}
// let's part the motor in a known position to begin with
// for (int i = 0; i < ST_COUNT / 2; i++) {
// motor->pulse();
// }
return 0;
}
StepperMotor::StepperMotor() {
currentPosition = 0;
targetPosition = 0;
enablePort = NULL;
enablePin = 0;
stepPort = NULL;
stepPin = 0;
reactionTime = 0;
totalSteps = 0;
}
int StepperMotor::getTargetPosition() {
return targetPosition;
}
void StepperMotor::setTargetPosition(int targetPosition) {
this->targetPosition = targetPosition;
}
void StepperMotor::pulse() {
palWritePad(enablePort, enablePin, false); // ebable stepper
palWritePad(stepPort, stepPin, true);
2017-06-12 15:31:55 -07:00
chThdSleepMilliseconds(boardConfiguration->idleStepperPulseDuration);
2015-07-10 06:01:56 -07:00
palWritePad(stepPort, stepPin, false);
2017-06-12 15:31:55 -07:00
chThdSleepMilliseconds(boardConfiguration->idleStepperPulseDuration);
2015-07-10 06:01:56 -07:00
palWritePad(enablePort, enablePin, true); // disable stepper
}
2017-06-13 06:28:05 -07:00
void StepperMotor::initialize(brain_pin_e stepPin, brain_pin_e directionPin, pin_output_mode_e directionPinMode,
float reactionTime, int totalSteps, brain_pin_e enablePin) {
2015-07-10 06:01:56 -07:00
this->reactionTime = maxF(1, reactionTime);
this->totalSteps = maxI(3, totalSteps);
if (stepPin == GPIO_UNASSIGNED || directionPin == GPIO_UNASSIGNED) {
return;
}
2017-07-28 11:27:37 -07:00
stepPort = getHwPort("step", stepPin);
this->stepPin = getHwPin("step", stepPin);
2015-07-10 06:01:56 -07:00
2017-06-13 06:28:05 -07:00
this->directionPinMode = directionPinMode;
this->directionPin.initPin("stepper dir", directionPin, &this->directionPinMode);
2015-07-10 06:01:56 -07:00
2017-07-28 11:27:37 -07:00
enablePort = getHwPort("enable", enablePin);
this->enablePin = getHwPin("enable", enablePin);
2015-07-10 06:01:56 -07:00
2017-05-15 05:40:54 -07:00
efiSetPadMode("stepper step", stepPin, PAL_MODE_OUTPUT_PUSHPULL);
efiSetPadMode("stepper enable", enablePin, PAL_MODE_OUTPUT_PUSHPULL);
2015-07-10 06:01:56 -07:00
palWritePad(this->enablePort, enablePin, true); // disable stepper
chThdCreateStatic(stThreadStack, sizeof(stThreadStack), NORMALPRIO, (tfunc_t) stThread, this);
}