Chapter 3 Development Of A MATLAB Data Acquisition And .

2y ago
113 Views
4 Downloads
1.92 MB
13 Pages
Last View : 28d ago
Last Download : 3m ago
Upload by : Olive Grimm
Transcription

Chapter 3Development of a MATLAB Data Acquisition andControl Toolbox for PIC Microcontrollers3.1.IntroductionData acquisition and control boards (DACBs) are essential for interfacing sensors/actuatorswith decision making devices such as a personal computer (PC). Thus, DACBs are used inmonitoring/instrumentation applications involving machinery, process, environment, etc., andin automatic control applications. Even though a variety of DACBs have become widelyavailable in the last 15 years, the systems that target the educational sector and provide supportfor icon-based programming environments, such as LabVIEW [5] and Simulink [44], tend tobe quite expensive. Moreover, instructional labs generally may not require the intrinsic highperformance features of many of the commercially available DACBs (e.g., high sampling rates,high resolution analog to digital converters (A2Ds), etc.) for the typical electro-mechanicallaboratory experiments. This Chapter proposes a microcontroller-based data acquisition andcontrol (DAC) system that is particularly suitable for educators interested in developing labexperiments that do not require high-cost, high-performance data acquisition hardware andyet can benefit from the icon-based programming environment of Simulink.Several recent papers [46–48] have focused on interfacing low-cost microcontrollers (such asBASIC Stamp 2 (BS2) and Peripheral Interface Controller (PIC)) with icon-based programming environments such as LabVIEW and Simulink. Chapter 2 have concentrated primarilyon endowing a PIC microcontroller with graphical user interface (GUI) capability by exploiting the GUI tools of MATLAB/Simulink. However, the methodology of Chapter 2 requiresmanually programming the microcontroller for all sensing, control computation, and actuationtasks and for serial communication with the GUI running on the PC. To program a PIC microcontroller or a BS2 microcontroller using PIC assembly programming language or PBASICprogramming language, respectively, requires knowledge and experience with the syntax of26

27these languages and is often tedious.This Chapter proposes a PIC microcontroller-based low-cost DAC system that exploitsMATLAB and Simulink as the key software components for implementing DAC algorithmsusing a block-diagram format. Specifically, this Chapter exploits a newly developed library ofPIC functions for Simulink and the serial communication capability of both the PIC microcontroller and MATLAB to produce a seamless integration between them. The framework ofthis Chapter completely obviates the need to manually program the PIC microcontroller bycreating a library of PIC microcontroller functions for Simulink. Specifically, the DAC toolbox of this Chapter facilitates: i) automatic generation of proper PIC assembly codes for avariety of sensors and actuators; ii) automatic programming of the PIC microcontroller; andiii) data communication between the PIC microcontroller and MATLAB. In an instructionallaboratory, this approach allows instructors and students to focus on hardware-in-the-loop implementation, experimental validation, and industry-style rapid control prototyping. Finally,this Chapter is in the spirit of Chapter 4, which provided a MATLAB DAC toolbox for the BS2microcontrollers. However, whereas the BS2 microcontroller costs over 45 and includes onlydigital input/output (I/O) functionality, thus requiring external A2Ds to interface with analog sensors, the PIC16F74 microcontroller, used in this Chapter, costs under 5 and includesbuilt-in A2D functionality.This Chapter is organized as follows. Section 3.2 describes the PIC microcontroller andthe related development hardware. Section 3.3 describes the software environment used in thisChapter. Section 3.4 gives details concerning the software integration of Simulink with the PICmicrocontroller. Section 3.5 illustrates the functionality and capability of the DAC hardwareand software of this Chapter by performing position control of a DC motor. Finally, Section3.6 provides some concluding remarks.3.2.Hardware EnvironmentThe main components of the DAC hardware of this Chapter are a PIC microcontroller,a PIC-PG2C programmer, and a PIC development board. A DB-9 serial cable is used tointerface the programmer/development board to a PC which hosts the MATLAB DAC toolbox.

