返回

我的模型的Matlab mex文件模拟结果不正确

发布时间:2022-05-29 15:42:11 252

我正在做一个项目,我想使用 simulink 编码器为电机速度控制模型生成 c 代码。代码已经生成,并且还根据我想要做的一些修改:我想通过改变我的控制参数来运行模拟(模型代码版本的可执行 mex 文件)。

主要目标是在matlab中遗传算法的回调函数中使用mex函数来计算最佳精确控制参数。

也许你们中的某个人过去曾尝试过类似的事情。我修改了生成的阶跃函数,您可以在描述后看到。如您所见,我的模型阶跃函数具有我添加的 3 个参数。

我在生成的函数中更改了 KR 和 KI 参数的默认值,并将它们替换为参数名称。在参数 r_sim 中我想在每一步模拟之后写模拟结果。

生成的函数是应用 ode4(Runge Kutta) 求解器算法的函数,我无法理解,因为它与模型阶跃函数一起工作,反之亦然:

写入 r_sim 指针时可能会发生错误。我检查了一下,只有一个值被写入 r_sim。

     /// this function is called from model step function 
     rt_ertODEUpdateContinuousStates(&speed_control_M->solverInfo,KR,KI,ptr); 
/* Model step function */
void speed_control_step(real_T KR,real_T KI, real_T* r_sim)
{
  real_T rtb_KR;
  ptr=r_sim;
  if (rtmIsMajorTimeStep(speed_control_M)) {
    /* set solver stop time */
    if (!(speed_control_M->Timing.clockTick0+1)) {
      rtsiSetSolverStopTime(&speed_control_M->solverInfo,
                            ((speed_control_M->Timing.clockTickH0 + 1) *
        speed_control_M->Timing.stepSize0 * 4294967296.0));
    } else {
      rtsiSetSolverStopTime(&speed_control_M->solverInfo,
                            ((speed_control_M->Timing.clockTick0 + 1) *
        speed_control_M->Timing.stepSize0 + speed_control_M->Timing.clockTickH0 *
        speed_control_M->Timing.stepSize0 * 4294967296.0));
    }
  }                                    /* end MajorTimeStep */

  /* Update absolute time of base rate at minor time step */
  if (rtmIsMinorTimeStep(speed_control_M)) {
    speed_control_M->Timing.t[0] = rtsiGetT(&speed_control_M->solverInfo);
  }

  /* Integrator: '/Integrator1_M' */
  speed_control_B.Integrator1_M = speed_control_X.Integrator1_M_CSTATE;

  /* Outport: '/ysim' */
  speed_control_Y.ysim = speed_control_B.Integrator1_M;

  *r_sim=speed_control_Y.ysim;
  r_sim++;
  /* Gain: '/KR' incorporates:
   *  Step: '/Step w_ref [rad//s]'2.5
   *
   *  Sum: '/Add2'
   */
  rtb_KR = ((real_T)!(speed_control_M->Timing.t[0] < 0.0) -
            speed_control_B.Integrator1_M) * KR;

  /* Gain: '/one_over_L' incorporates:
   *  Gain: '/K_M'
   *  Gain: '/R'
   *  Integrator: '/Integrator'
   *  Integrator: '/Integrator_Ak'
   *  Sum: '/Add6'
   *  Sum: '/add'
   */
  speed_control_B.one_over_L = (((speed_control_X.Integrator_CSTATE + rtb_KR) -
    1 * speed_control_B.Integrator1_M) - 3.0 *
    speed_control_X.Integrator_Ak_CSTATE) * 100.0;

  /* Gain: '/one_over_J' incorporates:
   *  Gain: '/K_M'
   *  Integrator: '/Integrator_Ak'
   */
  speed_control_B.one_over_J = 1 * speed_control_X.Integrator_Ak_CSTATE *
    100.0;

  /* Gain: '/KI' */
  speed_control_B.KI = KI * rtb_KR;
  
//   if (rtmIsMajorTimeStep(speed_control_M)) {
//     /* Matfile logging */
//     rt_UpdateTXYLogVars(speed_control_M->rtwLogInfo, (speed_control_M->Timing.t));
//   }                                    /* end MajorTimeStep */
  
  if (rtmIsMajorTimeStep(speed_control_M)) {
    /* signal main to stop simulation */
    {                                  /* Sample time: [0.0s, 0.0s] */
      if ((rtmGetTFinal(speed_control_M)!=-1) &&
          !((rtmGetTFinal(speed_control_M)-(((speed_control_M->Timing.clockTick1
               +speed_control_M->Timing.clockTickH1* 4294967296.0)) * 0.01)) >
            (((speed_control_M->Timing.clockTick1+
               speed_control_M->Timing.clockTickH1* 4294967296.0)) * 0.01) *
            (DBL_EPSILON))) {
        rtmSetErrorStatus(speed_control_M, "Simulation finished");
      }
    }

    rt_ertODEUpdateContinuousStates(&speed_control_M->solverInfo,KR,KI,ptr);
    
    /* Update absolute time for base rate */
    /* The "clockTick0" counts the number of times the code of this task has
     * been executed. The absolute time is the multiplication of "clockTick0"
     * and "Timing.stepSize0". Size of "clockTick0" ensures timer will not
     * overflow during the application lifespan selected.
     * Timer of this task consists of two 32 bit unsigned integers.
     * The two integers represent the low bits Timing.clockTick0 and the high bits
     * Timing.clockTickH0. When the low bit overflows to 0, the high bits increment.
     */
    if (!(++speed_control_M->Timing.clockTick0)) {
      ++speed_control_M->Timing.clockTickH0;
    }

    speed_control_M->Timing.t[0] = rtsiGetSolverStopTime
      (&speed_control_M->solverInfo);

    {
      /* Update absolute timer for sample time: [0.01s, 0.0s] */
      /* The "clockTick1" counts the number of times the code of this task has
       * been executed. The resolution of this integer timer is 0.01, which is the step size
       * of the task. Size of "clockTick1" ensures timer will not overflow during the
       * application lifespan selected.
       * Timer of this task consists of two 32 bit unsigned integers.
       * The two integers represent the low bits Timing.clockTick1 and the high bits
       * Timing.clockTickH1. When the low bit overflows to 0, the high bits increment.
       */
      speed_control_M->Timing.clockTick1++;
      if (!speed_control_M->Timing.clockTick1) {
        speed_control_M->Timing.clockTickH1++;
      }
    }
  }/* end MajorTimeStep */  
}

请查看代码并告诉我我做错了什么。由于我使用的是 Mac OS,实际上很难在 matlab 下调试代码。我尝试过但无法启动调试。

我希望根据您的经验,我会得到一些好的意见。

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
相关帖子