I'm trying to capture the output from a sensor attached my BeagleBoard xM while running the compiled code externally on the hardware from Simulink. I have the output being piped to a scope and I get a single integer value of '2' that displays on the scope.
function y = fcn()
%#codegen
persistent firstTime;
persistent pf;
persistent g;
persistent z;
z = '0';
g='0';
if isempty(firstTime)
firstTime = 0;
pf = coder.opaque('FILE *');
if isequal(coder.target, 'rtw')
cmd1 = c_string('i2cset -y 2 0x53 0x2D 0x8');
coder.ceval('system', coder.rref(cmd1));
end
end
y = coder.nullcopy(uint8(0));
res = coder.nullcopy(uint8(zeros(1, 10)));
readOnly = c_string('r');
readCmd = c_string('echo 3');
cmd2 = c_string('echo 3');
if isequal(coder.target, 'rtw')
pf = coder.ceval('popen', coder.rref(readCmd), coder.rref(readOnly));
coder.ceval('fgets', coder.wref(res), 10, pf);
g = coder.ceval('printf', c_string('%s'), c_string(res));
y = uint8(g);
coder.ceval('pclose', pf);
end
end
function str = c_string(str)
str = [str, 0];
end
Related
In the Simulink model below my interpreted function output is a vector with 38 elements. I have two functions that have the same outputs one of them works perfectly (desiredtrajectory_sim.m) but the other one doesn't (desiredtrajectory.m).
Any suggestions. thanks
Here is the Simulink model
function [desired_state] = desiredtrajectory_sim(in)
t = in(1);
Sf = [ 1; 2; pi/4];
dSf = [0;0;0];
Pf = [ 0.1*t; 0; 0.5*sin(0.03*pi*t) + 2; 0; 0.01*pi*t ; 0];
dPf = [ 0.1; 0; 0.5*0.03*pi*cos(0.03*pi*t); 0; 0.01*pi; 0];
pf = Sf(1); qf = Sf(2); betaf = Sf(3);
xf = Pf(1); yf = Pf(2); zf = Pf(3);
phif = Pf(4); thetaf = Pf(5); psif = Pf(6);
rf = sqrt(pf^2 + qf^2 - 2*pf*qf*cos(betaf));
h1 = sqrt(0.5*(pf^2 + qf^2 - 0.5*rf^2));
h2 = sqrt(0.5*(rf^2 + pf^2 - 0.5*qf^2));
h3 = sqrt(0.5*(qf^2 + rf^2 - 0.5*pf^2));
alpha1 = acos((4*(h1^2+h2^2)-9*pf^2)/(8*h1*h2));
alpha2 = acos((4*(h1^2+h3^2)-9*qf^2)/(8*h1*h3));
Rot = RPYtoRot_ZXY(phif, thetaf, psif);
r1 = Rot*[2/3*h1;0;0];
r2 = Rot*[2/3*h2*cos(alpha1);2/3*h2*sin(alpha1);0];
r3 = Rot*[2/3*h3*cos(alpha2);-2/3*h3*sin(alpha2);0];
pos_des1 = [xf;yf;zf] + r1;
pos_des2 = [xf;yf;zf] + r2;
pos_des3 = [xf;yf;zf] + r3;
omega = [0 -sin(psif) cos(thetaf)*cos(psif);...
0 -cos(psif) cos(thetaf)*sin(psif);...
1 0 -sin(thetaf)]*dPf(4:6);
vel_des1 = dPf(1:3) + cross(omega, r1);
vel_des2 = dPf(1:3) + cross(omega, r2);
vel_des3 = dPf(1:3) + cross(omega, r3);
acc_des = [0;0;0];
desired_state1 = [pos_des1;vel_des1;acc_des];
desired_state2 = [pos_des2;vel_des2;acc_des];
desired_state3 = [pos_des3;vel_des3;acc_des];
desired_state = [desired_state1;desired_state2;desired_state3; psif; 0; Pf;
Sf]
size(desired_state)
end
Here is the Simulink block and the error message
As you can notice the bus gives just one element compared to the previous one which gives 38 elements, although they have the same output.
function [desired_state] = desiredtrajectory(in)%(t, pos)
tm= in(1)
pos = in(2:10);
syms t xf yf zf phif thetaf psif pf qf betaf
rf = sqrt(pf^2+qf^2-2*pf*qf*cos(betaf));
h1 = sqrt(0.5*(pf^2+qf^2-0.5*rf^2));
h2 = sqrt(0.5*(rf^2+pf^2-0.5*qf^2));
h3 = sqrt(0.5*(qf^2+rf^2-0.5*pf^2));
alf1 = acos((4*(h1^2+h2^2)-9*pf^2)/(8*h1*h2));
alf2 = acos((4*(h1^2+h3^2)-9*qf^2)/(8*h1*h3));
Rot = RPYtoRot_ZXY(phif, thetaf, psif);
eps = [Rot*[2/3;0;0]+[xf;yf;zf]
Rot*[2/3*h2*cos(alf1);2/3*h2*sin(alf1);0]+[xf;yf;zf]
Rot*[2/3*h2*cos(alf2);-2/3*h3*sin(alf2);0]+[xf;yf;zf]];
X = [ xf yf zf phif thetaf psif pf qf betaf];
Sf = [ 1; 2; pi/4];
dSf = [0;0;0];
Pf = [ 0.1*t; 0; 0.5*sin(0.03*pi*t) + 2; 0; 0.01*pi*t ; 0];
dPf = [ 0.1; 0; 0.5*0.03*pi*cos(0.03*pi*t); 0; 0.01*pi; 0];
qd = [Pf; Sf];
qddot = [dPf; dSf];
jac = jacobian(eps,X);
%%%%%%%%%%%%%
pf = Sf(1); qf = Sf(2); betaf = Sf(3);
xf = Pf(1); yf = Pf(2); zf = Pf(3);
phif = Pf(4); thetaf = Pf(5); psif = Pf(6);
x1=pos(1);
y1=pos(2);
z1=pos(3);
x2=pos(4);
y2=pos(5);
z2=pos(6);
x3=pos(7);
y3=pos(8);
z3=pos(9);
qpf=[(x1+x2+x3)/3;...
(y1+y2+y3)/3;...
(z1+z2+z3)/3;...
atan2((2*z1/3-z2/3-z3/3),(2*y1/3-y2/3-y3/3)); ...
-atan2((2*z1/3-z2/3-z3/3),(2*x1/3-x2/3-x3/3)); ...
atan2((2*y1/3-y2/3-y3/3),(2*x1/3-x2/3-x3/3))];
qsf=[sqrt((x1-x2)^2+(y1-y2)^2+(z1-z2)^2); ...
sqrt((x1-x3)^2+(y1-y3)^2+(z1-z3)^2); ...
acos((pf^2+qf^2-rf^2)/(2*pf*qf))];
q = [qpf;qsf];
%%%%%%%%%%%%%
%%%pos_desired%%%%%%%
pos_des = eval(eps);
pos_des =subs(pos_des,t,tm);
jacval = eval(jac);
qd = eval(qd);%subs(qd,t,tm);
q = eval(q);
qe = qd - q;
qddot = eval(qddot);%subs(qddot,t,tm);
kappa=0.2*eye(9);
qrefdot = qddot + kappa*qe;
vel_des = jacval*qrefdot;
vel_des = subs(vel_des,t,tm);
acc_des = zeros(3,1);
yaw = 0;
yawdot = 0;
% =================== Your code ends here ===================
desired_state1 = [pos_des(1:3);vel_des(1:3);acc_des];
desired_state2 = [pos_des(4:6);vel_des(4:6);acc_des];
desired_state3 = [pos_des(7:9);vel_des(7:9);acc_des];
Pf = subs(Pf,t,tm);
Sf = subs(Sf,t,tm);
format short
digits(3);
desired_state = vpa([desired_state1;desired_state2;desired_state3; psif; 0;
Pf; Sf])
size(desired_state)
end
The second image shows that the output of the second function is a scalar - the dimension at the output of the block is 1 - not 38 as you believe it is.
That is, your functions do not give the same output as you believe they do.
The error is because the Selector blocks expect their inputs to be dimension 38, and they are not.
To determine why what you believe is happening is not actually what is happening, you could use the editor to set a break point in the m-code, run the model, then step through the code to determine why it gives a scalar output when you expect it to do otherwise.
Another approach would be to run your functions from the MATLAB Command Line with fake input data. Something like
tmp = desiredtrajectory(randn(10,1))
would be appropriate here.
The answer is that desiredtrajectory outputs desired_state, which is a symbolic variable. Yes it contains a 38 element vector, but Simulink treats the object itself is a scalar.
The real problem though is that you can't propagate a symbolic variable down a Simulink signal. You need the output to be a numeric vector.
One way to overcome this is to put the line
desired_state = double(desired_state);
at the end of your file to cast the symbolic object to a double, which will have 38 elements.
(However it's not clear why you are using symbolic math in the first place, and I'd suggest it would be better, and is certainly more efficient, if you didn't use it.)
I am attempting to write a function called expSeries which uses another function factFunc to evaluate e^x. I have already written the function factFunc, as shown below:
function fact = factFunc(n)
f = 1;
for a = 1:b
f = f*a;
end
fact = f;
end
I am now attempting to write the function expSeries which evaulates e^x using the Taylor series. This is what I have so far:
function expo = exponentialFunc(x)
terms = input('Enter the number of terms');
b = 0;
for i = 1:terms
b = x/factFunc(terms);
end
expo = b;
end
And in the main program, I have
n = exponentialFunc(4);
disp(n);
Where in this instance I am trying to find e^4. However, the output is not what expected. Does anyone have any idea where I am going wrong?
Fix to factFunc:
function fact = factFunc(n)
f = 1;
for a = 1:n
f = f*a;
end
fact = f;
end
Fix to exponentialFunc
function expo = exponentialFunc(x)
terms = input('Enter the number of terms');
b = 0;
for i = 0:terms-1
b = b + x^i/factFunc(i);
end
expo = b;
end
Example
>> exponentialFunc(4)
Enter the number of terms10
ans =
54.1541
Note exp(4) = 54.59815...
I have implemented 3 function for neural network regression:
1) a forward propagation function that given the training inputs and the net structure calculates the predicted output
function [y_predicted] = forwardProp(Theta,Baias,Inputs,NumberOfLayers,RegressionSwitch)
for i = 1:size(Inputs{1},2)
Activation = (Inputs{1}(:,i))';
for j = 2:NumberOfLayers - RegressionSwitch
Activation = 1./(1+exp(-(Activation*Theta{j-1} + Baias{j-1})));
end
if RegressionSwitch == 1
y_predicted(:,i) = Activation*Theta{end} + Baias{end};
else
y_predicted(:,i) = Activation;
end
end
end
2) a cost function that given the predicted and the desired output, calculates the cost of the network
function [Cost] = costFunction(y_predicted, y, Theta, Baias, Lambda)
Cost = 0;
for j = 1:size(y,2)
for i = 1:size(y,1)
Cost = Cost +(((y(i,j) - y_predicted(i,j))^2)/size(y,2));
end
end
Reg = 0;
for i = 1:size(Theta, 2)
for j = 1:size(Theta{i}, 1)
for k = 1:size(Theta{i}, 2)
Reg = Reg + (Theta{i}(j,k))^2;
end
end
end
for i = 1:size(Baias, 2)
for j = 1:length(Baias{i})
Reg = Reg + (Baias{i}(j))^2;
end
end
Cost = Cost + (Lambda/(2*size(y,2)))*Reg;
end
3) a back propagation function that calculates the partial derivative of the cost function for each weight in the network
function [dTheta, dBaias] = Deltas(Theta,Baias,Inputs,NumberOfLayers,RegressionSwitch, Epsilon, Lambda, y)
for i = 1:size(Theta,2)
for j = 1:size(Theta{i},1)
for k = 1:size(Theta{i},2)
dTp = Theta;
dTm = Theta;
dTp{i}(j,k) = dTp{i}(j,k) + Epsilon;
dTm{i}(j,k) = dTm{i}(j,k) - Epsilon;
y_predicted_p = forwardProp(dTp,Baias,Inputs,NumberOfLayers,RegressionSwitch);
y_predicted_m = forwardProp(dTm,Baias,Inputs,NumberOfLayers,RegressionSwitch);
Cost_p = costFunction(y_predicted_p, y, dTp, Baias, Lambda);
Cost_m = costFunction(y_predicted_m, y, dTm, Baias, Lambda);
dTheta{i}(j,k) = (Cost_p - Cost_m)/(2*Epsilon);
end
end
end
for i = 1:size(Baias,2)
for j = 1:length(Baias{i})
dBp = Baias;
dBm = Baias;
dBp{i}(j) = dTp{i}(j) + Epsilon;
dBm{i}(j) = dTm{i}(j) - Epsilon;
y_predicted_p = forwardProp(Theta,dBp,Inputs,NumberOfLayers,RegressionSwitch);
y_predicted_m =forwardProp(Theta,dBm,Inputs,NumberOfLayers,RegressionSwitch);
Cost_p = costFunction(y_predicted_p, y, Theta, dBp, Lambda);
Cost_m = costFunction(y_predicted_m, y, Theta, dBm, Lambda);
dBaias{i}(j) = (Cost_p - Cost_m)/(2*Epsilon);
end end end
I train the neural network with data from an exact mathematical function of the inputs.
The gradient descent seems to work as the cost decrease each iteration, but when i test the trained network the regression is terrible.
The functions are not meant to be efficient, but they should work so I am really frustrated to see they don't... The main function and the data are ok so the problem should be here. Can you please help me to spot it?
here is the "main":
clear;
clc;
Nodes_X = 5;
Training_Data = 1000;
x = rand(Nodes_X, Training_Data)*3;
y = zeros(2,Training_Data);
for j = 1:Nodes_X
for i = 1:Training_Data
y(1,i) = (x(1,i)^2)+x(2,i)-x(3,i)+2*x(4,i)/x(5,i);
y(2,i) = (x(5,i)^2)+x(2,i)-x(3,i)+2*x(4,i)/x(1,i);
end
end
vx = rand(Nodes_X, Training_Data)*3;
vy = zeros(2,Training_Data);
for j = 1:Nodes_X
for i = 1:Training_Data
vy(1,i) = (vx(1,i)^2)+vx(2,i)-vx(3,i)+2*vx(4,i)/vx(5,i);
vy(2,i) = (vx(5,i)^2)+vx(2,i)-vx(3,i)+2*vx(4,i)/vx(1,i);
end
end
%%%%%%%%%%%%%%%%%%%%%%ASSIGN NODES TO EACH LAYER%%%%%%%%%%%%%%%%%%%%%%%%%%%
NumberOfLayers = 4;
Nodes(1) = 5;
Nodes(2) = 10;
Nodes(3) = 10;
Nodes(4) = 2;
if length(Nodes) ~= NumberOfLayers || (Nodes(1)) ~= size(x, 1)
WARNING = msgbox('Nodes assigned incorrectly!');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%INITIALIZATION%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1:NumberOfLayers-1
Theta{i} = rand(Nodes(i),Nodes(i+1));
Baias{i} = rand(1,Nodes(i+1));
end
Inputs{1} = x;
Outputs{1} = y;
RegressionSwitch = 1;
Lambda = 10;
Epsilon = 0.00001;
Alpha = 0.01;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%TRAINING%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Epoch = 0;
figure;
hold on;
while Epoch <=20
%%%%%%%%%%%%%%%%%%%%FORWARD PROPAGATION%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
y_predicted = forwardProp(Theta,Baias,Inputs,NumberOfLayers,RegressionSwitch);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%COST%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Cost = costFunction(y_predicted, y, Theta, Baias, Lambda);
scatter(Epoch,Cost);
pause(0.01);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%BACK PROPAGATION%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[dTheta, dBaias] = Deltas(Theta,Baias,Inputs,NumberOfLayers,RegressionSwitch, Epsilon, Lambda, y);
%%%%%%%%%%%%%%%GRADIENT DESCENT%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1:size(Theta,2)
Theta{i} = Theta{i}-Alpha*dTheta{i};
end
for i = 1:size(Baias,2)
Baias{i} = Baias{i}-Alpha*dBaias{i};
end
Epoch = Epoch + 1;
end
hold off;
V_Inputs{1} = vx;
V_y_predicted = forwardProp(Theta,Baias,V_Inputs,NumberOfLayers,RegressionSwitch);
figure;
hold on;
for i = 1:size(vy,2)
scatter(vy(1,i),V_y_predicted(1,i));
pause(0.01);
end
hold off;
figure;
hold on;
for i = 1:size(vy,2)
scatter(vy(2,i),V_y_predicted(2,i));
pause(0.01);
end
hold off;
I have found a weird behavior of the parfor functionality in matlab. Have a look at the following code:
function [x,y,z] = parForTest(test3)
x = zeros(100,1);
y = zeros(100,1);
z = zeros(100,1);
test1 = ones(1,100);
test2 = zeros(1,100);
useSameTest2 = false;
if nargin == 1
test3Given = true;
else
test3Given = false;
end
parfor s = 1:numel(test1)
if useSameTest2
tmpTest = test2(1);
if test3Given, tmpTest3 = test3(1); end
else
tmpTest = test2(s);
if test3Given, tmpTest3 = test3(s); end
end
if test3Given
[xt, yt, zt] = oneCombi(test1(s), tmpTest, tmpTest3);
else
[xt, yt, zt] = oneCombi(test1(s), tmpTest);
end
%% store results
x(s) = xt;
y(s) = yt;
z(s) = zt;
end
end
function [xt, yt, zt] = oneCombi(t1, tmpTest, tmpTest3)
if nargin == 3
xt = 1;
yt = 1;
zt = 1;
else
xt = 0;
yt = 0;
zt = 0;
end
end
If you call this function by typing "[x,y,z] = parForTest();" you will get an error saying that there are not enough input arguments. If you run the whole thing with "for" instead of "parfor" it will work. It will also work, when you call "[x,y,z] = parForTest(ones(100,1));" Can somebody please explain me this behavior? It seems to be connected to the fact that I have an optional argument. If I add the line "test3 = [];" after "test3Given = false;" it will also work.
Matlab2014a with parallel computing toolbox.
You are using the optional input functionality incorrectly. I have noticed that matlab sometimes allows a call of the type F() even if the function is declared as function result = F(x, y). This call will be allowed if during the execution the variables x and y are not used. I know it is weird.
Now, your problem is related to the fact that you are using the parallel computing toolbox. When using parfor, Matlab is more stringent with the variables used inside the parfor-loop, that is why it works happily with the for-loop but not with the parfor. Matlab needs to ensure that all the workers receive the correct information, properly sliced, and properly defined (regardless of the execution path).
To solve your issue you need to define your input argument as varargin and then act on the different inputs with nargin. You need to modify your function as follows (see how the variable test3 is always defined, no matter if you call it as parForTest() or parForTest(whatever)):
function [x,y,z] = parForTest(varargin)
x = zeros(100,1);
y = zeros(100,1);
z = zeros(100,1);
test1 = ones(1,100);
test2 = zeros(1,100);
useSameTest2 = false;
if nargin == 1
test3Given = true;
test3 = varargin{1};
else
test3Given = false;
test3 = [];
end
parfor s = 1:numel(test1)
if useSameTest2
tmpTest = test2(1);
if test3Given, tmpTest3 = test3(1); end
else
tmpTest = test2(s);
if test3Given, tmpTest3 = test3(s); end
end
if test3Given
[xt, yt, zt] = oneCombi(test1(s), tmpTest, tmpTest3);
else
[xt, yt, zt] = oneCombi(test1(s), tmpTest);
end
% store results
x(s) = xt;
y(s) = yt;
z(s) = zt;
end
I would like to call (execute) an m-file (function) in a loop like this:
global m, r
m = 2;
for n = 2:10;
for r1 = 0:n-m;
r2 = n-m-r1;
r = [r1,r2];
[Call the function here?????????]
end
end
This is the function:
function main
x0 = [-0.5403,0.5471];
fsolve(#fcn,x0)
function z = fcn(X)
rand('twister',5409);
global m, r
a = rand(m,1);
for i = 1:m
sm(i) = 0.0;
for l = m-i+1:m
sm(i) = sm(i)+r(l);
end
s = 1.0/(i+sm(i));
g(i) = (a(i))^s;
end
prod = 1.0;
for k = 1:m
prod = prod * g(m+1-k);
u(k) = 1.0-prod;
x(k) = (sqrt(3)/pi)*log(u(k)/(1-u(k)));
end
sum = 0;
sum1 = 0;
sum2 = 0;
for j = 1:m
sum = sum+(r(j)+2)*(1/(1+exp((-pi/sqrt(3))*((x(j)-X(1))/X(2)))));
sum1 = sum1+(r(j)+2)*((x(j)-X(1))/X(2))*(1/(1+exp((-pi/sqrt(3))*((x(j)-X(1))/X(2)))));
sum2 = sum2+(x(j)-X(1))/X(2);
end
z(1) = pi/(X(2)*sqrt(3))*(-m+sum);
z(2) =(-1/X(2))*(m+(pi/sqrt(3))*(sum2-sum1));
Thank you very much for your help.
The functions main and fcn should be saved in a file called "main.m". You have to make sure this file is either in your current working directory or somewhere on the MATLAB path (as mentioned in a comment by Amro) so that MATLAB can get to it. Since main requires no input arguments and has no output arguments, you could then just call it in any one of the following ways:
main
main;
main()
main();
If you have a function Main.m
Main.m
function out = main(in)
% blah blah blah
You would call the function
in = 2;
out = main(in)
Does this make sense?
Personnally I would create your function without a main() part.
create a file called
fcn.m
with your function fcn in it, make sure it's in your working directory or in your matlab path and then call it inside your loop.
addpath(genpath('/the/path/to/your/function/');
global m, r
m = 2;
for n = 2:10;
for r1 = 0:n-m;
r2 = n-m-r1;
r = [r1,r2];
z=fcn(r)
end
end