uStepper / uStepperS32

7 stars 3 forks source link

Compiling with optimizations appears to cause silent bugs in the library. #12

Open Jesperoka opened 1 week ago

Jesperoka commented 1 week ago

Using arduino-cli and compiling with c++ compiler flags -O1, -O2 and -O3 appears to silently break the encoder angle reading.

Compiling with:

arduino-cli \
--config-file $PWD/arduino-cli.yaml \
compile \
--protocol "DFU" \
--output-dir $PWD/arduino/build/artifacts \
--build-cache-path $PWD/arduino/build/cache \
--build-path $PWD/arduino/build/artifacts \
--warnings more \
--log \
# --build-property compiler.cpp.extra_flags=" " \ # (i.e. no extra_flags) Works fine
--build-property compiler.cpp.extra_flags="-fno-access-control -Werror=implicit-fallthrough=5 -Wall -Wextra" \ # Works fine
# --build-property compiler.cpp.extra_flags="-O1" \ # encoder only reads 0.0 (and intermittently some other spurious values)
# --build-property compiler.cpp.extra_flags="-O2" \ # encoder only reads 0.0 (and intermittently some other spurious values)
# --build-property compiler.cpp.extra_flags="-O3" \ # encoder only reads 0.0 (and intermittently some other spurious values)

where the arduino-cli.yaml has the following options set:

# Configuration file for arduino command-line interface

board_manager:
  # the URLs to any additional Boards Manager package index files needed for your boards platforms.
  additional_urls:
    - https://raw.githubusercontent.com/uStepper/uStepperSTM32Hardware/master/package.json

# Directories used by Arduino CLI.
directories:

  # Directory used to store Boards/Library Manager index files and Boards Manager platform installations.
  data: arduino/data

  # Directory used to stage downloaded archives during Boards/Library Manager installations.
  downloads: arduino/downloads

  # The equivalent of the Arduino IDE's "sketchbook" directory. Library Manager installations are made to the libraries subdirectory of the user directory.
  user: arduino/user

# Configuration options for Arduino CLI's logs.
logging:
  # Path to the file where logs will be written.
  file: arduino/log.json

  # Output format for the logs. Allowed values are text or json.
  format: json

  # Messages with this level and above will be logged. Valid levels are: trace, debug, info, warn, error, fatal, panic.
  level: warn

# Settings related to the collection of data used for continued improvement of Arduino CLI.
metrics:
  # Controls the use of metrics.
  enabled: false

# Configuration options related to the compilation cache
build_cache:
  # The path to the build cache, default is $TMP/arduino.
  path: arduino/build/cache

  # Interval, in number of compilations, at which the cache is purged, defaults to 10. When 0 the cache is never purged.
  compilations_before_purge: 10

  # cache expiration time of build folders. If the cache is hit by a compilation the corresponding build files lifetime is renewed. The value format must be a valid input for time.ParseDuration(), defaults to 720h (30 days).
  ttl: 720h

And tested with the example file:

#include <UstepperS32.h>

UstepperS32 stepper;
float angle = 360.0; //amount of degrees to move

void setup(){
    stepper.setup();                  //Initialize uStepper S32
    stepper.checkOrientation(30.0);   //Check orientation of motor connector with +/- 30 microsteps movement
    stepper.setMaxAcceleration(2000); //use an acceleration of 2000 fullsteps/s^2
    stepper.setMaxVelocity(500);      //Max velocity of 500 fullsteps/s
    Serial.begin(115200);
}

void loop() {
    Serial.print("Angle: ");
    float angle = stepper.encoder.getAngleMovedRaw();
    Serial.print(angle); //print out angle moved since last reset
    Serial.println(" units");
}

The issue is solved for me personally by turning off optimizations.

Jesperoka commented 6 days ago

The arduino-cli.yaml setup is just because that's what I'm using for my work now, you should be able to reproduce the issue with a simpler setup :+1: