实时软件系统设计第二次作业

门禁控制系统的设计

本次作业采用matlab中的simulink来进行仿真,用stateflow来画状态机,用matlab自动生成C代码。

门禁控制系统的输入信号包括:

起落杆位置传感器:有两个位置值信号(升起/落下)

升起位置传感器:输入为1表示在升起位置,输入为0表示不在升起位置,状态图用sqwzcgq来表示。

下降位置传感器:输入为1表示在下降位置,输入为0表示不再下降位置,状态图用xlwzcgq来表示。

汽车入闸传感器:有两个值(True/False)输入为1表示汽车入闸,状态图用rzcgq来表示。

汽车出闸传感器:有两个值(True/False)输入为1表示汽车出闸,状态图用czcgq来表示。

门禁控制系统的输出信号包括:

起落杆电机控制信号:(上升/下降)升起控制信号sckzxh:输出为1控制电机上升,输出为0控制电机不转。

下降控制信号xjkzxh:输出为1控制电机下降,输出为0控制电机不转。

通行灯信号:(红灯/绿灯)红灯信号xhdhong:输出为1,红灯亮,绿灯信号xhdlv:输出为1,绿灯亮。

一辆汽车的通过流程为:

起落杆处于落下状态,通行灯为红灯。

汽车进入门禁系统,入闸传感器值变为True。

控制起落杆上升,直到起落杆位置传感器到达升起位置。

通行灯为绿灯。

汽车离开门禁,触发汽车出闸传感器值为True。

控制起落杆下降,直到起落杆位置传感器到达落下位置。

通行灯变为红灯。

转化为状态图为:

 

Matlab生成的程序代码,只粘贴出来了C文件和头文件,其他文件未粘贴出来

 

2

* sskzrje.c

3

*

4

* Code generation for model "sskzrje".

5

*

6

* Model version : 1.10

7

* Simulink Coder version : 8.7 (R2014b) 08-Sep-2014

8

* C source code generated on : Tue Dec 06 15:56:35 2016

9

*

10

* Target selection: grt.tlc

11

* Note: GRT includes extra infrastructure and instrumentation for prototyping

12

* Embedded hardware selection: 32-bit Generic

13

* Code generation objectives: Unspecified

14

* Validation result: Not run

15

*/

16

#include "sskzrje.h"

17

#include "sskzrje_private.h"

18

 

19

/* Named constants for Chart: '<Root>/Chart' */

20

#define sskzrje_IN_NO_ACTIVE_CHILD ((uint8_T)0U)

21

#define sskzrje_IN_lanjie ((uint8_T)1U)

22

#define sskzrje_IN_qiluoganshengqi ((uint8_T)2U)

23

#define sskzrje_IN_qiluoganxiajiang ((uint8_T)3U)

24

#define sskzrje_IN_tongxing ((uint8_T)4U)

25

 

26

/* Block states (auto storage) */

27

DW_sskzrje_T sskzrje_DW;

28

 

29

/* Real-time model */

30

RT_MODEL_sskzrje_T sskzrje_M_;

31

RT_MODEL_sskzrje_T *const sskzrje_M = &sskzrje_M_;

32

 

33

/* Model step function */

34

void sskzrje_step(void)

35

