Skip to content

Commit cde96aa

Browse files
authored
Reset Workspace Walls When Customizing Configurations (#139)
Have the app recompute workspace walls after succesfully saving new configurations
1 parent d46b8cd commit cde96aa

File tree

4 files changed

+83
-11
lines changed

4 files changed

+83
-11
lines changed

feedingwebapp/src/Pages/Constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ export const START_CARTESIAN_CONTROLLER_ACTION_NAME = 'ActivateCartesianControll
9191
export const START_CARTESIAN_CONTROLLER_ACTION_TYPE = 'ada_feeding_msgs/action/Trigger'
9292
export const START_JOINT_CONTROLLER_ACTION_NAME = 'ActivateJointController'
9393
export const START_JOINT_CONTROLLER_ACTION_TYPE = 'ada_feeding_msgs/action/Trigger'
94+
export const RECOMPUTE_WORKSPACE_WALLS_ACTION_NAME = 'recompute_workspace_walls'
95+
export const RECOMPUTE_WORKSPACE_WALLS_ACTION_TYPE = 'ada_feeding_msgs/action/Trigger'
9496

9597
/**
9698
* For states that call ROS services, this dictionary contains

feedingwebapp/src/Pages/Settings/CustomizeConfiguration.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ const CustomizeConfiguration = (props) => {
446446
localParamValues={currentConfigurationParams}
447447
setLocalParamValues={setCurrentConfigurationParams}
448448
resetToPresetSuccessCallback={resetToPresetSuccessCallback}
449+
resetWorkspaceWallsOnParameterUpdate={true}
449450
>
450451
{renderSettings()}
451452
</SettingsPageParent>

feedingwebapp/src/Pages/Settings/SettingsPageParent.jsx

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,22 @@ import PropTypes from 'prop-types'
1111
import { View } from 'react-native'
1212

1313
// Local imports
14-
import { useROS, createROSService, createROSServiceRequest, getValueFromParameter, getParameterFromValue } from '../../ros/ros_helpers'
14+
import {
15+
useROS,
16+
createROSService,
17+
createROSServiceRequest,
18+
getValueFromParameter,
19+
getParameterFromValue,
20+
callROSAction,
21+
createROSActionClient,
22+
createROSMessage,
23+
destroyActionClient
24+
} from '../../ros/ros_helpers'
1525
import {
1626
GET_PARAMETERS_SERVICE_NAME,
1727
GET_PARAMETERS_SERVICE_TYPE,
28+
RECOMPUTE_WORKSPACE_WALLS_ACTION_NAME,
29+
RECOMPUTE_WORKSPACE_WALLS_ACTION_TYPE,
1830
SET_PARAMETERS_SERVICE_NAME,
1931
SET_PARAMETERS_SERVICE_TYPE
2032
} from '../Constants'
@@ -30,6 +42,7 @@ const SettingsPageParent = (props) => {
3042

3143
// Get relevant local state variables
3244
const isResettingToPreset = useRef(false)
45+
const numTimesResetWorkspaceWalls = useRef(0)
3346

3447
// Flag to check if the current orientation is portrait
3548
const isPortrait = useMediaQuery({ query: '(orientation: portrait)' })
@@ -49,6 +62,14 @@ const SettingsPageParent = (props) => {
4962
let getParametersService = useRef(createROSService(ros.current, GET_PARAMETERS_SERVICE_NAME, GET_PARAMETERS_SERVICE_TYPE))
5063
let setParametersService = useRef(createROSService(ros.current, SET_PARAMETERS_SERVICE_NAME, SET_PARAMETERS_SERVICE_TYPE))
5164

65+
/**
66+
* Create the ROS Action Client. This is created in useRef to avoid
67+
* re-creating it upon re-renders.
68+
*/
69+
let recomputeWorkspaceWallsAction = useRef(
70+
createROSActionClient(ros.current, RECOMPUTE_WORKSPACE_WALLS_ACTION_NAME, RECOMPUTE_WORKSPACE_WALLS_ACTION_TYPE)
71+
)
72+
5273
/**
5374
* Get the paramater values in the specified namespace or, if they don't exist,
5475
* in the default namespace. Set the local state to these values.
@@ -130,20 +151,50 @@ const SettingsPageParent = (props) => {
130151
console.log('Sending SetParameter request', currentRequest)
131152
service.callService(currentRequest, (response) => {
132153
console.log('For request', currentRequest, 'received SetParameter response', response)
133-
// If this is wrapping up a reset to preset, call the callback
134-
if (isResettingToPreset.current) {
135-
let resetToPresetSuccessCallback = props.resetToPresetSuccessCallback.current
136-
resetToPresetSuccessCallback()
137-
isResettingToPreset.current = false
154+
// If it was succesful, and if the props specify, then call the action to update the
155+
// workspace walls.
156+
if (response.result.successful) {
157+
if (props.resetWorkspaceWallsOnParameterUpdate) {
158+
console.log('Calling recompute_workspace_walls action')
159+
// numTimesResetWorkspaceWalls is used to ensure the result callback
160+
// only gets registered the first time. This is okay because none of the
161+
// values in the callback are expected to change.
162+
callROSAction(
163+
recomputeWorkspaceWallsAction.current,
164+
createROSMessage({}),
165+
null,
166+
numTimesResetWorkspaceWalls.current > 0
167+
? null
168+
: (result) => {
169+
console.log('Received result from recompute_workspace_walls action', result)
170+
// If this is wrapping up a reset to preset, call the callback
171+
if (isResettingToPreset.current) {
172+
let resetToPresetSuccessCallback = props.resetToPresetSuccessCallback.current
173+
resetToPresetSuccessCallback()
174+
isResettingToPreset.current = false
175+
}
176+
}
177+
)
178+
numTimesResetWorkspaceWalls.current++
179+
} else {
180+
// If this is wrapping up a reset to preset, call the callback
181+
if (isResettingToPreset.current) {
182+
let resetToPresetSuccessCallback = props.resetToPresetSuccessCallback.current
183+
resetToPresetSuccessCallback()
184+
isResettingToPreset.current = false
185+
}
186+
}
138187
}
139188
})
140189
}, [
141-
props.paramNames,
190+
isResettingToPreset,
191+
numTimesResetWorkspaceWalls,
142192
props.localParamValues,
143-
settingsPresets,
193+
props.paramNames,
194+
props.resetToPresetSuccessCallback,
195+
props.resetWorkspaceWallsOnParameterUpdate,
144196
setParametersService,
145-
isResettingToPreset,
146-
props.resetToPresetSuccessCallback
197+
settingsPresets
147198
])
148199

149200
/**
@@ -155,6 +206,16 @@ const SettingsPageParent = (props) => {
155206
setGlobalParameter()
156207
}, [setGlobalParameter])
157208

209+
/**
210+
* When the component is unmounted, destroy the action client.
211+
*/
212+
useEffect(() => {
213+
let action = recomputeWorkspaceWallsAction.current
214+
return () => {
215+
destroyActionClient(action)
216+
}
217+
}, [])
218+
158219
/**
159220
* A callback for when the user asks to reset parameters to a preset.
160221
*/
@@ -294,6 +355,9 @@ SettingsPageParent.propTypes = {
294355
resetToPresetSuccessCallback: PropTypes.shape({
295356
current: PropTypes.func.isRequired
296357
}),
358+
// A prop to specify whether to invoke the action that resets workspace walls
359+
// when the global parameters have been updated
360+
resetWorkspaceWallsOnParameterUpdate: PropTypes.bool,
297361
// A function to call when the user is done with the page
298362
doneCallback: PropTypes.func.isRequired
299363
}
@@ -303,7 +367,8 @@ SettingsPageParent.defaultProps = {
303367
modalChildren: <></>,
304368
resetToPresetSuccessCallback: {
305369
current: () => {}
306-
}
370+
},
371+
resetWorkspaceWallsOnParameterUpdate: false
307372
}
308373

309374
export default SettingsPageParent

feedingwebapp/src/ros/ros_helpers.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ export function createROSServiceRequest(data) {
115115
return new ROSLIB.ServiceRequest(data)
116116
}
117117

118+
// TODO: Should we add a `destroyService` function like we have for the action client?
119+
// The difference is that roslibjs explicitly provides a function to destroy the action
120+
// client, but only provides a function to unadvertise a service client.
121+
118122
/**
119123
* Create a ROS Action Client.
120124
*

0 commit comments

Comments
 (0)