implementin NMPC in ROS using code generation
3 views (last 30 days)
Show older comments
Is it possible to to use Matlab code generation to implement a ROS node for NMPC?
I have defined a nlmpc object and I am perfoming closed-loop simulations in Matlab using the nlmpcmoveCodeGeneration() command after having created the coreData and onlineData struct using getCodeGenerationData().
Does anyone know if it is possible to implement a ROS node using code generation to import this same controller to ROS envionment? The reason behind this is that I have designed a controller using Matlab simulations, and I would now like to test the controller with real hardware that is operating with ROS.
Currently I am having the following error message when trying to use the code generation:
----------
>>
Non-constant expression or empty matrix. This expression must be constant because its value determines the size or class of some expression.
Error in ==> nlmpcmoveCodeGeneration Line: 265 Column: 20
Code generation failed: View Error Report
Error using codegen
----------
I am using the following script for the code generation. I am using Matlab version 2023a on Ubuntu 20.04
cfg = coder.config("exe");
cfg.Hardware = coder.hardware("Robot Operating System (ROS)");
cfg.Hardware.DeployTo = "Localhost";
cfg.Hardware.BuildAction = "Build and run";
Cfg.DynamicMemoryAllocation = 'off';
cfg.HardwareImplementation.ProdLongLongMode = true;
codegen myTestnode -config cfg
Please let me know if someone has any experience with similiar problems with NMPC or if somone has any suggestions on how to proceed with this.
Thank you!
4 Comments
Josh Chen
on 11 May 2023
Hi Mikael,
This simplified function looks like a good point to start. Could you please also provide a dummy "coreData" and "onlineData" so that I can try this out on my end and see where it failed?
One thing I observe in this script is "~isempty(msg_get1.X>0)", I believe this will always return "true". If you would like to only run the code when receiving a message with X larger than 0, you can remove "~isempty()".
Thanks,
Josh
Accepted Answer
Josh Chen
on 12 May 2023
Hi Mikael,
Thanks for sharing this additional information. I think using "goal variable" is a smart idea for generating the ROS node with mpc. The error message you saw earlier indicates that the first argument for nlmpcmoveCodeGeneration need to be constant. Based on the script you provided, it seems you are not planning to change the value of "coreData" anyway.
To let MATLAB coder understand this global variable will be a constant, you can specify it with "-globals" and "coder.Constant":
>> global_values = {'coreData', coder.Constant(coreData), 'onlineData', onlineData};
>> codegen myTestnode -config cfg -globals global_values
Thanks,
Josh
0 Comments
More Answers (0)
See Also
Categories
Find more on Code Generation 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!