Goal. Verify on the PC that the identified model + designed controller behave correctly, then generate embedded C code and flash the target β€” in one click.

Step 1 β€” simulate the closed loop

Open tutorial_picooz_simulation.slx:

⬇ tutorial_picooz_simulation.slx

Structure:

[ Setpoint Generator ] ──► (+)──► [ Controller ] ──► [ Identified Plant ] ──┐
                            β–²                      (picooz_motorBlade_m)    β”‚
                            β”‚                                               β”‚
                            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                           measured Ο‰

The plant in the simulation model is the same picooz_motorBlade_m.m ODE you identified on page 3, plugged in via a MATLAB Function block.

Apply a step sequence to the setpoint:

Time [s]Setpoint \(\omega_{\text{ref}}\)
0 – 5500 rad/s
5 – 101500 rad/s
10 – 150 rad/s

Check:

  • Rise time β€” matches your design target.
  • Overshoot β€” should be zero or small with a well-tuned controller.
  • Steady-state error β€” exact zero with the integral term active.
  • Control effort u(t) β€” must not saturate the PWM duty at Β±1 for your operating range.

Iterate on the controller gains until you are satisfied. This iteration takes seconds β€” the simulation runs faster than real time.

This is the MBD payoff you have been working toward

Everything you tune here carries unchanged to the hardware β€” because the hardware runs the same model. The target-specific parts (PWM, ADC, UART) are isolated in MPLAB Device Blocks that the simulation model simply stubs out.


Step 2 β€” generate embedded C code

Replace the identified plant block with the PWM HS FEP + ADC HS 12b hardware blocks (same inputs/outputs β€” they now talk to real peripherals). Switch Simulation β†’ Mode β†’ External.

⬇ tutorial_picooz_deploy_tunable.slx

Press Monitor & Tune.

Under the hood, in roughly this order:

  1. Embedded Coder traverses the model and emits C code for the controller subsystem.
  2. MPLAB Device Blocks emit C code for the peripherals (PWM HS FEP, ADC HS 12b, UART) and the rate-monotonic scheduler.
  3. XC-DSC compiles to .elf / .hex.
  4. MDB (or MPLAB X) programs the target through your debugger (PKOB, PICkit, ICD…).
  5. The target starts running your Simulink model β€” physically on the MCU, not on your PC.

Typical numbers on a dsPIC33AK512MPS506 @ 200 MIPS:

MetricValue
Controller (Super-Twisting) execution time< 2 Β΅s
Total code size (controller + scheduler + peripherals)~12 KB
RAM usage~2 KB
Control rate1 kHz (configurable; MCU can run much faster)

Code Replacement Library (CRL) β€” automatic optimisation

The Super-Twisting controller uses sqrt(). On dsPIC the toolbox automatically substitutes the generic floating-point sqrt with a hand-tuned assembly implementation from mchp/Lib_dsPIC/MACRO/ β€” the Code Replacement Library. Same for sin, cos, atan2 in more complex models.

This is transparent. Your model is unchanged; the generated C is optimised.


Step 3 β€” change target without rewriting a single line

The model you just deployed is not tied to the dsPIC33A. Change the Master block’s target to, for example, dsPIC33CK256MP508 or SAME70Q21B β€” the same Simulink model rebuilds for the new target. Peripheral pinning adapts automatically (PPS on dsPIC, PORT mux on SAM).

The controller’s generated C is unchanged. Only the initialisation code for the peripherals differs.

Industrial relevance. This is how a prototype developed on a €30 dev kit scales to a production BOM with a different MCU from the same family.


What’s next

The embedded code is on the target. Time to verify that the hardware response matches the simulation β€” the final MBD validation step.

Next β†’ 6. Validate & Beyond

Back β†’ 4. Controller Design