{

36

real_T rtb_ManualSwitch;

37

real_T rtb_ManualSwitch1;

38

real_T rtb_ManualSwitch2;

39

real_T rtb_ManualSwitch3;

40

 

41

/* ManualSwitch: '<Root>/Manual Switch' incorporates:

42

* Constant: '<Root>/constant'

43

* Constant: '<Root>/constant1'

44

*/

45

if (sskzrje_P.ManualSwitch_CurrentSetting == 1) {

46

rtb_ManualSwitch = sskzrje_P.constant_Value;

47

} else {

48

rtb_ManualSwitch = sskzrje_P.constant1_Value;

49

}

50

 

51

/* End of ManualSwitch: '<Root>/Manual Switch' */

52

 

53

/* ManualSwitch: '<Root>/Manual Switch1' incorporates:

54

* Constant: '<Root>/constant'

55

* Constant: '<Root>/constant1'

56

*/

57

if (sskzrje_P.ManualSwitch1_CurrentSetting == 1) {

58

rtb_ManualSwitch1 = sskzrje_P.constant_Value;

59

} else {

60

rtb_ManualSwitch1 = sskzrje_P.constant1_Value;

61

}

62

 

63

/* End of ManualSwitch: '<Root>/Manual Switch1' */

64

 

65

/* ManualSwitch: '<Root>/Manual Switch2' incorporates:

66

* Constant: '<Root>/constant'

67

* Constant: '<Root>/constant1'

68

*/

69

if (sskzrje_P.ManualSwitch2_CurrentSetting == 1) {

70

rtb_ManualSwitch2 = sskzrje_P.constant_Value;

71

} else {

72

rtb_ManualSwitch2 = sskzrje_P.constant1_Value;

73

}

74

 

75

/* End of ManualSwitch: '<Root>/Manual Switch2' */

76

 

77

/* ManualSwitch: '<Root>/Manual Switch3' incorporates:

78

* Constant: '<Root>/constant'

79

* Constant: '<Root>/constant1'

80

*/

81

if (sskzrje_P.ManualSwitch3_CurrentSetting == 1) {

82

rtb_ManualSwitch3 = sskzrje_P.constant_Value;

83

} else {

84

rtb_ManualSwitch3 = sskzrje_P.constant1_Value;

85

}

86

 

87

/* End of ManualSwitch: '<Root>/Manual Switch3' */

88

 

89

/* Chart: '<Root>/Chart' */

90

/* Gateway: Chart */

91

/* During: Chart */

92

if (sskzrje_DW.is_active_c3_sskzrje == 0U) {

93

/* Entry: Chart */

94

sskzrje_DW.is_active_c3_sskzrje = 1U;

95

 

96

/* Entry Internal: Chart */

97

/* Transition: '<S1>:6' */

98

sskzrje_DW.is_c3_sskzrje = sskzrje_IN_lanjie;

99

 

100

/* Entry 'lanjie': '<S1>:1' */

101

} else {

102

switch (sskzrje_DW.is_c3_sskzrje) {

103

case sskzrje_IN_lanjie:

104

/* During 'lanjie': '<S1>:1' */

105

if (rtb_ManualSwitch == 1.0) {

106

/* Transition: '<S1>:3' */

107

sskzrje_DW.is_c3_sskzrje = sskzrje_IN_qiluoganshengqi;

108

 

109

/* Entry 'qiluoganshengqi': '<S1>:2' */

110

} else {

111

if (rtb_ManualSwitch2 == 0.0) {

112

/* Transition: '<S1>:25' */

113

sskzrje_DW.is_c3_sskzrje = sskzrje_IN_qiluoganxiajiang;

114

 

115

/* Entry 'qiluoganxiajiang': '<S1>:16' */

116

}

117

}

118

break;

119

 

120

case sskzrje_IN_qiluoganshengqi:

121

/* During 'qiluoganshengqi': '<S1>:2' */

122

if (rtb_ManualSwitch3 == 1.0) {

123

/* Transition: '<S1>:15' */

124

sskzrje_DW.is_c3_sskzrje = sskzrje_IN_tongxing;

125

 

126

/* Entry 'tongxing': '<S1>:14' */

127

} else {

128

if (rtb_ManualSwitch == 0.0) {

129

/* Transition: '<S1>:22' */

130

sskzrje_DW.is_c3_sskzrje = sskzrje_IN_lanjie;

131

 

132

/* Entry 'lanjie': '<S1>:1' */

133

}

134

}

135

break;

136

 

137

case sskzrje_IN_qiluoganxiajiang:

138

/* During 'qiluoganxiajiang': '<S1>:16' */

139

if (rtb_ManualSwitch2 == 1.0) {

140

/* Transition: '<S1>:18' */

141

sskzrje_DW.is_c3_sskzrje = sskzrje_IN_lanjie;

142

 

143

/* Entry 'lanjie': '<S1>:1' */

144

} else {

145

if (rtb_ManualSwitch1 == 0.0) {

146

/* Transition: '<S1>:24' */

147

sskzrje_DW.is_c3_sskzrje = sskzrje_IN_tongxing;

148

 

149

/* Entry 'tongxing': '<S1>:14' */

150

}

151

}

152

break;

153

 

154

default:

155

/* During 'tongxing': '<S1>:14' */

156

if (rtb_ManualSwitch1 == 1.0) {

157

/* Transition: '<S1>:17' */

158

sskzrje_DW.is_c3_sskzrje = sskzrje_IN_qiluoganxiajiang;

159

 

160

/* Entry 'qiluoganxiajiang': '<S1>:16' */

161

} else {

162

if (rtb_ManualSwitch3 == 0.0) {

163

/* Transition: '<S1>:23' */

164

sskzrje_DW.is_c3_sskzrje = sskzrje_IN_qiluoganshengqi;

165

 

166

/* Entry 'qiluoganshengqi': '<S1>:2' */

167

}

168

}

169

break;

170

}

171

}

172

 

173

/* End of Chart: '<Root>/Chart' */

174

 

175

/* Matfile logging */

176

rt_UpdateTXYLogVars(sskzrje_M->rtwLogInfo, (&sskzrje_M->Timing.taskTime0));

177

 

178

/* signal main to stop simulation */

179

{ /* Sample time: [0.2s, 0.0s] */

180

if ((rtmGetTFinal(sskzrje_M)!=-1) &&

181

!((rtmGetTFinal(sskzrje_M)-sskzrje_M->Timing.taskTime0) >

182

sskzrje_M->Timing.taskTime0 * (DBL_EPSILON))) {

183

rtmSetErrorStatus(sskzrje_M, "Simulation finished");

184

}

185

}

186

 

187

/* Update absolute time for base rate */

188

/* The "clockTick0" counts the number of times the code of this task has

189

* been executed. The absolute time is the multiplication of "clockTick0"

190

* and "Timing.stepSize0". Size of "clockTick0" ensures timer will not

191

* overflow during the application lifespan selected.

192

* Timer of this task consists of two 32 bit unsigned integers.

193

* The two integers represent the low bits Timing.clockTick0 and the high bits

194

* Timing.clockTickH0. When the low bit overflows to 0, the high bits increment.

195

*/

196

if (!(++sskzrje_M->Timing.clockTick0)) {

197

++sskzrje_M->Timing.clockTickH0;

198

}

199

 

200

sskzrje_M->Timing.taskTime0 = sskzrje_M->Timing.clockTick0 *

201

sskzrje_M->Timing.stepSize0 + sskzrje_M->Timing.clockTickH0 *

202

sskzrje_M->Timing.stepSize0 * 4294967296.0;

203

}

