Forum: PC-Programmierung Mit Matlab USB Port auf Raspberry auslesen


von Julian F. (b0lle)


Lesenswert?

Hallo Mikrocontrollergemeinde,

ich werkle schon einige Zeit mit dem Raspberry. Neuerdings hat es mir 
der Zugriff über Matlab/Simulink auf den Pi angetan. Die Möglichkeiten 
die diese beiden Plattformen zusammen ergeben scheinen sehr vielfältig 
zu sein.
Jetzt hat sich mir aber folgendes Problem aufgetan:
Ich habe eine Maus am USB Port des Pi. Diese lese ich in einem C 
Programm aus und es funktioniert super, wenn ich den C-Code auf dem Pi 
direkt compiliere und auslese.
Ich bin mir aber nicht im klaren darüber, wie ich über Simulink diese 
USB Schnittstelle des Pi auslesen kann. Hat da jemand eine Idee?

Ich habe schon probiert eine S-Function in Simulink zu implementieren, 
die den USB Port ausliest. Leider stürzt das Programm umgehend ab, 
sobald die Funktion fread() ausgeführt wird.

Hier mein C-Code, um den USB Port mittels C auszulesen:
1
FILE *fmouse;
2
        char b[3];
3
        fmouse = fopen("/dev/input/mice","r");
4
        int xd=0,yd=0; //x/y movement delta
5
        int xo=0,yo=0; //x/y overflow (out of range -255 to +255)
6
        int run=0;   
7
     
8
    while(!run){
9
    
10
                fread(b,sizeof(char),3,fmouse);
11
                xo=(b[0]&64)>0;
12
                yo=(b[0]&128)>0;
13
                xd=b[1];
14
                yd=b[2];
15
                printf("xo=%d yo=%d xd=%d yd=%d\n",xo,yo,xd,yd);
16
         }
17
        fclose(fmouse);
18
}

und der Inhalt des S-Files - der ja eigentlich das Selbe machen sollte
1
/*
2
 * sfuntmpl_basic.c: Basic 'C' template for a level 2 S-function.
3
 *
4
 * Copyright 1990-2013 The MathWorks, Inc.
5
 */
6
7
8
/*
9
 * You must specify the S_FUNCTION_NAME as the name of your S-function
10
 * (i.e. replace sfuntmpl_basic with the name of your S-function).
11
 */
12
13
#define S_FUNCTION_NAME  odometrie
14
#define S_FUNCTION_LEVEL 2
15
16
/*
17
 * Need to include simstruc.h for the definition of the SimStruct and
18
 * its associated macro definitions.
19
 */
20
#include "simstruc.h"
21
 
22
#include <sys/types.h>                        
23
#include <sys/stat.h>
24
#include <fcntl.h>
25
//#include <termios.h>
26
#include <stdio.h>
27
#include <stdlib.h>
28
#include <string.h>
29
//#include <unistd.h>
30
#include <math.h>
31
32
33
34
static void mdlInitializeSizes(SimStruct *S)
35
{
36
    int_T nInputPorts  = 0;  /* number of input ports  */
37
    int_T nOutputPorts = 1;  /* number of output ports */
38
    int_T needsInput   = 0;  /* direct feed through    */
39
40
    int_T inputPortIdx  = 0;
41
    int_T outputPortIdx = 0;
42
    
43
 FILE *fmouse;
44
      char b[3];
45
46
    ssSetNumSFcnParams(S, 2);  /* Number of expected parameters */
47
    if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) 
48
        return;
49
    }
50
51
52
    ssSetNumContStates(    S, 0);   /* number of continuous states           */
53
    ssSetNumDiscStates(    S, 0);   /* number of discrete states             */
54
55
56
    if (!ssSetNumInputPorts(S, nInputPorts)) return;    
57
  
58
    if(!ssSetInputPortDimensionInfo(S, inputPortIdx, DYNAMIC_DIMENSION)) return;
59
   
60
    ssSetInputPortDirectFeedThrough(S, inputPortIdx, needsInput);
61
62
    
63
    if (!ssSetNumOutputPorts(S, nOutputPorts)) return;
64
65
    if(!ssSetOutputPortDimensionInfo(S,outputPortIdx,DYNAMIC_DIMENSION)) return;
66
67
    
68
    ssSetNumSampleTimes(   S, 1);   /* number of sample times                */
69
70
    /*
71
     * Set size of the work vectors.
72
     */
73
    ssSetNumRWork(         S, 0);   /* number of real work vector elements   */
74
    ssSetNumIWork(         S, 0);   /* number of integer work vector elements*/
75
    ssSetNumPWork(         S, 0);   /* number of pointer work vector elements*/
76
    ssSetNumModes(         S, 0);   /* number of mode work vector elements   */
77
    ssSetNumNonsampledZCs( S, 0);   /* number of nonsampled zero crossings   */
78
79
    ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
80
81
    ssSetOptions(          S, 0);   /* general options (SS_OPTION_xx)        */
82
83
} /* end mdlInitializeSizes */
84
85
86
87
static void mdlInitializeSampleTimes(SimStruct *S)
88
{
89
    ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
90
    ssSetOffsetTime(S, 0, 0.0);
91
92
}
93
94
95
96
#define MDL_INITIALIZE_CONDITIONS   /* Change to #undef to remove function */
97
#if defined(MDL_INITIALIZE_CONDITIONS)
98
99
  static void mdlInitializeConditions(SimStruct *S)
100
  {
101
  }
102
#endif /* MDL_INITIALIZE_CONDITIONS */
103
104
105
106
#define MDL_START  /* Change to #undef to remove function */
107
#if defined(MDL_START) 
108
 
109
  static void mdlStart(SimStruct *S)
110
  {
111
      FILE *fmouse;
112
      char b[3];
113
  }
114
#endif /*  MDL_START */
115
116
117
118
119
static void mdlOutputs(SimStruct *S, int_T tid)
120
{
121
    b[1] = u[0];
122
}
123
124
125
126
#define MDL_UPDATE  /* Change to #undef to remove function */
127
#if defined(MDL_UPDATE)
128
 
129
  static void mdlUpdate(SimStruct *S, int_T tid)
130
  {
131
      fread(b,sizeof(char),3,fmouse);
132
  }
133
#endif /* MDL_UPDATE */
134
135
136
137
#define MDL_DERIVATIVES  /* Change to #undef to remove function */
138
#if defined(MDL_DERIVATIVES)
139
  static void mdlDerivatives(SimStruct *S)
140
  {
141
  }
142
#endif /* MDL_DERIVATIVES */
143
144
145
146
static void mdlTerminate(SimStruct *S)
147
{
148
}
149
150
151
/*=============================*
152
 * Required S-function trailer *
153
 *=============================*/
154
155
#ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
156
#include "simulink.c"      /* MEX-file interface mechanism */
157
#else
158
#include "cg_sfun.h"       /* Code generation registration function */
159
#endif

Ich danke euch für jede Idee!

: Verschoben durch Admin
von M.K. B. (mkbit)


Lesenswert?

Ich vermute, dass das Problem daran liegt, dass fmouse nicht mit fopen 
geöffnet wurde und der Zugriff auf eine undefinierte Adresse 
fehlschlägt.


Ansonsten noch ein paar Anmerkungen zu deinem S-File:
(Ich hab mal die relevanten Stellen kopiert und kommentiert.)
1
static void mdlInitializeSizes(SimStruct *S)
2
{
3
    // Wir sind hier in einer Funktion, also sind beide Variablen nur lokal
4
    FILE *fmouse;
5
    char b[3];
6
7
}
8
9
static void mdlStart(SimStruct *S)
10
{
11
    // Siehe oben. Diese Variablen haben nichts mit den beiden oberen zu tun, trotz gleicher Namen.
12
    FILE *fmouse;
13
    char b[3];
14
}
15
16
static void mdlOutputs(SimStruct *S, int_T tid)
17
{
18
    // Wo sind b und u definiert?
19
    b[1] = u[0];
20
}
21
22
static void mdlUpdate(SimStruct *S, int_T tid)
23
{
24
    // Wo sind b und fmouse definiert?
25
    fread(b,sizeof(char),3,fmouse);
26
}

Sollte das ganze in S-Files sich anders verhalten, als in normalem C, 
dann bitte ich um Entschuldigung.

Sollten die Kommentare nicht klar sein, dann frag einfach.

von Julian F. (b0lle)


Lesenswert?

Hey Mk Bit,

vielen Dank für deinen Beitrag.
In S-Files funktioniert das tatsächlich anders als in normalen C-Files.
b und u sind zb die Ein- und Ausgabewerte.
Alles was in mdlInitializeSizes definiert wird, ist in allen anderen 
Routinen auch bekannt.

von M.K. B. (mkbit)


Lesenswert?

Danke Julian, das wusste ich noch nicht.

Dann sollte der Code wohl so passen, allerdings fehlt bei dir dann 
trotzdem noch ein fopen.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.