alphaville / optimization-engine

Nonconvex embedded optimization: code generation for fast real-time optimization + ROS support
https://alphaville.github.io/optimization-engine/
Other
512 stars 53 forks source link

MATLAB interface problem when nu=1 #91

Closed alphaville closed 5 years ago

alphaville commented 5 years ago

Describe the bug

There's a bug in MATLAB: if np is equal to 1. The request to the optimizer should be "parameter": [13.0], but it is "parameter": [, 13.0] instead (extra comma in the beginning).

To Solve

Replace consume with:

function out = consume(o, p)
   if length(p) > 1
   p_formatted_str = sprintf('%f, ', p(1:end-1))
   else 
       p_formatted_str = '';
   end
   req_str = sprintf('{"parameter":[%s %f]}', p_formatted_str, p(end))
   fwrite(o.udp_connection, req_str);
   json_response = fread(o.udp_connection, 50000, 'char')
   json_response = char(json_response')
   out = jsondecode(json_response);
end

I'll start a pull request later.