function msg = rosWriteBinaryOccupancyGrid(msg, map)
%rosWriteBinaryOccupancyGrid Write values to a ROS/ROS 2 message struct
%   MSG = rosWriteBinaryOccupancyGrid(MSG,MAP) will write occupancy
%   values and other meta information to the MSG from the
%   binaryOccupancyMap object MAP. The meta information
%   includes resolution, origin, width and height.
%
%   Example:
%       % Create a binary occupancy map
%       map = binaryOccupancyMap(randi([0,1],10));
%
%       % Create a nav_msgs/OccupancyGrid message
%       msg = rosmessage("nav_msgs/OccupancyGrid","DataFormat","struct");
%
%       % Write the map data to the msg
%       msg = rosWriteBinaryOccupancyGrid(msg,map);

%   Copyright 2020-2023 The MathWorks, Inc.
%#codegen

% Ensure the generated code is not inlined
    coder.inline('never');

    % Validate input argument
    validateattributes(msg, {'struct'},{'scalar'},'rosWriteBinaryOccupancyGrid');
    % Code generation only supports nav_msgs/OccupancyGrid as input message
    if ~isempty(coder.target)
        coder.internal.assert(strcmp(msg.MessageType,'nav_msgs/OccupancyGrid'),...
                              'ros:mlroscpp:codegen:InvalidMsgForSpMsgFun',...
                              'rosWriteBinaryOccupancyGrid','nav_msgs/OccupancyGrid');
    end

    % Get binary map from input message
    if isfield(msg, 'Data')
        % ROS message struct
        specialMsgUtil = ros.internal.SpecialMsgUtil;
    else
        % ROS 2 message struct
        specialMsgUtil = ros.internal.ros2.SpecialMsgUtil;
    end
    msg = specialMsgUtil.writeBinaryOccupancyGrid(msg, map);
end
