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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
| /**************************************************************************
* Simulation de la commande dynamique du robot Orthoglide *
* Génération de trajectoire en fonction du temps *
* *
* *
* *
***************************************************************************
* MENU PRINCIPAL *
**************************************************************************/
/* Spécification de la S-FUNCTION */
#define S_FUNCTION_NAME sim_generation_trajectoire_c
#define S_FUNCTION_LEVEL 2
/* Need to include simstruc.h for the definition of the SimStruct and
its associated macro definitions.*/
#include <math.h>
#include "simstruc.h"
#include "mex.h"
/* déclaration des paramètres*/
#define ki mxGetPr(ssGetSFcnParam( S, 0))
#define kp mxGetPr(ssGetSFcnParam( S, 1))
#define kv mxGetPr(ssGetSFcnParam( S, 2))
#define OI mxGetPr(ssGetSFcnParam( S, 3))
#define Qm mxGetPr(ssGetSFcnParam( S, 4))
#define Qp mxGetPr(ssGetSFcnParam( S, 5))
#define Traj mxGetPr(ssGetSFcnParam( S, 6))
#define alpha_0 mxGetPr(ssGetSFcnParam( S, 7))
#define alpha_f mxGetPr(ssGetSFcnParam( S, 8))
#define amplitude mxGetPr(ssGetSFcnParam( S, 9))
#define dx mxGetPr(ssGetSFcnParam( S, 10))
#define dy mxGetPr(ssGetSFcnParam( S, 11))
#define dz mxGetPr(ssGetSFcnParam( S, 12))
#define lbroche mxGetPr(ssGetSFcnParam( S, 13))
#define q0 mxGetPr(ssGetSFcnParam( S, 14))
#define temps_final mxGetPr(ssGetSFcnParam( S, 15))
#define tool mxGetPr(ssGetSFcnParam( S, 16))
#define wn mxGetPr(ssGetSFcnParam( S, 17))
#define NUM_DISC_STATES 9
#define NUM_CONT_STATES 0
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S, 18); /* Number of expected parameters */
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
return; /* Parameter mismatch will be reported by Simulink */
}
ssSetNumContStates(S, NUM_CONT_STATES);
ssSetNumDiscStates(S, NUM_DISC_STATES);
if (!ssSetNumInputPorts(S, 1)) return;
ssSetInputPortWidth(S, 0, 2);
/*
* Set direct feedthrough flag (1=yes, 0=no).
* A port has direct feedthrough if the input is used in either
* the mdlOutputs or mdlGetTimeOfNextVarHit functions.
* See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
*/
ssSetInputPortDirectFeedThrough(S, 0, 0);
if (!ssSetNumOutputPorts(S,1)) return;
ssSetOutputPortWidth(S, 0, 9);
ssSetNumSampleTimes(S, 1);
ssSetNumRWork(S, 0);
ssSetNumIWork(S, 0);
ssSetNumPWork(S, 0);
ssSetNumModes(S, 0);
ssSetNumNonsampledZCs(S, 0);
ssSetOptions(S, 0);
}
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
}
#define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function */
#if defined(MDL_INITIALIZE_CONDITIONS)
/* Function: mdlInitializeConditions ========================================
* Abstract:
* In this function, you should initialize the continuous and discrete
* states for your S-function block. The initial states are placed
* in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
* You can also perform any other initialization activities that your
* S-function may require. Note, this routine will be called at the
* start of simulation and if it is present in an enabled subsystem
* configured to reset states, it will be call when the enabled subsystem
* restarts execution to reset the states.
*/
static void mdlInitializeConditions(SimStruct *S)
{
}
#endif /* MDL_INITIALIZE_CONDITIONS */
#define MDL_START /* Change to #undef to remove function */
#if defined(MDL_START)
/* Function: mdlStart =======================================================
* Abstract:
* This function is called once at start of model execution. If you
* have states that should be initialized once, this is the place
* to do it.
*/
static void mdlStart(SimStruct *S)
{
}
#endif /* MDL_START */
static void sim_generation_trajectoire_c(SimStruct *S,const real_T t)
{
real_T var,var2,var3,var4,var5;
real_T res, R_Trj, phi, pi, gamma;
real_T pos_alpha, vit_alpha, acc_alpha;
real_T Px, Py, Pz, ppx, ppy, ppz, pppx, pppy, pppz;
real_T *state = ssGetRealDiscStates(S);
/************************** Trajectoire **********************************/
pi=3.14116;
// resolution
res=30;
//Rayon de la Trajectoire en métres [m]
R_Trj=150/1000;
//Angle entre l'axe X et le plan de la trajectoire
phi=0*pi/180;
mexPrintf("%f\n=",R_Trj);
//Angle entre v et l'axe Z
gamma=45*pi/180;
if (t<*temps_final)
{
var=(t)/(*temps_final);
var2=var*var;
var3=var2*var;
var4=var3*var;
var5=var4*var;
pos_alpha=((10.0*var3)-(15.0*var4)+(6.0*var5))*(*amplitude)+*alpha_0;
vit_alpha=((30.0*var2)-(60.0*var3)+(30.0*var4))/(*temps_final)*(*amplitude);
acc_alpha=((60.0*var)-(180.0*var2)+(120.0*var3))/(*temps_final*(*temps_final))*(*amplitude);
Px=*dx+(R_Trj-*lbroche*sin(gamma))*cos(pos_alpha);
Py=*dy+(R_Trj-*lbroche*sin(gamma))*sin(pos_alpha);
Pz=*dz+(*lbroche*cos(gamma));
//Vitesse du Point p
ppx=-(R_Trj-*lbroche*sin(gamma))*vit_alpha*sin(pos_alpha) ;
ppy=(R_Trj-*lbroche*sin(gamma))*vit_alpha*cos(pos_alpha);
ppz=-*lbroche*sin(gamma);
//Acceleration de Point p
pppx=-(R_Trj-*lbroche*sin(gamma))*(vit_alpha*vit_alpha*cos(pos_alpha)+acc_alpha*sin(pos_alpha));
pppy=-(R_Trj-*lbroche*sin(gamma))*(vit_alpha*vit_alpha*sin(pos_alpha)+acc_alpha*cos(pos_alpha));
pppz=-*lbroche*cos(gamma);
state[0]=Px;
state[1]=Py;
state[2]=Pz;
state[3]=ppx;
state[4]=ppy;
state[5]=ppz;
state[6]=pppx;
state[7]=pppy;
state[8]=pppz;
}
else
{
mexPrintf("%f\n=", "Ok");
Px=*dx+(R_Trj-*lbroche*sin(gamma))*cos(*alpha_f);
Py=*dy+(R_Trj-*lbroche*sin(gamma))*sin(*alpha_f);
Pz=*dz+(*lbroche*cos(gamma));
ppx=0;
ppy=0;
ppz=-*lbroche*sin(gamma);
pppx=0;
pppy=0;
pppz=-*lbroche*cos(gamma);
}
}
/*************************************************************************
* Fin du menu principal *
* /*************************************************************************/
/* Function: mdlOutputs =======================================================
* Abstract:
* In this function, you compute the outputs of your S-function
* block. Generally outputs are placed in the output vector(s),
* ssGetOutputPortSignal.
*
* Qcourant=[q,dq,ddq];
*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
real_T *Cart= ssGetOutputPortRealSignal(S,0);
const real_T t = ssGetT(S);
const real_T *state = ssGetRealDiscStates(S);
sim_generation_trajectoire_c(S, t);
Cart[0]=state[0];
Cart[1]=state[1];
Cart[2]=state[2];
Cart[3]=state[3];
Cart[4]=state[4];
Cart[5]=state[5];
Cart[6]=state[6];
Cart[7]=state[7];
Cart[8]=state[8];
//mexPrintf("%f\nCart=", Cart[0]);
}
/* Function: mdlTerminate =====================================================
* Abstract:
* In this function, you should perform any actions that are necessary
* at the termination of a simulation. For example, if memory was allocated
* in mdlStart, this is the place to free it.
*
*/
static void mdlTerminate(SimStruct *S)
{
}
#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 |
Partager