MODULE 5
Python for Instrumentation
Control System Types: Open-Loop Control, Closed-Loop Control
Open-Loop Control System:
An Open-Loop Control System is a type of control system where the output is not fed back
to the input for comparison. In other words, the system operates based on predetermined
instructions or input without any correction based on the output. It is a simpler system that does
not adjust based on the actual performance or output of the system.
Key Features:
• No Feedback: The output is not used to modify the input.
• Predictable but Less Accurate: The system works according to set instructions, and its
performance depends on the accuracy of the input and the environment.
• Simplicity: It is generally less complex and cheaper to implement.
• Examples: A washing machine (where the washing time is set but the performance is not
checked), a toaster, or a microwave.
Advantages:
• Simple and easy to design.
• Less expensive to build.
• Suitable for processes where the conditions are stable and predictable.
Disadvantages:
• No error correction mechanism.
• Performance may degrade due to external disturbances or changes in the system.
Closed-Loop Control System:
A Closed-Loop Control System (also called a feedback control system) continuously
monitors the output and adjusts the input to maintain the desired performance. The system
includes sensors or feedback devices that compare the actual output with the desired output,
making real-time adjustments as needed.
Key Features:
• Feedback Mechanism: The system constantly compares the output with the input and makes
adjustments accordingly.
• Accuracy: More accurate, as it can correct errors and compensate for disturbances.
• Complexity: More complex to design and maintain than open-loop systems.
• Examples: Temperature control in air conditioners, cruise control in vehicles, and automatic
temperature regulation in ovens.
Advantages:
• Corrects errors by adjusting the input based on output.
• Can adapt to changes in the system or external disturbances.
• Provides higher accuracy and stability.
Disadvantages:
• More complex and expensive to build.
• Requires more sensors and control components.
• Can be slower due to continuous feedback processing.
Open-Loop Control Algorithm: Python Programs
Example: Simple Heating System (Open-Loop Control)
In this example, we'll control a heater for a set amount of time, without feedback to
correct the temperature.
import time
class OpenLoopHeatingSystem:
def __init__(self, target_temperature):
self.current_temperature = 20 # Initial temperature
self.target_temperature = target_temperature
def turn_on_heater(self):
# Simulating the heating process
print("Heater turned on.")
while self.current_temperature < self.target_temperature:
self.current_temperature += 1
print(f"Current Temperature: {self.current_temperature}°C")
[Link](1) # Simulate time passing, pause the program for 1 second to simulate the
passage of time between heating increments.
print("Target temperature reached!")
# Instantiate the OpenLoopHeatingSystem class
heating_system = OpenLoopHeatingSystem(target_temperature=30)
heating_system.turn_on_heater()
Closed-Loop Control Algorithm:
Example: Temperature Control System (Closed-Loop Control)
Here we will implement a simple PID (Proportional-Integral-Derivative) controller to
maintain the desired temperature by adjusting the heating rate.
import time
class ClosedLoopTemperatureControl:
def __init__(self, target_temperature):
self.current_temperature = 20 # Initial temperature
self.target_temperature = target_temperature
self.heater_power = 0 # Initial heater power setting
def pid_control(self, error):
# Simple Proportional controller (P controller for simplicity)
Kp = 1.0 # Proportional gain
return Kp * error
def adjust_heating(self, control_signal):
# Adjust the heater power based on the control signal
self.heater_power = max(0, min(control_signal, 100)) # Clamp between 0 and 100
print(f"Adjusting heater power to: {self.heater_power}%")
def update_temperature(self):
# Simulate the heating process
self.current_temperature += self.heater_power * 0.1 # Temperature change proportional
to heater power
print(f"Current Temperature: {self.current_temperature}°C")
def control_heating(self):
while self.current_temperature < self.target_temperature:
error = self.target_temperature - self.current_temperature
control_signal = self.pid_control(error)
self.adjust_heating(control_signal)
self.update_temperature()
[Link](1) # Simulate time passing
print("Target temperature reached!")
# Instantiate the ClosedLoopTemperatureControl class
temperature_control = ClosedLoopTemperatureControl(target_temperature=30)
temperature_control.control_heating()
Bang-Bang Controllers:
A Bang-Bang Controller is a type of simple feedback control system that operates in a binary
fashion, switching between two extreme states (on or off) to maintain the desired system
output. It is a special case of a feedback control system where the control signal (output of the
controller) only has two possible values, typically the maximum and minimum values (often
referred to as "bang" and "bang").
This type of controller is particularly useful in applications where precise continuous control
is not necessary or is too complex, and a simple on/off control is sufficient.
How Does a Bang-Bang Controller Work?
1. On or Off Operation:
o When the system error (the difference between the desired output and the current output)
exceeds a certain threshold, the controller activates one extreme (e.g., "on").
o Once the system error is within an acceptable range, the controller switches to the other
extreme (e.g., "off").
o This results in a rapid switching between two states, hence the name "Bang-Bang."
2. No Gradual Adjustment:
o Unlike traditional control systems (like PID), a Bang-Bang controller does not gradually
adjust the output. Instead, it moves between two extremes to achieve the desired result.
Applications of Bang-Bang Controllers:
• Heating systems: Turning a heater on or off to maintain a desired temperature.
• Motors: Controlling the speed or position of a motor by switching between two speeds (e.g.,
full speed or no speed).
• Pressure Control: Maintaining a fluid or gas pressure by either opening or closing a valve.
Advantages of Bang-Bang Controllers:
• Simplicity: They are easy to design and implement, as they only require checking if the error
is above or below a threshold.
• Cost-effective: Since there is no need for precise control mechanisms, the hardware for a
Bang-Bang controller is usually simpler and cheaper.
• Fast Response: The system can react quickly to large changes in the error since it is either
fully on or fully off.
Disadvantages of Bang-Bang Controllers:
• Oscillation: If the error is near the setpoint, the system may continuously switch between the
on and off states, leading to oscillations around the target value.
• Poor Precision: It is not suitable for applications requiring precise control, as the output is
either fully on or off with no gradual adjustment.
• Wear and Tear: Constant switching between extremes can lead to mechanical wear in the
system, especially in motors or actuators.
Bang-Bang Control Example in Python:
Bang-Bang controller to maintain a temperature within a range. The heater will turn on if the
temperature is below a certain threshold and turn off when it reaches the target temperature.
import time
class BangBangController:
def __init__(self, target_temperature):
self.current_temperature = 20 # Initial temperature
self.target_temperature = target_temperature
self.heater_on = False # Heater status (off)
def control_heating(self):
while self.current_temperature < self.target_temperature - 1 or self.current_temperature
> self.target_temperature + 1:
error = self.target_temperature - self.current_temperature
if error > 1: # If the temperature is too low, turn the heater on
if not self.heater_on:
self.heater_on = True
print("Heater ON")
elif error < -1: # If the temperature is too high, turn the heater off
if self.heater_on:
self.heater_on = False
print("Heater OFF")
# Simulate the heating process
if self.heater_on:
self.current_temperature += 1 # Increase temperature
else:
self.current_temperature -= 1 # Decrease temperature
print(f"Current Temperature: {self.current_temperature}°C")
[Link](1) # Simulate time passing
print("Target temperature reached!")
# Instantiate the BangBangController class
controller = BangBangController(target_temperature=30)
controller.control_heating()
Sequential Control Systems:
A Sequential Control System is a type of control system in which operations or processes
occur in a specific sequence, often in a predefined order, based on certain conditions or triggers.
These systems are typically used in applications where tasks must be performed step by step,
with each step depending on the completion or status of the previous one.
Key Features of Sequential Control Systems:
1. Predefined Sequence of Operations: The system follows a fixed order of operations or events.
2. Event-Driven: Operations are often triggered by specific events or conditions.
3. Automation of Tasks: Sequential control automates processes that need to be performed in a
set order, eliminating the need for manual intervention.
4. Step-by-Step Execution: The system moves from one step to the next only when the previous
step is completed or a certain condition is met.
Types of Sequential Control Systems:
1. Discreet Sequential Control Systems:
o In these systems, discrete events or operations occur in a sequence.
o The system moves from one state to another based on external conditions or inputs.
2. Continuous Sequential Control Systems:
o These systems control processes that continuously move through different stages, though each
stage is performed in a sequence.
o For example, controlling the stages of a manufacturing process that involves both continuous
flow and discrete actions.
Examples of Sequential Control Systems:
1. Manufacturing and Assembly Lines:
o In an automated production line, tasks like sorting, placing, assembling, and packaging must
be performed in a specific order. Sequential control ensures each step is completed before
moving to the next.
2. Elevator Systems:
o An elevator system operates in a sequential order: when a button is pressed, the elevator moves
to the corresponding floor, opens its door, waits for passengers, and then moves to the next
floor. This process follows a fixed sequence.
3. Traffic Light Control:
o Traffic lights operate in a sequence of red, green, and yellow lights, and the change from one
state to another is controlled by specific timing or sensor input.
4. Bottling or Packaging Systems:
o A sequential control system may control a machine that bottles or packages products. Each
step, such as filling, capping, labeling, and sealing, needs to happen in a precise order.
5. Robot Arm Control:
o A robotic arm used in a factory might pick up parts, move them to a specific location, assemble
them, and then place the completed assembly in a packaging area. Each action needs to follow
the previous step in a set sequence.
Sequential Control Logic:
Sequential control is often implemented using Programmable Logic Controllers (PLCs),
Microcontrollers, or Relay-Based Systems. These systems rely on ladder logic, state
machines, or flow charts to define the sequence of operations.
Example: Traffic Light Control Using Sequential Logic
A simple sequential control system for a traffic light changes the lights in a specific order.
import time
class TrafficLightController:
def __init__(self):
[Link] = "Red"
def change_light(self):
if [Link] == "Red":
[Link] = "Green"
elif [Link] == "Green":
[Link] = "Yellow"
elif [Link] == "Yellow":
[Link] = "Red"
def run(self):
for _ in range(6): # Loop to simulate multiple light changes
print(f"Traffic Light is {[Link]}")
[Link](2)
self.change_light()
# Instantiate the TrafficLightController class
controller = TrafficLightController()
[Link]()
Common Python Libraries Used for Control System Simulation
Concept: Open-Loop Control System for Motor Speed
In an open-loop system, the output is not fed back to correct the input.
The controller sends a fixed control signal (voltage) to the actuator (motor) regardless of the
actual speed.
Python Program: Open-Loop Motor Speed Control Simulation
Below is a Python simulation that sets a motor speed based on a fixed input voltage using a
first-order transfer function.
Proportional Control (P-Control):
Proportional Control is one of the simplest and most commonly used control algorithms,
particularly in feedback control systems. It is a type of feedback control where the control
output is directly proportional to the error signal (the difference between the desired setpoint
and the current measured value). The primary goal of proportional control is to reduce the error
by adjusting the control output in proportion to the magnitude of that error.
How Proportional Control Works:
In a proportional control system, the control output is calculated by multiplying the error signal
by a constant factor known as the proportional gain (Kp).
Key Features of Proportional Control:
1. Simple Implementation: Proportional control is simple to implement because it only requires
a calculation of the error and multiplication by a constant gain.
2. Instant Response: The control system responds instantly to changes in the error, making it
useful for quick adjustments.
3. Error Reduction: It reduces the error proportionally. However, the system may not bring the
error to exactly zero.
Advantages of Proportional Control:
• Simplicity: P-control is easy to understand and implement.
• Quick Response: The system reacts quickly to changes in the error signal.
• Cost-Effective: Because of its simplicity, proportional control is often less expensive than
more complex control algorithms.
Disadvantages of Proportional Control:
• Steady-State Error: A proportional controller may not completely eliminate the error,
especially when the error is small. This can lead to a steady-state error, meaning that the system
cannot reach the exact desired setpoint.
• Insufficient for Large Errors: If the error is large, proportional control alone might not be
able to bring the system to the setpoint effectively.
• Instability: If the proportional gain KpK_pKp is too high, the system may become unstable,
resulting in oscillations around the setpoint.
PI Control (Proportional-Integral Control):
A PI (Proportional-Integral) Controller is an advanced version of the proportional controller
(P-controller). It combines two elements: Proportional Control (P) and Integral Control (I).
By adding the integral term, the PI controller can eliminate the steady-state error that a pure
proportional controller might leave. This makes it especially useful for systems that require
precise control and where steady-state errors are undesirable.
How PI Control Works:
1. Proportional Term (P):
o The proportional term reacts to the current error value, just like in the P-controller. The control
output is proportional to the error.
o The proportional term tries to reduce the error by applying a correction based on the magnitude
of the error.
2. Integral Term (I):
o The integral term sums up past errors over time. It accumulates the error to remove steady-state
error.
o As the system accumulates the error over time, the integral term adjusts the control output to
correct any bias or offset that might remain after the proportional control.
Key Features of PI Control:
1. Elimination of Steady-State Error:
o The integral action ensures that the system eliminates the steady-state error that is common in
pure P-control systems.
2. Improved Accuracy:
o By considering the history of errors, the PI controller can achieve a more accurate control,
especially in systems with small but persistent errors.
3. Combination of Speed and Accuracy:
o The proportional term provides quick error correction, while the integral term ensures that the
system reaches and maintains the setpoint with no residual error.
Advantages of PI Control:
• Eliminates Steady-State Error: The integral term ensures that the system reaches the exact
setpoint, unlike a P-controller, which may leave a residual error.
• Simplicity: PI controllers are relatively simple to implement and tune, compared to more
advanced controllers like PID.
• Suitable for Many Applications: PI controllers are widely used in systems where precision
is important, such as temperature control, motor speed control, and process control.
Disadvantages of PI Control:
• Integral Windup: If the error is large for a long period (for example, when the setpoint is
suddenly changed), the integral term can accumulate a large error value, leading to integral
windup, which can cause the system to overshoot or become unstable.
• Slower Response: The integral action can make the system respond slower compared to a P-
controller, especially when the error is large.
• Tuning: While tuning the proportional gain Kp is straightforward, tuning the integral gain Ki
can be more complex.
Industrial Applications in Instrumentation
PI controllers are widely used where steady-state accuracy is important and system inertia is
not very high:
• Temperature control in furnaces and reactors
• Level control in tanks
• Pressure regulation in boilers
• Flow control systems
• Speed control of DC/AC motors
Example: PI Control for Temperature Regulation
import time
class PIControl:
def __init__(self, setpoint):
self.current_temperature = 20 # Initial temperature
[Link] = setpoint
[Link] = 1.5 # Proportional gain
[Link] = 0.1 # Integral gain
[Link] = 0 # Integral term
self.heater_power = 0 # Initial heater power
def calculate_heater_power(self, error):
# Calculate the integral term
[Link] += error # Sum up the error over time
# Calculate the total control output (PI control)
self.heater_power = [Link] * error + [Link] * [Link]
return self.heater_power
def control_temperature(self):
while abs(self.current_temperature - [Link]) > 0.5: # Targeting to stay within 0.5
degrees of the setpoint
error = [Link] - self.current_temperature # Calculate the error
power = self.calculate_heater_power(error) # Calculate the required heater power
self.current_temperature += power * 0.1 # Simulate temperature change based on
power (simplified)
print(f"Current Temperature: {self.current_temperature}°C, Heater Power:
{power}%")
[Link](1) # Simulate time passing
print("Target temperature reached!")
# Create an instance of the PIControl class
controller = PIControl(setpoint=25)
controller.control_temperature()
PID Control (Proportional-Integral-Derivative Control):
A PID controller is one of the most widely used control algorithms in industrial control
systems. It combines three different control actions: Proportional (P), Integral (I), and
Derivative (D). By doing so, a PID controller can address a wider range of control issues
compared to simpler controllers like P or PI.
How PID Control Works:
Key Features of PID Control:
1. Proportional Control (P): Corrects the current error.
2. Integral Control (I): Removes steady-state error by considering the history of past errors.
3. Derivative Control (D): Reduces the impact of future error by anticipating changes and
improving stability.
Advantages of PID Control:
1. Comprehensive Control: The combination of all three terms allows the PID controller to
handle a wide range of dynamic behaviors, providing both fast response and eliminating
steady-state error.
2. Precise and Accurate: PID control can help achieve more precise regulation, ensuring that the
system reaches the desired setpoint without oscillations or steady-state error.
3. Improved Stability: The derivative term helps prevent overshoot and oscillations, improving
the stability of the system.
Disadvantages of PID Control:
1. Tuning Complexity: One of the main challenges of using a PID controller is tuning the
three gains (KpK_pKp, KiK_iKi, and KdK_dKd). Improper tuning can lead to instability,
slow response, or excessive overshoot.
2. Derivative Noise: The derivative term is highly sensitive to noise, which can lead to
oscillations in systems with noisy measurements.
3. Computational Complexity: In some cases, computing the derivative and integral terms can
be computationally intensive, especially in real-time applications.
Example: PID Control for Temperature Regulation
import time
class PIDControl:
def __init__(self, setpoint):
self.current_temperature = 20 # Initial temperature
[Link] = setpoint
[Link] = 1.5 # Proportional gain
[Link] = 0.1 # Integral gain
[Link] = 0.01 # Derivative gain
self.previous_error = 0 # Previous error for derivative calculation
[Link] = 0 # Integral term
self.heater_power = 0 # Initial heater power
def calculate_heater_power(self, error):
# Proportional term
P = [Link] * error
# Integral term
[Link] += error
I = [Link] * [Link]
# Derivative term
D = [Link] * (error - self.previous_error)
# Total control output
self.heater_power = P + I + D
# Store current error for the next iteration
self.previous_error = error
return self.heater_power
def control_temperature(self):
while abs(self.current_temperature - [Link]) > 0.5: # Targeting within 0.5°C of
setpoint
error = [Link] - self.current_temperature # Calculate the error
power = self.calculate_heater_power(error) # Calculate the required heater power
self.current_temperature += power * 0.1 # Simulate temperature change based on
power (simplified)
print(f"Current Temperature: {self.current_temperature}°C, Heater Power:
{power}%")
[Link](1) # Simulate time passing
print("Target temperature reached!")
# Create an instance of the PIDControl class
controller = PIDControl(setpoint=25)
controller.control_temperature()
Hybrid Control Systems:
A Hybrid Control System combines elements from multiple control strategies or paradigms
to take advantage of their strengths and mitigate their weaknesses. These systems are designed
to address complex, dynamic environments where traditional control methods (like PID) may
not be sufficient. Hybrid control systems can combine discrete and continuous systems, as well
as multiple types of controllers, to adapt to different conditions and optimize system
performance.
In practical terms, a hybrid control system may integrate:
• Discrete control (like event-based or logic-based control),
• Continuous control (such as PID or state-space control),
• Switching logic that determines when to use which control approach.
Types of Hybrid Control Systems:
1. Discrete-Event and Continuous Systems:
o Hybrid control systems often combine continuous control for regulating
physical quantities (like temperature, speed, or pressure) with discrete-event
control for event-based decision-making (like system switching or triggering
actions).
o For example, a hybrid control system might control a heating system where the
temperature is continuously regulated by a PID controller, while certain actions
(e.g., turning the heater on or off) are triggered by discrete logic when certain
conditions are met (e.g., when the temperature goes below a threshold).
2. Switching Control Systems:
o In these systems, the controller switches between different control strategies
based on the system's state or environmental conditions.
o For example, a system could switch from a PID controller to a bang-bang
controller when a system is in a certain state (e.g., when reaching an operational
limit or when disturbances become significant).
3. Model Predictive Control (MPC) with Hybrid Elements:
o MPC is a control strategy that uses a model of the system to predict future states
and optimize control actions. In hybrid systems, MPC may be integrated with
discrete-event control elements or finite state machines to manage complex
systems with both continuous dynamics and discrete decisions.
o A classic example of this is in automated vehicles or robotics, where the
continuous dynamics of movement are combined with discrete decisions like
path planning and obstacle avoidance.
4. Artificial Intelligence (AI) and Machine Learning (ML) in Hybrid Control:
o Hybrid control can also involve AI or ML techniques that make real-time decisions based on
large amounts of data. For instance, a robot could use a neural network for decision-making in
specific tasks (discrete control), while using PID or state feedback control for precise motion
control (continuous control).
o These systems are capable of learning from the environment and improving their performance
over time, making them particularly useful in highly dynamic and unpredictable environments.
Hybrid control systems combine multiple control strategies:
A Hybrid Control System combines two or more control strategies (such as continuous,
discrete, and logical/event-based control) into a single, integrated system to achieve better
performance, flexibility, and reliability.
It operates partly in:
• Continuous-time domain → uses classical control (P, PI, PID, etc.)
• Discrete-time domain → uses digital or logical decisions (like ON–OFF, sequencing, or
switching logic)
Hybrid control systems coordinate different control strategies at different operating
conditions or system states.
Common Combinations:
Combination Description Example
PID used for fine control; On–Off Temperature control
PID + On–Off Control
used for safety or startup in furnace
Continuous + Discrete Continuous level control integrated Multi-tank liquid level
Sequential Control with valve sequencing system
Model-based + Logical Model predictive control with Chemical batch
Control discrete decision-making reactor
Adaptive + Rule-based Control gain adjusted using fuzzy
HVAC systems
Control rules
PID + Fuzzy Logic Control PID with adaptive gain tuning using Nonlinear motor
(Fuzzy-PID) fuzzy inference control
Working Principle (Example)
Let’s consider a Hybrid Temperature Control System in a chemical reactor:
1. PID Control:
o Maintains temperature precisely near the setpoint (continuous control).
2. On–Off Control:
o Activates a cooling fan or heater in case of large deviation (discrete event control).
3. Supervisory Logic:
o Decides which controller to use based on process conditions (logic-based switching).
Hybrid logic:
If error > 10°C → use ON–OFF control (heater fully ON)
If error < 10°C → switch to PID mode for fine control
If temperature > 100°C → activate safety shutdown
This combines continuous PID control with discrete logic-based switching.
Examples of Hybrid Control Systems:
1. Automated Manufacturing Systems:
o In automated manufacturing or industrial plants, hybrid control systems are used to optimize
both the continuous processes (like temperature control or pressure regulation) and discrete
event control (like the opening/closing of valves or starting/stopping of machines). This can
include switching between different control strategies depending on the production phase or
disturbances in the process.
2. Autonomous Vehicles:
o Autonomous vehicles use a hybrid control system to manage both continuous systems (such as
steering, braking, and acceleration) and discrete decision-making systems (like obstacle
detection and path planning). These vehicles may switch from a PID-based control of speed to
a more event-based control when approaching an intersection or a pedestrian.
3. Robotics:
o In robotics, hybrid control systems combine continuous control (for precise motor control and
movement) with logic-based control (for task execution and decision-making). For example, a
robot might use PID control to maintain arm position while using finite state machines to
execute high-level tasks like picking up an object.
4. Energy Systems:
o In energy systems, hybrid control can be applied to optimize power generation from both
renewable and non-renewable sources. The system might continuously regulate energy flow
using feedback control (PID) while switching between different energy sources or adjusting
power output based on demand or system conditions using discrete control.
5. Smart Grids:
o Hybrid control systems are used in smart grids to manage both the continuous flow of
electricity and the discrete switching between different power generation sources or circuits.
The system may use feedback control to regulate voltage and frequency while making discrete
decisions about power distribution or switching in response to peak load conditions.
Advantages of Hybrid Control Systems:
1. Flexibility:
o Hybrid control systems provide flexibility by allowing the system to adapt to various
operational conditions. For instance, by using both continuous and discrete control methods,
the system can handle a wider range of scenarios.
2. Improved Performance:
o By combining the best features of different control strategies (like the precision of continuous
control and the adaptability of discrete control), hybrid systems can improve overall system
performance, stability, and robustness.
3. Better Handling of Complex Systems:
o Many real-world systems, such as automated factories or robotic systems, have both continuous
dynamics and discrete events. Hybrid control systems are designed to manage these
complexities effectively.
4. Optimized Resource Utilization:
o Hybrid control can help optimize resource use, whether it’s energy, material, or time, by
making intelligent decisions about which control strategy to apply at each moment.
Challenges of Hybrid Control Systems:
1. Complexity:
o Designing and implementing a hybrid control system can be more complex than using a single
control approach. The interaction between continuous and discrete components must be
carefully modeled and managed to avoid instability or unexpected behavior.
2. Tuning and Stability:
o The tuning of hybrid control systems can be challenging, especially when switching between
different control strategies. Ensuring stability during transitions between control modes is a
key concern.
3. Computational Requirements:
o Some hybrid systems, especially those involving AI or real-time decision-making, can have
high computational demands. This can limit their applicability in real-time applications where
speed and low latency are crucial.
Example: a hybrid control system that combines a PID controller for continuous control and
a logic-based control for switching between two states (e.g., heating and cooling).
import time
class HybridControl:
def __init__(self, setpoint):
self.current_temperature = 20 # Initial temperature
[Link] = setpoint
[Link] = 1.5 # Proportional gain
[Link] = 0.1 # Integral gain
[Link] = 0.01 # Derivative gain
self.previous_error = 0
[Link] = 0
self.heater_power = 0
[Link] = 'Heating' # Initial state: Heating or Cooling
def calculate_pid(self, error):
# PID control calculation
P = [Link] * error
[Link] += error
I = [Link] * [Link]
D = [Link] * (error - self.previous_error)
self.previous_error = error
return P + I + D
def control_temperature(self):
while True:
error = [Link] - self.current_temperature
power = self.calculate_pid(error)
# Hybrid logic: switch state based on temperature
if self.current_temperature > [Link] + 2: # Cooling condition
[Link] = 'Cooling'
power = -power # Negative power for cooling
elif self.current_temperature < [Link] - 2: # Heating condition
[Link] = 'Heating'
self.current_temperature += power * 0.1 # Simulate temperature change
print(f"Current Temperature: {self.current_temperature}°C, State: {[Link]}, Power:
{power}%")
[Link](1)
# Create an instance of the HybridControl class
controller = HybridControl(setpoint=25)
controller.control_temperature()
Python Programs:
[Link] a simple closed-loop control system using Python that maintains a liquid level in a tank.
Concept: Closed-Loop Level Control System
A closed-loop control system continuously monitors the liquid level in a tank and
automatically adjusts the inflow valve (actuator) to maintain the desired setpoint.
Basic Block Diagram:
Python Simulation: Closed-Loop Level Control
Here’s a complete Python program simulating a tank level control system with a
Proportional Controller (P-Control).
2. Develop a Python program that simulates a simple sequential control system for controlling
a traffic light with the following sequence: Green Light: Lasts for 10 seconds, Yellow Light:
Lasts for 3 seconds. Red Light: Lasts for 7 seconds.
Solution :
This is a classic Sequential Control System example, widely used in Instrumentation and
Automation courses to demonstrate event-driven control and timing logic.
Let’s first understand the concept, then build the Python simulation for the traffic light
system.
Concept of Sequential Control System
A Sequential Control System performs operations in a predetermined order based on time
or events.
Example: Traffic Light Control
The sequence repeats cyclically:
1. Green Light → ON for 10 seconds
2. Yellow Light → ON for 3 seconds
3. Red Light → ON for 7 seconds
This process repeats continuously, ensuring smooth and safe vehicle movement.
Python Program for Traffic Light Sequential Control
Here’s the full simulation code with time-based sequential switching: