You are here

Control machines with your machine

This article is intended for new Linux users who wish to use their Linux-box for some real work. Speed control of an industrial motor? Sounds complicated? It’s not as complex an affair as it sounds. What’s interesting is that a PC powered with a Linux based Real-Time Operating System (RTOS) can be used to control anything from a small motor to a complex industrial drive with the utmost reliability. This article presents the implementation of a Digital PI (Proportional+Integral) Controller on a PC running RTAI (Real Time Application Interface). A system developed on Linux-2.4.24 kernel patched with RTAI-3.1 was used for the speed control of a 12V DC motor.

Control systems

Control systems can be broadly divided into two: open loop systems and closed loop systems. Systems which do not automatically correct the variations in their output are open loop systems. This means that the output is not fed back to the input for correction. For instance, consider a traffic control system where the traffic signals are operated on a time basis. The system will not measure the density of the traffic before giving the signals thereby making it an open loop system.

Consider now a temperature control system, say an air conditioner. The output of this system is the desired room temperature. Obviously, this depends on the time that the supply to heater/cooler remains ON. Depending on the actual temperature sensed by sensors and the desired temperature, corrective measures are taken by the controller. Thus the system automatically corrects any changes in output by monitoring the same; hence, making it a closed loop system.

An open loop system can be modified into a closed loop system by providing a feedback. The provision for feedback automatically corrects the changes in the output due to disturbances. So, the closed loop system is also called an automatic control system. The general block diagram of an automatic control system is shown in figure 1.

Figure 1: Closed system block diagram

Automatic controller

A controller is a device introduced into the system to modify an error signal and to produce the required control signal. An automatic controller compares the actual value of the plant output with the desired value, determines the deviation, and produces a control signal. This will reduce the deviation to zero or to a smaller value. According to the manner in which the controller produces the control signal (called control action), controllers are classified as proportional (P), integral (I), derivative (D) or combinations of these (PI, PD and PID).

The proportional controller is a device that produces a control signal u(t), which is proportional to the input error signal, e(t) (error signal is the difference between the actual value and the desired value) i.e.:

u(t) = Kp * e(t)

where Kp = proportional gain or constant; proportional controller amplifies the error signal by an amount Kp. The drawback of the P-controller is that it leads to a constant steady state error. Integral control is used to reduce the steady state error to zero. This device produces a control signal u(t) which is proportional to the integral of the input error signal:

u(t) = Ki * integral { e(t)*dt }

where Ki = integral gain or constant. Integral control means that we are considering the sum of all errors over an interval. So this always gives us a measure of variation over a constant interval. The other choice is derivative control where the control signal (u(t)) is proportional to the derivative of the input error signal (e(t)). We consider the derivative of e(t) at a given instant as the difference between present and previous errors. A large positive derivative value indicates a rapid change in output variable (here the speed of motor). In other words, the rate of change of speed is more. The drawback in integral controller is that it may lead to oscillatory response. For these reasons combination of P, I and D are used. Most (75-90%) of controllers in current use are PID. In this article, I will look at the design of a PI controller.

The PI controller looks at:

the current value of the error; and

the integral of the error over a recent time interval.

This not only determines how much correction to apply, but for how long. Each of the above two quantities are multiplied by a “tuning constant” (Kp and Ki respectively) and added together. Depending on the application, one may want a faster convergence speed or a lower overshoot. By adjusting the weighting constants, Kp and Ki, the PI is set to give the most desirable performance. The implementation of software PI controller is discussed later in this article.

Figure 2: A digital PI controller

Today, digital controllers are being used in many large and small-scale control systems, replacing analog controllers. It’s now common practice to implement PI controllers in its digital avatar, which means the controller algorithm is implemented in software rather than in hardware. The trend toward digital control is mainly due to the availability of low-cost digital computers. Moreover, as the complexity of a control system increases, demands for flexibility, adaptability and optimality increases. Digital computers used as compensator are a better option in such cases. A general purpose computer, if used, lends itself to time-shared use of other control functions in the plant.

The systems that ensure timing requirements are termed as real-time systems. An appropriate operating system should be used to satisfy time constraints

Why real-time?

Using general purpose computers is a disadvantage, if the operating system employs various tasks to be executed on a time shared basis. An example of such an operating system is GNU/Linux, where the time constraints required by the control system are not met.

The systems that ensure timing requirements are termed as real-time systems. An appropriate operating system should be used to satisfy time constraints.

Real-time systems

A real-time system can be defined as “a system capable of guaranteeing the timing requirements of the processes under its control”. It must be fast and predictable. Fast means that it has a low latency (that is, it responds to external, asynchronous events in a short time). The lower the latency, the better the system will respond to events which require immediate attention. Predictable means that it is able to determine a task’s completion time with certainty. It is desirable that time-critical and non time-critical activities coexist in a real-time system. Both are called tasks, and a task with a hard timing requirement is called a real-time task.

What is RTAI?

RTAI stands for Real-Time Application Interface. Strictly speaking, it is not a real-time operating system, such as VXworks or QNX. It is based on the Linux kernel, providing it with the ability to make itself fully pre-emptable. RTAI offers the services of the Linux kernel core, adding the features of an industrial real-time operating system. It lets you write applications with strict timing constraints for your favourite operating system. Like Linux itself, this software is a community effort.

PC parallel port as analog I/O interface

The PI Controller described in this article uses the PC parallel port as an analog I/O interface. Just two bits are used as analog interfaces through a technique called Pulse Width Modulation (PWM). This technique allows an analog interface to be built without the use of A/D and D/A converters. Analog voltages and currents can be used to control processes directly. As intuitive and simple as analog control may seem, it isn’t always economically attractive or practical. Analog circuits tend to drift over time and can, therefore, be very difficult to tune. By controlling analog circuits digitally, system costs and power consumption can be drastically reduced. Pulse Width Modulation (PWM) is a powerful technique for controlling analog circuits with digital signals, and is a way of encoding digitally analog signal levels. The duty cycle of a square wave is modulated to encode a specific analog signal level, as shown in figure 3.

Figure 3: Pulse Width Modulation (PWM)

The PWM signal is still digital because, at any instant, it is either on or off. The relationship between the on-time and the off-time varies according to the analog level to be represented. Given a sufficient bandwidth, any analog value may be encoded with PWM.

Figure 4: PWM generation wave form

In this work, the PWM technique is used to generate the gating signals for the Chopper circuit. The speed of a DC motor can be controlled by varying its input voltage. A DC Chopper circuit can do this job.

DC Chopper

A chopper is a static device that converts fixed DC input voltage to a variable DC output voltage directly. A chopper may be thought of as a DC equivalent of an AC transformer, since they behave in an identical manner. Chopper systems offer smooth control, high efficiency and fast response.

The power semi-conductor devices, used for a chopper circuit, can be power BJT and power MOSFET. Like a transformer, a chopper can be used to step up or step down the fixed DC input voltage. Here, for motor speed control, a step down chopper circuit with a power MOSFET is used. It’s basically a high speed semi-conductor switch. It connects source to load and disconnects the load from source at a high speed, according to the gating signal. The average load voltage is given by the formulas in figure 5.

Figure 5: Chopper equation

Figure 6: Chopper circuit

Vs is the source voltage, Ton is the on-time, Toff is the off-time, T = Ton + Toff is the chopping period and Ton/T is the duty cycle.

Preparing the parallel port

Although the parallel interface is simple, it can trigger interrupts. This capability is used by the printer to notify the lp driver that it is ready to accept the next character in the buffer. A simple outb() call to a control register makes the port ready to generate interrupts; the parallel interface generates an interrupt whenever the electrical signal at pin 10 (the so-called ACK bit) changes from low to high. The simplest way to force the interface to generate interrupts is to connect pins 9 and 10 of the parallel connector. A short length of wire inserted into the appropriate holes in the parallel port connector on the back of the system will create this connection. Pin 9 is the most significant bit of the parallel data byte.

Real-time speed controller

Figure 7 illustrates the block diagram representing the closed loop speed control of a 6V DC motor controlled using a DC Chopper circuit. The switching duty ratio of the chopper is controlled using gating signals (PWM) from the PC parallel port. The PC is running the speed sensing (shaft encoder) module to read the current speed measured by the encoder. This device works by generating a square wave pulse produced when a perforated disc (keyed onto the shaft of the motor) cuts an IR beam. This square wave pulse train is fed back to the 10th pin of the parallel port. The controller program is running as a hard, real-time user program.

The well implemented PI controllers are usually hard, real-time systems. I have implemented a Digital PI Controller in a standard PC machine running Linux/RTAI (Linux-2.4.24 kernel patched with the RTAI-3.1)

Figure 7: Real-time speed controller

The well implemented PI controllers are usually hard, real-time systems. I have implemented a Digital PI Controller in a standard PC machine running Linux/RTAI (Linux-2.4.24 kernel patched with the RTAI-3.1). The I/O was done by PWM through two pins of the parallel port: one for input and the other for output, dispensing with the need for A/D converter which is the most expensive component in a data-acquisition system. The PWM coding/ decoding has been dealt with by software routines implemented in Linux threads with LXRT RTAI hard, real-time constraints. The main routine, which is responsible for the actual PI calculation has also been implemented in a thread with hard, real-time constraints. The communication between threads was made through global variables. The controller program produces the necessary actuating signal (in terms of a PWM pulse), which, in turn, depends on an error signal produced by the difference of the set speed (a constant in the program) and the present speed. The chopper varies the average DC output voltage, used to control the speed of the DC motor. The motor used here is a small 6/12V motor commonly found in most tape recorders. The following sections present the speed sensing (shaft encoder) module and the controller program followed by a brief description of the same.

