1. Do not share user accounts! Any account that is shared by another person will be blocked and closed. This means: we will close not only the account that is shared, but also the main account of the user who uses another person's account. We have the ability to detect account sharing, so please do not try to cheat the system. This action will take place on 04/18/2023. Read all forum rules.
    Dismiss Notice
  2. For downloading SimTools plugins you need a Download Package. Get it with virtual coins that you receive for forum activity or Buy Download Package - We have a zero Spam tolerance so read our forum rules first.

    Buy Now a Download Plan!
  3. Do not try to cheat our system and do not post an unnecessary amount of useless posts only to earn credits here. We have a zero spam tolerance policy and this will cause a ban of your user account. Otherwise we wish you a pleasant stay here! Read the forum rules
  4. We have a few rules which you need to read and accept before posting anything here! Following these rules will keep the forum clean and your stay pleasant. Do not follow these rules can lead to permanent exclusion from this website: Read the forum rules.
    Are you a company? Read our company rules

SMC3 code update using ResponsiveAnalogRead library to mitigate cogging effect for Flight Sim

Discussion in 'DIY Motion Simulator Building Q&A / FAQ' started by Jango, May 15, 2025.

  1. Jango

    Jango Member

    Joined:
    Aug 12, 2024
    Messages:
    73
    Location:
    Mauritius
    Balance:
    380Coins
    Ratings:
    +27 / 1 / -0
    My Motion Simulator:
    2DOF, DC motor
    Good day everyone,


    I was reading/ researching a bit on motor cogging - while i feel it a little bit when hand flying (in msfs), it is more pronounced when on autopilot. I guess the changes are more gradual.

    I, unfortunately, am not a programmer nor a specialist on motor control, hence was engaging with ChatGPT on possible improvement of the step size.

    It suggested to use the library ResponsiveAnalogRead in the SMC code. I thought to post it here and it even wrote part of the code (yet to see if it has errors). I'll see when i have time to check it, i also use Flypt for the interface.








    // SMC3.ino with ResponsiveAnalogRead support for Arduino UNO
    // Only modified sections shown; rest of SMC3 code remains unchanged

    #include <EEPROM.h>
    #include <ResponsiveAnalogRead.h>

    // MotorData structure remains unchanged

    // Create ResponsiveAnalogRead objects for each motor
    ResponsiveAnalogRead feedbackReader[] = {
    ResponsiveAnalogRead(A0, true), // Motor 1
    ResponsiveAnalogRead(A1, true), // Motor 2
    ResponsiveAnalogRead(A2, true) // Motor 3
    };

    // Optional: tune the responsiveness and noise filtering
    void ConfigureResponsiveAnalogRead() {
    for (int i = 0; i < 3; i++) {
    feedbackReader.setSnapMultiplier(0.01); // Lower = smoother
    }
    }

    // Helper to get raw analog input
    int GetRawAnalogValue(int pin) {
    return analogRead(pin);
    }

    // Modified InitMotor to prime the filter
    void InitMotor(MotorData &m, int Index) {
    m.feedback = 0;
    m.target = NEUTRAL_POS;
    m.idle = 0;
    m.pwmOut = 0;
    m.disabled = true;
    m.cumError = 0;
    m.dTerm_x100 = 0;
    m.lastPos = 0;
    m.kdCounter = 0;
    m.deadzone = 0;
    m.cutoffMin = 50;
    m.cutoffMax = INV(m.cutoffMin);
    m.clipMin = 190;
    m.clipMax = INV(m.clipMin);
    m.centerOffset = 0;
    m.kp_x100 = 120;
    m.ki_x100 = 1;
    m.kd_x100 = 10;
    m.ks = 5;
    m.pwmMin = 0;
    m.pwmMax = 180;
    m.pwmRev = 200;
    m.number = Index + 1;
    m.reverse = Reverse[Index];
    m.invert = Invert[Index];
    m.scale = Scale[Index];
    m.feedbackPin = FeedbackPin[Index];
    m.enaPinA = EnaPinA[Index];
    m.enaPinB = EnaPinB[Index];
    m.pwmPin = PwmPin[Index];

    pinMode(m.feedbackPin, INPUT);
    pinMode(m.enaPinA, OUTPUT);
    pinMode(m.enaPinB, OUTPUT);
    pinMode(m.pwmPin, OUTPUT);

    feedbackReader[Index].update();
    feedbackReader[Index].setValue(GetRawAnalogValue(m.feedbackPin));

    m.idle = m.lastPos = GetFeedback(m);
    }

    // Modified GetFeedback to use filtered signal
    int GetFeedback(MotorData &m) {
    int index = m.number - 1;
    feedbackReader[index].update();
    int value = feedbackReader[index].getValue();
    if (m.reverse) {
    value = INV(value);
    }
    return value;
    }

    // Call this in setup()
    void setup() {
    InitMotor(Motor1, 0);
    InitMotor(Motor2, 1);
    InitMotor(Motor3, 2);

    ConfigureResponsiveAnalogRead(); // NEW LINE to configure filters

    // ... rest of setup code from original SMC3
    }
  2. Joe Cortexian

    Joe Cortexian Active Member Gold Contributor

    Joined:
    Sep 8, 2021
    Messages:
    160
    Balance:
    965Coins
    Ratings:
    +33 / 0 / -0
    My Motion Simulator:
    3DOF
    SMC already uses special fast analog input. There are some odd instructions in the setup() routine that set it up. This library might do something more but I am skeptical.

    Right now in SMCUtils there is a number on the bottom if the screen which will be around 4000. This is the frequency of the control loop. So every 250 micro seconds it updates the data. So if you get this code running look at that number and see if it will run faster. The max it will run is 4000 because there is code which delays if it’s less than 250us. Remove that to see a max number.

    That being said there is a bug in the PID calculation. It is likely to be the cause of your issues. This is a long standing bug I suggest you try it.

    https://www.xsimulator.net/communit...or-driver-and-windows-utilities.4957/page-134

    there is a download on this page go back a page to look at the discussion. Maybe post your analog input question there to get more views.
    • Like Like x 1