.. _set_on_monitoring_state: set_on_monitoring_state ------------------------------------------ This function registers a callback that automatically detects changes in the operation state of the robot controller. It is useful for executing specific actions automatically when the robot’s state changes. **Definition** |br| ``DRFLEx.h`` within class `CDRFLEx`, public section .. code-block:: cpp void set_on_monitoring_state(TOnMonitoringStateCB pCallbackFunc) { _set_on_monitoring_state(_rbtCtrl, pCallbackFunc); }; **Parameter** .. list-table:: :widths: 20 20 20 40 :header-rows: 1 * - **Parameter Name** - **Data Type** - **Default Value** - **Description** * - pCallbackFunc - :ref:`TOnMonitoringStateCB ` - - - Callback function pointer to be registered for robot state monitoring **Return** |br| None **Example** .. code-block:: cpp std::atomic g_bNeedServoOn(false); void OnMonitoringStateCB(const ROBOT_STATE eState) { switch ((unsigned char)eState) { case STATE_SAFE_OFF: // Request servo on from the main loop g_bNeedServoOn = true; break; default: break; } } drfl.set_on_monitoring_state(OnMonitoringStateCB); // Main loop: call set_robot_control outside the callback while (true) { if (g_bNeedServoOn) { g_bNeedServoOn = false; drfl.set_robot_control(CONTROL_SERVO_ON); } std::this_thread::sleep_for(std::chrono::milliseconds(100)); } This example registers a **state monitoring callback** that sets a flag when the controller enters the **SAFE OFF** state; the main loop then turns on the robot servo. Call set_robot_control outside the callback; see :ref:`Rules for callback functions `.