• AVR Freaks

Helpful ReplyHot!On Processor in the Loop (PIL) using the Blockset

Author
ASabir
Starting Member
  • Total Posts : 66
  • Reward points : 0
  • Joined: 2015/03/02 11:29:45
  • Location: 0
  • Status: offline
2015/05/16 02:05:45 (permalink)
0

On Processor in the Loop (PIL) using the Blockset

Hello Everyone and Lubin,
 
I am looking to implement a Processor-in-the-Loop (PIL) configuration using the blockset. I would like to run the generated code on the target hardware (dsPIC), acting as a controller. The plant will be run in Simulink (on the host computer). The plant model can be anything e.g. a wind turbine, a chemical reactor, etc.
 
In my particular case, I am looking to send an analog signal from the Simulink to the dsPIC, generate PWM signals based on the analog signal, and send those PWM signals to Simulink, acting as plant input. My questions are:
 
1. Does the blockset support this PIL functionality?
 
2. If yes, which communication protocol will be best suited for host-target communication?
 
Thank you.
post edited by ASabir - 2015/05/16 03:07:39
#1
Lubin
Moderator
  • Total Posts : 387
  • Reward points : 5
  • Joined: 2007/03/31 07:38:15
  • Location: Bayonne, France
  • Status: offline
Re: On Processor in the Loop (PIL) using the Blockset 2015/05/16 04:58:22 (permalink)
0
Hi Sabir,
 
This might be the first PIL or HIL related question on that forum. Thanks !
The next release (i.e. v3.37) does support PIL as well as External-Mode through the UART communication channel.
It should be quite easy to use. Anyway, we need to add examples and more documentation about it.
 
I am not clear with what you are willing to do exactly.
PIL simulation is not to be "real-time". Even if it is possible to drive peripheral (PWM, ADC), PIL is mostly to check that (target) controllers behave properly with (host) "simulated" plant. It is also useful to check for correctness of the running controller: comparing a (host) simulated controller with the one running on target.
 
You will have the possibility to instrument the code to profile execution time. Simulink provides the possibility to profile subsystems when they are set as "atomic".
 
Lubin
#2
ASabir
Starting Member
  • Total Posts : 66
  • Reward points : 0
  • Joined: 2015/03/02 11:29:45
  • Location: 0
  • Status: offline
Re: On Processor in the Loop (PIL) using the Blockset 2015/05/16 06:05:15 (permalink)
0
Thank you for the inputs Lubin.
 

PIL simulation is not to be "real-time". Even if it is possible to drive peripheral (PWM, ADC), PIL is mostly to check that (target) controllers behave properly with (host) "simulated" plant.

 
This is exactly what I am trying to do. To be precise, I am trying to use the dsPIC on MCLV-2 to control a motor of higher power rating (power ratings of MCLV-2 are limited). Using simulink, a plant of any rating can be tested. All that is needed from the dsPIC is the PWM to drive a high rating 'simulated' power module in Simulink.
 
I hope this clarifies my objective Smile: Smile
 
Will it be possible to do a basic minimal example?
post edited by ASabir - 2015/05/16 06:07:53
#3
Lubin
Moderator
  • Total Posts : 387
  • Reward points : 5
  • Joined: 2007/03/31 07:38:15
  • Location: Bayonne, France
  • Status: offline
Re: On Processor in the Loop (PIL) using the Blockset 2015/05/16 07:18:10 (permalink)
4 (1)
No clean example to share so far.
 
Brief instructions are as follow:
2 Simulink models are required: one top model running on the host side (PC), and one "sub" model running on the target (dsPIC).
Both models must have the same settings (Best is to "save-as" the sub-model, and modify this new model to make the top model)
 
1) Target side
The sub-model must have
- One Master block that will define the targeted chip and its basic settings like quartz, PLL etc.
- One UART Configuration block, that will set the RS232 settings for the communication target-host for the host side.
The sub model would have as many in/out port (standard Simulink library) as there are signal to be sent/receive from the host.
 
