From d4c344cd5c0c6a77050e5b6436c9feba2d91c9be Mon Sep 17 00:00:00 2001 From: Amal Nanavati Date: Thu, 22 Aug 2024 17:05:05 -0700 Subject: [PATCH] Prevent RobotMotion modal from starting in pause when customizing --- .../src/Pages/Home/MealStates/RobotMotion.jsx | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/feedingwebapp/src/Pages/Home/MealStates/RobotMotion.jsx b/feedingwebapp/src/Pages/Home/MealStates/RobotMotion.jsx index b99958a9..85aba9db 100644 --- a/feedingwebapp/src/Pages/Home/MealStates/RobotMotion.jsx +++ b/feedingwebapp/src/Pages/Home/MealStates/RobotMotion.jsx @@ -59,6 +59,10 @@ const RobotMotion = (props) => { actionStatus: null }) + // Track the number of times the action has been called, to ensure that we do not + // process responses that are received before the action is called. + const actionCallCount = useRef(0) + // Get the relevant global variables const paused = useGlobalState((state) => state.paused) const setPaused = useGlobalState((state) => state.setPaused) @@ -147,6 +151,10 @@ const RobotMotion = (props) => { */ const responseCallback = useCallback( (response) => { + if (actionCallCount.current === 0) { + console.log('Ignoring response message because an action has not yet been called', response) + return + } console.log('Got response message', response) if (response.response_type === 'result' && response.values.status === MOTION_STATUS_SUCCESS) { setActionStatus({ @@ -173,7 +181,7 @@ const RobotMotion = (props) => { } } }, - [setLastMotionActionResponse, setActionStatus, setPaused, robotMotionDone] + [actionCallCount, setLastMotionActionResponse, setActionStatus, setPaused, robotMotionDone] ) /** @@ -203,6 +211,7 @@ const RobotMotion = (props) => { const callRobotMotionAction = useCallback( (feedbackCb, responseCb) => { if (!paused) { + actionCallCount.current += 1 setActionStatus({ actionStatus: ROS_ACTION_STATUS_EXECUTE }) @@ -210,7 +219,7 @@ const RobotMotion = (props) => { callROSAction(robotMotionAction, props.actionInput, feedbackCb, responseCb) } }, - [paused, robotMotionAction, props.actionInput] + [actionCallCount, paused, robotMotionAction, props.actionInput] ) /**