28Specifically, the DB-9 cable allows i) programming the PIC microcontroller from the PC andii) data communication between the PIC and the PC. In this Chapter, an IBM-compatiblePentium 4 PC running Microsoft Windows XP operating system is used. See Figure 3.1 for apictorial representation of the aforementioned hardware environment.PCPIC development boardPIC microcontrollerDB-9 serial cablePIC-PG2C programmerFigure 3.1: Hardware environment3.2.1.PIC MicrocontrollerThe DAC platform of this Chapter uses a PIC16F74 [43] microcontroller. In this Chapter,five of the six I/O pins of port A and three I/O pins of port E are reserved for eight 8-bitA2Ds, eight I/O pins of port B are reserved for eight digital inputs, two of the eight I/O pins ofport C are reserved for two PWM outputs, and eight I/O pins of port D are reserved for eightdigital outputs. Finally, an external 20 MHz high-speed crystal oscillator is used to supplyoperating clock cycles to the PIC. See Ref. [37, 38] for more details on hardware and softwarefeatures of PIC microcontrollers.3.2.2.PIC-PG2C ProgrammerThe user specified PIC program, which is created on a PC, is downloaded from the PC toa PIC microcontroller by serial communication. Serial communication between the PC andthe PIC microcontroller is enabled by using a DB-9 serial connection between the PC anda PIC development programmer that hosts the PIC microcontroller. Two widely used PICdevelopment programmers are Microchip’s PICSTART Plus and Olimex’s PIC-PG2C [49]. Inthis Chapter, the handy and low-cost PIC-PG2C programmer (see Figure 3.2) is used. Incontrast to other PIC programmers, the PIC-PG2C programmer receives power from the PC’s

29serial port thus obviating the need for any additional power supply. Finally, the PIC-PG2Cprogrammer requires IC-Prog [50], a freely available software, to download PIC HEX code tothe PIC microcontroller. Note that the PIC HEX code is obtained from the PIC assemblycode by using the MPASM assembler [51], also available for free.(a)(b)Figure 3.2: (a) PIC-PG2C programmer and a PIC (b) PIC-PG2C programmer with a PICmounted3.2.3.PIC Development BoardThe PIC development board of this Chapter is a modified version from the board discussedin Chapter 2. It consists of: i) a PIC16F74 microcontroller; ii) a 20MHz crystal oscillatorto supply operating clock cycles to the PIC microcontroller; iii) the RS232 driver/receivercircuitry for serial data communication with the PC; iv) a DB-9 connector; and v) a breadboard area for custom circuits and easy connectivity between the PIC microcontroller andsensors/actuators. As discussed in Chapter 2, the Maxim’s MAX232 IC [41] with five 1µF capacitors serves as the RS232 driver/receiver for voltage-level transformation between PC-basedand the PIC-based logic.3.3.Software EnvironmentThe software environment for this Chapter consists of MATLAB version 6.5, Simulinkversion 5.0, the PIC assembly language, a newly developed Simulink library for the PIC microcontroller, MPASM, and IC-Prog. The MATLAB toolbox for the PIC microcontroller consistsof a Simulink library of PIC microcontroller functions such that based on the user selected configuration of individual I/O pins of the PIC microcontroller, Simulink automatically produces

30and downloads the proper PIC assembly code to the microcontroller. Moreover, the MATLABtoolbox also allows data communication between the PIC microcontroller and MATLAB. Thus,the MATLAB toolbox for the PIC microcontroller completely obviates the need to manuallyprogram the PIC microcontroller. Note that the MATLAB toolbox automatically executesthe assembler program MPASM and the download program IC-Prog, both of which usuallyrequire command line execution. See Refs. [51, 52] for details on programming the PIC microcontroller in command line via serial communication. The MATLAB toolbox for the PICmicrocontrollers has two main components: i) a Simulink model file named Template.mdl andii) a block library named PICLibrary.3.3.1.Template.mdlThe Template.mdl model file (see Figure 3.3) is a predesigned Simulink model file whichmust be used to design Simulink block diagrams for interaction with the PIC microcontroller.A function named TotalCompile has been embedded within the callback parameters of theTemplate.mdl so that the TotalCompile function executes at the beginning of each Simulinkblock diagram cycle, before the block diagram actually runs. Details of various tasks performedby the TotalCompile function are provided in a later subsection. Finally, note that renamingthe Template.mdl file still preserves the callback property embedded in the file, whereas openinga new Simulink model file does not.Template.mdl model properties are modified to callthe TotalCompile function at the beginning ofeach Simulink block diagram cycleFigure 3.3: Template and model properties

313.3.2.PICLibraryThe PICLibrary is a custom library of Simulink blocks (in the form of s-functions) that interface with sensors and actuators connected to the PIC microcontroller. The following blocksare currently included in the PICLibrary: A2D, PinStateIn, PWM, and PinStateOut. Moreover, the library includes a block labeled IOBlock that is required in all user-designed Simulinkdiagrams to enable serial communication between the PIC microcontroller and MATLAB.Hardware settings and parameter requirements of each block are detailed below.1) A2D block (see Figure 3.4) configures the analog to digital conversion module of the PICmicrocontroller. Note that five of the six I/O pins of port A and three I/O pins of port E ofthe PIC16F74 microcontroller can be configured as eight 8-bit A2Ds. Thus, analog sensors canbe directly interfaced to any of the eight A2D pins and the corresponding pin number can bepassed as the parameter required by the A2D block.Figure 3.4: A2D block and parameter2) PinStateIn block (see Figure 3.5) configures the I/O pins of port B of the PIC16F74 microcontroller to serve as digital inputs. Specifically, each of the eight pins of port B can serveas a digital input by passing the corresponding pin number as a parameter to the PinStateInblock.3) PWM block (see Figure 3.6) configures PWM modules of the PIC microcontroller. Specifically, two of the eight I/O pins of port C of the PIC16F74 microcontroller can be configuredas PWM outputs. Since the PIC16F74 microcontroller does not include a digital to analogconverter, in this Chapter, we use the PWM outputs to produce the required analog voltage

32output by varying the duty cycle of the PWM signal. Thus, two analog actuators can beinterfaced to the two I/O pins of port C that produce PWM outputs and the correspondingpin numbers are passed as the parameter required by the PWM block.Figure 3.5: PinStateIn block and parameterFigure 3.6: PWM block and parameter4) PinStateOut block (see Figure 3.7) configures the I/O pins of port D of the PIC16F74microcontroller to serve as digital outputs. Specifically, each of the eight pins of port D canserve as a digital output by passing the corresponding pin number as a parameter to thePinStateOut block.5) IOBlock is necessary for every Simulink block diagram that requires interaction with thePIC microcontroller. It performs the following tasks: i) initiate serial communication between MATLAB and the PIC microcontroller when the Simulink block diagram is initiallyexecuted; ii) transmit and receive data between MATLAB and the PIC microcontroller whilethe Simulink block diagram is running; and iii) terminate serial communication between MAT-

33LAB and the PIC microcontroller when the Simulink block diagram is stopped. The callbackfunction properties of the IOBlock include start serial and stop serial functions that initiateand terminate serial communication, respectively. In the Simulink block diagram, the IOBlockis programmed to have the first priority for execution. This ensures that all sensor and actuator data in MATLAB are first received and sent, respectively, which then is used by thecorresponding sensor and actuator blocks in the Simulink block diagram.Figure 3.7: PinStateOut block and parameterFigure 3.8: IOBlock and parametersSeveral Simulink blocks, such as an integrator block, require the knowledge of samplingperiod for their proper use in a given Simulink block diagram. In this Chapter, the IOBlock isused to determine, experimentally, the sampling period of the Simulink block diagram. Here,sampling period is defined as the time required to execute one entire cycle of the Simulinkblock. The IOBlock determines the sampling period by averaging the time taken to run auser-specified number of cycles of the Simulink block diagram. An averaged sampling period

