Forum: Mikrocontroller und Digitale Elektronik Code übersetzen


von gast (Gast)


Angehängte Dateien:

Lesenswert?

Gute Abend Forum,

ich wende mich mal an Euch da ich seit Tagen (Nächten) schon nicht 
weiter komme! Es geht um eine Codeübersetzung für die scheinbar mein 
Wissen nicht ausreicht. Dabei geht es um den Kalman Filter. Diesen habe 
ich als Basic-Quellcode im Netz gefunden:

http://www.elektor.de/forum/foren-ubersicht/foren-zu-elektor-projekten/elektorwheelie/kalman-filter.1510605.lynkx

Diesen versuche ich nun ins Bascom zu bringen. Ich habe schon die ganze 
Bascomhilfe abgegrast, doch der Code will und will nicht. Anbei die 
Datei des Basic-Codes und hier was ich bisher erreicht habe. Ich hoffe 
mir kann hier jemand weiter helfen.

Vielen Dank im Voraus…

1
$regfile = "m128def.dat"
2
$crystal = 10000000
3
$hwstack = 40
4
$swstack = 16
5
$framesize = 32
6
$baud = 9600
7
8
9
Dim Kalman_dt As Single
10
Dim Kalman_gyro_sensor As Single
11
Dim Kalman_adxl_sensor As Single
12
Dim Neigungswinkel As Single
13
Dim Buf_ad_gyro As Integer
14
Dim Buf_ad_adxl As Integer
15
Dim Ad_gyro As Integer
16
Dim Ad_adxl As Integer
17
Dim Gyro_zero As Integer
18
Dim Adxl_zero As Integer
19
20
Dim Gyro1dkalman_x_angle As Single
21
Dim Gyro1dkalman_x_bias As Single
22
Dim Gyro1dkalman_p_00 As Single
23
Dim Gyro1dkalman_p_01 As Single
24
Dim Gyro1dkalman_p_10 As Single
25
Dim Gyro1dkalman_p_11 As Single
26
Dim Gyro1dkalman_q_angle As Single
27
Dim Gyro1dkalman_q_gyro As Single
28
Dim Gyro1dkalman_r_angle As Single
29
30
Dim Wert As Single
31
32
Dim Y As Single
33
Dim S As Single
34
Dim K_0 As Single
35
Dim K_1 As Single
36
37
Dim Temp As Single
38
Dim Temp1 As Single
39
Dim Ausgabe As String * 4
40
41
42
Declare Sub Init_gyro1dkalman(byval Q_angle As Single , Byval Q_gyro As Single , Byval R_angle As Single)
43
Declare Sub Ars_predict(byval Dotangle As Single , Byval Dt As Single)
44
Declare Function Ars_update(byval Angle_m As Single) As Single
45
Declare Function Kalman_filter_auswertung(gyro As Single , Adxl As Single , Dt As Single) As Single
46
47
48
Config Lcdpin = Pin , Db4 = Portc.4 , Db5 = Portc.5 , Db6 = Portc.6 , Db7 = Portc.7 , E = Portc.1 , Rs = Portc.0
49
Config Lcd = 16 * 2
50
Cls
51
52
53
Config Adc = Single , Prescaler = 128 , Reference = Off
54
Start Adc
55
56
57
'Init
58
Call Init_gyro1dkalman(0.0001 , 0.0003 , 0.3)               'Kalman Filter
59
Kalman_dt = 0.0157                                          'time passed in s since last call
60
61
Gyro_zero = Getadc(5)
62
Adxl_zero = Getadc(2)
63
64
65
Do                                                          'Endlosschleife
66
67
   Ad_gyro = Getadc(5)
68
   Ad_adxl = Getadc(2)
69
70
   Buf_ad_gyro = Ad_gyro - Gyro_zero
71
   Buf_ad_adxl = Ad_adxl - Adxl_zero
72
73
   Kalman_gyro_sensor = Buf_ad_gyro * 1.4634135             ' /1025*3.0V/0.002
74
   Kalman_adxl_sensor = Buf_ad_adxl * 1.5139                ' /1025*3.0V/0.0019333 0,348V for 180° = 1,9333mV per °
75
76
   Neigungswinkel = Kalman_filter_auswertung(kalman_gyro_sensor , Kalman_adxl_sensor , Kalman_dt)
77
   Ausgabe = Fusing(neigungswinkel , "##.###")
78
79
   Cls
80
   Lcd Ausgabe
81
   Waitms 150
82
83
Loop
84
85
End
86
87
88
89
'#############################################################################
90
91
'######################## Kalman Filter - Datei ##############################
92
93
      'struct Gyro1DKalman
94
95
        '* These variables represent our state matrix x
96
97
98
    'End Structure
99
100
    '#######################################################################
101
102
    '***********************************************************************
103
    '*                                                                     *
104
    '* This file contains the code for the kalman filter that uses the     *
105
    '* sensor data as inputs.                                              *
106
    '*                                                                     *
107
    '***********************************************************************
108
    '*                                                                     *
109
    '*    Author:         Tom Pycke                                        *
110
    '*    Filename:       ars.c                                            *
111
    '*    Date:           17/10/2007                                       *
112
    '*    File Version:   1.00                                             *
113
    '*                                                                     *
114
    '***********************************************************************
115
    '*                                                                     *
116
    '* Comments:                                                           *
117
    '*   To help others to understand the kalman filter, I used one of     *
118
    '*   the most accessible sources with information on it:               *
119
    '*   http://en.wikipedia.org/wiki/Kalman_filter                        *
120
    '*   The code is split into 2 parts: a Predict function and an         *
121
    '*   Update function, just as the wikipedia page does.                 *
122
    '*   The model I used in the kalman filter is the following:           *
123
    '*   Our gyroscope measures the turnrate in degrees per second. This   *
124
    '*   is the derivative of the angle, called dotAngle. The bias is the  *
125
    '*   output of our gyro when the rotationrate is 0 degrees per second  *
126
    '*   (not rotating). Because of drift it changes over time.            *
127
    '*   So mathematically we just integrate the gyro (dt is timespan      *
128
    '*   since last integration):                                          *
129
    '*              angle = angle + (dotAngle - bias) * dt                 *
130
    '*   When we include the bias in our model, the kalman filter will     *
131
    '*   try to estimate the bias, thanks to our last input: the measured  *
132
    '*   angle. This is just an estimate and comes from the accelerometer. *
133
    '*   So the state used in our filter had 2 dimensions: [ angle, bias ] *
134
    '*   Jump to the functions to read more about the actual               *
135
    '*   implementation.                                                   *
136
    '*                                                                     *
137
    '***********************************************************************
138
139
Sub Init_gyro1dkalman(byval Q_angle As Single , Byval Q_gyro As Single , Byval R_angle As Single)
140
   Gyro1dkalman_q_angle = Q_angle
141
   Gyro1dkalman_q_gyro = Q_gyro
142
   Gyro1dkalman_r_angle = R_angle
143
End Sub
144
145
146
Sub Ars_predict(byval Dotangle As Single , Byval Dt As Single)
147
148
   'Gyro1dkalman_x_angle = Gyro1dkalman_x_angle +(dt *(dotangle - Gyro1dkalman_x_bias))
149
   Temp = Dotangle - Gyro1dkalman_x_bias
150
   Temp = Temp * Dt
151
   Gyro1dkalman_x_angle = Gyro1dkalman_x_angle + Temp
152
153
154
   'Gyro1dkalman_p_00 = Gyro1dkalman_p_00 +( -dt *(gyro1dkalman_p_10 + Gyro1dkalman_p_01) + Gyro1dkalman_q_angle * Dt)
155
   Temp = -dt
156
   Temp1 = Gyro1dkalman_p_10 + Gyro1dkalman_p_01
157
   Temp = Temp * Temp1
158
   Temp1 = Gyro1dkalman_q_angle * Dt
159
   Temp = Temp + Temp1
160
   Gyro1dkalman_p_00 = Gyro1dkalman_p_00 + Temp
161
162
163
   'Gyro1dkalman_p_01 = Gyro1dkalman_p_01 +( -dt * Gyro1dkalman_p_11)
164
   Temp = -dt
165
   Temp = Temp * Gyro1dkalman_p_11
166
   Gyro1dkalman_p_01 = Gyro1dkalman_p_01 + Temp
167
168
169
   'Gyro1dkalman_p_10 = Gyro1dkalman_p_10 +( -dt * Gyro1dkalman_p_11)
170
   Temp = -dt
171
   Temp = Temp * Gyro1dkalman_p_11
172
   Gyro1dkalman_p_10 = Gyro1dkalman_p_10 + Temp
173
174
175
176
   'Gyro1dkalman_p_11 = Gyro1dkalman_p_11 +( + Gyro1dkalman_q_gyro * Dt)
177
   Temp = Gyro1dkalman_q_gyro * Dt
178
   Gyro1dkalman_p_11 = Gyro1dkalman_p_11 + Temp
179
180
181
182
End Sub
183
184
185
Function Ars_update(byval Angle_m As Single) As Single
186
187
      Y = Angle_m - Gyro1dkalman_x_angle
188
      S = Gyro1dkalman_p_00 + Gyro1dkalman_r_angle
189
      K_0 = Gyro1dkalman_p_00 / S
190
      K_1 = Gyro1dkalman_p_10 / S
191
192
      'Gyro1dkalman_x_angle = Gyro1dkalman_x_angle +(k_0 * Y)
193
      Temp = K_0 * Y
194
      Gyro1dkalman_x_angle = Gyro1dkalman_x_angle + Temp
195
196
      'Gyro1dkalman_x_bias = Gyro1dkalman_x_bias +(k_1 * Y)
197
      Temp = K_1 * Y
198
      Gyro1dkalman_x_bias = Gyro1dkalman_x_bias + Temp
199
200
      'Gyro1dkalman_p_00 = Gyro1dkalman_p_00 -(k_0 * Gyro1dkalman_p_00)
201
      Temp = K_0 * Gyro1dkalman_p_00
202
      Gyro1dkalman_p_00 = Gyro1dkalman_p_00 - Temp
203
204
205
      'Gyro1dkalman_p_01 = Gyro1dkalman_p_01 -(k_0 * Gyro1dkalman_p_01)
206
      Temp = K_0 * Gyro1dkalman_p_01
207
      Gyro1dkalman_p_01 = Gyro1dkalman_p_01 - Temp
208
209
      'Gyro1dkalman_p_10 = Gyro1dkalman_p_10 -(k_1 * Gyro1dkalman_p_00)
210
      Temp = K_1 * Gyro1dkalman_p_00
211
      Gyro1dkalman_p_10 = Gyro1dkalman_p_10 - Temp
212
213
      'Gyro1dkalman_p_11 = Gyro1dkalman_p_11 -(k_1 * Gyro1dkalman_p_01)
214
      Temp = K_1 * Gyro1dkalman_p_01
215
      Gyro1dkalman_p_11 = Gyro1dkalman_p_11 - Temp
216
217
      Return Gyro1dkalman_x_angle                           'was mache ich hiermit???
218
219
End Function
220
221
Function Kalman_filter_auswertung(gyro As Single , Adxl As Single , Dt As Single) As Single
222
'----------------Kalman Filter----------------------------------
223
'ars_predict(Kalman_gyro_sensor, Kalman_dt)    'Kalman predict
224
'Neigungswinkel_Filter = ars_update(Kalman_adxl_sensor)
225
'---------------------------------------------------------------
226
227
    Call Ars_predict(gyro , Dt)                             'Kalman predict
228
    Wert = Ars_update(adxl)
229
230
    Return Wert                                             'und damit???
231
End Function

von Quengel (Gast)


Lesenswert?

In der "schöngeistigen" Literatur ist es ähnlich:

Wer nicht verstanden hat, was der Autor schrieb,
kann es auch nicht übersetzen...

von gast (Gast)


Lesenswert?

vielen Dank für die Hilfe...

von Bernd B. (Firma: BB) (berndb)


Lesenswert?

Hi Gast,

und Bascom sagt also "Ich will nicht" ? oder  ....... ?

Gruss Berndb

von gast (Gast)


Lesenswert?

Hi Bernd,

ich muss einfach nur wissen ob ich mit der Syntax richtig liege in 
Vergleich zu der Basicvariante. Die Variable "Ausgabe" ist bei mir immer 
00.00
Leider verstehe ich nicht warum und wende mich daher an dieses Forum.

Gruß
Andre

von ich (Gast)


Lesenswert?

Ja und ab welcher Zeile treten Abweichungen zu den theoretisch 
berechneten Werten auf?

Wenn der Neigungswinkel "richtig" ist, dann ist der Fehler lokalisiert

>Ausgabe = Fusing(neigungswinkel , "##.###")

>Cls
>Lcd Ausgabe


Ist damit fehlerhaft.

von gast (Gast)


Lesenswert?

die Theorie sollte eigentlich stimmen. Soweit meine Mathekenntnisse es 
zulassen habe ich es nachgerechtet. Problem ist halt dass ich nicht 
kontrollieren kann, da mir die Ausgabe fehlt. Ich gehe also stark von 
einem Syntaktischen Problem aus.
Ist die Werteübergabe „Single an String“ (Ausgabe) soweit richtig? Laut 
Bascom-Hilfe ist das OK, bin mir trotzdem nicht sicher weil Garnichts 
kommt.

Gruß
Andre

von Läubi .. (laeubi) Benutzerseite


Lesenswert?

gast schrieb:
> Ich gehe also stark von
> einem Syntaktischen Problem aus

Dann müsste dir der Compiler ja einen Syntax-Fehler melden...

von mue-c (Gast)


Lesenswert?

Der Compiler gibt ja auch Fehler aus.
Return mit etwas dahinter ist in Bascom nicht erlaubt.
Also ändere das mal in Zeile 217 und 230.
Dann starte den Simulator und gebe mal Werte vor und schau, ob die 
Berechnung das gleiche Ergebnis liefert, wie in VB.

von Wolfgang S. (wsm)


Lesenswert?

Ich suche auch gerade diesen Code.

Werde mich kommende Woche damit beschäftigen.

Schreib mir mal ne Mail.

Ggf. ist die Rückgabe des Funktionswertes am Schluss nicht korrekt.

Es gibt auch noche eine Alternative zu diesem Code. Ist eine 
Vereinfachung speziell für BASCOM. Funktioniert bei mir auch ganz 
passabel.

W.

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.