Starting in R2017b we are shipping capabilities for online tuning of PID controllers.
The best way to do this is by using Closed-Loop PID Autotuner block. You add this block to your Simulink model that you will generate code from, and you configure the model to activate autotuning based on a provided trigger signal.
The algorithm in this block will conduct an online experiment on your plant by injecting a small excitation signal and measuring the output. Beasued on measured data it will estimate plant frequency response and will compute PID gains that can automatically update existing PID gains once tuning is finished.
To tune PID gains on hardware (in generated code) with this capaility, you will need access to a license of Simulink Control Design and Simulink Coder.