1

I have 2 Simulink models, one is a spring-mass-damper system

and one is a PI controller

with parameters: m = 1;c = 10;k = 50;Kp = 30;Ki = 70; reference = 0.2; After generating C++ codes using Simulink Coder for these 2 models, I tried to combine them using 1 new project in CodeBlock. The code order is as follow:

while(1){

  springMass.spring_mass_damper_U.Force = piController.PI_controller_Y.force;
  springMass.step();
  piController.PI_controller_U.position = springMass.spring_mass_damper_Y.position;
  piController.step();

  timeStep += 1;
  if(timeStep == 51) break;
} 

However, the result (the position value at each time step) was quite different from the Simulink's even though it converses to 0.2 eventually. I tried to connect them both in one Simulink model from which I generated a C++ file, then ran this file but the result was still different from the Simulink file and also from the combination of 2 generated codes. Could anyone give me a suggestion on how this happens? Thank you very much in advance!

jhhoff02
  • 1,179
  • 1
  • 17
  • 24
  • When you ran the model in simulink, did you run it with a fixed or variable step solver? This would explain the differences. – scotty3785 Apr 18 '17 at 11:36

0 Answers0