[Spice_discussion] Query CSPICE within Matlab MEX dynamical model

Alex Pascarella alexpascarella96 at gmail.com
Tue Apr 5 13:57:17 PDT 2022


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: <http://naif.jpl.nasa.gov/pipermail/spice_discussion/attachments/20220405/32d35c77/attachment.html>


More information about the Spice_discussion mailing list