204

 

205

/* Model initialize function */

206

void sskzrje_initialize(void)

207

{

208

/* Registration code */

209

 

210

/* initialize non-finites */

211

rt_InitInfAndNaN(sizeof(real_T));

212

 

213

/* initialize real-time model */

214

(void) memset((void *)sskzrje_M, 0,

215

sizeof(RT_MODEL_sskzrje_T));

216

rtmSetTFinal(sskzrje_M, 10.0);

217

sskzrje_M->Timing.stepSize0 = 0.2;

218

 

219

/* Setup for data logging */

220

{

221

static RTWLogInfo rt_DataLoggingInfo;

222

sskzrje_M->rtwLogInfo = &rt_DataLoggingInfo;

223

}

224

 

225

/* Setup for data logging */

226

{

227

rtliSetLogXSignalInfo(sskzrje_M->rtwLogInfo, (NULL));

228

rtliSetLogXSignalPtrs(sskzrje_M->rtwLogInfo, (NULL));

229

rtliSetLogT(sskzrje_M->rtwLogInfo, "tout");

230

rtliSetLogX(sskzrje_M->rtwLogInfo, "");

231

rtliSetLogXFinal(sskzrje_M->rtwLogInfo, "");

232

rtliSetLogVarNameModifier(sskzrje_M->rtwLogInfo, "rt_");

233

rtliSetLogFormat(sskzrje_M->rtwLogInfo, 0);

234

rtliSetLogMaxRows(sskzrje_M->rtwLogInfo, 1000);

235

rtliSetLogDecimation(sskzrje_M->rtwLogInfo, 1);

236

rtliSetLogY(sskzrje_M->rtwLogInfo, "");

237

rtliSetLogYSignalInfo(sskzrje_M->rtwLogInfo, (NULL));

238

rtliSetLogYSignalPtrs(sskzrje_M->rtwLogInfo, (NULL));

239

}

240

 

241

/* states (dwork) */

242

(void) memset((void *)&sskzrje_DW, 0,

243

sizeof(DW_sskzrje_T));

244

 

245

/* Matfile logging */

246

rt_StartDataLoggingWithStartTime(sskzrje_M->rtwLogInfo, 0.0, rtmGetTFinal

247

(sskzrje_M), sskzrje_M->Timing.stepSize0, (&rtmGetErrorStatus(sskzrje_M)));

248

 

249

/* InitializeConditions for Chart: '<Root>/Chart' */

250

sskzrje_DW.is_active_c3_sskzrje = 0U;

251

sskzrje_DW.is_c3_sskzrje = sskzrje_IN_NO_ACTIVE_CHILD;

252

}

