c语言 阶跃检测程序,C语言S-Function实现离散数字PID时,系统直接阶跃输出

使用Simulink中的S-Function模块,用C语言编写位置式离散数字PID时,函数处理只是做了Ki=1的积分计算,说白了PID中只有I项积分项。系统直接阶跃输出

//c语言 S-Function文件如下

/*

* sfuntmpl_basic.c: Basic 'C' template for a level 2 S-function.

*

* Copyright 1990-2013 The MathWorks, Inc.

*/

/*

* You must specify the S_FUNCTION_NAME as the name of your S-function

* (i.e. replace sfuntmpl_basic with the name of your S-function).

*/

#define S_FUNCTION_NAME  Discrete_Position_PID_SFunc

#define S_FUNCTION_LEVEL 2

/*

* Need to include simstruc.h for the definition of the SimStruct and

* its associated macro definitions.

*/

#include "simstruc.h"

int OutputRef,OutputReal;

static int IntegrateSum;

/* Error handling

* --------------

*

* You should use the following technique to report errors encountered within

* an S-function:

*

*       ssSetErrorStatus(S,"Error encountered due to ...");

*       return;

*

* Note that the 2nd argument to ssSetErrorStatus must be persistent memory.

* It cannot be a local variable. For example the following will cause

* unpredictable errors:

*

*      mdlOutputs()

*      {

*         char msg[256];         {ILLEGAL: to fix use "static char msg[256];"}

*         sprintf(msg,"Error due to %s", string);

*         ssSetErrorStatus(S,msg);

*         return;

*      }

*

*/

/*====================*

* S-function methods *

*====================*/

/* Function: mdlInitializeSizes ===============================================

* Abstract:

*    The sizes information is used by Simulink to determine the S-function

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在S-Function中使用C语言实现PID控制器的步骤如下: 1. 在S-Function的头文件中添加需要用到的头文件,如math.h和stdio.h。 2. 定义PID控制器的参数,包括比例系数、积分系数和微分系数。 3. 定义PID控制器的变量,包括上一次误差、积分误差和输出。 4. 在S-Function输出函数中实现PID控制器的控制算法,包括计算误差、积分误差和输出。 5. 在S-Function的初始化函数中初始化PID控制器的变量。 6. 在S-Function的终止函数中清空PID控制器的变量。 下面是一个简单的PID控制器的C语言代码实现: ```c #include "math.h" #include "stdio.h" #define Kp 1.0 // 比例系数 #define Ki 0.1 // 积分系数 #define Kd 0.01 // 微分系数 double last_error = 0; // 上一次误差 double integral_error = 0; // 积分误差 double output = 0; // 输出 void pid_control(double setpoint, double input) { double error = setpoint - input; // 计算误差 double derivative_error = error - last_error; // 计算微分误差 integral_error += error; // 计算积分误差 output = Kp * error + Ki * integral_error + Kd * derivative_error; // 计算输出 last_error = error; // 更新上一次误差 } void init_pid() { last_error = 0; integral_error = 0; output = 0; } void terminate_pid() { last_error = 0; integral_error = 0; output = 0; } ``` 在S-Function输出函数中调用pid_control函数即可实现PID控制器的控制算法。在S-Function的初始化函数中调用init_pid函数进行变量初始化,在S-Function的终止函数中调用terminate_pid函数进行变量清空。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值