2) Host side
The Top model must include the sub-model through the Model Reference block (Simulink standard library).
This Model Reference block must have its Simulation mode set to "Processor-in-the-loop (PIL)".
Within the Microchip toolbar, use the PIL setup interface to select which UART Configuration block must be used for PIL communication on the target side, and to set RS232 parameters on the host.
 
3) With top model, click on the "run" button (i.e. "play") to start the simulation. 
The reference model will be compiled and programmed on the target. Then the top model simulate, with the target in the loop through the model reference block.
#4
ASabir
Starting Member
  • Total Posts : 66
  • Reward points : 0
  • Joined: 2015/03/02 11:29:45
  • Location: 0
  • Status: offline
Re: On Processor in the Loop (PIL) using the Blockset 2015/05/16 23:05:57 (permalink)
0
Thank you Lubin, for the detailing the procedure! I will give it a try out.
 
 
#5
ASabir
Starting Member
  • Total Posts : 66
  • Reward points : 0
  • Joined: 2015/03/02 11:29:45
  • Location: 0
  • Status: offline
Re: On Processor in the Loop (PIL) using the Blockset 2015/09/10 05:31:07 (permalink)
0
Hello Lubin,
 
Sorry for updating this thread after such a long time. I am only now getting back to the PIL. 
 
I am using v3.37 of the blockset, with MATLAB R2013b. I tried to run a very simple DC Motor speed control model using a proportional-integral controller to be implemented in a PIL configuration on MCLV-2. The DC motor model will be run in Simulink while the controller model will be run on MCLV-2. I followed the steps that you mentioned in the earlier post. However I got the following error:
An error occurred while creating an instance of connectivity configuration class "mchppil.ConnectivityConfig". Check that this is a valid connectivity configuration class.
Caused by:
Expected input number 2, ticksPerSecond, to be positive.

 
Any ideas what could be wrong here? Please find the top-level and reference models attached.
 
Thanks,
Sabir
post edited by ASabir - 2015/09/10 05:33:43
#6
Lubin
Moderator
  • Total Posts : 387
  • Reward points : 5
  • Joined: 2007/03/31 07:38:15
  • Location: Bayonne, France
  • Status: offline
Re: On Processor in the Loop (PIL) using the Blockset 2015/09/11 05:46:22 (permalink)
5 (1)
Hi Sabir,
 
It seems that Matlab R2013b require to define the timer to do profiling even when profiling is not set.
 
Try adding one block "PIL Profiling" from the Microchip library (within the Profiling folder). Problem should be solved.
 
You might then activate the profiling report. Options are within the Simulink Configuration panel:  
Code Generation => Verification => Measure task execution time & Measure function execution times & Save options (All Measurement and Analysis data).
 
Lubin
 
#7
ASabir
Starting Member
  • Total Posts : 66
  • Reward points : 0
  • Joined: 2015/03/02 11:29:45
  • Location: 0
  • Status: offline
Re: On Processor in the Loop (PIL) using the Blockset 2015/09/11 08:46:05 (permalink) ☄ Helpfulby Lubin 2015/09/12 15:24:36
0
Thanks for the input, Lubin.
 
Following your suggestions, I got the model working in PIL configuration. 
 
Here is a working version of a simple example of DC Motor speed control using a PI controller in PIL configuration. The MATLAB R2013b is used along with Microchip blockset v3.37. The controller is implemented on MCLV-2 development board (reference model: "PI_Controller.slx") while the DC motor is modeled in Simulink (top model: "DCM_PIcontrol_PIL.slx"). A simulated controller is also added to the top model for comparing results.
 
Regards,
Sabir
#8
Lubin
Moderator
  • Total Posts : 387
  • Reward points : 5
  • Joined: 2007/03/31 07:38:15
  • Location: Bayonne, France
  • Status: offline
Re: On Processor in the Loop (PIL) using the Blockset 2015/09/12 15:24:14 (permalink)
0
Hi Sabir,
 
Congratulation! As far as I know, this is the first public example of Processor In the Loop (PIL) for a dsPIC. Thanks for sharing.
 
