From eedc5c42ed95fc0fbab6b4f62150092ebc72f20c Mon Sep 17 00:00:00 2001 From: rusEfi Date: Sun, 29 Mar 2015 18:04:56 -0500 Subject: [PATCH] auto-sync --- firmware/controllers/idle_thread.cpp | 15 ++++++++++++ firmware/hw_layer/hardware.cpp | 6 ----- firmware/hw_layer/stepper.cpp | 36 ++++++++++++++++++++++------ firmware/hw_layer/stepper.h | 7 ++++-- 4 files changed, 49 insertions(+), 15 deletions(-) diff --git a/firmware/controllers/idle_thread.cpp b/firmware/controllers/idle_thread.cpp index 3ec51c2c53..03936d9a5d 100644 --- a/firmware/controllers/idle_thread.cpp +++ b/firmware/controllers/idle_thread.cpp @@ -31,6 +31,7 @@ #include "pin_repository.h" #include "engine_configuration.h" #include "engine.h" +#include "stepper.h" #if EFI_IDLE_CONTROL || defined(__DOXYGEN__) @@ -43,6 +44,8 @@ EXTERN_ENGINE static OutputPin idlePin; static SimplePwm idleValvePwm; +static StepperMotor iacMotor; + /** * Idle level calculation algorithm lives in idle_controller.c */ @@ -64,6 +67,10 @@ static void setIdleControlEnabled(int value) { showIdleInfo(); } +static void setIdleValvePosition(int position) { + iacMotor.targetPosition = position; +} + static void setIdleValvePwm(percent_t value) { if (value < 0.01 || value > 99.9) return; @@ -131,6 +138,11 @@ static void applyIdleSolenoidPinState(PwmConfig *state, int stateIndex) { void startIdleThread(Logging*sharedLogger, Engine *engine) { logger = sharedLogger; + if (boardConfiguration->idleStepperDirection != GPIO_UNASSIGNED) { + iacMotor.initialize(boardConfiguration->idleStepperStep, boardConfiguration->idleStepperDirection); + } + + /** * Start PWM for IDLE_VALVE logical / idleValvePin physical */ @@ -154,6 +166,9 @@ void startIdleThread(Logging*sharedLogger, Engine *engine) { addConsoleAction("idleinfo", showIdleInfo); addConsoleActionI("set_idle_rpm", setIdleRpmAction); addConsoleActionF("set_idle_pwm", setIdleValvePwm); + + addConsoleActionI("set_idle_position", setIdleValvePosition); + addConsoleActionI("set_idle_enabled", (VoidInt) setIdleControlEnabled); } diff --git a/firmware/hw_layer/hardware.cpp b/firmware/hw_layer/hardware.cpp index 7bc1c6eb70..b322bafc4f 100644 --- a/firmware/hw_layer/hardware.cpp +++ b/firmware/hw_layer/hardware.cpp @@ -15,7 +15,6 @@ #include "console_io.h" #include "adc_inputs.h" -#include "stepper.h" #include "vehicle_speed.h" #include "trigger_input.h" @@ -52,8 +51,6 @@ EXTERN_ENGINE ; extern bool hasFirmwareErrorFlag; -static StepperMotor iacMotor; - static Mutex spiMtx; int maxNesting = 0; @@ -270,8 +267,6 @@ void initHardware(Logging *l, Engine *engine) { initMax31855(sharedLogger, getSpiDevice(boardConfiguration->max31855spiDevice), boardConfiguration->max31855_cs); #endif /* EFI_MAX_31855 */ -// iacMotor.initialize(GPIOD_11, GPIOD_10); - #if EFI_CAN_SUPPORT initCan(); #endif /* EFI_CAN_SUPPORT */ @@ -279,7 +274,6 @@ void initHardware(Logging *l, Engine *engine) { // init_adc_mcp3208(&adcState, &SPID2); // requestAdcValue(&adcState, 0); - #if EFI_SHAFT_POSITION_INPUT || defined(__DOXYGEN__) // todo: figure out better startup logic initTriggerCentral(sharedLogger, engine); diff --git a/firmware/hw_layer/stepper.cpp b/firmware/hw_layer/stepper.cpp index 9c9422db54..f53ee62a0a 100644 --- a/firmware/hw_layer/stepper.cpp +++ b/firmware/hw_layer/stepper.cpp @@ -17,23 +17,46 @@ static msg_t stThread(StepperMotor *motor) { chRegSetThreadName("stepper"); - palWritePad(motor->directionPort, motor->directionPin, true); + palWritePad(motor->directionPort, motor->directionPin, false); - // let's part the motor in a known position to begin with + // let's park the motor in a known position to begin with for (int i = 0; i < ST_COUNT; i++) { motor->pulse(); } - palWritePad(motor->directionPort, motor->directionPin, false); + while (true) { + int targetPosition = motor->targetPosition; + int currentPosition = motor->currentPosition; - // let's part the motor in a known position to begin with - for (int i = 0; i < ST_COUNT / 2; i++) { + if (targetPosition == currentPosition) { + chThdSleepMilliseconds(ST_DELAY_MS); + continue; + } + bool_t isIncrementing = targetPosition > currentPosition; + palWritePad(motor->directionPort, motor->directionPin, isIncrementing); + 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; + directionPort = NULL; + stepPort = NULL; +} + void StepperMotor::pulse() { palWritePad(stepPort, stepPin, true); chThdSleepMilliseconds(ST_DELAY_MS); @@ -42,8 +65,6 @@ void StepperMotor::pulse() { } void StepperMotor::initialize(brain_pin_e stepPin, brain_pin_e directionPin) { - position = 0; - stepPort = getHwPort(stepPin); this->stepPin = getHwPin(stepPin); @@ -55,3 +76,4 @@ void StepperMotor::initialize(brain_pin_e stepPin, brain_pin_e directionPin) { chThdCreateStatic(stThreadStack, sizeof(stThreadStack), NORMALPRIO, (tfunc_t) stThread, this); } + diff --git a/firmware/hw_layer/stepper.h b/firmware/hw_layer/stepper.h index d5bb0943dd..5d5ce311e4 100644 --- a/firmware/hw_layer/stepper.h +++ b/firmware/hw_layer/stepper.h @@ -11,17 +11,20 @@ class StepperMotor { public: + StepperMotor(); void initialize(brain_pin_e stepPin, brain_pin_e directionPin); void pulse(); GPIO_TypeDef * directionPort; ioportmask_t directionPin; + + int currentPosition; + int targetPosition; + private: GPIO_TypeDef * stepPort; ioportmask_t stepPin; - int position; - THD_WORKING_AREA(stThreadStack, UTILITY_THREAD_STACK_SIZE); };