0
votes

I am trying to determine the pose (x,y,theta) of a differential drive robot using ode45. The code I have below solves for the x position, but I am having an issue with the initial condition. I set it to 0 since at time 0 the robot is assumed to be at the origin, but I get an error. How do I set the initial condition for ode45 such that I get the expected output?

I tried to make the initial conditions vector of the same length as dxdt by setting initial conditions as a 41x1 zeros matrix, but I didn't understand the output and I don't believe I did it correctly.

% 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);

I want to solve the differential equation dxdt for 0 to 20 seconds with an initial condition of 0. I expect the output to give me a vector of time from 0 to 20 and an array of for x. The problem I believe lies with the initial condition. MATLAB gives me an error in the live editor telling me, " @(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."

1

1 Answers

1
votes

The issue is not the initial condition. You need to define dxdt as a function i.e.

% 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

Then when you use ode45 you should tell it to pass the t and y variables as arguments to dxdt like

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

This should then work. In this case, as dxdt only takes the default arguments, you could also write

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

The error you got indicates that at some point you made dxdt into a vector of length 69, whilst MATLAB was expecting to get back 1 value for dxdt when it passed one t and one y to your dxdt 'function'. Whenever you get errors like this, I'd recommend putting

clear all

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

at the top of your script to avoid polluting your workspace with previously defined variables.