ceres-solver / ceres-solver

A large scale non-linear optimization library

Home Page:http://ceres-solver.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The 'if' condition in Update function goes into hard fault handler in microcontroller, but not when run in PC.

sharan-rv opened this issue · comments

The if condition in the below code when run on STM32L4 microcontroller hangs in the hard fault handler. This code was tested in the PC where it passed the check. Has anyone faced this issue before and can point me to a direction to debug this issue? Thanks.

  bool Update(const Function& function, const Parameters& x) {
    if (!function(x.data(), residuals_.data(), jacobian_.data())) {
      return false;
    }

Where the parameter const Function& function is passed as AutoDiffFunction f(my_functor); and whereas the values passed to const Parameters& x contains the value generated by the code

Eigen::Matrix<double,PARAM_SIZE,1> x = init_param.parameter_vec;
Eigen::Matrix<double, PARAM_SIZE, 1>v;
for (int i = 0; i < PARAM_SIZE; i++) {
  v(i) = x[i];
}
.
.
.
.
solver.Solve(f, &v);

@sharan-rv this seems like an issue with the compiler or the runtime. I am guessing you are using TinySolver. I recommend stepping through the code to see what fault is generated and why.

@sandwichmaker Thanks for the suggestion. At line 227 in tiny_solver.h, you added a to-do comment that says "Deal with failure here". This is the exact point where I observed the exception. I am curious to know what you encountered as a reason for adding this comment. It can hint me other possibilities to debug.

@sharan-rv what is happening here is that I am ignoring the return value of Update. The reason I have not dealt with it is an API issue (we do not have a good way of communicating failures in the TinySolver API yet) there was not programmatic reason for this.

its possible that your compiler/runtime is barfing at the unhandled return value.
you could try two things.

you could get the return value from Update and quit if its false.

you could also change the Update function and have it not return a value at all.

Hi @sandwichmaker , thanks for helping so far.

More precisely, the hard fault is happening in this line:
jtj_ = jacobian_.transpose() * jacobian_;
in tiny_solver.h in the bool Update(const Function& function, const Parameters& x), after comment out the following line to dig deeper:

   if (!function(x.data(), residuals_.data(), jacobian_.data())) {
      return false;
    }

We aren't sure why this is happening, as the debugger doesnt tell us why the hard fault happens.

Ok we found out this happens when your problem is under parametrized or running out of memory. We noticed the cost becomes very big for some iterations. We will work on solving this from our side.

Sounds good. Closing as this is not an issue with Ceres Solver.