It worked out of the box on my side; it only requires updating the COM port used on the PC side.
required hardware: MCLV V2, UART link to that board, a dsPIC 33EP256MC506 PIM.
 
One remark about COM port setting : it seems that once Simulink open has open one a COM port for either PIL or External Mode, changes on COM port settings are not effective until Matlab/Simulink is closed and restarted (This seems to close the COM port which could then be open with new parameters).
 
I modified the example to present a simulation vs PIL result (PI controller executed onboard rather than simulated). I did few changes on your models; see attachments.
 
Illustrations:
  • PIL_PI_Controller_SubSystem.png presents the subsystem that run on the dsPIC during PIL.
  • PIL_DCM_PiControl_TopSystem.png presents the top model used for PIL simulation with scope results. It illustrates the exact match between simulation and execution of the controller on the dsPIC.
  • PIL_Code_Profiling_Report.png presents code-profiling report measured during PIL execution. This require to enable options : Model Configuration Parameters -> Code Generation -> Verification -> Measure task/function execution times + Save All measurement ; After Simulation, type « executionProfile.report » at Matlab prompt to open the profiling report.
Lubin

Attached Image(s)

#9
ASabir
Starting Member
  • Total Posts : 66
  • Reward points : 0
  • Joined: 2015/03/02 11:29:45
  • Location: 0
  • Status: offline
Re: On Processor in the Loop (PIL) using the Blockset 2015/09/12 23:45:32 (permalink)
0
Hi Lubin,
 
Great job on reorganizing the model, makes it much easier to work with. Thanks for the input.
 
My idea is to extend this example to the BLDC motor speed control using PWM. This will require the top-level model to read-in the six PWM signals generated by the dsPIC in the reference PIL model, whose duty cycle is calculated by the PI controller. In other words, you can say it is the PIL version of the BLDC speed control example available in the original blockset examples (for MCLV). Success in doing so will be of great use, especially in utilizing the dsPIC to the study of large power systems employing the voltage source converters (VSC) driven by PWM pulses.
 
Firstly, is it possible for the top model to read the PWM signals from the reference model? I think it will require six "Out" ports (Simulink standard library) inside the reference PIL model for the six PWM signals to be read from the dsPIC ports RB10-RB15, right? The 'Out' ports must be connected to the blockset's 'digital output' block?
 
I will work on it and let you know the progress.

Regards,
Sabir
#10
Lubin
Moderator
  • Total Posts : 387
  • Reward points : 5
  • Joined: 2007/03/31 07:38:15
  • Location: Bayonne, France
  • Status: offline
Re: On Processor in the Loop (PIL) using the Blockset 2015/09/14 00:51:14 (permalink)
0
Hi Sabir,
 
PIL is used to validate correctness of generated code for the logical/calculation part.
While peripheral should work in PIL in the reference model, please note that PIL simulation is not realtime ; One should be carefull when using peripheral out of their « real-time » context.
 
I understand from your question that you want to feed your motor model directly with a digital PWM signal, rather than its « analog equivalent ». Such simulation would require a high timing resolution which is hardly compatible with the rotor dynamic time which is much slower. The simulation would be endless and unpractical. If you whish to simulate PWM signal feeding a continuous time motor model, you might do it for few period only paying attention to the simulink solver selected (standard continuous time solver for differential equation does not play well with stiff digital signals).
 
The Microchip blockset will not simulate PWM signals. Reading any PWM signal from a digital port would not work good either because of the high frequency and resolution of the PWM signals.
You could recreate theses PWM signals with standard simulink blocks.
#11
ASabir
Starting Member
  • Total Posts : 66
  • Reward points : 0
  • Joined: 2015/03/02 11:29:45
  • Location: 0
  • Status: offline
Re: On Processor in the Loop (PIL) using the Blockset 2015/09/14 04:16:15 (permalink)
0
Hi Lubin,
 
Point taken. Probably better to simulate high frequency high resolution signals like PWM on the host side rather than on the target.
 
