rusefi-1/firmware/hw_layer/stepper.cpp

57 lines
1.4 KiB
C++
Raw Normal View History

2014-12-24 16:05:31 -08:00
/**
* @file stepper.cpp
*
* http://rusefi.com/wiki/index.php?title=Hardware:Stepper_motor
*
* @date Dec 24, 2014
2015-01-12 15:04:10 -08:00
* @author Andrey Belomutskiy, (c) 2012-2015
2014-12-24 16:05:31 -08:00
*/
#include "stepper.h"
#include "pin_repository.h"
2015-01-04 10:04:12 -08:00
#define ST_DELAY_MS 10
2014-12-24 16:05:31 -08:00
2015-01-04 10:04:12 -08:00
#define ST_COUNT 100
2014-12-24 16:05:31 -08:00
static msg_t stThread(StepperMotor *motor) {
chRegSetThreadName("stepper");
2015-01-04 10:04:12 -08:00
palWritePad(motor->directionPort, motor->directionPin, true);
2014-12-24 16:05:31 -08:00
// let's part the motor in a known position to begin with
for (int i = 0; i < ST_COUNT; i++) {
motor->pulse();
}
2015-01-04 10:04:12 -08:00
palWritePad(motor->directionPort, motor->directionPin, false);
// let's part the motor in a known position to begin with
for (int i = 0; i < ST_COUNT / 2; i++) {
motor->pulse();
}
2014-12-24 16:05:31 -08:00
}
void StepperMotor::pulse() {
palWritePad(stepPort, stepPin, true);
chThdSleepMilliseconds(ST_DELAY_MS);
palWritePad(stepPort, stepPin, false);
chThdSleepMilliseconds(ST_DELAY_MS);
}
void StepperMotor::initialize(brain_pin_e stepPin, brain_pin_e directionPin) {
position = 0;
stepPort = getHwPort(stepPin);
this->stepPin = getHwPin(stepPin);
2015-01-04 10:04:12 -08:00
directionPort = getHwPort(directionPin);
this->directionPin = getHwPin(directionPin);
mySetPadMode2("st step", stepPin, PAL_MODE_OUTPUT_PUSHPULL);
2015-01-02 15:05:43 -08:00
mySetPadMode2("st dir", directionPin, PAL_MODE_OUTPUT_PUSHPULL);
2014-12-24 16:05:31 -08:00
chThdCreateStatic(stThreadStack, sizeof(stThreadStack), NORMALPRIO, (tfunc_t) stThread, this);
}