When I try to generate C++ code for generalizedInverseKinematics I get the following error Dimension 2 is fixed on the left-hand side but varies on the right ([1 x 8] ~= [1 x
3 views (last 30 days)
Show older comments
This is the class and entry point function that I am trying to convert to c++ code
%_____________________________________________________________________________________________
classdef GeneralizedIK
%UNTITLED Summary of this class goes here
% Detailed explanation goes here
properties
robot
gik
ik
posTgt1
posTgt2
posTgt3
posTgt4
weights
initialguess
pos
end
methods
function obj = GeneralizedIK()
%UNTITLED Construct an instance of this class
% Detailed explanation goes here
obj.robot = importrobot('baxter.urdf');
pos = 'position';
obj.gik = generalizedInverseKinematics(...
'ConstraintInputs',{pos,pos,pos,pos},...
'RigidBodyTree',obj.robot,...
'SolverAlgorithm','LevenbergMarquardt');
obj.ik = inverseKinematics('RigidBodyTree',obj.robot,...
'SolverAlgorithm','LevenbergMarquardt');
obj.weights = [0 0 0 1 1 1];
obj.initialguess = obj.robot.homeConfiguration;
obj.posTgt1 = constraintPositionTarget('right_lower_shoulder');
obj.posTgt2 = constraintPositionTarget('right_lower_elbow');
obj.posTgt3 = constraintPositionTarget('right_lower_forearm');
obj.posTgt4 = constraintPositionTarget('right_hand');
end
function [sol] = compute_theta(obj,vec1,vec2,vec3,vec4)
%METHOD1 Summary of this method goes here
% Detailed explanation goes here
obj.posTgt1.TargetPosition = vec1;
obj.posTgt2.TargetPosition = vec2;
obj.posTgt3.TargetPosition = vec3;
obj.posTgt4.TargetPosition = vec4;
tform1 = trvec2tform(vec1);
tform2 = trvec2tform(vec2);
sol = obj.ik('right_lower_shoulder',tform1,obj.weights,obj.initialguess);
sol = obj.ik('right_lower_elbow',tform2,obj.weights,sol);
sol = obj.gik(sol,obj.posTgt1,obj.posTgt2,obj.posTgt3,obj.posTgt4);
end
end
end
%_____________________________________________________________________________________________
function [sol] = gik_main() %#codegen
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
robot = importrobot('baxter.urdf');
gik = GeneralizedIK();
randconfig = robot.randomConfiguration;
tform1 = getTransform(robot,randconfig,'right_lower_shoulder','base');
vec1 = tform2trvec(tform1);
tform2 = getTransform(robot,randconfig,'right_lower_elbow','base');
vec2 = tform2trvec(tform2);
tform3 = getTransform(robot,randconfig,'right_lower_forearm','base');
vec3 = tform2trvec(tform3);
tform4 = getTransform(robot,randconfig,'right_hand','base');
vec4 = tform2trvec(tform4);
%[randconfig.JointPosition]
sol = gik.compute_theta(vec1,vec2,vec3,vec4);
%[sol.JointPosition]
end
3 Comments
Karsh Tharyani
on 28 Jun 2022
Hi Aravindh,
I would suggest you reach out to our Technical Support team with the reproducible of the issue. That will enable us to diagnose this in an efficient manner.
Best,
Karsh
Answers (1)
Michael
on 28 Jun 2022
I looks like you are trying to save the output of the validatestring function over the entries in value, which I assumed is preallocated earlier in the program. Since your valid strings input to the function are listed in a cell array, the function output should be a character vector (see the validatestring documentation on the matchedStr input argument). That means that the output will be a variable sized. See the difference between the declarations below:
ex1 = 'an example';
size(ex1)
%ans =
% 1 10
ex2 = "an example";
size(ex2)
%ans =
% 1 1
Coder probably sees each entry of value as fixed size. You could probably specify each entry as a variable size using something like
value = cell(3,1);
entryA = 'A char array';
entryB = 'B char array';
coder.varsize(entryA);
coder.varsize(entryB);
value{1} = entryA;
value{2} = entryB;
but this likely is two 'hard-coded'. Insteade, I would probably try to cast the output of validatestring as a string and then save as a separate array to avoid variable size issues. You likely should also be initially specifing the entryies of value as strings rather than char arrays. This way the size of each entry of value is a fixed 1x1 string rather than a variable size 1x: character array. Something like this:
entryA = "A short string";
entryB = "A much longer string to show the size of entryB is still 1x1";
value = {entryA; entryB};
tempValue = 'preAllocCharArray';
coder.varsize(tempValue,[1,100],[1 0]);%Is 100 enough for your application?
for i = 1:numel(value)
tempValue = validatestring(value{i},{'orientation','position','etc...'},'generalizedInverseKinematics','ConstraintInputs')
value(i) = convertCharsToStrings(tempValue);
end
Note that I haven't checked this code with coder, but I think it will work. I had to go through my code recently and covert a bunch of character arrays to strings to deal with this variable size business.
I hope this helps.
-Michael
0 Comments
See Also
Categories
Find more on Simulink Coder 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!