-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace bindings by WBT blocks through Simulink function calls #15
Conversation
- Add Configuration block at the root level of the main model. - Set robot name from environment variable. - Fix `WBTConfigRobotSim` structure in configRobot.m. - Fix `confVisualizer.world_H_base` computation error ( H(4,:) was = [1 1 1 1] instead of [0 0 0 1] ) - Consolidate init.m, configRobot.m and initVisualizer.m.
…ggle button for finding the `Configuration` block. This makes clear that `RobotDynamicsWithContacts` library depends on a `Configuration` block.
… and `get_feet_jacobians`
- The frame name in a WBT is a static input parameter, not a input signal.
… that the feet frames are preset in the WBT blocks.
…straints related to that phase (supported features, table varying sizes, ...) - OSQP Matlab bindings not supported, nor `quadprog`. Use qpOASES Simulink block instead through a Simulink function `simFunc_qpOASES` sub-system. Selection is done through `setOSQP` and `useQPOASES` bool flags. - Some fields of the `robot_config` input of `step_block` Matlab System are modified or modifiable in run-time, so they cannot be Tunable. Don't set this input to `Nontunable`. - `Nontunable` inputs have to be initialized. - Cleanup `x_iDyn` variables formerly used for interfacing bindings functions. - Matrices need to be properly initialized (sparse iterative filling of matrix, as done in Matlab, is not supported, dynamic size matrices). - Unsupported features or functions like `optimoptions`, `quadprog` or `osqp` binding will cause the code generation compiler to fail even if they are wrapped in a if or switch condition. - Other features not supported in code generation are: `mat2cell`, implicit indexing ":".
- 'Size mismatch. Input 1 expected value of size [24 x 24]. Found size [:? x :?]. Mismatched varying and fixed sizes indicate a probable run-time error. If this diagnostic is incorrect, use indexing to explicitly make the varying size fixed.': Issue occurs due to imput `H` of `simFunc_qpOASES`. Simulink functions inputs are defined and set through ports, and ports need to have fixed sizes, in this case [3*<num_vertices>,3*<num_vertices>]=[24,24]. For the ports size, <num_vertices> is defined as the constant `contact_config.total_num_vertices`, while `H` size depends on `J_feet`, which is computed by `compute_J_and_JDot_nu()` from the non constant property `num_vertices` of the `Contacts` object. Set `num_vertices` `Contacts` property as `Constant`. - 'Size mismatch. Input 3 expected value of size [48 x 24]. Found size [:? x 24]. Mismatched varying and fixed sizes indicate a probable run-time error. If this diagnostic is incorrect, use indexing to explicitly make the varying size fixed.': occurs due to wrong size of 'A' built through `blkdiag`. - 'Function call resolved to MEX file '/Users/nunoguedelha/dev/osqp-matlab/osqp_mex.mexmaci64. Only MATLAB files are supported for code generation': conditional code is compiled even if the respective run condition is false (`obj.useOSQP=false`). So, flags `obj.useOSQP` and `obj.useQPOASES` have to be defined as constants. - 'Size mismatch (size [0 x 0] ~= size [3 x 29]). The size to the left is the size of the left-hand side of the assignment'. Can't assign a table (e.g. result of an operation) to a table of different size. For that reason, the pattern appending chunks to a table does not work: A = []; A = [A;B]. Refactor the computation of J, the stacked jacobians relative to the vertices that will be in contact.
- 'Dimension 1 is fixed on the left-hand side but varies on the right ([23 x 1] ~= [:? x 1])'. 's' from the right side of the equation, is computed in `State.ode_step()`. Its size appears dynamic because it is set as `s = y(end,:)`. Setting `s = y(end,1:numel(obj.s))` solves the problem. Same fix for `s_dot`. - 'Dimension 1 is fixed on the left-hand side but varies on the right ([4 x 4] ~= [:? x 4])'. Replace all instances of ':' by explicit indexes in `State.ode_step()`. - 'Dimension 1 of field 'motorGrpI' is fixed on the left-hand side but varies on the right ([23 x 1] ~= [:? x 1])': replace `(obj.robot_config.N_DOF,1)` by `size(s)`. - 'Simulink does not support persistent variables in System objects inside MATLAB System blocks.': replaced persistent variable by a class property.
- This was leading to a wrong configuration of the GetBiasForces WBT block, and subsequently, the wrong computation of the robot generalized accelerations, causing the robot to stay still indefinitely.
…lines - When a vertex is in contact, the respective line in `Aeq` is only zeros. When all the vertices of the feet are in contact with the ground, `Aeq` is a null matrix, and such null block in the overall contraints matrix causes qpOASES solver to fail, and the contact forces to be null. - For avoiding this issue, we use directly the lower/upper bounds inputs for the solver problem variable. The constraint F=0 is set using the "lb=ub=0" inputs, while an unconstrained F is obtained by setting "lb=-inf, ub=inf". This could have been obtained also by having `Aeq=Identity` and changing dynamically `beq` between 0 and inf the same way as lb, ub. - Minor fix in `expandedIdxesVerticesAtZeroVel` computation: vectorization through (:) operation instead of (1:end).
80e74c9
to
f2184bd
Compare
Ping @GiulioRomualdi , @Giulero . |
… at 40ms - treat the visualizer as atomic unit - set its sample time to 40ms - treat the fake controller as atomic unit - set its sample time to 1ms
For the last commit, refer to #5 (comment). |
Hi @nunoguedelha ! Maybe I've something wrong in my configuration, but I've got this error An error occurred in the block 'test_matlab_system_19/RobotDynWithContacts/MATLAB System' during compile.
Caused by:
Simulink detected an error
'Simulink encountered an error while allocating a handle object in MATLAB System block 'test_matlab_system_19/RobotDynWithContacts/MATLAB System'. Create the handle object only once in the corresponding System object 'robotDynWC.step_block'.'.
The error occurred for MATLAB System block 'test_matlab_system_19/RobotDynWithContacts/MATLAB System'.
To prevent this error, use one of the following:
* Modify the System object to avoid code that does not support code generation.
* Change 'Simulate using' parameter to 'Interpreted Execution'. Also, when the simulink model stops the file manager moves to |
|
||
% Do you want to enable the Visualizer? | ||
confVisualizer.visualizeRobot = true; | ||
|
||
%% ADD CONFIGURATION FILES | ||
% Select the robot name from YARP_ROBOT_NAME | ||
robotName = getenv('YARP_ROBOT_NAME'); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to add to the path src
and lib
here? I had to add them manually
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That shouldn't be necessary if you launch the right version of test_matlab_system_19.mdl
from the repo root path. When the model is loaded, the PostLoadFcn
function runs the following commands:
% Set path to the utility functions and to WBC library
import wbc.*
addpath(genpath('./src'));
addpath('./lib/RobotDynamicsWithContacts');
which adds all the required paths, including those you mentioned. for some reason you're loading the wrong mdl version. Try starting MATLAB from scratch...
% Detailed explanation goes here | ||
|
||
properties (Access = private) | ||
w_H_b (4, 4) double;; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double semicolon
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in next commit.
properties (Access = private) | ||
w_H_b (4, 4) double;; | ||
s; | ||
base_pose_dot (6, 1) double;; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double semicolon
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in next commit.
J_LRfoot = simFunc_getFrameFreeFloatingJacobianRFoot(obj.w_H_b,obj.s); | ||
otherwise | ||
error('Unsupported "getFrameFreeFloatingJacobian" input parameter.'); | ||
ack = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about this. If the error occurs, the program should exit and the ack will be not changed, isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely. There was actually a warning. I kept the ack
variable for conserving the previous interface of the usd KinDynComputations
methods. I had in mind that that we might want to get back to using the bindings if some day we manage to generate C++ code from that. For that reason I created a KinDynComputations
class inside a new package matlab-whole-body-simulators/lib/+iDynTree2WBTmappers
.
For the sake of coherence, I removed the call to error
in this method, as the ack
is checked by the caller, which triggers an error on its own.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in the next commit.
indexesVerticesAtZeroVel = (allIndexes(mapVerticesAtZeroVel)-1)*3+1; % each vertex has 3 components | ||
expandedIdxesVerticesAtZeroVel = [indexesVerticesAtZeroVel;indexesVerticesAtZeroVel+1;indexesVerticesAtZeroVel+2]; | ||
expandedIdxesVerticesAtZeroVel = expandedIdxesVerticesAtZeroVel(:); | ||
J = J_feet(expandedIdxesVerticesAtZeroVel,1:end); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love your vectorization! :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:D actually it was the easiest solution to have a code supported by the code generator (dynamic matrices building is not supported), and have a variable J
.
lib/+wbs/@Robot/Robot.m
Outdated
error('[Robot: get_feet_jacobians] Unable to retrieve the left foot jacobian'); | ||
end | ||
|
||
if (~obj.KinDynModel.kinDynComp.getFrameFreeFloatingJacobian(obj.RFoot_frameID, obj.J_RFoot_iDyntree)) | ||
[ack,J_RFoot] = obj.KinDynModel.kinDynComp.getFrameFreeFloatingJacobianLRfoot('RFoot'); | ||
if (~ack) | ||
error('[Robot: get_feet_jacobians] Unable to retrieve the left foot jacobian'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: left -> right
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bad copy paste :D
Fixed in next commit.
Hi @Giulero , it stops in |
Rework completed. @Giulero |
…rary use of qpOASES
@Giulero , @GiulioRomualdi , I've updated the README for reflecting the Config.USE_OSQP flag deprecation. |
I tried starting Matlab from scratch and run |
Hi @Giulero I'm trying to reproduce it in my environment... |
Let's see if @CarlottaSartore (MATLAB 2019b + Linux) and @VenusPasandi (MATLAB2020b + Linux) have the same issue. I definitely can't reproduce it (MATLAB 2019b + MacOS). |
My computer Configuration:
Able to run ~1 sec of simulation (then the robot falls) and get this error An error occurred while running the simulation and the simulation was terminated
|
@nunoguedelha , I got the same error as @Giulero said #15 (comment) as following
|
Just for clarifying for anyone else joining the discussion:
This error is an old error in the visualizer that occurs only when the robot gets to that weird position..
this is what we are analyzing. @VenusPasandi , could you please specify your machine configuration? Apart from MATLAB version (2020b), do you share the same configuration (superbuild, etc) as @CarlottaSartore ? |
After discussing with @VenusPasandi and @CarlottaSartore , we confirm that both the configurations they used are identical except for the MATLAB version. I will upgrade my MATLAB version tomorrow and analyze the problem. Let's wait until Friday at least before we merge this PR. |
With Ubuntu 20.04 and Matlab 2020a I had the same error as @CarlottaSartore in #15 (comment). The robot was kind of exploding. Not sure what is the expected behavior. |
Btw, I suggest checking also the terminal output in which you launched Matlab. In my case I had:
that may explain why the robot was exploding. |
Yes absolutely. At that point the robot was already under the floor a priori. We were having a a similar error way before this PR. Regarding the pqOASES failure, we were planing to handle the error more properly, allowing a given number of consecutive failures while using the last valid QP result, and stopping the simulation if that max number is reached. I've just opened #16 for this. For clarifying, the robot falling and sinking in the ground is an expected behaviour. The controller used in the test model is a fake controller, we are sending 0 torques to the joints and letting the robot fall on the ground to check the contacts handling. |
For the record, this is what I see whole_body_simulators.mp4I also noticed that in the Diagnostic Viewer I have the message
It is printed at every visualization update. I confirm that the qpOases errors appear only when the robot is down below the ground. |
Unfortunately, lately I cannot see videos attached to Github or Gitlab from Linux (probably because of Linux 20?). But I guess it matches my previous description. Regarding the Warning, I have seen this for a while but as it is not a priori critical nor related to recent developments, I never had time to pay much attention. |
WIP: Issue analysis (in Matlab internal code)Error message A search on this keyword led to the help page
If the first 2 lines in
The problem is in the very first line Found where the problem is...The issue occurs when we create a local structure in a function, set one of the fields as a handle pointing to a class object, and return that structure to the calling method which then sets another class property with that. The function "return" is done "by copy", not by reference, and this is probably what is triggering the error. I'll detail better later. Fixing... |
@VenusPasandi @CarlottaSartore @Giulero @S-Dafarra I've edited the analysis #15 (comment). The fix is working. Pushing it in a few. |
… in MATLAB System block" - The error was occuring only after upgrade to MATLAB 2020b. (Check #15) - Removed parent class "handle" from KinDynComputations class definition. - Saved changes in 2020b format file `robotDynamicsWithContacts_lib_2020b.slx`. - Exported changes in 2019b format file `robotDynamicsWithContacts_lib.slx`. - Renamed test model files adding respective suffixes 2019b, 2020a and 2020b. (test models content haven't changed apart from the Simulink version)
Fix pushed and tested. @Giulero, @GiulioRomualdi or @CarlottaSartore , can you please review the fix (very few changes) such that we merge and unblock the other depending PRs? Additional info for the review
|
Hi @GiulioRomualdi , @Giulero , any news on the approval? Sorry to insist, it's just that this PR is critical for improving the simulator performance in another project depending on it, specially this week. CC @CarlottaSartore |
Thx @Giulero ! Merging |
Implements #5 and subsequent fixes (refer to #5 (comment)).
Default iCub simulation should work exactly as before (should render same robot behavior), except that it should run approximately 3 times faster.