Can I include an ode45 in a for loop
Show older comments
I'm working on an LQR controller for an autonomous drone and I'm having problems with the ode45 solver. My code goes like this:
T = 20;
dt = 0.1;
X0 = zeros(12,1);
for i=0:dt:T
timeRange = i:dt:i+dt;
P = TrajectoryRef(X0);
U = Command_lqr(X0,P);
X = EquationSolver(U,X0,timeRange);
%since ode45 has an independent timestep i register the last value and sign it as the next input
X0 = X(size(X,1),:);
end
But i keep getting wrong values in the result.
12 Comments
Walter Roberson
on 17 Jul 2020
? It is not obvious to me where you are calling ode45 or where you would want to call ode45 ?
Mohamed Amine BELYAMANI
on 18 Jul 2020
Edited: Mohamed Amine BELYAMANI
on 18 Jul 2020
Walter Roberson
on 18 Jul 2020
You define
function EquationSolver(U,X0,timeRange)
but you have
X = EquationSolver(U,X0,timeRange);
Notice that the EquationSolver you defined does not return any outputs.
Mohamed Amine BELYAMANI
on 18 Jul 2020
Edited: Mohamed Amine BELYAMANI
on 18 Jul 2020
Walter Roberson
on 18 Jul 2020
Instead of
timeRange = i:dt:i+dt;
use
timeRange = i + [0, dt/2, dt];
Then because timeRange will be a vector of more than 2 items, the number of results that will be reported will be the same as the number of elements in the vector, and you can index the result at row 3 always (unless it hits a singularity.)
Mohamed Amine BELYAMANI
on 19 Jul 2020
Walter Roberson
on 19 Jul 2020
Is your general strategy along the line of
loop:
- use knowledge of your current state and your goals to run a short term ode prediction
- command the device
- get feedback from where it actually ended up (e.g. because of wind the flight might not have gone as planned)
You appear to have only three steps in your loop, but it seems to me that you would also prefer to have feedback from sensors so that you could predict the next necessary next step, and it is not obvious to me where that sensor step is. Or is the sensor information included with the information about where you actually end up after a period?
Mohamed Amine BELYAMANI
on 20 Jul 2020
Walter Roberson
on 20 Jul 2020
Please describe your current strategy, since we as outside observerse do not know what TrajectoryRef or Command_lqr do. At the moment it kind of looks to me as if you have steps in the wrong order. What is it that the output of the EquationSolver tells you?
I have not done any drone control work... I would kind of expect the outline to go like
- taking into account current position and velocities, pick a general target. This should probably involve path planning to avoid obstacles
- taking into account current position and velocities, pick a short-term target that you would hope to get to in one time-step. If your sensors and algorithms were really good this could include obstacle avoidance, but you need a lot more experience if you hope to be able to just barely miss a corner knowing that the wind pattern might be different on the other part. The first stage general target would generally try to stay out of "mistakes were made" range of obstacles.
- using the current position and velocities as some of the initial conditions, run an inverse kinematics or Boundary Value Problem to figure out what power to apply to which motors to achieve the short term destination
- apply the predicted needed powers
- get information back about where you ended up after the time step, including updated positions and velocities
- loop back
You seem to be doing a forward ODE rather than a BVP, and it is not clear to me why you are doing that.
Also, I think the ODE should always be run in time 0 to dt, not from elapsed time until now. You are doing corrections as you go, so at any timestep it was never the case that you ran a consistent set of original inputs that would have led you from the origin to where you are now if you had just run the ODE that long. You are now where you are now, and you want to make predictions about times relative to now.
It is possible that my general outline is not practical in the form stated, due to the time required to solve the ode, during which you have to keep flying. Ideally after giving the command you would keep computing unstead of just waiting doing nothing until the device reported back.
Because of the potential cost of running a BVP, I could imagine that it might be necessary in practice to use an approximation ODE that is easier / faster to solve but which perhaps does not take into account all terms, relying on the fact that the reports back about where you really got to imply you have to make corrections anyhow. The implication might be that in still air your simplified ODE might not reflect the most efficient way to fly, but that in changing wind conditions the model errors might be less than the errors imposed by outside force.
Mohamed Amine BELYAMANI
on 20 Jul 2020
Walter Roberson
on 20 Jul 2020
Hmmm... I do not know enough about how that class of drones are controlled. I presume that some of the controls have to do with angles of surfaces, and that other controls have to do with velocities of motors. But I do not know, for example, anything about slew rates, about how quickly controls can be changed, about whether it is necessary to keep track of current acceleration of a control because potentially the rate of change of acceleration might be limited. And I do not know anything about issues such as difficulty in getting a motor started again... I know that in some cases (at least for much larger devices) there are cases when starting a motor is hard enough that it can make sense to have a forward motor and a reverse motor both, with one running at idling speed and not enough to overcome the full power of the other motor, so that neither motor ever needs to stop... https://www.motioncontrolonline.org/content-detail.cfm/Motion-Control-News/Understanding-Backlash-and-Stiction/content_id/310
I have never worked with drones, so I do not know which of these things need to be taken into consideration in practice.
Mohamed Belyamani
on 27 Jul 2020
Edited: Mohamed Belyamani
on 27 Jul 2020
I'm looking into it as well, for now the ode solver is capable of achieving the takeoff of the drone without any problems, but the real issue lies in controlling the pitch and roll, so i'm looking into implementing BVP or InverseKinematic to see if there is any change.
About the path planning, since i'm working with ultrasonic sensors for now, is it possible to simulate or implement the sensors in a way that would detect obstacles along the way. I've started working with Simscape since I modeled my drone in SolidWorks and I saw fit to actually have the command as PWM. I know that the best solution is working with Gazebo using ROS but I couldn't get to install the software since it is kinda difficult (for me) to install it on Windows.
Answers (0)
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!