Lab Guide 1 - Getting Started with the DC Servomotor
In questa pagina 3
1. Activity goal
Become familiar with the DC servomotor unit used in the next laboratory activities. By the end you should be able to implement and configure a Simulink model designed for real-time simulation: a simulation that runs in real time and allows direct interaction with a physical device. Background on the hardware and on the toolbox is in Experimental Setup and Simulink Desktop Real-TimeDescription of the laboratory setup (Quanser SRV-02 DC servomotor unit with voltage driver and encoder, NI data acquisition board with BNC-2110 terminal board, PC workstation) and of the Simulink Desktop Real-Time toolbox with its normal, accelerator and external execution modes.Experimental Setup and Simulink Desktop Real-Time →; the model of the motor is in Modeling a DC ServomotorA permanent-magnet DC motor is modeled by an electrical equation with a back-EMF term and a mechanical equation with inertia and viscous friction, a gearbox just adds the load inertia and friction divided by N squared on the motor side, and for the laboratory servomotor (voltage driver plus gearmotor plus disc) this gives a transfer function from input voltage to load angle that reduces to a first-order lag times an integrator.Modeling a DC Servomotor →.
2. Experimental setup
- Quanser SRV-02 servomotor: the physical device to be controlled (the plant). A DC motor with a built-in planetary gearbox drives a mechanical load attached to its output shaft. The load considered in this course is a simple disc inertia. An incremental optical encoder is connected directly to the output shaft and measures the position of the load. A potentiometer is also connected to the shaft through a gear coupling (with an anti-backlash mechanism) and can be used as an alternative load position sensor. The DC motor is driven by a linear voltage driver based on a power operational amplifier. From "Motor Input" to "Motor Current A" the driver behaves as a first-order low-pass filter with DC gain and cut-off frequency A shunt resistor in series with the motor senses the armature current (the current in the rotor windings).
Photographs of the unit: top panel (a) with numbered parts for the connectors (potentiometer outputs, Motor INPUT, Encoder and Encoder 90°, the 24 Vdc input, Motor Current A and B, the cable) and (b) the three gears on the top plate with the output shaft, (c) the DC motor and the planetary gearbox, (d) the voltage driver, the encoder and the potentiometer, (e) the disc inertia mounted on the output shaft.
National Instruments data acquisition board (NI-DAQ PCI-6221 or PCIe-6321). The PCI-6221 (workstations PCFA05 to PCFA12) supports:
- 16 single-ended or 8 differential analog input channels with 16 bit ADC resolution, maximum sample rate (per channel) of and configurable ADC input range (, , , );
- 2 analog output channels with 16 bit DAC resolution, fixed output range and maximum sample rate of (single-channel output mode) or (dual-channel);
- 24 digital I/O channels;
- 2 general-purpose 32-bit counters/timers (PWM generation, encoder counting, frequency measurement, event counting, etc.).
The PCIe-6321 (workstations PCFA01 to PCFA04, as in Table 1) has similar specifications except for: DAC maximum sampling rates (single channel) and (dual channel); and 4 general-purpose counters/timers instead of 2.
National Instruments BNC-2110 terminal board.
BNC-terminated connection cables (three single-channel cables plus one 5-channel cable for each workbench).
DC power supply with adjustable voltage output (laptop power adapter).
PC workstation running MATLAB/Simulink; the Simulink Desktop Real-Time (SLDRT) toolbox is used for the real-time simulation that interacts directly with the setup.
Internal circuits shown on the sheet: the voltage driver (, , , , , , supplies ); the potentiometer output (supply , , then in series with the output and to ground, on Pot 1 Out / Pot 2 Out); the V Reference output obtained from a divider of two resistors on (so ).
Table 1 - nominal data (from the data-sheets)
| DC gearmotor | value |
|---|---|
| Motor type | brushed DC motor |
| Motor id | Faulhaber 2338S006S |
| Nominal voltage | |
| Nominal output power | |
| Motor efficiency | |
| Armature resistance | |
| Armature inductance | |
| No-load speed | |
| No-load current | |
| Stall torque | |
| Back-EMF constant | |
| Torque constant | |
| Rotor inertia | |
| Gearbox type | planetary gearbox |
| Gearbox id | Micromotor SA 23/1 |
| Gearbox ratio | |
| Gearbox efficiency | |
| Moment of inertia of external 72-tooth gear | |
| Moment of inertia of extra disc |
(Consistency check: converting with gives , equal to the torque constant in SI units, as it must for an ideal DC motor.)
| Sensors | value |
|---|---|
| Encoder: type / sensed quantity | optical incremental encoder, output shaft angular position (incremental) |
| Encoder id | Hewlett-Packard HEDS-5540#A06 |
| Pulses per rotation (ppr) | (all units except "MOTORE 8" and "10"), (on "MOTORE 8" and "10" units); times larger when adopting quadrature encoding for counting the pulses |
| Potentiometer 1 | output shaft angular position (absolute), Spectrol 138-0-0-103, , range , supply |
| Potentiometer 2 | flexible joint angular displacement (absolute), Spectrol 357-0-0-103, , range , supply |
| Voltage driver | value |
|---|---|
| Driver type | linear voltage driver (LM1875 power op-amp) |
| Driver gain | |
| Driver cut-off frequency |
| I/O board | value |
|---|---|
| Board id | NI PCI-6221 (PCFA05 to PCFA12), NI PCIe-6321 (PCFA01 to PCFA04) |
| ADC range / resolution | , , , / bits |
| DAC range / resolution | / bits |
3. Real-time simulation
MATLAB/Simulink supports all steps of the control design flow in the course, and implementation and experimental testing are done with the SLDRT toolbox: a real-time kernel for executing Simulink models on Windows or macOS, with library blocks to interface with many I/O types (analog and digital signals, encoder/counter inputs, PWM/frequency outputs, serial/UDP/TCP). To run a model in real time the model is first converted into executable code, which is loaded in memory and executed by the SLDRT kernel when the real-time simulation begins. MATLAB does it transparently in two stages: the Simulink Coder translates the model into C source code, then the code is compiled with a supported C compiler.
3.1 Configure a Simulink model for SLDRT
- Set the working directory: with the
cdcommand in the MATLAB command window, or with the Current Directory field of the MATLAB toolbar. - Create a new model: New, Model from the MATLAB toolbar, or run
slLibraryBrowserand click the New button of the Simulink Library Browser. - Save the model in the working directory (Simulation tab: Save, Save as ...). The Simulink Coder generates auxiliary files in the current working directory, so the model must be saved in the right directory to avoid files in unintended places.
- Set the model parameters for real-time execution: open the Configuration Parameters window with Model Settings in the Modeling tab.
- 4-a, Solver node. Stop time: an appropriate final time (
Infto run until manually stopped). Type: Fixed-step, because the Simulink Coder does not support variable-step solvers. Solver: an appropriate one; with continuous-time blocks a suitable ODE solver such asode5 (Dormand-Prince); if the model contains only discrete-time blocks,discrete (no continuous state). Under Additional options, Fixed step size (fundamental sample time): the sample time, e.g. for . Click Apply. - 4-b, Data Import/Export node. Uncheck Single simulation output, to export data logged by Scope and To Workspace blocks as independent variables (if left checked, a single container variable of type
Simulink.SimulationOutputis created). Under Additional parameters uncheck Limit data points to last, so that all the simulation data are saved. Apply. - 4-c, Code generation node. In Target selection, click Browse in the System target file list; in the System Target Browser select
sldrt.tlc(Simulink Desktop Real-Time) and click OK. - 4-d. Click OK in the Configuration Parameters window.
- 4-e. In the model window select the Desktop Real-Time tab; in the Mode section select Run in Kernel to enable real-time execution.
- 4-a, Solver node. Stop time: an appropriate final time (
- Buffer size for data exchange with the real-time application: in the Prepare section, click Control Panel to open the External Mode Control Panel; click Signal & Triggering; in Trigger options, field Duration, enter the length (number of sample points) of the buffer used to exchange data between Simulink and the real-time application. Example: with sample time , Duration samples stores of data (from to ). The window also shows Source
manual, Modenormal, Delay0and the option "Arm when connecting to target" checked. Click OK to close both windows. Note: when the buffer is full it is completely erased before saving new data, so it always holds only the last "Duration" samples. - Save the model with the new configuration (Simulation tab, Save).
3.2 Interface the model with the hardware
Open the Simulink Library Browser, go to the Simulink Desktop Real-Time blockset (blocks include Counter Input, Digital Input, Digital Output, Encoder Input, Analog Input, Analog Output). Drag into the model:
- Analog Input: reads values from a specified input channel (or list of channels) of the ADC of the selected board;
- Analog Output: writes values to a specified output channel (or list of channels) of the DAC;
- Encoder Input: reads data from a specified Encoder Pulse Counter integrated in the board.
Example model: a Constant block feeds the Analog Output; the Encoder Input goes to a Scope; the Analog Input goes through a Demux to a Scope. The Analog Input acquires "Motor Current A" and "Motor Current B" (separated by a Demux and displayed on a Scope); the Analog Output sends the voltage set by the Constant to the Motor Input; the Encoder Input retrieves the pulse count of the encoder.
Analog Output block (double-click):
- Data acquisition board: choose the board (
National Instruments PCI-6221orPCIe-6321, depending on the workbench) in the drop-down list (the "board setup" button lets you check the settings); if no board is installed click Install new board, choose National Instruments and then the board model. - Board setup: in the Counter 0 mode list select Quadrature encoder, to count both the rising and falling edges of the two encoder outputs (relevant only for the Encoder Input block configured later). Click Test: if successful, "Board Test OK" appears; OK to confirm.
- Timing: Sample Time in seconds, or to inherit it from the model configuration. Other fields visible on the dialog: Maximum missed ticks (), Show "Missed Ticks" port, Yield CPU when waiting.
- Input/Output: Output channels
1(the DAC channel physically connected to "Motor Input" through the "AO 0" connector of the BNC-2110); Output range-10 to 10 V; Block input signalVolts; Initial value and Final value both , to make sure the motor is off at the beginning and at the end of the simulation. OK.
Encoder Input block:
- choose the board; Sample Time (or );
- Input channels
1(counter 0, configured for quadrature counting; it is connected to the "Encoder" and "Encoder 90°" outputs of the SRV-02 through the "USER 1" and "USER 2" terminals of the BNC-2110); - Quadrature mode:
quadruple(counting in quadruple encoding mode); - Reset input function:
none(counter reset only at the beginning of the simulation: the initial encoder position is the zero reference); - Output data type
double. OK.
Analog Input block:
- choose the board; Sample Time (or );
- Input channels
[1,2](the first two ADC channels, connected to "Motor Current A" and "B" through "AI 0" and "AI 1" of the BNC-2110); - Input range
-10 to 10 V(to cover the armature voltage range); Block output signalVolts; Output data typedouble. OK.
Save the model before proceeding (good practice: save regularly to avoid losing data after a MATLAB crash).
3.3 Export the simulation results to the MATLAB workspace
Use the To Workspace block (Simulink, Sinks) or the logging option of the Scope block:
- double-click the Scope, then Configuration Properties; open the logging section (called History tab in the text, Logging tab in the figure). Uncheck Limit data points to last; check Log data to workspace (Save data to workspace) and enter in Variable name the name of the variable that will hold the logged data (created at the end of the simulation; in the figure
simdata); - in Format select the appropriate format. Structure With Time is recommended: it stores time and signal values in a single structure. With
simresas the name of the variable (the text usessimres, the figure showssimdata, with the same meaning), the fields are retrieved with
t = simres.time
y = simres.signals(n).data(:,m)
simres.signals.datawhere the first line gives the time values, the second the -th signal in the -th set of scope axes, and the third is the shorthand when the Scope has only one signal and one set of axes. Note: do not select the Dataset format, it is not supported by SLDRT.
3.4 Connect the SRV-02 unit to the BNC-2110
With the BNC cables:
- "AO 0" of the BNC-2110 to "Motor Input" of the SRV-02: the Simulink model controls the motor by sending voltage signals;
- "Motor Current A" and "Motor Current B" of the SRV-02 to "AI 0" and "AI 1": real-time monitoring of the armature current;
- "Encoder" and "Encoder 90°" of the SRV-02 to "USER 1" and "USER 2": position feedback. "USER 1" and "USER 2" are internally connected to the two (quadrature) inputs of counter 0 through the spring terminal block of the Digital and timing I/O section of the BNC-2110.
Cable colours in the photograph: red on Motor INPUT, yellow on Motor Current A, black on Motor Current B, green on Encoder, blue on Encoder 90°.
- IMPORTANT: before proceeding verify that the regulated output voltage of the power supply adapter is set to . Connect the supply to the SRV-02 with the plug-in connector. (The front-panel label in the photographs reads 24 Vdc; the guide states , the value to set.)
PRECAUTION: when the SRV-02 is turned on, avoid accidental short-circuits by making sure that the voltage-driver terminals do not touch conductive materials (rings, metal clips, etc.).
3.5 Run the model in real time
In the Simulink Desktop Real-Time tab click Run in Real-Time. This starts the sequence:
- Build: the Simulink Coder converts the model to C source files stored in the current working directory; a C compiler compiles and links them into the real-time application (visible as files
test1_sldrt_win64,test1.rxw64, ... in the folder). Manual trigger: Run in Real Time, Build; - Connect: the Simulink model is connected with the SLDRT kernel, enabling exchange of commands, parameters and data logging. Manual trigger: Run in Real Time, Connect;
- Start: the real-time application is loaded in memory and executed by the SLDRT kernel. Manual trigger: Run in Real Time, Start.
The build can be monitored in the Diagnostic Viewer (link "View diagnostic" at the bottom of the model window); a successful build ends with "Build process completed successfully".
- Build: the Simulink Coder converts the model to C source files stored in the current working directory; a C compiler compiles and links them into the real-time application (visible as files
While the simulation is running you can change the armature voltage in real time by changing the value of the Constant block connected to the Analog Output. Because of static friction the shaft may not rotate at low voltages: increase the voltage gradually until the shaft starts spinning. Use the Scope to monitor the encoder pulse count and the voltage drop across the shunt resistor.
To stop, click the Stop button in the Simulink toolbar (during the run the toolbar shows Stop, Tune Parameters and Hold Updates).
Versione ripasso
Goal: configure a Simulink model for real-time simulation with the unit connected (see Experimental Setup and Simulink Desktop Real-TimeDescription of the laboratory setup (Quanser SRV-02 DC servomotor unit with voltage driver and encoder, NI data acquisition board with BNC-2110 terminal board, PC workstation) and of the Simulink Desktop Real-Time toolbox with its normal, accelerator and external execution modes.Experimental Setup and Simulink Desktop Real-Time →).
Setup: SRV-02 (DC motor, planetary gearbox, disc inertia, encoder, potentiometer, voltage driver), NI DAQ PCI-6221 or PCIe-6321, BNC-2110 terminal board, BNC cables, DC supply (set ), PC with MATLAB/Simulink and SLDRT.
- Driver: , ; shunt senses armature current.
- Key data: , , torque constant , back-EMF , rotor inertia , gearbox ratio (efficiency ), nominal voltage , encoder ppr ( with quadrature), ADC bit (), DAC bit .
Model configuration (Model Settings)
- Solver: Fixed-step (Simulink Coder needs it);
ode5if continuous blocks,discrete (no continuous state)otherwise; step for . - Data Import/Export: uncheck Single simulation output and Limit data points to last.
- Code generation: system target file
sldrt.tlc. - Desktop Real-Time tab: Run in Kernel. Control Panel, Signal & Triggering, Duration buffer length (e.g. samples at is ); buffer keeps only the last samples.
Blocks
- Analog Output: channel (AO 0 to Motor Input), range , initial and final value .
- Encoder Input: channel (counter 0, USER 1/USER 2), quadrature mode quadruple, reset
none; board setup: Counter 0 mode Quadrature encoder. - Analog Input: channels
[1,2](AI 0, AI 1: Motor Current A, B), range . - Sample time inherits the model sample time.
Logging: Scope Configuration Properties, uncheck Limit data points, Log data to workspace, format Structure With Time (never Dataset).
t = simres.time
y = simres.signals(n).data(:,m)
simres.signals.dataWiring: AO 0 to Motor Input; Motor Current A, B to AI 0, AI 1; Encoder, Encoder 90° to USER 1, USER 2. Check supply voltage first; avoid short circuits at the driver terminals.
Run: Run in Real-Time does Build, Connect, Start (each can be triggered by hand); change the Constant value live; static friction prevents rotation at low voltage; Stop to end.
Typical error: saving the model outside the working directory, or leaving a variable-step solver, so the code generation fails or scatters files.
Passi di configurazione del modello (sezione 3.1):
- Impostare la cartella di lavoro e salvare il modello in quella cartella (Simulink Coder vi genera i file).
- Solver: Fixed-step;
ode5se ci sono blocchi continui,discrete (no continuous state)se solo discreti; passo fisso s per ms. - Data Import/Export: deselezionare Single simulation output e Limit data points to last.
- Code generation: system target file
sldrt.tlc, poi scheda Desktop Real-Time, Run in Kernel. - Control Panel, Signal & Triggering: Duration = lunghezza del buffer in campioni. Esempio: con s, Duration campioni = s (da a s). Il buffer pieno si svuota e tiene solo gli ultimi campioni.
Blocchi (sezione 3.2):
- Analog Output: scheda PCI-6221 o PCIe-6321, Counter 0 mode Quadrature encoder, canale
1(AO 0), range a V, valore iniziale e finale per avere il motore spento all'inizio e alla fine. - Encoder Input: canale
1(counter 0, USER 1 e USER 2), modalitàquadruple, resetnone: la posizione iniziale è il riferimento zero. - Analog Input: canali
[1,2](AI 0 e AI 1, correnti Motor Current A e B), range a V, coperto l'intervallo di V dell'armatura.
Dati nominali (tabella 1): tensione nominale V, potenza nominale W, rendimento , velocità a vuoto rpm, corrente a vuoto A, coppia di stallo N cm, costante di BEMF mV/rpm, costante di coppia N m/A.
Verifica di coerenza: V s/rad, uguale alla costante di coppia in unità SI, come deve essere per un motore DC ideale.
Sensori: encoder ottico HEDS-5540#A06, 500 impulsi per giro (1024 su MOTORE 8 e 10), con la codifica in quadratura. Potenziometro 1 (posizione assoluta dell'albero, range ); potenziometro 2 (spostamento del giunto flessibile, range ).
Esecuzione in tempo reale (sezione 3.5):
- Build: Simulink Coder genera il codice C, il compilatore lo lega in un'applicazione real-time (per esempio
test1_sldrt_win64). - Connect: il modello si collega al kernel SLDRT per comandi, parametri e registrazione dati.
- Start: l'applicazione viene caricata in memoria ed eseguita dal kernel. Durante l'esecuzione si cambia la tensione con il blocco Constant. Per l'attrito statico può servire aumentare gradualmente la tensione fino a far partire l'albero.
Registrazione dei dati (sezione 3.3): nel blocco Scope, deselezionare Limit data points to last, attivare Log data to workspace, scegliere Structure With Time (non Dataset, non supportato da SLDRT).
Precauzioni: prima di accendere, verificare che l'alimentatore sia impostato a V (l'etichetta della foto riporta Vdc); evitare cortocircuiti sui morsetti del driver.
Errori tipici:
- Lasciare il solver a passo variabile: il Simulink Coder non lo supporta.
- Usare il formato Dataset per i log: non è supportato da SLDRT.
- Impostare Duration pensando in secondi invece che in campioni.
- Dimenticare il
Quadrature encodersulla scheda: il contatore non conta entrambi i fronti.