253

 

254

/* Model terminate function */

255

void sskzrje_terminate(void)

256

{

257

/* (no terminate code required) */

258

}

259

 

/*

2

* sskzrje.h

3

*

4

* Code generation for model "sskzrje".

5

*

6

* Model version : 1.10

7

* Simulink Coder version : 8.7 (R2014b) 08-Sep-2014

8

* C source code generated on : Tue Dec 06 15:56:35 2016

9

*

10

* Target selection: grt.tlc

11

* Note: GRT includes extra infrastructure and instrumentation for prototyping

12

* Embedded hardware selection: 32-bit Generic

13

* Code generation objectives: Unspecified

14

* Validation result: Not run

15

*/

16

#ifndef RTW_HEADER_sskzrje_h_

17

#define RTW_HEADER_sskzrje_h_

18

#include <float.h>

19

#include <string.h>

20

#include <stddef.h>

21

#ifndef sskzrje_COMMON_INCLUDES_

22

# define sskzrje_COMMON_INCLUDES_

23

#include "rtwtypes.h"

24

#include "rtw_continuous.h"

25

#include "rtw_solver.h"

26

#include "rt_logging.h"

27

#endif /* sskzrje_COMMON_INCLUDES_ */

28

 

29

#include "sskzrje_types.h"

30

 

31

/* Shared type includes */

32

#include "multiword_types.h"

33

#include "rt_nonfinite.h"

34

 

35

/* Macros for accessing real-time model data structure */

36

#ifndef rtmGetFinalTime

37

# define rtmGetFinalTime(rtm) ((rtm)->Timing.tFinal)

38

#endif

39

 

40

#ifndef rtmGetRTWLogInfo

41

# define rtmGetRTWLogInfo(rtm) ((rtm)->rtwLogInfo)

42

#endif

43

 

44

#ifndef rtmGetErrorStatus

45

# define rtmGetErrorStatus(rtm) ((rtm)->errorStatus)

46

#endif

47

 

48

#ifndef rtmSetErrorStatus

49

# define rtmSetErrorStatus(rtm, val) ((rtm)->errorStatus = (val))

50

#endif

51

 

52

#ifndef rtmGetStopRequested

53

# define rtmGetStopRequested(rtm) ((rtm)->Timing.stopRequestedFlag)

54

#endif

55

 

56

#ifndef rtmSetStopRequested

57

# define rtmSetStopRequested(rtm, val) ((rtm)->Timing.stopRequestedFlag = (val))

58

#endif

59

 

60

#ifndef rtmGetStopRequestedPtr

61

# define rtmGetStopRequestedPtr(rtm) (&((rtm)->Timing.stopRequestedFlag))

62

#endif

63

 

64

#ifndef rtmGetT

65

# define rtmGetT(rtm) ((rtm)->Timing.taskTime0)

66

#endif

67

 

68

#ifndef rtmGetTFinal

69

# define rtmGetTFinal(rtm) ((rtm)->Timing.tFinal)

70

#endif

71

 

72

/* Block states (auto storage) for system '<Root>' */

73

typedef struct {

74

uint8_T is_active_c3_sskzrje; /* '<Root>/Chart' */

75

uint8_T is_c3_sskzrje; /* '<Root>/Chart' */

76

} DW_sskzrje_T;

77

 

78

/* Parameters (auto storage) */

79

