시뮬링크에서 스페이스바로 에고 차량 정지(Stopping the Ego Vehicle with the Spacebar in Simulink)

3 visualizzazioni (ultimi 30 giorni)
RoadRunner에서 아래의 MATLAB System 객체 코드를 ego 차량의 behavior로 적용하여, BrakeCmd = true; 명령어를 통해 차량을 정지시키는 기능을 구현하였습니다.
이제 BrakeCmd = true; 대신 스페이스바 입력으로 차량을 정지시키고자 합니다. ChatGPT의 안내에 따르면 이를 위해서는 Simulink를 사용해야 한다고 합니다.
현재의 함수 기반 코드를 Simulink 모델 로직으로 변환해야 하는데, 제가 구성한 모델 형태에서는 정상적으로 동작하지 않고 있습니다.
질문은 다음과 같습니다.
  • 현재 구현된 함수 코드를 Simulink 블록 다이어그램으로 변환하려면 로직을 어떻게 구성해야 하는지
  • 스페이스바 입력을 받아 ego 차량을 정지시키는 Simulink 모델 구성 방법
  • 함수에서 스페이스바를 사용하여 BrakeCmd = true; 명령어가 자동 생성되도록 하는 방법
===eng===
In RoadRunner, I applied the MATLAB System object code below as the ego vehicle’s behavior so that the command BrakeCmd = true; makes the car stop.
Now, I want to stop the car using the spacebar instead of BrakeCmd = true;. According to ChatGPT, I need to use Simulink for this.
I need to change my current function-based code into a Simulink model, but the model I built doesn’t work properly.
Here are my questions:
How should I build the logic to convert my current function code into a Simulink block diagram?
How can I design a Simulink model that stops the ego vehicle when the spacebar is pressed?
How can I make the function automatically set BrakeCmd = true; when the spacebar is pressed?
==========code==========
classdef ego < matlab.System
properties (Access = private)
mActorSimulationHdl
mScenarioSimulationHdl
mActor
mLastTime = 0
mBrakeApplied = false
end
methods (Access = protected)
function sz = getOutputSizeImpl(~)
sz = [1 1];
end
function st = getSampleTimeImpl(obj)
st = createSampleTime(obj,'Type','Discrete','SampleTime',0.02);
end
function t = getOutputDataTypeImpl(~)
t = "double";
end
function resetImpl(obj)
obj.mBrakeApplied = false;
end
function setupImpl(obj)
obj.mScenarioSimulationHdl = Simulink.ScenarioSimulation.find( ...
'ScenarioSimulation','SystemObject',obj);
obj.mActorSimulationHdl = Simulink.ScenarioSimulation.find( ...
'ActorSimulation','SystemObject',obj);
obj.mActor.pose = obj.mActorSimulationHdl.getAttribute('Pose');
obj.mActor.velocity = obj.mActorSimulationHdl.getAttribute('Velocity');
obj.mLastTime = obj.getCurrentTime;
obj.mBrakeApplied = false;
assignin('base','BrakeCmd',false);
fprintf('Initialization complete: BrakeCmd = false (driving mode)\n');
end
function stepImpl(obj, ~)
try
brakeCmd = evalin('base','BrakeCmd');
fprintf('Brake command: %d\n', brakeCmd);
catch
brakeCmd = false;
assignin('base','BrakeCmd',false);
fprintf('BrakeCmd variable created (false)\n');
end
pose = obj.mActorSimulationHdl.getAttribute('Pose');
velocity = obj.mActorSimulationHdl.getAttribute('Velocity');
fprintf('Current velocity: [%.2f %.2f %.2f]\n', velocity(1), velocity(2), velocity(3));
currentTime = obj.getCurrentTime;
elapsedTime = currentTime - obj.mLastTime;
if elapsedTime < 0
elapsedTime = 0;
end
obj.mLastTime = currentTime;
if brakeCmd && ~obj.mBrakeApplied
fprintf('Emergency brake! Stopping vehicle and ending simulation\n');
obj.mActorSimulationHdl.setAttribute('Velocity',[0 0 0]);
obj.mBrakeApplied = true;
error('Human-in-the-Loop: Simulation ended due to brake command');
elseif ~brakeCmd
pose(1,4) = pose(1,4) + velocity(1)*elapsedTime;
pose(2,4) = pose(2,4) + velocity(2)*elapsedTime;
pose(3,4) = pose(3,4) + velocity(3)*elapsedTime;
end
obj.mActorSimulationHdl.setAttribute('Pose', pose);
obj.mActor.pose = pose;
obj.mActor.velocity = velocity;
end
function releaseImpl(~)
fprintf('Simulation cleanup complete\n');
end
end
end
===simulink model===

Risposte (1)

saish
saish il 22 Ago 2025
I see you are using a keyboard block to detect the SPACE key and then switching between 0 and 13.89 as the input to Velocity.
You can do the following -
  1. RoadRunner expects the Velocity input as a 3*1 vector [vx; vy; vz]. At the moment you are feeding a scalar (0 or 13.89), which will not update the ego vehicle correctly. To stop the car, feed [0;0;0]. To drive forward, feed [13.89;0;0] or whatever speed you want along X direction.
  2. Make sure your keyboard block and Switch logic run at the same fixed step sample time as your model (0.02s). This ensures the Reader and Writer update in sync with your keyboard input.
  3. Instead of a raw keyboard block, you can use a MATLAB UI listener that sets a Boolean workspace variable BrakeCmd whenever SPACE key is pressed. In Simulink, a Constant block reads this variable and drives the Switch. You can write matlab code on the following logic:
If BrakeCmd == 1 then output – [0; 0; 0] to “Writer.Velocity
Else – pass Reader.Velocity

Community Treasure Hunt

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

Start Hunting!