Here is an example of speed control of the Brushless DC Motor using a six-step converter using a PI controller in PIL configuration on the MCLV-2 board. The model is built from an existing example in the toolbox library and requires Simulink's Simpowersystems Toolbox to run.
 
Regards,
Sabir
 
#12
ASabir
Starting Member
  • Total Posts : 66
  • Reward points : 0
  • Joined: 2015/03/02 11:29:45
  • Location: 0
  • Status: offline
Re: On Processor in the Loop (PIL) using the Blockset 2015/09/19 13:09:21 (permalink)
3 (1)
Real and Reactive Power Control of a Grid-Connected Voltage-Sourced Converter (PIL)
 
Hi All,
 
Here is another example showing the application of PIL control on a grid-connected voltage source converter. It is a modified version of an example from the the book "Voltage-Sourced Converters in Power Systems" by A. Yazdani and R. Iravani (ISBN: 9780470521564). 
 
In comparison to the previously posted examples on this thread, the controller in this example is computationally more complex. All of the phase-locked loop (PLL), Park's transform (abc to dq0) and inverse transform (dq0 to abc), and the decoupled d-q axis current controllers are implemented on the dsPIC 33EP256MC506 PIM on the MCLV-2 board. The power system modelling and the PWM gating pulses are done in Simulink top level model. The model requires Simpowersystems toolbox to run.
 
For more details on the design and modelling of this example, please refer to the textbook. 
 
 
Regards,
Sabir
 
P.S. For your PIL applications, convert the signal's data type to fixed point format instead of integer before passing on the signal to the controller (as shown) to preserve accuracy and precision, and avoid faulty results. 
post edited by ASabir - 2015/09/20 00:50:19
#13
LukasB
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2017/07/05 02:37:09
  • Location: 0
  • Status: offline
Re: On Processor in the Loop (PIL) using the Blockset 2017/07/10 14:12:10 (permalink)
0
Hello,
 
first of all, thank you Sabir for sharing your working Modell and thank you Lubin for your great Job in this Forum.
 
I also want to implement a PIL project. Therefore I have to understand the strukture of such a modell. The PIL project you provide is very valuable for this. I have the same board (MCLV-2) with the same MCU (dsPIC33EP256MC506) so I thought I could run the file without a problem ... but unfortunately I can't. I use the attached files and get the following error:
 
Error:The timeout of 10 seconds for receiving data from the rtiostream interface has been exceeded. There might be multiple reasons for this failure.
and
Error:Output argument "lSections" (and maybe others) not assigned during call to "coder.profile.ExecutionTime/get.Sections".
 
The whole Diagnostics are attached. I can't see what to do next.
 
Thank you for you help.
Lukas
post edited by Burgm - 2017/07/11 01:21:09

Attached Image(s)

#14
Lubin
Moderator
  • Total Posts : 387
  • Reward points : 5
  • Joined: 2007/03/31 07:38:15
  • Location: Bayonne, France
  • Status: offline
Re: On Processor in the Loop (PIL) using the Blockset 2017/07/11 00:54:32 (permalink)
0
Hi Lukas,
 
Models seems ok. This error is typical when the Host (PC) do not receive anything from the target and can't make the handshake at PIL startup.
you might check two points:
  1. Is the target running ?
    Test with blinking led standalone model. If you have a wrong oscillator setting, the chip is likely not to running.
  2. Is the UART Rx & Tx channels working Ok ?
    Check Tx: With a standalone model, try sending values on the uart, check with a hyperterminal or picgui interface that you are seeing something.
    Check Rx & Tx: With a standalone model, build a model that receive one value on UART Rx and send it back with a bias (+1) on Tx. Then check with the PC that you get 'b' when you send 'a' on the UART.
Theses tests should help tracking the problem down.
Lubin
 
PS: with older matlab release, handling of the COM port is sometime weird. After few tests with different COM port setting for the host, you might better restart matlab...
#15
LukasB
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2017/07/05 02:37:09
  • Location: 0
  • Status: offline
Re: On Processor in the Loop (PIL) using the Blockset 2017/07/11 09:12:04 (permalink)
0
Hi Lubin,
 
