Proportional Control Demo using Slider in Tkinter

Posted

in

by

Understanding Proportional Control:
Proportional control is a basic feedback control technique that adjusts the control signal proportionally to the error between a desired setpoint and the process variable. The process variable represents the current state of the system being controlled. By continuously monitoring and adjusting the control signal, the system strives to minimize the error and achieve the desired setpoint.

I have created this simple program using python and tkinter library.

When this program is run. A slider will appear which you can move.

A set point of 50 is given as the default value.
When you start the program the slider will be at 0 position. As you increase your slider you will see a change in the control signal parameter.

This Control Signal will be 0 at your set point which is 50.
As you go past the set point the control signal will become negative.

The system will keep changing the control signal to make the slider reach it’s set point.

Code

import tkinter as tk
import time
import webbrowser

# Proportional control function
def proportional_control(error):
    # Proportional gain
    Kp = 0.5
    control_signal = Kp * error
    return control_signal

# Main loop
def main_loop():
    setpoint = 50  # Desired setpoint
    process_variable = 0  # Initial process variable
    while True:
        # Read process variable from the slider
        process_variable = slider.get()

        # Calculate the error
        error = setpoint - process_variable

        # Apply proportional control
        control_signal = proportional_control(error)

        # Actuate the control signal (in this example, update the label)
        control_label.configure(text="Control Signal: {:.2f}".format(control_signal))

        time.sleep(0.1)  # Sleep for 0.1 seconds


# Callback function for the slider
def slider_callback(value):
    feedback_label.configure(text="Feedback Stimulus: {:.2f}".format(float(value)))

# Open exasub.com in a web browser
def open_link(event):
    webbrowser.open("http://www.exasub.com")

# Create the main Tkinter window
window = tk.Tk()
window.title("Proportional Control Demo")

# Create the slider for adjusting the feedback stimulus
slider = tk.Scale(window, from_=0, to=100, orient=tk.HORIZONTAL, length=300, command=slider_callback)
slider.grid(row=0, column=0, columnspan=2, padx=10, pady=10)

# Create a label to display the feedback stimulus value
feedback_label = tk.Label(window, text="Feedback Stimulus: {:.2f}".format(slider.get()))
feedback_label.grid(row=1, column=0, padx=10, pady=5)

# Create a label to display the control signal value
control_label = tk.Label(window, text="Control Signal: ")
control_label.grid(row=1, column=1, padx=10, pady=5)

# Add a link to exasub.com
link = tk.Label(window, text="Visit exasub.com", fg="blue", cursor="hand2", font=("Arial", 14))
link.grid(row=2, column=0, columnspan=2, padx=10, pady=5)
link.bind("<Button-1>", open_link)

# Start the main loop in a separate thread
import threading
main_loop_thread = threading.Thread(target=main_loop)
main_loop_thread.start()

# Start the Tkinter event loop
window.mainloop()


Let’s dive into the code provided and understand how the proportional control demo works.

# Main loop
def main_loop():
    setpoint = 50  # Desired setpoint
    process_variable = 0  # Initial process variable
    while True:
        # Read process variable from the slider
        process_variable = slider.get()

        # Calculate the error
        error = setpoint - process_variable

        # Apply proportional control
        control_signal = proportional_control(error)

        # Actuate the control signal (in this example, update the label)
        control_label.configure(text="Control Signal: {:.2f}".format(control_signal))

        time.sleep(0.1)  # Sleep for 0.1 seconds

Explanation of the Code:
The provided code demonstrates a simple scenario where the process variable is obtained from a slider widget. Here’s a breakdown of the code’s key components:

  1. Setpoint: The setpoint variable represents the desired value or setpoint that we want the process variable to reach.
  2. Process Variable: The process_variable variable holds the current value of the system being controlled, obtained from the slider widget.
  3. Error Calculation: The error is calculated by subtracting the process variable from the setpoint. The error represents the deviation of the process variable from the desired value.
  4. Proportional Control: The proportional_control function, not provided in the code snippet, applies the proportional control algorithm. This function takes the error as input and computes the control signal accordingly.
  5. Actuation: In this example, the control signal is applied by updating a label (control_label) to display the control signal value. In a real-world scenario, the control signal would be used to actuate a physical system, such as adjusting a motor’s speed or a valve’s position.
  6. Timing: To ensure the control loop operates at a reasonable speed, a small delay of 0.1 seconds is introduced using time.sleep(0.1). This delay allows the control system to stabilize before the next iteration.

Understanding Proportional Control:
Proportional control works by adjusting the control signal in proportion to the error. The control signal can be interpreted as an effort or corrective action to reduce the error. In this demo, the control signal is calculated by the proportional_control function, which is not provided in the code snippet.

The proportional control algorithm typically involves multiplying the error by a constant gain, known as the proportional gain (Kp). The control signal is then obtained by multiplying the error with Kp. The value of Kp determines the system’s responsiveness to the error, and finding the appropriate gain is crucial for stable and efficient control.

Conclusion:
The proportional control demo showcased in this blog post provides a basic understanding of how proportional control operates within a control system. By continuously adjusting the control signal based on the error between the setpoint and the process variable, proportional control helps bring the system closer to the desired state. Proportional control is just one of many control techniques, and understanding its principles is vital for delving into more advanced control strategies.

Remember that proportional control alone may not be sufficient for complex systems, as it lacks the ability to anticipate and account for system dynamics. Nonetheless, it forms the foundation for more advanced control techniques like PID (Proportional-Integral-Derivative) control.

So go ahead, experiment with the demo code, and explore the fascinating world of control systems!

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *