hey; I'm getting this error and don't understand why. hope someone can help...
Persistent variable 'time_k1' is undefined on some execution paths. Function 'MATLAB Function5' (#61.404.411), line 24, column 17: "time_k1" Launch diagnostic report.
here is my func:
function [theta,V,Vx,Vy] = meas(Acc,gyro,time)
persistent theta_k1 wz_k1 Vx_k1 Vy_k1 Ax_k1 Ay_k1 time_k1
wz = gyro(3);
Ax = Acc(1);
Ay = Acc(2);
if time<0.01
theta_k1 = 0;
wz_k1 = 0;
Vx_k1 = 0;
Vy_k1 = 0;
Ax_k1 = 0;
Ay_k1 = 0;
time_k1 = time;
theta = 0;
V = 0;
Vx = 0;
Vy = 0;
else
% Integration for theta:
dt = time - time_k1;
theta = theta_k1 + dt*(wz + wz_k1)/2;
% Integration for V:
Vx = Vx_k1 + dt*(Ax + Ax_k1)/2;
Vy = Vy_k1 + dt*(Ay + Ay_k1)/2;
V = sqrt(Vx^2+Vy^2);
% Store for next step:
theta_k1 = theta;
wz_k1 = wz;
Vx_k1 = Vx;
Vy_k1 = Vy;
Ax_k1 = Ax;
Ay_k1 = Ay;
end

Antworten (1)

Torsten
Torsten am 8 Jul. 2022
Bearbeitet: Torsten am 8 Jul. 2022

0 Stimmen

You get this error because you didn't assign a value to the variable "time" when you called "meas" or a value >= 0.01.
If you give values to Acc, gyro and time (as I can see, Acc is an array of size at least 2 and gyro is an array of size at least 3) and then call "meas", you will have no problems.

6 Kommentare

Dekel Mashiach
Dekel Mashiach am 8 Jul. 2022
These values come from a sensor measurement
Steven Lord
Steven Lord am 8 Jul. 2022
I don't think that's the root cause of the problem. Remember that if this function is called with a non-scalar value of time, all the elements of time must be less than 0.01 for that if statement's body to execute. If time is the variable over which you're integrating, integral will call your function with a vector of values for that variable unless you tell it your function is 'ArrayValued'.
Therefore the code that assigns time to the variable time_k1 doesn't execute but the code in the else does, which assumes time_k1 has been assigned a value. In MATLAB that may not be a problem, as the persistent variable will have been initialized to [], but from the formatting of the message I suspect you're trying to generate code (using MATLAB Coder?) and the code generation process can be stricter than MATLAB.
If you are calling integral the persistent variables are probably not the right approach to use. There's no guarantee that the time variable in a subsequent call will always contain values greater than the values of the time variable in subsequent calls, if integral needs to evaluate the integrand multiple times in an interval to accurately compute the value of the integral on that interval. If you show us the mathematical formula for the function you're trying to integrate we may be able to offer some guidance.
Dekel Mashiach
Dekel Mashiach am 9 Jul. 2022
Bearbeitet: Dekel Mashiach am 9 Jul. 2022
I have an mpu6050 sensor that gives acc and gyro I basically tried to do an integral on the acceleration and get velocity and in the same way perform an integral on the velocity and get an angle. I did the integral in time with the entry of clock time.. I don't know how to fix this error...
Torsten
Torsten am 9 Jul. 2022
Do you need to do the integrations in real time or can you save the data and make the evaluation after the process has finished ?
Dekel Mashiach
Dekel Mashiach am 9 Jul. 2022
I need to do the integrations in real time
Torsten
Torsten am 9 Jul. 2022
Bearbeitet: Torsten am 9 Jul. 2022
Change the line
if time<0.01
to
if time == 0
and call the function "meas" with time = 0 and arbitrary data for "Acc" and "gyro" before sensor data are being transfered to "meas".
Further in the "else" part, I think you will have to add the line
time_k1 = time
to store it for the next step.

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 8 Jul. 2022

Bearbeitet:

am 9 Jul. 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by