How to use C-code with external cost function

Hi :wave:

I am trying to generate C-code using python interface. I have set my cost function to ‘EXTERNAL’. The cost function has parameters in order to change the reference. I am using this C-code in my ROS2 node.

When I build the C-code its giving me an error “model entry: yref not available in module ocp_nlp_cost_external”.

I have set dummy yref and yref_e to numpy arrays of zeroes in the python script. I have also set “ny” and ny_e" to the number according to my states and inputs.

How do I solve this model entry: yref not available in module ocp_nlp_cost_external error

Thank you for your help,


The external function module does not have y_\mathrm{ref} as part of the cost function definition.
Thus, it can not be set for this cost module.
Please check the cost function documentation.
You can use the EXTERNAL cost module with casadi parameters for varying references or the like.


Hi @FreyJo

I am still trying to use “EXTERNAL” cost function in the python interface and generate the C-code. I am getting this error.

Error: Failed to render ‘’
Reason: Variable cost.yref[j] not found in context while rendering ‘’: the evaluated version was cost.yref.0. Maybe the index is out of bounds?

Do I need to set yref if I am setting cost as external?

Could you please help me with this?

Thank you for your help,

Hi @FreyJo

I was setting the ocp.dims.y and ocp.dims.y_e. That was the problem. I removed it and I was able to generate the C-code.

Thank you

1 Like