@@ -221,6 +221,9 @@ void OBCameraNode::setupCameraCtrlServices() {
221221 std::shared_ptr<SetString::Response> response) {
222222 setReadCustomerData (request, response);
223223 });
224+ change_state_srv_ = node_->create_service <lifecycle_msgs::srv::ChangeState>(
225+ " change_state" , std::bind (&OBCameraNode::handleChangeStateRequest, this ,
226+ std::placeholders::_1, std::placeholders::_2));
224227}
225228
226229void OBCameraNode::setExposureCallback (const std::shared_ptr<SetInt32::Request>& request,
@@ -1069,6 +1072,63 @@ void OBCameraNode::resetCaptureServiceVariables() {
10691072 number_of_depth_frames_captured_ = 0 ;
10701073}
10711074
1075+ void OBCameraNode::handleChangeStateRequest (
1076+ const std::shared_ptr<lifecycle_msgs::srv::ChangeState::Request> request,
1077+ std::shared_ptr<lifecycle_msgs::srv::ChangeState::Response> response) {
1078+
1079+ RCLCPP_INFO (logger_, " Received request to change state '%d' with label '%s'" ,
1080+ request->transition .id , request->transition .label .c_str ());
1081+
1082+ response->success = true ;
1083+ if (request->transition .id == lifecycle_msgs::msg::Transition::TRANSITION_ACTIVATE) {
1084+ RCLCPP_INFO_STREAM (logger_, " Recieved request to turn ON camera streams" );
1085+ if (pipeline_started_){
1086+ RCLCPP_WARN_STREAM (logger_, " Camera streams already ON" );
1087+ response->success = true ;
1088+ return ;
1089+ }
1090+ try {
1091+ setupProfiles ();
1092+ startStreams ();
1093+ RCLCPP_INFO_STREAM (logger_, " Camera streams are now ON" );
1094+ } catch (const ob::Error& e) {
1095+ response->success = false ;
1096+ RCLCPP_ERROR_STREAM (logger_, " Failed to start camera streams: " << e.getMessage ());
1097+ } catch (const std::exception& e) {
1098+ response->success = false ;
1099+ RCLCPP_ERROR_STREAM (logger_, " Failed to start camera streams: " << e.what ());
1100+ } catch (...) {
1101+ response->success = false ;
1102+ RCLCPP_ERROR_STREAM (logger_, " Failed to start camera streams: Unknown Error" );
1103+ }
1104+ }
1105+ else if (request->transition .id == lifecycle_msgs::msg::Transition::TRANSITION_DEACTIVATE) {
1106+ RCLCPP_INFO_STREAM (logger_, " Recieved request to turn OFF camera streams" );
1107+ if (!pipeline_ || !pipeline_started_){
1108+ RCLCPP_WARN_STREAM (logger_, " Camera streams already OFF" );
1109+ response->success = true ;
1110+ return ;
1111+ }
1112+ try {
1113+ stopStreams ();
1114+ RCLCPP_INFO_STREAM (logger_, " Camera streams are now OFF" );
1115+ } catch (const ob::Error& e) {
1116+ response->success = false ;
1117+ RCLCPP_ERROR_STREAM (logger_, " Failed to stop camera streams: " << e.getMessage ());
1118+ } catch (const std::exception& e) {
1119+ response->success = false ;
1120+ RCLCPP_ERROR_STREAM (logger_, " Failed to stop camera streams: " << e.what ());
1121+ } catch (...) {
1122+ response->success = false ;
1123+ RCLCPP_ERROR_STREAM (logger_, " Failed to stop camera streams: Unknown Error" );
1124+ }
1125+ }
1126+ else {
1127+ response->success = false ;
1128+ RCLCPP_ERROR_STREAM (logger_, " Unsupported transition ID: " << request->transition .id );
1129+ }
1130+ }
1131+
10721132void OBCameraNode::sendSoftwareTriggerCallback (
10731133 const std::shared_ptr<CameraTrigger::Request>& request,
10741134 std::shared_ptr<CameraTrigger::Response>& response) {
0 commit comments