gin66 / FastAccelStepper

A high speed stepper library for Atmega 168/328p (nano), Atmega32u4, Atmega 2560, ESP32, ESP32S2, ESP32S3, ESP32C3 and Atmel SAM Due
MIT License
282 stars 67 forks source link

linearAcceleration seems to be dependend on the setting of non-linear acceleration #222

Closed Paulvandenbogaard closed 6 months ago

Paulvandenbogaard commented 6 months ago

Working with a Mega2560. Stepper NUMA24, driver DM542T. PlatformIO, FastAccelStepper 0.30.10.

Trying to figure out what LinearAcceleration can do for me. Reading the comments/docs I understand using setLinearAcceleration(steps) will use this amount of steps to reach the speed already set.

With the following "setup()" function:

void setup() {

   Serial.begin(115200);
   engine.init();
   stepper = engine.stepperConnectToPin(stepPinStepper);
   if (stepper) {
      Serial.println("Got stepper");
      stepper->setDirectionPin(dirPinStepper);

      int8_t rv = stepper->setSpeedInHz(hz);
      Serial.print("Result : "); Serial.println(rv);
      Serial.print("MaxSpeedInTics : "); Serial.println( stepper->getMaxSpeedInTicks() );
      Serial.print("MaxSpeedInHz   : "); Serial.println( stepper->getMaxSpeedInHz() );
      rv = stepper->moveTo(0L);
      Serial.print("moveTo(0): "); Serial.println(rv);
      // stepper->setAcceleration(50000L);
      stepper->setLinearAcceleration(1000L);
      delay(100); 
      rv = stepper->moveTo(0L);
      Serial.print("moveTo(0): "); Serial.println(rv);
      Serial.print("Speed: "); Serial.println(hz);
   }
   while (1) ;
}

The resulting output is:

Got stepper
Result : 0
MaxSpeedInTics : 320
MaxSpeedInHz   : 50000
moveTo(0): -3
moveTo(0): -3
Speed: 4000

Both moveTo's report -3 (MOVE_ERR_ACCELERATION_IS_UNDEFINED)

I now uncomment the line:

      // stepper->setAcceleration(50000L);

and rerun:

Result : 0
MaxSpeedInTics : 320
MaxSpeedInHz   : 50000
moveTo(0): -3
moveTo(0): 0
Speed: 4000

Now only the first moveTo reports an error, while the second used after a call of setAcceleration is done, is fine.

I changed the value of the parameter for setAcceleration and it makes the speed of the stepper go different (as expected).

Why is there a need to use the setAcceleration() when trying to do this with setLinearAcceleration() only? Do I need a different approach to work with setLinearAcceleration() only?

gin66 commented 6 months ago

The doc says: “ setLinearAcceleration expects as parameter the number of steps, where the acceleration is increased linearly from standstill up to the configured acceleration value

The acceleration is configured by setAcceleration(). So the behavior is as expected.

The rotation force being applied to the stepper axes is proportional to the acceleration. So a normal ramp applies constant acceleration till maximum speed. This means, from standstill a constant force is applied, which the driven system may not like so much (e.g. a glass full of water being moved linearly). setLinearAcceleration allows for a smoother start, where the force is ramping up, too.