diff --git a/lambda/airgate.c b/lambda/airgate.c index e60c5ce..04c27ec 100644 --- a/lambda/airgate.c +++ b/lambda/airgate.c @@ -4,9 +4,9 @@ * Created on: 19.02.2016 * Author: dode@luniks.net * - * Simple (maybe naive) stepper motor control using the DRV8825 with a linear - * acceleration profile. An absolute position from 0 to 255 can be set which - * relates to the actual number of degrees by the SCALE constant and the + * Simple (maybe naive) stepper motor control with a linear acceleration + * profile using the DRV8825. An absolute position from 0 to 255 can be set + * which relates to the actual number of degrees by the SCALE constant and the * stepping mode. If a new position is set while the motor is busy, it is * decelerated before it starts to move to the new position. * The idea is to be able to set the airgate position from 0 - 100%. @@ -43,12 +43,10 @@ * starts the motor by starting the timer. */ static void start(void) { - if (bit_is_clear(PORT, PIN_SLEEP)) { - // wake up driver - PORT |= (1 << PIN_SLEEP); - // wakeup time - _delay_ms(2); - } + // set rated current for max torque + PORT &= ~(1 << PIN_CURRENT); + // some time to stabilize? + _delay_us(3); // set dir if (dir == 1) { PORT &= ~(1 << PIN_DIR); @@ -56,7 +54,7 @@ PORT |= (1 << PIN_DIR); } // setup time - _delay_us(1); + _delay_us(3); // set start speed OCR2A = MIN_SPEED; // start timer2 @@ -74,18 +72,18 @@ /** * Calculates the direction and steps to take to get to the target position, - * the ramp length for the acceleration profile, sets the speed and starts - * the motor. + * the ramp length for the acceleration profile and starts the motor. */ static void set(void) { int16_t diff = (((int16_t)target) << SCALE) - pos; if (diff != 0) { - dir = MAX(-1, MIN(diff, 1)); + dir = (diff > 0) - (diff < 0); steps = abs(diff); ramp = MIN(abs(MAX_SPEED - MIN_SPEED), steps >> 1); start(); } else { - setSleepMode(); + // set reduced current to save power + PORT |= (1 << PIN_CURRENT); } } @@ -135,6 +133,15 @@ return target; } -void setSleepMode(void) { - PORT &= ~(1 << PIN_SLEEP); +void setSleepMode(bool const on) { + if (on) { + // wake up driver + PORT |= (1 << PIN_SLEEP); + // wakeup time + // should not be woken up just before stepping anyway, the power supply + // might need much more time to stabilize + // _delay_ms(2); + } else { + PORT &= ~(1 << PIN_SLEEP); + } } diff --git a/lambda/airgate.h b/lambda/airgate.h index d39f490..a9b88de 100644 --- a/lambda/airgate.h +++ b/lambda/airgate.h @@ -29,19 +29,19 @@ * Sets the airgate position 0 - 100%. The actual number of degrees the motor * spins depends on the SCALE and the stepping mode. If the motor is currently * moving to a target position when this function is called, it is first - * decelerated and then moves to the new target position. + * decelerated and then starts moving to the new target position. */ void setAirgate(uint8_t const position); /** - * Returns the current airgate position, assuming the motor did all the steps + * Returns the current airgate position, assuming the motor does all the steps * it was requested to do. */ uint8_t getAirgate(void); /** - * Sets the driver in sleep mode. + * Wakes up the driver or puts it in sleep mode. */ -void setSleepMode(void); +void setSleepMode(bool const on); #endif /* AIRGATE_H_ */ diff --git a/lambda/interrupts.c b/lambda/interrupts.c index ebdf46a..f90c472 100644 --- a/lambda/interrupts.c +++ b/lambda/interrupts.c @@ -85,10 +85,11 @@ // enable oxygen sensor heater control output pin DDR |= (1 << PIN_HEATER); - // enable dir and step pin for stepper motor driver + // enable output pins for stepper motor driver DDR |= (1 << PIN_SLEEP); DDR |= (1 << PIN_STEP); DDR |= (1 << PIN_DIR); + DDR |= (1 << PIN_CURRENT); } void setupSleepMode(void) { diff --git a/lambda/lambda.c b/lambda/lambda.c index 5c2db3b..747216e 100644 --- a/lambda/lambda.c +++ b/lambda/lambda.c @@ -55,6 +55,9 @@ initInterrupts(); initTimers(); + // wake up stepper motor driver + setSleepMode(true); + alert_P(1, 1, 31, PSTR(MSG_WELCOME), PSTR(""), false); // spend some time on being polite while (getTime() < 3) {} diff --git a/lambda/pins.h b/lambda/pins.h index 4374476..c9c2c2f 100644 --- a/lambda/pins.h +++ b/lambda/pins.h @@ -35,12 +35,14 @@ #define PIN_BEEPER_TOGGLE COM1A0 /** Pin for the oxygen sensor heater */ #define PIN_HEATER PB2 -/** Pin for the stepper motor driver sleep mode **/ +/** Pin for stepper motor driver sleep mode **/ #define PIN_SLEEP PB3 -/** Pin for the stepper motor driver step input */ +/** Pin for stepper motor driver step input */ #define PIN_STEP PB4 -/** Pin for the stepper motor driver direction input */ +/** Pin for stepper motor driver direction input */ #define PIN_DIR PB5 +/** Pin for stepper motor current control */ +#define PIN_CURRENT PB6 /* Pins for the LCD */ #define LCD_PORT PORTD