【问题标题】:How to correctly use ode45 function in MATLAB for differential drive robot?如何在 MATLAB 中正确使用 ode45 函数用于差动驱动机器人?
【发布时间】:2019-02-10 19:52:17
【问题描述】:

我正在尝试使用 ode45 确定差动驱动机器人的位姿 (x,y,theta)。我下面的代码解决了 x 位置,但我遇到了初始条件的问题。我将它设置为 0,因为在时间 0 机器人被假定在原点,但我得到一个错误。如何设置 ode45 的初始条件以获得预期的输出?

我试图通过将初始条件设置为 41x1 零矩阵来制作与 dxdt 长度相同的初始条件向量,但我不理解输出,我不相信我做得正确。

% Given conditions for a differential drive robot:
B = 20; % (cm) distance along axle between centers of two wheels
r = 10; % (cm) diameter of both wheels
w_l = 5*sin(3*t); % (rad/s) angular rate of left wheel
w_r = 5*sin(3*t); % (rad/s) angular rate of right wheel
v_l = r*w_l; % (cm/s) velocity of left wheel
v_r = r*w_r; % (cm/s) velocity of right wheel
v = (v_r+v_l)/B; % (cm/s) velocity of robot
theta = 90; % constant orientation of robot since trajectory is straight

% Solve differential equation for x:
dxdt = v*cos(theta); % diff equaition for x
tspan = [0 20]; % time period to integrate over
x0 = 0; % initial condition since robot begins at origin
[t,x] = ode45(@(t,y) dxdt, tspan, x0);

我想在初始条件为0 的情况下求解020 秒的微分方程dxdt。我希望输出给我一个从020 的时间向量和一个x 的数组。我认为问题在于初始条件。 MATLAB 在实时编辑器中给我一个错误,告诉我“@(t,y)dxdt returns a vector of length 69, but the length of initial conditions vector is 1. The vector returned by @(t,y)dxdt and the initial conditions vector must have the same number of elements.

【问题讨论】:

    标签: matlab ode robotics


    【解决方案1】:

    问题不是初始条件。您需要将dxdt 定义为函数,即

    % Define differential equation
    function derivative = dxdt(t,y)
    
    % Given conditions for a differential drive robot:
    B = 20; % (cm) distance along axle between centers of two wheels
    r = 10; % (cm) diameter of both wheels
    w_l = 5*sin(3*t); % (rad/s) angular rate of left wheel
    w_r = 5*sin(3*t); % (rad/s) angular rate of right wheel
    v_l = r*w_l; % (cm/s) velocity of left wheel
    v_r = r*w_r; % (cm/s) velocity of right wheel
    v = (v_r+v_l)/B; % (cm/s) velocity of robot
    theta = 90; % constant orientation of robot since trajectory is straight
    
    derivative = v*cos(theta); % diff equation for x
    
    end
    

    那么当你使用ode45 时,你应该告诉它把ty 变量作为参数传递给dxdt 之类的

    [t,x] = ode45(@(t,y) dxdt(t,y), tspan, x0);
    

    这应该可以工作。在这种情况下,dxdt 只接受默认参数,你也可以这样写

    [t,x] = ode45(@dxdt, tspan, x0);
    

    你得到的错误表明你在某个时候将dxdt 变成了一个长度为 69 的向量,而当 MATLAB 将一个 t 和一个 y 传递给你的dxdt'功能'。每当你遇到这样的错误时,我建议把

    clear all

    `clearvars` % better than clear all - see am304's comment below
    

    在脚本的顶部,以避免先前定义的变量污染您的工作区。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多