34is not expected to provide the exact sampling period for each Simulink block cycle and its useis not recommended when hard real-time constraints are to be enforced.3.4.Integration of Simulink and PICWhen blocks from the PICLibrary are used in the Template.mdl model file, a sequenceof operations specified by the TotalCompile function are performed before the Simulink blockdiagram begins to run. The main role of the TotalCompile function is to program the PICmicrocontroller and to facilitate serial communication between MATLAB and the PIC microcontroller. As seen in Figure 3.3, the TotalCompile function is set as a “Simulation startfunction” of “Callbacks” option in the “Model properties” of Template.mdl.The TotalCompile function performs the following sequence of tasks. First, global variablesare declared and used to share data with Simulink blocks of PICLibrary. Second, sensor andactuator blocks used in the Simulink diagram are matched with the corresponding Simulinkblocks in the PICLibrary. Furthermore, each block is categorized as a sensor or an actuator andits name is stored in an array of sensor/actuator structures with the specified block properties.The sensor/actuator array information is also used when data is serially communicated. Third,using the sensor/actuator block information gathered in the previous step, a PIC assemblycode is generated. This step is facilitated by the fact that for each sensor/actuator block in thePICLibrary the corresponding PIC assembly code has already been created and saved as an mfile. Fourth, a portion of the IOBlock MATLAB code is generated to allow serial communicationbetween MATLAB and the PIC microcontroller. This MATLAB code sends and receives thesame amount of data that the PIC microcontroller receives and sends, respectively. Fifth, thePIC microcontroller is programmed in two steps: i) using the MPASM assembler the PICassembly code, generated in step 3 above, is converted to the corresponding PIC HEX codeand ii) using the IC-Prog the PIC HEX code is downloaded to a PIC microcontroller installedon a PIC-PG2C programmer. Figure 3.9 shows a flow diagram of the three steps involved inprogramming the PIC microcontroller.After the TotalCompile function completes its sequence of tasks, the Simulink block diagrambegins to execute when the user confirms that the PIC microcontroller has been removed from

35PIC assembly code isgenerated by TotalCompilePIC assembly code is convertedto PIC HEX code by MPASMPIC HEX code is downloadedby IC-Prog via the serial portFigure 3.9: Flow diagram of programming the PIC microcontrollerthe PIC-PG2C programmer and properly installed onto the PIC development board. At thisstage, serial communication between the PIC microcontroller and MATLAB also begins. IfSimulink is stopped and needs to be run again, without any changes to the configuration ofthe PIC microcontroller I/O pins, then the PIC microcontroller need not be reprogrammed.Once the Simulink block diagram begins to execute, the PIC and PC exchange sensoryfeedback and actuator commands via serial data communication. Specifically, special function8-bit PIC registers are used for the serial communication of sensor/actuator data [43, 47]. TheIOBlock receives/transmits data from/to the PIC and stores the data in sensor/actuator globalvariables.3.5.Example—DC Motor ControlTo illustrate the functionality and capability of the DAC hardware and software of thisChapter, position control of a DC motor is performed. Specifically, a DC motor test-bed isinterfaced with a PIC-based DAC board and a control algorithm is implemented using Simulinkand the MATLAB toolbox for the PIC microcontroller. The DC motor test-bed, shown inFigure 3.10, consists of an armature controlled DC motor, instrumented with a continuousrotation potentiometer, and a power module.The potentiometer output is used to obtain the necessary feedback signals and to providea real-time display of the angular position of the motor. To control the angular position of theDC motor, the PIC microcontroller applies a controlled voltage signal produced by a controlalgorithm running on Simulink.

36Digitized sensor datafrom PICAnalog sensor dataAnalog control outputDC motor and power moduleControl outputfrom SimulinkPIC development boardSimulink block diagramFigure 3.10: Hardware layer schematicIn this Chapter, a proportional-integral-derivative (PID) controller [53] is used for theangular position control of the DC motor. The functionality of various Simulink blocks used inFigure 3.10 (see Figure 3.11 for an exploded view) is as follows. The A2D Pot block serves asan A2D block to convert the analog output of the potentiometer into an 8-bit digital data. ThePID Controller block encapsulates the standard PID control algorithm. The inputs to the PIDController block are: i) the desired angular position of the DC motor and ii) the potentiometersignal (the digitized output of A2D Pot block). The output of the PID Controller block is acontrolled voltage signal to be applied to the motor. In Figure 3.11, the PID Controller blockoutput is processed by the PWM Motor block which serves as a PWM block. Note thatthe power amplifier module of the DC motor shown in Figure 3.10 requires a 5 volt directcurrent (VDC) to drive the DC motor. Accordingly, the PID control algorithm outputs a 5VDC control signal. However, the PIC microcontroller can output only 0—5 VDC usingthe PWM output. Thus, the 5VDC output of the PID controller is appropriately transformedto command the PWM Motor block with a 0—5 VDC command signal. Finally, by processingthe PWM output from the PIC microcontroller using a simple operational amplifier basedcircuitry, the 0—5 VDC PWM output is converted into a 5VDC signal for input to thepower amplifier module.An analytical model of the DC motor under PID control, yielding a third-order closed-looptransfer function [53], is used to determine numerical values of the PID controller gains. Specifically, by requiring the closed-loop transfer function to have: i) a pair of complex-conjugatepoles with a damping ratio and natural frequency of 0.69 and 1.93, respectively, and ii) a thirdreal pole at -80, the PID control gains are computed to be KP 1.43, KI 1.97, and KD 0.5.