thanks for the fast reply. I followed all your advices from the External mode thread. You can see my UART settings in a post from me there (http://www.microchip.com/forums/FindPost/1003142). UART communication works in both ways. Sending an 'a' with HTerm gave me back 'b'.
 
Stepping up the the priority of Rx and Tx to 5, as you advised there in the last post, gave the solution to me. Thank you very much!
 
Is it correct, that the numbers in the Profiling Report nanoseconds are?

Attached Image(s)

#16
Lubin
Moderator
  • Total Posts : 387
  • Reward points : 5
  • Joined: 2007/03/31 07:38:15
  • Location: Bayonne, France
  • Status: offline
Re: On Processor in the Loop (PIL) using the Blockset 2017/07/11 13:28:40 (permalink)
0
Hi Lukas,
 
Some post describe basic checks to do which might be out of context. I try making such post useful to the widest audience.
 
Glad that you get PIl working. I hope External mode was also ok, otherwise, please let us known through the related thread.
 
Profiling report values are provided in nanoseconds. The measurement uses a timer whose resolution is 25ns in your case (from your sub-model "Timer for PIL profiling" block).
You must ensure that this timer will not overflow. The max Delta Time to be measured must be below 1 638 375ns (From your measurements, you have some margin). You can lower resolution to increase this limit if required.
 
You might correlate PIL measurements with a scope measurement of either the busy flag (from Master block), the MCU load pin (MCU load block), or the Pin set with the “tasks state block”  (see Profiling in MCHP block library). All these profiling functions work with standalone real time model providing a fine analysis tool for timing report.
 
You are doing very few calculations with Floating points values (double). You might speed-up your execution time if you convert theses with 16 bits integer/fixed point, or 32 bit integers/fixed point values.
Last, if you keep going with Floating point (which might be required with few sensitive operations), you might save some time by checking "Remove code from floating-point to integer conversion that wraps out-of-range values" in Configuration Parameters => Optimization.
 
Lubin
#17
LukasB
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2017/07/05 02:37:09
  • Location: 0
  • Status: offline
Re: On Processor in the Loop (PIL) using the Blockset 2017/09/05 08:49:48 (permalink)
0
Hello,
I managed to run the attached PIL simulation. When trying to run the exact same simulation on a different computer I get the following error.
 
Cannot find S-function module ‘MCHP_EmptySFunction’. S-Function modules must exist as either source files or pre-compiled object files on the MATLAB path.
 
Has anyone ever faced this problem? Thank you for your answer.
 
Best Regards,
Lukas
#18
Virland
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2019/10/18 05:08:55
  • Location: 0
  • Status: offline
Re: On Processor in the Loop (PIL) using the Blockset 2019/10/19 03:31:09 (permalink)
0
Hello to all.
I'm trying to develop a QPSK modem based on the DsPIC33FJ with a custom hand-made board.
I have a Simulink model, that is perfectly work in simulator. But after compiled by an automatic code generator and flashed,it works not so good.
For debug my algorithms, I try to apply PIL and ran into a problem - it refuses to compile:

"Error: Simulink Coder Error: LibWriteModelInput may not be called for referenced models; you may wish to guard its invocation with! IsModelReferenceTarget ()."

I tried to configure all the models according to the instructions of Lubin (post # 4).
Host-model, Sub-model and compilation log attached.

P.S. I apologize for my English(not my native language), it is not as good as I want.
https://www.dropbox.com/s/dj7t07is1hha0dg/Firmware.slx?dl=0
https://www.dropbox.com/s/kdy40gfpynd4l2q/PIL.slx?dl=0
https://www.dropbox.com/s/y8lkdkpg41hheef/PIL_simulation_20191019_135514.txt?dl=0
#19
Virland
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2019/10/18 05:08:55
  • Location: 0
  • Status: offline
Re: On Processor in the Loop (PIL) using the Blockset 2019/10/19 03:36:26 (permalink)
0
I'm using Matlab 2019b and MPLAB blockset v.3.45.05
#20
Jump to:
© 2019 APG vNext Commercial Version 4.5