-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsfun_cflie_simple.cpp
218 lines (183 loc) · 5.67 KB
/
sfun_cflie_simple.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* File : sfun_cflie_simple.cpp
* Abstract :
*
*
*
*
*
*
*/
#include <iostream>
#include "mex.h"
#include "CCrazyflie.h"
using namespace std;
#define S_FUNCTION_LEVEL 2
#define S_FUNCTION_NAME sfun_cflie_simple
#include "simstruc.h"
#define IS_PARAM_DOUBLE(pVal) (mxIsNumeric(pVal) && !mxIsLogical(pVal) &&\
!mxIsEmpty(pVal) && !mxIsSparse(pVal) && !mxIsComplex(pVal) && mxIsDouble(pVal))
/*===================*
*S-function methods *
*===================*/
#define MDL_CHECK_PARAMETERS
#if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
/*
* Check to make sure that each parameter is 1-d and positive
*/
static void mdlCheckParameters(SimStruct *S)
{
const mxArray *pVal0 = ssGetSFcnParam(S,0);
if ( !IS_PARAM_DOUBLE(pVal0)) {
ssSetErrorStatus(S, "Parameter to S-function must be a double scalar");
return;
}
}
#endif
/* Function: mdlInitializeSizes ===========================================
* Abstract:
*
*
*/
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S,1); //Numer of expected parameters
#if defined(MATLAB_MEX_FILE)
if (ssGetNumSFcnParams(S) == ssGetSFcnParamsCount(S)) {
mdlCheckParameters(S);
if (ssGetErrorStatus(S) != NULL) {
return;
}
} else {
return; /* Parameter mismatch will be reported by Simulink */
}
#endif
ssSetSFcnParamTunable(S,0,0);
ssSetNumContStates(S,0);
ssSetNumDiscStates(S,0);
int NUM_INPUT_PORTS = 1;
if(!ssSetNumInputPorts(S,NUM_INPUT_PORTS)) return;
for (int i=0; i<NUM_INPUT_PORTS; i++) {
ssSetInputPortWidth(S,i,4);
ssSetInputPortDirectFeedThrough(S, i, 1);
//ssSetInputPortRequiredContiguous(S,i,1);
}
int NUM_OUTPUT_PORTS = 1;
if(!ssSetNumOutputPorts(S,NUM_OUTPUT_PORTS)) return;
ssSetOutputPortWidth(S,0,4);
ssSetOutputPortDataType(S,0,SS_DOUBLE);
ssSetNumSampleTimes(S,1);
ssSetNumRWork(S,0);
ssSetNumIWork(S,0);
ssSetNumPWork(S,3); // reserve element in the pointers vector to store a C++ object
ssSetNumModes(S,0);
ssSetNumNonsampledZCs(S,0);
ssSetSimStateCompliance(S, USE_CUSTOM_SIM_STATE);
ssSetOptions(S,0);
}
/* Function: mdlInitializeSampleTimes =====================================
* Abstract:
*
*
*/
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S,0, mxGetScalar(ssGetSFcnParam(S,0)));
ssSetOffsetTime(S,0, 0.0);
ssSetModelReferenceSampleTimeDefaultInheritance(S);
}
#define MDL_START /* Change to #undef to remove function */
#if defined(MDL_START)
/* Function: mdlStart =====================================================
* Abstract:
*
*
*/
static void mdlStart(SimStruct *S)
{
int_T i;
int_T nInputPorts = ssGetNumInputPorts(S);
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
CCrazyRadio *crRadio = new CCrazyRadio("radio://0/80/250K");
ssGetPWork(S)[0] = crRadio;
bool initDone = false;
// ssGetPWork(S)[2] = initDone;
if(crRadio->startRadio()){
CCrazyflie *cflieCopter = new CCrazyflie(crRadio);
cflieCopter->setSendSetpoints(true);
ssGetPWork(S)[1] = cflieCopter;
// ssGetPWork(S)[2] = cflieCopter->cycle();
/* Unlock motors*/
cflieCopter->setThrust(0);
}
else {
mexPrintf("\n Radio could not be started!");
ssSetErrorStatus(S, "Radio could not be started!");
}
ssPrintf("Done Initializing");
// usleep(100);
}
#endif /* MDL_START */
/* Function: mdlOutputs ===================================================
* Abstract:
*
*
*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
double *output = (double *)ssGetOutputPortSignal(S,0);
//output[0] = 5;
UNUSED_ARG(tid);
bool initDone = ssGetPWork(S)[2];
// Read input port signals
int_T nInputPorts = ssGetNumInputPorts(S);
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
int_T nu = ssGetInputPortWidth(S,0);
//ssPrintf("Port width: %d\n", nu);
int_T thrust = *uPtrs[0];
int_T roll = *uPtrs[1];
int_T pitch = *uPtrs[2];
int_T yaw = *uPtrs[3];
CCrazyflie *Copter = (CCrazyflie *) ssGetPWork(S)[1];
if(Copter->cycle()){
// Send command
Copter->setThrust(thrust);
Copter->setRoll(roll);
Copter->setPitch(pitch);
Copter->setYaw(yaw);
Copter->setSendSetpoints(true);
//ssPrintf("Sending command ---> thrust: %d, roll: %d, pitch: %d, yaw: %d\n",thrust, roll, pitch, yaw);
// Get sensor data
int tr = Copter->thrust();
float r = Copter->roll();
float p = Copter->pitch();
float y = Copter->yaw();
//ssPrintf("Sensor data ---> thrust: %d, roll: %F, pitch: %F, yaw: %F\n",tr, r, p, y);
//usleep(50);
//output[0] = Copter->thrust();
output[0] = Copter->roll();
output[1] = Copter->pitch();
output[2] = Copter->yaw();
output[3] = Copter->batteryLevel();
// usleep(100);
}
}
/* Function: mdlTerminate =================================================
* Abstract:
*
*
*/
static void mdlTerminate(SimStruct *S)
{
CCrazyRadio *radio = (CCrazyRadio *) ssGetPWork(S)[0];
//CCrazyflie *copter = (CCrazyflie *) ssGetPWork(S)[2];
delete radio;
//delete copter;
}
/*=============================*
* Required S-function trailer *
*=============================*/
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif