Trying to get imageToVehilce to C/C++ code generate in Simulink

1 view (last 30 days)
I've been working with the example "TransformAnImageOfARoadToABirdseyeviewImageExample.mlx". I've taken the parts that I would like to use out of it for getting an image pixel to the vehicle location frame. The bird's eye view image is not necessary to output for the application (might be added later for debugging, but currently not necessary).
As I've been trying to compile the project it is saying that the compiler doesn't have enough information to know the size of the cameraIntrinsics or monoCamera. I've set the hardware implementation to be ROS. The xypixels is set to be inherited from the prevoius block (which is a 28x2). I've been trying to get a setting for dynamic memory. Is there a way to initialize the variables? Or set for dynamic memory allocation?
function xyvehicle= image2distance(xypixels)
%#codegen
%Transform Road Image to Bird's-Eye-View Image
%Create a bird's-eye-view image from an image obtained by a front-facing camera mounted on a vehicle. Display points within the bird's-eye view using the vehicle and image coordinate systems.
%Define the camera intrinsics and create an object containing these intrinsics.
focalLength = [309.4362 344.2161];
principalPoint = [318.9034 257.5352];
imageSize = [480 640];
camIntrinsics = cameraIntrinsics(focalLength,principalPoint,imageSize);
%Set the height of the camera to be about 2 meters above the ground. Set the pitch of the camera to 14 degrees toward the ground.
height = 2.1798;
pitch = 14;
%
%Create an object containing the camera configuration.
sensor = monoCamera(camIntrinsics,height,'Pitch',pitch);
%
% %Define the area in front of the camera that you want to transform into a bird's-eye view. Set an area from 3 to 30 meters in front of the camera, with 6 meters to either side of the camera.
distAhead = 30;
spaceToOneSide = 6;
bottomOffset = 3;
outView = [bottomOffset,distAhead,-spaceToOneSide,spaceToOneSide];
%Set the output image width to 250 pixels. Compute the output length automatically from the width by setting the length to NaN.
outImageSize = [1024,250];
%Create an object for performing bird's-eye-view transforms, using the previously defined parameters.
birdsEye = birdsEyeView(sensor,outView,outImageSize);
xyvehicle = imageToVehicle(birdsEye,xypixels);
  2 Comments
Matthew DiBello
Matthew DiBello on 31 May 2022
How are you generating C/C++ code? Based on your description, it seems that this MATLAB function is included in a Simulink model. Is this correct?
I can generate code for this snippet using a simple MATLAB Coder example, so I think there is more context required to see how the problem is occurring. Can you share a simple model that demonstrates this issue?
You also mentioned trying to configure dynamic memory allocation. Are you trying to enable (or disable it)?
Cheers!
Matthew
squared
squared on 31 May 2022
The code would be generated through the code generation function in Simulink (buidling a ROS package). It isn't the MATLAB coder way of generating code. The project itself will have combinations of the toolboxes (ROS, Vision, Neural Networks, maybe Cuda coder). The dynamic memory allocation is coming from an error handler in Simulink.
I'm trying to enable the dynamic memory allocation. It is a currently a work around to get it working. Allocating memory would be a much better method, but I haven't figured out how to do the allocation of memory in a user-function m-file that is being called in Simulink yet.

Sign in to comment.

Answers (1)

Ashutosh Thakur
Ashutosh Thakur on 23 Jan 2024
Edited: Ashutosh Thakur on 23 Jan 2024
Hi,
I can understand that you want to enable the dynamic memory allocation for the variables which are either part of MATLAB Function or a M-File.
By default there is a limitation in Simulink to support the variables having dynamic size.The variable which needs to have dynamic allocation of memory has to be allocated the maximum size that those variables could achieve in the context of the program. I would like to suggest some approaches to have a dynamic memory allocation for the variables in Simulink for code generation:
  • Open the Simulink Model and in the Modelling Tab and click on the Model Explorer button, and a dialog box would get opened.
  • In the dialogue box, select those variables which need to be dynamically allocated by clicking on it and select the checkbox of "Variable size" present in the right side of the dialog box.
  • You can also use the "coder.varsize" function to explicitly define the variables to have variable-size data.
  • Additionally, if you are increasing the size of an array, make sure to do so by using the "end + 1" operation in the MATLAB code.
Kindly refer the following links to have more detailed information in handling dynamic memory allocation in Simulink:
I hope the above suggestions help you in resolving issues related to the dynamic memory allocation.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!