Speed sensing (shaft encoder) module

Once interrupts are enabled, the parallel port generates an interrupt whenever there is low to high transition at pin 10 (ACK bit). This module includes the operations that should be performed when the interrupt occurs. The init_module function registers a handler for interrupt number 7 (IRQ number). In the first execution of the function lpt_irq_handler (void), the count is zero and the control enters the if statement. The 64 bit time-stamp-counter (tsc) value is obtained by the rdtsc macro function defined in msr.h. By getting two tsc values at consecutive interrupts, the number of CPU clock cycles in between these interrupts can be calculated, and hence so can the frequency. Frequency, once measured, is communicated with a User space program via a FIFO. Thus, the User space program can calculate the real-time speed. Here is the code which implements this idea:

The rt_request_global_irq and rt_enable_irq functions together instruct the RTAI kernel to service IRQ 7 (the parallel port interrupt). After compiling this module, insert it into the kernel address space using insmod. Check /proc/modules to ensure that the above has been inserted.

Controller program

The controller program is responsible for three things:

for reading the current cpu_clock_count from the speed sensing module—i.e., from the FIFO—which is stored in an array for future reference;

for the PI task; and

for encoding the PI digital output—which is stored in a variable—into an external PWM wave signal, which is the gating signal for chopper circuit.

The algorithm used for implementing PI controller is summarised as follows:

Create a periodic task with period Tc.

Read the state of the parallel input pins.

Compute control signal and update PWM signal.

Update output-write thread.

Wait for the next task period, Tc.

The main function creates five hard, real-time threads, which are shown in listings 1-4. An LXRT task starts out as an ordinary POSIX thread. It gets transformed to a hard, real-time thread by calling the function available as part of the LXRT library. The effect of this transition is that the thread is no longer managed by the normal Linux Scheduler—it comes under the supervision of the RTAI Scheduler, which guarantees that its timings do not get disturbed by activities taking place within the Linux kernel or in other user-space threads. The main() thread runs as a low priority task.

The PI task

The PI task is the core of the application, where the controller calculation is actually made. The PI controller receives, as input, the error given by e(t) = r(t) - y(t), and computes the control variable c(t) which is its output. The control signal c(t) is calculated from the discrete PI equation given in figure 8.

Figure 8: PI equation

The PI thread looks at:

the current value of the error;

the integral of the error over a recent time interval;

the compute control signal and update PWM signal; and

the update output-write thread

This thread has been assigned the fifth highest priority among the five hard, real-time tasks. The code is given in Listing 1.

Task rt_task_out_put

Listing 2 shows a hard, real-time LXRT program generating a PWM of frequency 50Hz on a parallel port pin to feed the motor drive (DC Chopper). The PWM is generated by comparing the instantaneous values of a sine wave and a triangular wave of the required frequency. The instantaneous values of the two waves are generated in the threads given in Listing 3. These values are compared in the thread rt_task_out_put to generate the PWM. This thread has been assigned the third highest priority.

Task rt_task_in_put

This task continuously reads values from the FIFO. (Note that the shaft encoder writes real-time speed to the FIFO) and stores it to the variable cpu_clock_count. The value of this variable is converted to speed in RPM and is stored in rt_speed. This thread has been assigned the second highest priority. The code for this process is shown in Listing 4

Conclusion

The above experiment illustrates the use of a PI controller for the closed loop speed control of a small DC motor, which can usually found in tape recorders. The performance of this PI controller was found to be acceptable for such a motor. However on attempting to model a converter along similar lines for a real large scale industrial drive, I found that the large number of control parameters required a more in depth and detailed controller design, using the concepts of control system theory.

“Value your freedom, or you will lose it, teaches history.‘Don’t bother us with politics’, respond those who don’t want to learn.”—Richard Stallman

Author information

Biography

Sreejith is a Linux enthusiast living in India. He has completed a B-Tech in Electrical and Electronics Engineering at Govt. Engineering College, Thrissur (Kerala, India). He is currently working in Trivandrum, India as an Embedded Systems Engineer for inDSP Audio Technologies Pvt. Ltd. His interests include Linux kernel programming, device drivers, embedded systems, robotics and process control. He spends his free time reading books on Linux and exploring the same.