hj91 / node-red-contrib-easy-pid-controller

This Node-RED node provides a PID (Proportional-Integral-Derivative) controller functionality. It utilizes the simple-pid-controller package to perform the control logic.
GNU General Public License v3.0
1 stars 1 forks source link

Add the ability to stop PID #3

Open victordvs85 opened 11 months ago

victordvs85 commented 11 months ago

While the flow is running, add the ability to stop the PID, stop the accumulation of error coefficients, or reset the accumulated error coefficients.

hj91 commented 11 months ago

const PIDController = require('simple-pid-controller');

module.exports = function(RED) { function EasyPIDControllerNode(config) { // ... (existing code here)

    // Extended state
    let paused = false;
    let resetError = false;

    // ... (existing initialization code)

    node.on('input', function(msg) {
        try {
            if (msg.topic === 'SV') {
                // ... (existing code)
            }

            // Handle pause message
            if (msg.topic === 'pausePID') {
                paused = msg.payload === true;
                node.status({ fill: paused ? "orange" : "green", shape: "ring", text: paused ? "PID paused" : "PID active" });
            }

            // Handle reset accumulated error message
            if (msg.topic === 'resetError') {
                resetError = true; // We'll handle the actual reset in the PID processing code below
                node.status({ fill: "grey", shape: "ring", text: "PID error reset" });
            }

            // ... (existing 'SV', 'auto', and 'PV' handling code)

            if (pidTimer == null) {
                pidTimer = setInterval(function() {
                    if (!paused) {
                        if (resetError) {
                            controller.i = 0; // Reset the integral term
                            resetError = false; // Clear the reset flag
                        }

                        // ... (Rest PID processing code)
                    }
                }, node.dt * 1000);
            }

        } catch (error) {
            // ... (existing error handling code)
        }
    });

    // ... (existing 'close' handling code)
}

RED.nodes.registerType("easy-pid-controller", EasyPIDControllerNode);

}

Try adding this modification to existing code and let me know if it works well so that i can commit it in updated release