37Figure 3.11: Simulink block diagram: Exploded viewThis analog PID controller is implemented using Simulink’s ODE4 (Runge-Kutta) algorithmwith a sampling period of 0.13 second. For the computed gains, the experimental responseexhibits an average 2 percent settling time of 5.9sec and a percentage overshoot of 18.25%. Forcomparison, the theoretical values for the 2 percent settling time and the percentage overshootare 3sec and 14%, respectively. Figure 3.12 shows a sample experimental response of the DCmotor angular position under PID control implemented using the framework of this Chapter.Figure 3.12: DC motor angular position tracking response

383.6.Conclusio

a PIC microcontroller by serial communication. Serial communication between the PC and the PIC microcontroller is enabled by using a DB-9 serial connection between the PC and a PIC development programmer that hosts the PIC microcontroller. Two widely used PIC development programmers are Microchip’s PICS

Related Documents:

Part One: Heir of Ash Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Chapter 15 Chapter 16 Chapter 17 Chapter 18 Chapter 19 Chapter 20 Chapter 21 Chapter 22 Chapter 23 Chapter 24 Chapter 25 Chapter 26 Chapter 27 Chapter 28 Chapter 29 Chapter 30 .

TO KILL A MOCKINGBIRD. Contents Dedication Epigraph Part One Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Part Two Chapter 12 Chapter 13 Chapter 14 Chapter 15 Chapter 16 Chapter 17 Chapter 18. Chapter 19 Chapter 20 Chapter 21 Chapter 22 Chapter 23 Chapter 24 Chapter 25 Chapter 26

DEDICATION PART ONE Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 PART TWO Chapter 12 Chapter 13 Chapter 14 Chapter 15 Chapter 16 Chapter 17 Chapter 18 Chapter 19 Chapter 20 Chapter 21 Chapter 22 Chapter 23 .

About the husband’s secret. Dedication Epigraph Pandora Monday Chapter One Chapter Two Chapter Three Chapter Four Chapter Five Tuesday Chapter Six Chapter Seven. Chapter Eight Chapter Nine Chapter Ten Chapter Eleven Chapter Twelve Chapter Thirteen Chapter Fourteen Chapter Fifteen Chapter Sixteen Chapter Seventeen Chapter Eighteen

18.4 35 18.5 35 I Solutions to Applying the Concepts Questions II Answers to End-of-chapter Conceptual Questions Chapter 1 37 Chapter 2 38 Chapter 3 39 Chapter 4 40 Chapter 5 43 Chapter 6 45 Chapter 7 46 Chapter 8 47 Chapter 9 50 Chapter 10 52 Chapter 11 55 Chapter 12 56 Chapter 13 57 Chapter 14 61 Chapter 15 62 Chapter 16 63 Chapter 17 65 .

HUNTER. Special thanks to Kate Cary. Contents Cover Title Page Prologue Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Chapter 15 Chapter 16 Chapter 17 Chapter

Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Chapter 15 Chapter 16 Chapter 17 Chapter 18 Chapter 19 Chapter 20 . Within was a room as familiar to her as her home back in Oparium. A large desk was situated i

The Hunger Games Book 2 Suzanne Collins Table of Contents PART 1 – THE SPARK Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8. Chapter 9 PART 2 – THE QUELL Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Chapter 15 Chapter 16 Chapter 17 Chapt