struct P_sskzrje_T_ {

80

real_T constant_Value; /* Expression: 1

81

* Referenced by: '<Root>/constant'

82

*/

83

real_T constant1_Value; /* Expression: 0

84

* Referenced by: '<Root>/constant1'

85

*/

86

uint8_T ManualSwitch_CurrentSetting; /* Computed Parameter: ManualSwitch_CurrentSetting

87

* Referenced by: '<Root>/Manual Switch'

88

*/

89

uint8_T ManualSwitch1_CurrentSetting;/* Computed Parameter: ManualSwitch1_CurrentSetting

90

* Referenced by: '<Root>/Manual Switch1'

91

*/

92

uint8_T ManualSwitch2_CurrentSetting;/* Computed Parameter: ManualSwitch2_CurrentSetting

93

* Referenced by: '<Root>/Manual Switch2'

94

*/

95

uint8_T ManualSwitch3_CurrentSetting;/* Computed Parameter: ManualSwitch3_CurrentSetting

96

* Referenced by: '<Root>/Manual Switch3'

97

*/

98

};

99

 

100

/* Real-time Model Data Structure */

101

struct tag_RTM_sskzrje_T {

102

const char_T *errorStatus;

103

RTWLogInfo *rtwLogInfo;

104

 

105

/*

106

* Timing:

107

* The following substructure contains information regarding

108

* the timing information for the model.

109

*/

110

struct {

111

time_T taskTime0;

112

uint32_T clockTick0;

113

uint32_T clockTickH0;

114

time_T stepSize0;

115

time_T tFinal;

116

boolean_T stopRequestedFlag;

117

} Timing;

118

};

119

 

120

/* Block parameters (auto storage) */

121

extern P_sskzrje_T sskzrje_P;

122

 

123

/* Block states (auto storage) */

124

extern DW_sskzrje_T sskzrje_DW;

125

 

126

/* Model entry point functions */

127

extern void sskzrje_initialize(void);

128

extern void sskzrje_step(void);

129

extern void sskzrje_terminate(void);

130

 

131

/* Real-time Model object */

132

extern RT_MODEL_sskzrje_T *const sskzrje_M;

133

 

134

/*-

135

* The generated code includes comments that allow you to trace directly

136

* back to the appropriate location in the model. The basic format

137

* is <system>/block_name, where system is the system number (uniquely

138

* assigned by Simulink) and block_name is the name of the block.

139

*

140

* Use the MATLAB hilite_system command to trace the generated code back

141

* to the model. For example,

142

*

143

* hilite_system('<S3>') - opens system 3

144

* hilite_system('<S3>/Kp') - opens and selects block Kp which resides in S3

145

*

146

* Here is the system hierarchy for this model

147

*

148

* '<Root>' : 'sskzrje'

149

* '<S1>' : 'sskzrje/Chart'

150

*/

151

#endif /* RTW_HEADER_sskzrje_h_ */

152

 

/*

2

* sskzrje_private.h

3

*

4

* Code generation for model "sskzrje".

5

*

6

* Model version : 1.10

7

* Simulink Coder version : 8.7 (R2014b) 08-Sep-2014

8

* C source code generated on : Tue Dec 06 15:56:35 2016

9

*

10

* Target selection: grt.tlc

11

* Note: GRT includes extra infrastructure and instrumentation for prototyping

12

* Embedded hardware selection: 32-bit Generic

13

* Code generation objectives: Unspecified

14

* Validation result: Not run

15

*/

16

#ifndef RTW_HEADER_sskzrje_private_h_

17

#define RTW_HEADER_sskzrje_private_h_

18

#include "rtwtypes.h"

19

#include "builtin_typeid_types.h"

20

#include "multiword_types.h"

21

 

22

/* Private macros used by the generated code to access rtModel */

23

#ifndef rtmSetTFinal

24

# define rtmSetTFinal(rtm, val) ((rtm)->Timing.tFinal = (val))

25

#endif

26

 

27

#ifndef rtmGetTPtr

28

# define rtmGetTPtr(rtm) (&(rtm)->Timing.taskTime0)

29

#endif

30

#endif /* RTW_HEADER_sskzrje_private_h_ */

31

 
原文地址:https://www.cnblogs.com/shishiruanjiankongzhi/p/6137989.html