Skip to content

Commit

Permalink
Call AcquisitionReport service after food-on-fork question (#121)
Browse files Browse the repository at this point in the history
* Added Toast to check logic

* Add AcquisitionReport service call

* Fix response processing
  • Loading branch information
egordon authored Jan 19, 2024
1 parent 19b671d commit 90c95e2
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 7 deletions.
7 changes: 4 additions & 3 deletions feedingwebapp/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions feedingwebapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"axios": "^1.6.5",
"body-parser": "^1.20.2",
"bootstrap": "^5.1.3",
"caniuse-lite": "^1.0.30001579",
"cors": "^2.8.5",
"express": "^4.18.2",
"mdb-react-ui-kit": "^3.0.0",
Expand Down
2 changes: 2 additions & 0 deletions feedingwebapp/src/Pages/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ ROS_SERVICE_NAMES[MEAL_STATE.R_DetectingFace] = {
export { ROS_SERVICE_NAMES }
export const CLEAR_OCTOMAP_SERVICE_NAME = 'clear_octomap'
export const CLEAR_OCTOMAP_SERVICE_TYPE = 'std_srvs/srv/Empty'
export const ACQUISITION_REPORT_SERVICE_NAME = 'ada_feeding_action_select/action_report'
export const ACQUISITION_REPORT_SERVICE_TYPE = 'ada_feeding_msgs/srv/AcquisitionReport'
export const GET_PARAMETERS_SERVICE_NAME = 'ada_feeding_action_servers/get_parameters'
export const GET_PARAMETERS_SERVICE_TYPE = 'rcl_interfaces/srv/GetParameters'
export const SET_PARAMETERS_SERVICE_NAME = 'ada_feeding_action_servers/set_parameters'
Expand Down
6 changes: 6 additions & 0 deletions feedingwebapp/src/Pages/GlobalState.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ export const useGlobalState = create(
// message received from the face detection node where a
// face was detected and within the distance bounds of the camera.
moveToMouthActionGoal: null,
// Last RobotMotion action response
lastMotionActionResponse: null,
// Whether or not the currently-executing robot motion was paused by the user
paused: false,
// Flag to indicate robot motion trough teleoperation interface
Expand Down Expand Up @@ -171,6 +173,10 @@ export const useGlobalState = create(
set(() => ({
biteAcquisitionActionGoal: biteAcquisitionActionGoal
})),
setLastMotionActionResponse: (lastMotionActionResponse) =>
set(() => ({
lastMotionActionResponse: lastMotionActionResponse
})),
setMoveToMouthActionGoal: (moveToMouthActionGoal) =>
set(() => ({
moveToMouthActionGoal: moveToMouthActionGoal
Expand Down
43 changes: 40 additions & 3 deletions feedingwebapp/src/Pages/Home/MealStates/BiteAcquisitionCheck.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// React Imports
import React, { useCallback } from 'react'
import React, { useCallback, useRef } from 'react'
import Button from 'react-bootstrap/Button'
import { useMediaQuery } from 'react-responsive'
import { toast } from 'react-toastify'
import { View } from 'react-native'

// Local Imports
import '../Home.css'
import { useGlobalState, MEAL_STATE } from '../../GlobalState'
import { MOVING_STATE_ICON_DICT } from '../../Constants'
import { useROS, createROSService, createROSServiceRequest } from '../../../ros/ros_helpers'
import { ACQUISITION_REPORT_SERVICE_NAME, ACQUISITION_REPORT_SERVICE_TYPE } from '../../Constants'

/**
* The BiteAcquisitionCheck component appears after the robot has attempted to
Expand All @@ -31,23 +34,57 @@ const BiteAcquisitionCheck = () => {
let iconWidth = isPortrait ? '28vh' : '28vw'
let iconHeight = isPortrait ? '18vh' : '18vw'

// Configure AcquisitionReport service
const lastMotionActionResponse = useGlobalState((state) => state.lastMotionActionResponse)
/**
* Connect to ROS, if not already connected. Put this in useRef to avoid
* re-connecting upon re-renders.
*/
const ros = useRef(useROS().ros)
/**
* Create the ROS Service Client for reporting success/failure
*/
let acquisitionReportService = useRef(createROSService(ros.current, ACQUISITION_REPORT_SERVICE_NAME, ACQUISITION_REPORT_SERVICE_TYPE))

/**
* Callback function for when the user indicates that the bite acquisition
* succeeded.
*/
const acquisitionSuccess = useCallback(() => {
console.log('acquisitionSuccess')
toast.info('Reporting Food Acquisition Success!')
// Create a service request
let request = createROSServiceRequest({
loss: 0.0,
action_index: lastMotionActionResponse.action_index,
posthoc: lastMotionActionResponse.posthoc,
id: lastMotionActionResponse.selection_id
})
// Call the service
let service = acquisitionReportService.current
service.callService(request, (response) => console.log('Got acquisition report response', response))
setMealState(MEAL_STATE.R_MovingToStagingConfiguration)
}, [setMealState])
}, [lastMotionActionResponse, setMealState])

/**
* Callback function for when the user indicates that the bite acquisition
* failed.
*/
const acquisitionFailure = useCallback(() => {
console.log('acquisitionFailure')
toast.info('Reporting Food Acquisition Failure.')
// Create a service request
let request = createROSServiceRequest({
loss: 1.0,
action_index: lastMotionActionResponse.action_index,
posthoc: lastMotionActionResponse.posthoc,
id: lastMotionActionResponse.selection_id
})
// Call the service
let service = acquisitionReportService.current
service.callService(request, (response) => console.log('Got acquisition report response', response))
setMealState(MEAL_STATE.R_MovingAbovePlate)
}, [setMealState])
}, [lastMotionActionResponse, setMealState])

/**
* Get the ready for bite text to render.
Expand Down
6 changes: 5 additions & 1 deletion feedingwebapp/src/Pages/Home/MealStates/RobotMotion.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ const RobotMotion = (props) => {
const paused = useGlobalState((state) => state.paused)
const setPaused = useGlobalState((state) => state.setPaused)

// Setter for last motion action response
const setLastMotionActionResponse = useGlobalState((state) => state.setLastMotionActionResponse)

/**
* Connect to ROS, if not already connected. Put this in useRef to avoid
* re-connecting upon re-renders.
Expand Down Expand Up @@ -143,6 +146,7 @@ const RobotMotion = (props) => {
setActionStatus({
actionStatus: ROS_ACTION_STATUS_SUCCEED
})
setLastMotionActionResponse(response.values)
robotMotionDone()
} else {
if (
Expand All @@ -163,7 +167,7 @@ const RobotMotion = (props) => {
}
}
},
[setActionStatus, setPaused, robotMotionDone]
[setLastMotionActionResponse, setActionStatus, setPaused, robotMotionDone]
)

/**
Expand Down

0 comments on commit 90c95e2

Please sign in to comment.