From alexpascarella96 at gmail.com Fri Apr 1 12:39:05 2022 From: alexpascarella96 at gmail.com (Alex Pascarella) Date: Fri, 1 Apr 2022 14:39:05 -0500 Subject: [Spice_discussion] Query CSPICE within Matlab MEX dynamical model Message-ID: Hi all, For my research, I have a dynamical model implemented as a Matlab file that I compile into .mex file through the codegen command. In the dynamical model file, I need to query the position of the planets using spkezr. Since mice comes as a precompiled MEX application, I can't use it within my matlab file, because that is not compatible with code generation. My current workaround is as follows: function [...] = dynamics(..) [...] % Include C files required for code generation coder.updateBuildInfo('addIncludePaths', '[...]\mice\include') coder.updateBuildInfo('addIncludePaths', '[...]\mice\src\cspice') libPriority = ''; libPreCompiled = true; libLinkOnly = true; libName = 'cspice.lib'; libPath = '[...]\mice\lib'; coder.updateBuildInfo('addLinkObjects', libName, libPath, libPriority, libPreCompiled, libLinkOnly); coder.cinclude('[...]\mice\src\cspice\SpiceUsr.h') % Preallocate output starg = zeros(6, 1); lt = 0; % Query cspice coder.ceval('spkezr_c', [targ 0], et, [ref 0], [abcorr 0], [obs 0], coder.wref(starg), coder.wref(lt)); [...] end The code above compiles successfully when using codegen and the .mex files produces the expected results, however there is a glaring issue: if an error is encountered within the spkezr_c call (e.g. kernels are not loaded, strings are incorrect, etc.) the entire Matlab application crashes without errors. Is there any workaround that would allow me to fix this problem and keep using codegen to generate the .mex file? Thanks! Alex -------------- next part -------------- An HTML attachment was scrubbed... URL: From alexpascarella96 at gmail.com Tue Apr 5 13:57:17 2022 From: alexpascarella96 at gmail.com (Alex Pascarella) Date: Tue, 5 Apr 2022 15:57:17 -0500 Subject: [Spice_discussion] Query CSPICE within Matlab MEX dynamical model Message-ID: Hi all, After receiving very useful feedback from the SPICE team, I was able to solve the issues I was running into and integrate cspice within my MEX model. A full example can be found here: https://urldefense.us/v3/__https://github.com/EllissoideRotondo/mex_cspice_wrapper_example__;!!PvBDto6Hs4WbVuu7!YrD03VH5oZcfwDu-TMtyN2C3ZTI-iZIw_jWjZnUSv0K6j0h3xqxFRbw8yOyQR4AIjG0WRFikIx4dOQ$ . Some important takeaways: - To avoid crashes, it is necessary to call the cspice function erract_c and set it to "RETURN" before any other cspice calls, which prevents cspice from throwing an exception. This only needs to be called once per program run. - It is a good idea to call errdev_c and set the output device to NULL, which prevents cspice from trying to dump errors to stdout. This also needs to be run only once. - Any exceptions encountered by cspice can be captured using the cspice functions failed_c (check if any error occured), getmsg_c (get the error message), and reset_c (reset the error status). Note that reset_c must be called any time an exception occurs, otherwise cspice will keep returning an error on subsequent calls. Once captured, a Matlab exception can be thrown using the error command (in MEX, you will need to define coder.exstrisinc('error'), as the command is not natively compatible with code generation). - As a final note, I also recommend upgrading to the latest version of Matlab (R2022a) if possible. There is a bug in earlier versions that occasionally causes Matlab to crash when running certain MEX files in debug mode. Thanks, Alex Pascarella -------------- next part -------------- An HTML attachment was scrubbed... URL: