Skip to content

Commit b173099

Browse files
committed
added ability to unsubscribe from a topic
1 parent 546a6d7 commit b173099

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

feeding_web_app_ros2_test/feeding_web_app_ros2_test/FaceDetection.py

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ def toggle_face_detection_callback(self, request, response):
7979
Callback function for the ToggleFaceDetection service. Safely toggles
8080
the face detection on or off depending on the request.
8181
"""
82+
self.get_logger().info(
83+
"Incoming service request. turn_on: %s" % (request.turn_on)
84+
)
8285
if request.turn_on:
8386
# Reset counters
8487
self.num_consecutive_images_without_face = 0

feedingwebapp/src/Pages/Home/MealStates/BiteInitiation.jsx

+14-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ import Row from 'react-bootstrap/Row'
88

99
// Local Imports
1010
import '../Home.css'
11-
import { connectToROS, createROSService, createROSServiceRequest, subscribeToROSTopic } from '../../../ros/ros_helpers'
11+
import {
12+
connectToROS,
13+
createROSService,
14+
createROSServiceRequest,
15+
subscribeToROSTopic,
16+
unsubscribeFromROSTopic
17+
} from '../../../ros/ros_helpers'
1218
import { convertRemToPixels, scaleWidthHeightToWindow } from '../../../helpers'
1319
import {
1420
FACE_DETECTION_IMG_TOPIC,
@@ -63,7 +69,13 @@ const BiteInitiation = (props) => {
6369
[setDetectedMouthCenter, setMouthDetected, readyForBite]
6470
)
6571
useEffect(() => {
66-
subscribeToROSTopic(ros.current, FACE_DETECTION_TOPIC, FACE_DETECTION_TOPIC_MSG, faceDetectionCallback)
72+
let topic = subscribeToROSTopic(ros.current, FACE_DETECTION_TOPIC, FACE_DETECTION_TOPIC_MSG, faceDetectionCallback)
73+
// In practice, because the values passed in in the second argument of
74+
// useEffect will not change on re-renders, this return statement will
75+
// only be called when the component unmounts.
76+
return () => {
77+
unsubscribeFromROSTopic(topic, faceDetectionCallback)
78+
}
6779
}, [faceDetectionCallback])
6880

6981
// Create the ROS Service. This is created in local state to avoid

feedingwebapp/src/ros/ros_helpers.js

+10
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ export function subscribeToROSTopic(ros, topicName, topicType, callback) {
7575
return topic
7676
}
7777

78+
/**
79+
* Unsubscribe from a ROS topic.
80+
*
81+
* @param {object} topic The ROSLIB.Topic.
82+
* @param {function} callback The callback function to unsubscribe.
83+
*/
84+
export function unsubscribeFromROSTopic(topic, callback) {
85+
topic.unsubscribe(callback)
86+
}
87+
7888
/**
7989
* Create a ROS Service.
8090
*

0 commit comments

Comments
 (0)