@@ -136,6 +136,7 @@ class MachineStateSwitchContext final
136136 using SHUTDOWN_REBOOT_CALLBACK = std::function<int (bool )>;
137137 using EXCEPTION_PLAYSOUND_CALLBACK = std::function<void (int32_t )>;
138138 using CONTROL_TAIL_LED_CALLBACK = std::function<void (bool , bool )>;
139+ using LED_SHUTDOWN_CALLBACK = std::function<bool (bool )>;
139140
140141public:
141142 explicit MachineStateSwitchContext (rclcpp::Node::SharedPtr node_ptr)
@@ -306,6 +307,10 @@ class MachineStateSwitchContext final
306307 {
307308 control_tail_led = callback;
308309 }
310+ void SetControlLedShutdownCallback (LED_SHUTDOWN_CALLBACK callback)
311+ {
312+ control_led_shutdown = callback;
313+ }
309314 void DogWakeup (const std_msgs::msg::Bool::SharedPtr msg)
310315 {
311316 if (machine_state_keep_) {
@@ -434,7 +439,7 @@ class MachineStateSwitchContext final
434439 }
435440 machine_state_keep_ = true ;
436441 prohibit_shutdown_ = true ;
437- INFO (" [MachineState-Switch]: keep machine state to start" );
442+ INFO (" [MachineState-Switch]: keep machine state to start, duration is %ds " , request-> ticks );
438443 keep_timer_ = mssc_node_->create_wall_timer (
439444 std::chrono::seconds (request->ticks ),
440445 std::bind (&MachineStateSwitchContext::KeepTimerCallback, this ), timer_callback_group_);
@@ -445,6 +450,15 @@ class MachineStateSwitchContext final
445450 response->success = true ;
446451 response->code = 0 ;
447452 } else {
453+ // 复用服务接口,强行退出状态保持
454+ if (request->ticks == 0 ) {
455+ machine_state_keep_ = false ;
456+ prohibit_shutdown_ = false ;
457+ INFO (" [MachineState-Switch]: request received, keep machine state to stop" );
458+ response->success = true ;
459+ response->code = 0 ;
460+ return ;
461+ }
448462 response->success = true ;
449463 response->code = -1 ;
450464 }
@@ -661,23 +675,27 @@ class MachineStateSwitchContext final
661675 if (mssc_machine_state == MsscMachineState::MSSC_OTA) {
662676 return ;
663677 }
664- // 状态机切换超时强制关机
665- std::thread t{[&]() {
666- std::this_thread::sleep_for (std::chrono::seconds (5 ));
667- shutdown_or_reboot (false );
668- }
669- };
670- t.detach ();
678+ // 展示关机灯效
679+ control_led_shutdown (true );
671680
672681 // 先返回关机结果,再执行关机
673682 std::thread t_shutdown{[&]() {
674- std::this_thread::sleep_for (std::chrono::seconds (3 ));
683+ std::this_thread::sleep_for (std::chrono::seconds (2 ));
675684 std::lock_guard<std::mutex> lck (state_mtx_);
676685 machine_state_handler_map[MachineStateChild::MSC_TEARDOWN]();
677686 shutdown_or_reboot (false );
678687 }
679688 };
680689 t_shutdown.detach ();
690+
691+ // 状态机切换超时强制关机
692+ std::thread t{[&]() {
693+ std::this_thread::sleep_for (std::chrono::seconds (15 ));
694+ WARN (" [MachineState-Switch]: shutdown timeout, force shutdown" );
695+ shutdown_or_reboot (false );
696+ }
697+ };
698+ t.detach ();
681699 response->success = true ;
682700 }
683701
@@ -691,23 +709,26 @@ class MachineStateSwitchContext final
691709 if (mssc_machine_state == MsscMachineState::MSSC_OTA) {
692710 return ;
693711 }
694-
695- // 状态机切换超时强制重启
696- std::thread t{[&]() {
697- std::this_thread::sleep_for (std::chrono::seconds (5 ));
698- shutdown_or_reboot (true );
699- }
700- };
701- t.detach ();
712+ // 展示关机灯效
713+ control_led_shutdown (true );
702714
703715 std::thread t_reboot{[&]() {
704- std::this_thread::sleep_for (std::chrono::seconds (3 ));
716+ std::this_thread::sleep_for (std::chrono::seconds (2 ));
705717 std::lock_guard<std::mutex> lck (state_mtx_);
706718 machine_state_handler_map[MachineStateChild::MSC_TEARDOWN]();
707719 shutdown_or_reboot (true );
708720 }
709721 };
710722 t_reboot.detach ();
723+
724+ // 状态机切换超时强制重启
725+ std::thread t{[&]() {
726+ WARN (" [MachineState-Switch]: reboot timeout, force reboot" );
727+ std::this_thread::sleep_for (std::chrono::seconds (15 ));
728+ shutdown_or_reboot (true );
729+ }
730+ };
731+ t.detach ();
711732 response->success = true ;
712733 }
713734
@@ -877,6 +898,7 @@ class MachineStateSwitchContext final
877898 EXCEPTION_PLAYSOUND_CALLBACK play_sound {[](int32_t ) {}};
878899 SHUTDOWN_REBOOT_CALLBACK shutdown_or_reboot {[](bool ) {return 0 ;}};
879900 CONTROL_TAIL_LED_CALLBACK control_tail_led {[](bool , bool ) {}};
901+ LED_SHUTDOWN_CALLBACK control_led_shutdown {[](bool ) {return 0 ;}};
880902 std::unique_ptr<cyberdog::manager::StateContext> machine_state_ptr_ {nullptr };
881903 bool machine_state_keep_ {false };
882904 bool prohibit_shutdown_ {false };
0 commit comments