code generation for path planning and vehicle control -凯发k8网页登录
this example shows how to modify a simulink® model of a path planning and vehicle control algorithm, generate c code, and verify the generated code using software-in-the-loop (sil) simulation.
introduction
developing a path planning and vehicle control algorithm often involves designing and simulating an algorithm model in simulink, implementing the algorithm in c code, and integrating the algorithm code into an external software environment for deployment into a vehicle. automatically generating and verifying code from the algorithm model ensures functional equivalence between the simulation and implementation.
the automated parking valet in simulink example showed how to design a path planning and vehicle control algorithm. this example shows how to modify the design for implementation in c . this steps in this workflow are:
partition the design into algorithm and test bench models.
modify the algorithm model to support code generation.
generate c code from the algorithm model.
verify the behavior of the generated code using sil simulation.
you can then integrate the generated code into an external software project for further testing in a vehicle.
partition the algorithm and test bench
the original model from the automated parking valet in simulink example has already been partitioned into separate algorithm and test bench models.
algorithm model:
automatedparkingvaletalgorithm
specifies the path planning and vehicle control functionality to be implemented in c .test bench model:
automatedparkingvalettestbench
specifies the stimulus and environment to test the algorithm model.
simulate test bench model
the automatedparkingvalettestbench
model specifies the stimulus and environment to test the automatedparkingvaletalgorithm
model. the main components of the automatedparkingvalettestbench
include:
algorithm model reference: the
automatedparkingvaletalgorithm
model block is referenced by a model block. the model block and supports simulating the referenced model in different modes of simulation including normal and sil modes. to learn more about the model block, refer to (simulink).costmap: the costmap creator block creates the costmap of the environment and outputs it as a bus signal.
behavior planner: the behavior planner block triggers a sequence of navigation tasks based on the global route plan by providing an intermediate goal and configuration.
vehicle model: to demonstrate the performance of the algorithm, the parking valet controller is applied to the vehicle model block, which contains a vehicle body 3dof block.
the automatedparkingvalettestbench
model is also configured to log the pose(currpose
) and longitudinal velocity (currvelocity
) of the vehicle and the status of whether the goal from the behavioral planner was reached (goalreached
). these signals are logged to the workspace variable logsout
.
simulate the test bench model with the algorithm in normal mode.
open_system('automatedparkingvalettestbench') snapnow set_param('automatedparkingvalettestbench/automatedparkingvaletalgorithm','simulationmode','normal'); sim('automatedparkingvalettestbench') helperplotsimulationsignals(logsout) snapnow
the first figure displays the path that the vehicle traversed from the parking lot input to the final parking space. the second figure plots the velocity and goal-reached signals. notice that the vehicle velocity is smooth and continuous when transitioning between goals.
modify algorithm model to support code generation
the automatedparkingvaletalgorithm
model specifies the functionality to be implemented in c . the main components of the automatedparkingvaletalgorithm
model are:
path planner: plans a feasible path through the environment map using a object.
trajectory generator: smooths the reference path by fitting splines and converts the smoothed path into a trajectory by generating a speed profile.
vehicle controller: controls the steering and velocity of the vehicle to follow the generated path and speed profile.
open and update the algorithm model.
open_system('automatedparkingvaletalgorithm') set_param('automatedparkingvaletalgorithm','simulationcommand','update');
the automatedvaletparking
model includes several modifications from the automated parking valet in simulink example to support code generation. the most significant modifications are specifying fixed-size component interfaces and explicit rate transitions.
variable-size component interfaces have been replaced with fixed-size interface to enable generating and verifying c code with sil simulation.
the variable-size
poses
signal has been split into a fixed-size outport (refposesarray
) with an additional outport specifying the size (refposessize
).the
costmapbus
bus associated with thecostmap
input port contains only fixed-size elements, since the costmap does not change size in this example.
the automatedvaletparking
model contains multiple rates. the color of the blocks represents different sample times. path planning and trajectory generation is performed at a 0.1s sample time and is colored green. vehicle control is performed at a 0.05s sample time and is colored red. to learn more about displaying sample time colors, refer to (simulink).
explicit rate transition blocks have been inserted into the model to treat each rate as a separate task.
a rate transition block has been inserted to the fixed-size
currpose
signal.a helper varsize rows rate transition block (named rt) has been inserted to variable-size signals that connect blocks of different rates.
treating each rate as a specific task enables generating a c class with separate method entry points for each rate. generating separate methods for each rate simplifies integration into multi-tasking software schedulers or operating systems in the vehicle. to learn more about treating rates as separate tasks, refer to (embedded coder).
configure and generate code from algorithm model
configuring the automatedparkingvaletalgorithm
model to generate code includes setting parameters to:
generate c code with entry points for each rate.
apply common optimizations.
generate a report to facilitate exploring the generated code.
set and view model parameters to enable c code generation.
helpersetmodelparametersforcodegeneration('automatedparkingvaletalgorithm')
set automatedparkingvaletalgorithm configuration parameters: parameter value description _______________________________ _______________ ______________________________________________________________________________________________________________________ {'systemtargetfile' } {'ert.tlc' } {'code generation>system target file' } {'targetlang' } {'c' } {'code generation>language' } {'solvertype' } {'fixed-step' } {'solver>type' } {'fixedstep' } {'auto' } {'solver>fixed-step size (fundamental sample time)' } {'enablemultitasking' } {'on' } {'solver>treat each discrete rate as a separate task' } {'prodlonglongmode' } {'on' } {'hardware implementation>support long long' } {'blockreduction' } {'on' } {'simulation target>block reduction' } {'matlabdynamicmemalloc' } {'on' } {'simulation target>simulation target>dynamic memory allocation in matlab functions' } {'optimizeblockiostorage' } {'on' } {'simulation target>signal storage reuse' } {'inlineinvariantsignals' } {'on' } {'simulation target>inline invariant signals' } {'buildconfiguration' } {'faster runs'} {'code generation>build configuration' } {'rtwverbose' } {'of' } {'code generation>verbose build' } {'combinesignalstatestructs' } {'on' } {'code generation>interface>combine signal/state structures' } {'supportvariablesizesignals' } {'on' } {'code generation>interface>support variable-size signals' } {'efficientfloat2intcast' } {'on' } {'code generation>optimization>remove code from floating-point to integer conversions that wraps out-of-range values'} {'zeroexternalmemoryatstartup'} {'off' } {'code generation>optimization>remove root level i/o zero initialization (inverse logic)' } {'customsymbolstrglobalvar' } {'$n$m' } {'code generation>symbols>global variables' } {'customsymbolstrtype' } {'$n$m_t' } {'code generation>symbols>global types' } {'customsymbolstrfield' } {'$n$m' } {'code generation>symbols>field name of global types' } {'customsymbolstrfcn' } {'apv_$n$m$f' } {'code generation>symbols>subsystem methods' } {'customsymbolstrtmpvar' } {'$n$m' } {'code generation>symbols>local temporary variables' } {'customsymbolstrmacro' } {'$n$m' } {'code generation>symbols>constant macros' }
generate code and the code generation report from the algorithm model.
slbuild('automatedparkingvaletalgorithm');
### starting build procedure for: automatedparkingvaletalgorithm
use the code generation report to explore the generated code. to learn more about the code generation report, refer to (simulink coder). use the code interface report link in the code generation report to explore these generated methods:
initialize
: call once on initialization.step0
: call periodically every 0.05s to execute trajectory generation and vehicle control.step1
: call periodically every 0.1s seconds to execute path planning.terminate
: call once on termination.
additional get and set methods for signal interface are declared in automatedparkingvaletalgorithm.h
and defined in automatedparkingvaletalgorithm.c
.
verify implementation with sil simulation
software-in-the-loop (sil) simulation provides early insight into the behavior of a deployed application. to learn more about sil simulation, refer to (embedded coder).
sil simulation enables you to: * verify that the compiled generated code on the host is functionally equivalent to the normal mode. * log execution times of generated code on the host computer. these times can be an early indicator of performance of the generated code. for accurate execution time measurements, profile the generated code when it is integrated into the external environment or when using with processor-in-the-loop(pil) simulation. to learn more about sil profiling, refer to (embedded coder).
configure algorithm and test bench model parameters to support sil simulation and log execution profiling information.
helpersetmodelparametersforsil('automatedparkingvaletalgorithm'); helpersetmodelparametersforsil('automatedparkingvalettestbench');
set automatedparkingvaletalgorithm configuration parameters: parameter value description ________________________________ ____________________ ____________________________________________________________ {'systemtargetfile' } {'ert.tlc' } {'code generation>system target file' } {'targetlang' } {'c' } {'code generation>language' } {'codeexecutionprofiling' } {'on' } {'code generation>verification>measure task execution time'} {'codeprofilingsaveoptions' } {'alldata' } {'code generation>verification>save options' } {'codeexecutionprofilevariable'} {'executionprofile'} {'code generation>verification>workspace variable' } set automatedparkingvalettestbench configuration parameters: parameter value description ________________________________ ____________________ ____________________________________________________________ {'systemtargetfile' } {'ert.tlc' } {'code generation>system target file' } {'targetlang' } {'c' } {'code generation>language' } {'codeexecutionprofiling' } {'on' } {'code generation>verification>measure task execution time'} {'codeprofilingsaveoptions' } {'alldata' } {'code generation>verification>save options' } {'codeexecutionprofilevariable'} {'executionprofile'} {'code generation>verification>workspace variable' }
simulate the test bench model with the algorithm in sil mode and plot the results.
open_system('automatedparkingvalettestbench') set_param('automatedparkingvalettestbench/automatedparkingvaletalgorithm','simulationmode','software-in-the-loop (sil)'); save_system('automatedparkingvaletalgorithm'); sim('automatedparkingvalettestbench');
### starting build procedure for: automatedparkingvaletalgorithm
helperplotsimulationsignals(logsout, executionprofile) snapnow
### successful completion of build procedure for: automatedparkingvaletalgorithm build summary top model targets built: model action rebuild reason =============================================================================================================== automatedparkingvaletalgorithm code generated and compiled code generation information file does not exist. 1 of 1 models built (0 models already up to date) build duration: 0h 2m 19.629s ### generated code for 'automatedparkingvaletalgorithm' is up to date because no structural, parameter or code replacement library changes were found. ### successful completion of build procedure for: automatedparkingvaletalgorithm build summary 0 of 1 models built (1 models already up to date) build duration: 0h 0m 4.78s ### preparing to start sil simulation ... building with 'microsoft visual c 2019 (c)'. mex completed successfully. ### updating code generation report with sil files ... ### starting sil simulation for component: automatedparkingvaletalgorithm ### application stopped ### stopping sil simulation for component: automatedparkingvaletalgorithm
the execution time for the step0
and step1
methods are shown in the lower plot. the plots indicate that the maximum execution time is required at the lower rate (step1
) after a goal pose is achieved. this lower rate is expected because it corresponds to the time when a new path is planned.
conclusion
this example demonstrated a workflow to generate and verify c code for a path planner and vehicle control algorithm. compiling and verifying the code with sil simulation established confidence that the generated code is functionally correct before integrating into an external software environment. the workflow was demonstrated as an extension of the automated parking valet in simulink example and is generally applicable to designing and implementing path planning applications.