Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "ListenSocket.h"
#include "Integrator.h"
#include "MotorCtrl.h"
 
void ListenSocket::GetPtrs(void)
{
    extern Integrator * X4_Integrator;
    extern MotorCtrl * X4_MotorCtrl;
 
    ptIntegrator = X4_Integrator;
    ptMotorCtrl = X4_MotorCtrl;
    return;
}
 
double ListenSocket::getVit_d(int i)
{
    return Vit_d[i];
}
 
void ListenSocket::listen_on_sock(void)
 
{
    orcIntSigMask();
    /** Create CAN Socket/Communication Channel */
    if (ssyCANServerOpen(&nSockFd) == SSY_DEF_SOCK_ERROR)
    {
        printf("testCANClient: ssyCANClientCreate Failed errno = %d \n", errno);
        //return SSY_DEF_SOCK_ERROR;
    }
    sMsg[0]='I';
    nCommStatus = ssyCANSendMsg(nSockFd, sMsg, strlen(sMsg)+1, 0x15);
    if (nCommStatus == SSY_DEF_SOCK_ERROR)
    {
        printf("ssyCANSendMsg Failed errno = %d\n", errno);
        //    return SSY_DEF_SOCK_ERROR;
    }
    else
        printf("Connection ok\n");
 
    nCommStatus = ssyCANReceiveMsg(nSockFd, sMsg, SSY_DEF_BUFFERSIZE, &ident);
 
    printf("Init waiting connection Msg = %s \n", sMsg);
 
    while (!(end_int))
    { //while
        //do{ //do
        //wait socket
        /** Receive Msg from remote host */
        //printf("testUDPClient: ssyUDPReceiveMsg \n");
        nCommStatus = ssyCANReceiveMsg(nSockFd, sMsg, SSY_DEF_BUFFERSIZE, &ident);
        if (nCommStatus == SSY_DEF_SOCK_ERROR)
        {
            printf("testCANClient: ssyCANReceiveMsg Failed errno = %d\n", errno);
            //return SSY_DEF_SOCK_ERROR;
        }
        printf("testCANClient: Received Msg = %d from %d \n", sMsg[0], ident);
 
        orcMutexLock(ptIntegrator->ComputeLsoda);
        orcSemGive(ptIntegrator->SynchroLsoda);
        orcSemTake(ptIntegrator->LsodaFinished,ORCFOREVER);
 
        Time = ptIntegrator->GetTime();
        for (i=0;i<NEQ;i++) x[i] = ptIntegrator->getX(i);
        for (i=0;i<NMOT;i++) Volts[i] = ptMotorCtrl->getVolts(i);
 
        modelx4_volts(&neq, &Time, x, dot_x, Volts);
        for (i=0; i<17;i++) extended_state[i] = x[i];
        for (i=0; i<3;i++) extended_state[i+17] = dot_x[i+10];
        output(extended_state, b_m, wg_m, pos_m, vit_m, acc_m, vitmot);
 
        orcMutexUnlock(ptIntegrator->ComputeLsoda);
 
        switch (sMsg[0])
        { //switch
        case GETACC:
            for (i=0;i<3;i++) AccB[i] = dble2short(acc_m[i],GMAX);
           // sprintf(sMsg,"%hd %hd %hd",AccB[0],AccB[1],AccB[2]);
              for(i=0;i<=2;i++)
		{
		  /*sMsg[0]=(char)(AccB[i]>>8);
		    sMsg[1]=(char)AccB[i];
		    sMsg[2]=0x00;*/
		  sMsg[0]=0x12;
		  sMsg[1]=0x34;
		  sMsg[2]=0x00;
 
            nCommStatus = ssyCANSendMsg(nSockFd, sMsg, 2, 0xA0+i);
            if (nCommStatus == SSY_DEF_SOCK_ERROR)
            {
                printf("ssyCANSendMsg Failed errno = %d\n", errno);
                //    return SSY_DEF_SOCK_ERROR;
            }
		}
 
            break;
        case GETVMOT:
            fprintf(vitmot1,"%f %f \n",Time,vitmot[0]);
            fprintf(vitmot2,"%f %f \n",Time,vitmot[1]);
            fprintf(vitmot3,"%f %f \n",Time,vitmot[2]);
            fprintf(vitmot4,"%f %f \n",Time,vitmot[3]);
 
            for (i=0;i<4;i++) VmotB[i] = dble2short(vitmot[i],VITMOTMAX);
          //  sprintf(sMsg,"%hd %hd %hd %hd",VmotB[0],VmotB[1],VmotB[2],VmotB[3]);
          sMsg[0]=(char)(VmotB[i]>>8);
	  sMsg[1]=(char)VmotB[i];
	  sMsg[2]=0x00; 
         nCommStatus = ssyCANSendMsg(nSockFd, sMsg, strlen(sMsg)+1, 0x20+i);
            if (nCommStatus == SSY_DEF_SOCK_ERROR)
            {
                printf("ssyCANSendMsg Failed errno = %d\n", errno);
                //    return SSY_DEF_SOCK_ERROR;
            }
            break;
        case GETMAG:
           // for (i=0;i<3;i++) MagB[i] = dble2short(b_m[i],MAGMAX);
          //  sprintf(sMsg,"%hd %hd %hd,",MagB[0],MagB[1],MagB[2]);
 
for(i=0;i<=2;i++)
{
 
sMsg[0]=(char)(MagB[i]>>8);
sMsg[1]=(char)MagB[i];
sMsg[2]=0x00;
            nCommStatus = ssyCANSendMsg(nSockFd, sMsg, strlen(sMsg)+1, 0x30+i);
            if (nCommStatus == SSY_DEF_SOCK_ERROR)
            {
                printf("ssyCANSendMsg Failed errno = %d\n", errno);
                //    return SSY_DEF_SOCK_ERROR;
            }
 }
            break;
        case GETGYRO:
            fprintf(qp1,"%f %f \n",Time,wg_m[0]);
            fprintf(qp2,"%f %f \n",Time,wg_m[1]);
            fprintf(qp3,"%f %f \n",Time,wg_m[2]);
            fprintf(q1,"%f %f \n",Time,extended_state[0]);
            fprintf(q2,"%f %f \n",Time,extended_state[1]);
            fprintf(q3,"%f %f \n",Time,extended_state[2]);
            fprintf(q4,"%f %f \n",Time,extended_state[3]);
 
            for (i=0;i<3;i++) GyroB[i] = dble2short(wg_m[i],WMAX);
            sprintf(sMsg,"%hd %hd %hd,",GyroB[0],GyroB[1],GyroB[2]);
            nCommStatus = ssyCANSendMsg(nSockFd, sMsg, strlen(sMsg)+1, 0x40+i);
            if (nCommStatus == SSY_DEF_SOCK_ERROR)
            {
                printf("ssyCANSendMsg Failed errno = %d\n", errno);
                //    return SSY_DEF_SOCK_ERROR;
            }
            break;
        case GETUS:
            break;
        case GETPRES:
            break;
        case GETGPS:
            /*fprintf(x_pos,"%f %f \n",Time,pos_m[0]);
            fprintf(y_pos,"%f %f \n",Time,pos_m[1]);
            fprintf(z_pos,"%f %f \n",Time,pos_m[2]);
 
            for (i=0;i<3;i++) GPSB[i] = dble2short(pos_m[i],DISTMAX);
            sprintf(sMsg,"%hd %hd %hd,",GPSB[0],GPSB[1],GPSB[2]);
            nCommStatus = ssyCANSendMsg(nSockFd, sMsg, strlen(sMsg)+1, CTRL_IP, nPortClient);
            if (nCommStatus == SSY_DEF_SOCK_ERROR)
            {
                printf("ssyCANSendMsg Failed errno = %d\n", errno);
                //    return SSY_DEF_SOCK_ERROR;
            }*/
            break;
        case GETVREL:
            fprintf(dx,"%f %f \n",Time,vit_m[0]);
            fprintf(dy,"%f %f \n",Time,vit_m[1]);
            fprintf(dz,"%f %f \n",Time,vit_m[2]);
 
            for (i=0;i<3;i++) VrelB[i] = dble2short(vit_m[i],VMAX);
            sprintf(sMsg,"%hd %hd %hd",VrelB[0],VrelB[1],VrelB[2]);
            nCommStatus = ssyCANSendMsg(nSockFd, sMsg, strlen(sMsg)+1, 0x80+i);
            if (nCommStatus == SSY_DEF_SOCK_ERROR)
            {
                printf("ssyCANSendMsg Failed errno = %d\n", errno);
                //    return SSY_DEF_SOCK_ERROR;
            }
            break;
        case PUTVI:
            sscanf(sMsg,"%c %hd %hd %hd %hd",&val,&Vit_dB[0],&Vit_dB[1],&Vit_dB[2],&Vit_dB[3]);
            for (i=0;i<4;i++) Vit_d[i] = short2dble(Vit_dB[i],VITMOTMAX);
 
            fprintf(vmd1,"%f %f \n",Time,Vit_d[0]);
            fprintf(vmd2,"%f %f \n",Time,Vit_d[1]);
            fprintf(vmd3,"%f %f \n",Time,Vit_d[2]);
            fprintf(vmd4,"%f %f \n",Time,Vit_d[3]);
            break;
        case 'E':
            printf("received 'E' \n");
            end_int = 1;
            clean_exit(end_int);
            continue;
        default:
            break;
        }
    } //while(!end_int);
    printf("LISTENSOCKET thread finished \n");
}
 
void ListenSocket::open_files(void)
{
    strcpy(qf1 , "./q1") ;
    strcpy(qf2 , "./q2") ;
    strcpy(qf3 , "./q3") ;
    strcpy(qf4 , "./q4") ;
    strcpy(qpf1 , "./qp1") ;
    strcpy(qpf2 , "./qp2") ;
    strcpy(qpf3 , "./qp3") ;
//strcpy(thrf , "./thr") ;
    strcpy(vmdf1 , "./vmd1") ;
    strcpy(vmdf2 , "./vmd2") ;
    strcpy(vmdf3 , "./vmd3") ;
    strcpy(vmdf4 , "./vmd4") ;
    strcpy(vitmotf1 , "./vitmot1") ;
    strcpy(vitmotf2 , "./vitmot2") ;
    strcpy(vitmotf3 , "./vitmot3") ;
    strcpy(vitmotf4 , "./vitmot4") ;
    strcpy(xf , "./x_pos") ;
    strcpy(yf , "./y_pos") ;
    strcpy(zf , "./z_pos") ;
    strcpy(dxf , "./dx") ;
    strcpy(dyf , "./dy") ;
    strcpy(dzf , "./dz") ;
 
    q1 = fopen(qf1,"w") ;
    q2 = fopen(qf2,"w") ;
    q3 = fopen(qf3,"w") ;
    q4 = fopen(qf4,"w") ;
    qp1 = fopen(qpf1,"w") ;
    qp2 = fopen(qpf2,"w") ;
    qp3 = fopen(qpf3,"w") ;
//thr = fopen(thrf,"w");
    vmd1 = fopen(vmdf1,"w") ;
    vmd2 = fopen(vmdf2,"w") ;
    vmd3 = fopen(vmdf3,"w") ;
    vmd4 = fopen(vmdf4,"w") ;
    vitmot1 = fopen(vitmotf1,"w") ;
    vitmot2 = fopen(vitmotf2,"w") ;
    vitmot3 = fopen(vitmotf3,"w") ;
    vitmot4 = fopen(vitmotf4,"w") ;
    x_pos = fopen(xf,"w") ;
    y_pos = fopen(yf,"w") ;
    z_pos = fopen(zf,"w") ;
    dx = fopen(dxf,"w") ;
    dy = fopen(dyf,"w") ;
    dz = fopen(dzf,"w") ;
 
    fprintf(q1,"%f %f \n",Time,extended_state[0]);
    fprintf(q2,"%f %f \n",Time,extended_state[1]);
    fprintf(q3,"%f %f \n",Time,extended_state[2]);
    fprintf(q4,"%f %f \n",Time,extended_state[3]);
    /* fprintf(q2,"%f %f \n",Time,rtl[0]); */
    /* fprintf(q3,"%f %f \n",Time,rtl[1]); */
    /* fprintf(q4,"%f %f \n",Time,rtl[2]); */
    fprintf(qp1,"%f %f \n",Time,extended_state[4]);
    fprintf(qp2,"%f %f \n",Time,extended_state[5]);
    fprintf(qp3,"%f %f \n",Time,extended_state[6]);
//fprintf(thr,"%f %f \n",Time,D_attitude[2]);
    fprintf(vitmot1,"%f %f \n",Time,extended_state[13]);
    fprintf(vitmot2,"%f %f \n",Time,extended_state[14]);
    fprintf(vitmot3,"%f %f \n",Time,extended_state[15]);
    fprintf(vitmot4,"%f %f \n",Time,extended_state[16]);
    fprintf(x_pos,"%f %f \n",Time,extended_state[7]);
    fprintf(y_pos,"%f %f \n",Time,extended_state[8]);
    fprintf(z_pos,"%f %f \n",Time,extended_state[9]);
    fprintf(dx,"%f %f \n",Time,extended_state[10]);
    fprintf(dy,"%f %f \n",Time,extended_state[11]);
    fprintf(dz,"%f %f \n",Time,extended_state[12]);
    return;
}
 
void ListenSocket::close_files(void)
{
 
    fclose(q1);
    fclose(q2);
    fclose(q3);
    fclose(q4);
    fclose(qp1);
    fclose(qp2);
    fclose(qp3);
    //fclose(thr);
    fclose(vmd1);
    fclose(vmd2);
    fclose(vmd3);
    fclose(vmd4);
    fclose(vitmot1);
    fclose(vitmot2);
    fclose(vitmot3);
    fclose(vitmot4);
    fclose(x_pos);
    fclose(y_pos);
    fclose(z_pos);
    fclose(dx);
    fclose(dy);
    fclose(dz);
    return;
}
 
void * ListenSocket::thread_create_listen_on_sock(void * i)
{
    ListenSocket* listensocket = static_cast<ListenSocket*> (i);
    listensocket->listen_on_sock();
}
 
void ListenSocket::StartThread()
{
 
    GetPtrs();
    if (orcSpawn(&(ListenSocketThread),
                 "ListenSocket",
                 PRI_CLK-2,
                 NORMAL_STACK,
                 (SOSO) thread_create_listen_on_sock,
                 static_cast<void*> (this)) == ERROR)
        printf("ERROR orcspawn ListenSocket");
}
 
ListenSocket::ListenSocket()
{
    printf("ListenSocket constructor \n");
    orcIntSigMask();
    end_int = 0;
    neq = NEQ;
    open_files();
    nPort = SOCK_PORT;
    //nPortClient = SOCK_PORT;
    return;
}
ListenSocket::~ListenSocket()
{
  printf("closing files \n");
    close_files();
 
//   /** Close CAN Socket */
    printf("testCANClient: Close CAN Socket\n");
    if (ssyCANClose(nSockFd) == SSY_DEF_SOCK_ERROR)
    {
        printf("testCANClient: ssyCANClose Failed errno = %d\n", errno);
        //return SSY_DEF_SOCK_ERROR;
    }
    // if (orcTaskIdVerify(ListenSocketThread) == OK)
//     {
//         printf("deleting ListenSocket \n");
//         orcTaskDelete(ListenSocketThread);
//     } else printf("ListenSocket not OK \n");
//     printf("ListenSocket deleted \n");
    return;
}

voilà tout le programme, quand j'exécute ça bloque au niveau de la ligne :



Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
      printf("Connection ok\n");
 
    nCommStatus = ssyCANReceiveMsg(nSockFd, sMsg, SSY_DEF_BUFFERSIZE, &ident);