ChatGPT~Error1015You are being rate limited

文章讲述了在使用Chatgpt时遇到Youarebeingratelimited错误的原因,主要是因为使用过于频繁。解决方案建议减少使用频率或等待,同时提及可能与网络状况有关。作者分享了自己的经历并鼓励读者保持良好心态。
部署运行你感兴趣的模型镜像

目录

问题背景

问题的原因

下来说说解决方案

总结


问题背景

今天使用Chatgpt的时候突然出现"You are being rate limited"的错误提示。

问题的原因

小问题了,又不是第一次被弄出来了,莫慌。

让我们先看看Chatgpt自己是怎么解释这个问题的:

 简单点说就是用的太多,被卡出来了,也是最近正在搞数学建模,使用的频率确实频繁。

下来说说解决方案

先看看Chatgpt自己是怎么说的:

好吧:如果你着急使用Chatgpt4那么你可以试着使用上面的方法,如果你不着急使用,等到明天自己就好了,不用太担心。

总结

心态放好,这种情况一般也不会发生,它总会发生在你着急使用它的时候,没错,当你着急用它解决问题的时候,它就会耍小脾气,给它点时间让他冷静一下就好了。

其实这和网络也有点关系,比方说现在我正在写blog,页面底下还时不时跳出一个,页面请求超时的红色提醒,这就很耐人寻味了。

本章的内容就到这里了,觉得对你有帮助的话就支持一下博主把~

这是我的GitHub网址,感兴趣的话可以给我个star✨

🌌点击下方个人名片,交流会更方便哦~
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓

 

您可能感兴趣的与本文相关的镜像

GPT-oss:20b

GPT-oss:20b

图文对话
Gpt-oss

GPT OSS 是OpenAI 推出的重量级开放模型,面向强推理、智能体任务以及多样化开发场景

#pragma once #include <AP_Common/AP_Common.h> #include <AP_Param/AP_Param.h> #include <AP_Math/AP_Math.h> #include <AC_PID/AC_P.h> // P library #include <AC_PID/AC_PID.h> // PID library #include <AC_PID/AC_P_1D.h> // P library (1-axis) #include <AC_PID/AC_P_2D.h> // P library (2-axis) #include <AC_PID/AC_PI_2D.h> // PI library (2-axis) #include <AC_PID/AC_PID_Basic.h> // PID library (1-axis) #include <AC_PID/AC_PID_2D.h> // PID library (2-axis) #include <AP_Scripting/AP_Scripting_config.h> #include "AC_AttitudeControl.h" // Attitude control library #include <AP_Logger/LogStructure.h> // position controller default definitions #define POSCONTROL_ACCEL_NE_MSS 1.0f // default horizontal acceleration in m/s². This is overwritten by waypoint and loiter controllers #define POSCONTROL_JERK_NE_MSSS 5.0f // default horizontal jerk m/s³ #define POSCONTROL_STOPPING_DIST_UP_MAX_M 3.0f // max stopping distance (in m) vertically while climbing #define POSCONTROL_STOPPING_DIST_DOWN_MAX_M 2.0f // max stopping distance (in m) vertically while descending #define POSCONTROL_SPEED_MS 5.0f // default horizontal speed in m/s #define POSCONTROL_SPEED_DOWN_MS 1.5f // default descent rate in m/s #define POSCONTROL_SPEED_UP_MS 2.5f // default climb rate in m/s #define POSCONTROL_ACCEL_D_MSS 2.5f // default vertical acceleration in m/s² #define POSCONTROL_JERK_D_MSSS 5.0f // default vertical jerk m/s³ #define POSCONTROL_THROTTLE_CUTOFF_FREQ_HZ 2.0f // low-pass filter on acceleration error (unit: Hz) #define POSCONTROL_OVERSPEED_GAIN_U 2.0f // gain controlling rate at which z-axis speed is brought back within SPEED_UP and SPEED_DOWN range #define POSCONTROL_RELAX_TC 0.16f // This is used to decay the I term to 5% in half a second class AC_PosControl { public: /// Constructor AC_PosControl(AP_AHRS_View& ahrs, const class AP_Motors& motors, AC_AttitudeControl& attitude_control); // do not allow copying CLASS_NO_COPY(AC_PosControl); // Sets the timestep between controller updates (seconds). // This should match the IMU sample time used by the controller. void set_dt_s(float dt) { _dt_s = dt; } // Returns the timestep used in the controller update (seconds). float get_dt_s() const { return _dt_s; } // Updates internal NED position and velocity estimates from AHRS. // Falls back to vertical-only data if horizontal velocity or position is invalid or vibration forces it. // When high_vibes is true, forces use of vertical fallback for velocity. void update_estimates(bool high_vibes = false); // Returns the jerk limit for horizontal path shaping in m/s³. // Used to constrain acceleration changes in trajectory generation. float get_shaping_jerk_NE_msss() const { return _shaping_jerk_ne_msss; } /// /// 3D position shaper /// // Sets a new NED position target in meters and computes a jerk-limited trajectory. // Updates internal acceleration commands using a smooth kinematic path constrained // by configured acceleration and jerk limits. // The path can be offset vertically to follow the terrain by providing the current // terrain level in the NED frame and the terrain margin. Terrain margin is used to // constrain horizontal velocity to avoid vertical buffer violation. void input_pos_NED_m(const Vector3p& pos_ned_m, float pos_terrain_target_d_m, float terrain_margin_m); // Returns a scaling factor for horizontal velocity in m/s to ensure // the vertical controller maintains a safe distance above terrain. float terrain_scaler_D_m(float pos_terrain_d_m, float terrain_margin_m) const; /// /// Lateral position controller /// // Sets maximum horizontal speed (cm/s) and acceleration (cm/s²) for NE-axis shaping. // Can be called anytime; transitions are handled smoothly. // All arguments should be positive. // See NE_set_max_speed_accel_m() for full details. void NE_set_max_speed_accel_cm(float speed_cms, float accel_cmss); // Sets maximum horizontal speed (m/s) and acceleration (m/s²) for NE-axis shaping. // These values constrain the kinematic trajectory used by the lateral controller. // All arguments should be positive. void NE_set_max_speed_accel_m(float speed_ms, float accel_mss); // Sets horizontal correction limits for velocity (cm/s) and acceleration (cm/s²). // Should be called only during initialization to avoid control discontinuities. // All arguments should be positive. // See NE_set_correction_speed_accel_m() for full details. void NE_set_correction_speed_accel_cm(float speed_cms, float accel_cmss); // Sets horizontal correction limits for velocity (m/s) and acceleration (m/s²). // These values constrain the PID correction path, not the desired trajectory. // All arguments should be positive. void NE_set_correction_speed_accel_m(float speed_ms, float accel_mss); // Returns maximum horizontal speed in cm/s. // See NE_get_max_speed_ms() for full details. float NE_get_max_speed_cms() const { return NE_get_max_speed_ms() * 100.0; } // Returns maximum horizontal speed in m/s used for shaping the trajectory. float NE_get_max_speed_ms() const { return _vel_max_ne_ms; } // Returns maximum horizontal acceleration in m/s² used for trajectory shaping. float NE_get_max_accel_mss() const { return _accel_max_ne_mss; } // Sets maximum allowed horizontal position error in meters. // Used to constrain the output of the horizontal position P controller. void NE_set_pos_error_max_m(float error_max_m) { _p_pos_ne_m.set_error_max(error_max_m); } // Returns maximum allowed horizontal position error in meters. float NE_get_pos_error_max_m() const { return _p_pos_ne_m.get_error_max(); } // Initializes NE controller to a stationary stopping point with zero velocity and acceleration. // Use when the expected trajectory begins at rest but the starting position is unspecified. // The starting position can be retrieved with get_pos_target_NED_m(). void NE_init_controller_stopping_point(); // Smoothly decays NE acceleration over time to zero while maintaining current velocity and position. // Reduces output acceleration by ~95% over 0.5 seconds to avoid abrupt transitions. void NE_relax_velocity_controller(); // Softens NE controller for landing by reducing position error and suppressing I-term windup. // Used to make descent behavior more stable near ground contact. void NE_soften_for_landing(); // Fully initializes the NE controller with current position, velocity, acceleration, and attitude. // Intended for normal startup when the full state is known. // Private function shared by other NE initializers. void NE_init_controller(); // Sets the desired NE-plane acceleration in m/s² using jerk-limited shaping. // Smoothly transitions to the specified acceleration from current kinematic state. // Constraints: max acceleration and jerk set via NE_set_max_speed_accel_m(). void input_accel_NE_m(const Vector2f& accel_ne_msss); // Sets desired NE-plane velocity and acceleration (cm/s, cm/s²) using jerk-limited shaping. // See input_vel_accel_NE_m() for full details. void input_vel_accel_NE_cm(Vector2f& vel_ne_cms, const Vector2f& accel_ne_cmss, bool limit_output = true); // Sets desired NE-plane velocity and acceleration (m/s, m/s²) using jerk-limited shaping. // Calculates target acceleration using current kinematics constrained by acceleration and jerk limits. // If `limit_output` is true, applies limits to total command (desired + correction). void input_vel_accel_NE_m(Vector2f& vel_ne_ms, const Vector2f& accel_ne_mss, bool limit_output = true); // Sets desired NE position, velocity, and acceleration (cm, cm/s, cm/s²) with jerk-limited shaping. // See input_pos_vel_accel_NE_m() for full details. void input_pos_vel_accel_NE_cm(Vector2p& pos_ne_cm, Vector2f& vel_ne_cms, const Vector2f& accel_ne_cmss, bool limit_output = true); // Sets desired NE position, velocity, and acceleration (m, m/s, m/s²) with jerk-limited shaping. // Calculates acceleration trajectory based on current kinematics and constraints. // If `limit_output` is true, limits apply to full command (desired + correction). void input_pos_vel_accel_NE_m(Vector2p& pos_ne_m, Vector2f& vel_ne_ms, const Vector2f& accel_ne_mss, bool limit_output = true); // Returns true if the NE position controller has run in the last 5 control loop cycles. bool NE_is_active() const; // Disables NE position correction by setting the target position to the current position. // Useful to freeze positional control without disrupting velocity control. void NE_stop_pos_stabilisation(); // Disables NE position and velocity correction by setting target values to current state. // Useful to prevent further corrections and freeze motion stabilization in NE axes. void NE_stop_vel_stabilisation(); // Applies a scalar multiplier to the NE control loop. // Set to 0 to disable lateral control; 1 for full authority. void NE_set_control_scale_factor(float ne_control_scale_factor) { _ne_control_scale_factor = ne_control_scale_factor; } // Runs the NE-axis position controller, computing output acceleration from position and velocity errors. // Uses P and PID controllers to generate corrections which are added to feedforward velocity/acceleration. // Requires all desired targets to be pre-set using the input_* or set_* methods. void NE_update_controller(); /// /// Vertical position controller /// // Sets maximum climb/descent rate (cm/s) and vertical acceleration (cm/s²) for the U-axis. // Descent rate may be positive or negative and is always interpreted as a descent. // See D_set_max_speed_accel_m() for full details. // All values must be positive. void D_set_max_speed_accel_cm(float decent_speed_max_cms, float climb_speed_max_cms, float accel_max_u_cmss); // Sets maximum climb/descent rate (m/s) and vertical acceleration (m/s²) for the U-axis. // These values are used for jerk-limited kinematic shaping of the vertical trajectory. // All values must be positive. void D_set_max_speed_accel_m(float decent_speed_max_ms, float climb_speed_max_ms, float accel_max_d_mss); // Sets vertical correction velocity and acceleration limits (cm/s, cm/s²). // Should only be called during initialization to avoid discontinuities. // See D_set_correction_speed_accel_m() for full details. // All values must be positive. void D_set_correction_speed_accel_cm(float decent_speed_max_cms, float climb_speed_max_cms, float accel_max_u_cmss); // Sets vertical correction velocity and acceleration limits (m/s, m/s²). // These values constrain the correction output of the PID controller. // All values must be positive. void D_set_correction_speed_accel_m(float decent_speed_max_ms, float climb_speed_max_ms, float accel_max_d_mss); // Returns maximum vertical acceleration in m/s² used for shaping the climb/descent trajectory. float D_get_max_accel_mss() const { return _accel_max_d_mss; } // Returns maximum allowed positive (upward) position error in meters. float get_pos_error_up_m() const { return _p_pos_d_m.get_error_min(); } // Returns maximum allowed negative (downward) position error in meters. float get_pos_error_down_m() const { return _p_pos_d_m.get_error_max(); } // Returns maximum climb rate in cm/s. // See get_max_speed_up_ms() for full details. float get_max_speed_up_cms() const { return get_max_speed_up_ms() * 100.0; } // Returns maximum climb rate in m/s used for shaping the vertical trajectory. float get_max_speed_up_ms() const { return _vel_max_up_ms; } /// Returns maximum descent rate in m/s (zero or positive). float get_max_speed_down_ms() const { return _vel_max_down_ms; } // Initializes U-axis controller to current position, velocity, and acceleration, disallowing descent. // Used for takeoff or hold scenarios where downward motion is prohibited. void D_init_controller_no_descent(); // Initializes U-axis controller to a stationary stopping point with zero velocity and acceleration. // Used when the trajectory starts at rest but the initial altitude is unspecified. // The resulting position target can be retrieved with get_pos_target_NED_m(). void D_init_controller_stopping_point(); // Smoothly decays U-axis acceleration to zero over time while maintaining current vertical velocity. // Reduces requested acceleration by ~95% every 0.5 seconds to avoid abrupt transitions. // `throttle_setting` is used to determine whether to preserve positive acceleration in low-thrust cases. void D_relax_controller(float throttle_setting); // Fully initializes the U-axis controller with current position, velocity, acceleration, and attitude. // Used during standard controller activation when full state is known. // Private function shared by other vertical initializers. void D_init_controller(); // Sets the desired vertical acceleration in m/s² using jerk-limited shaping. // Smoothly transitions to the target acceleration from current kinematic state. // Constraints: max acceleration and jerk set via D_set_max_speed_accel_m(). void input_accel_D_m(float accel_d_mss); // Sets desired vertical velocity and acceleration (m/s, m/s²) using jerk-limited shaping. // Calculates required acceleration using current vertical kinematics. // If `limit_output` is true, limits apply to the combined (desired + correction) command. void input_vel_accel_D_m(float &vel_d_ms, float accel_d_mss, bool limit_output = true); // Generates a vertical trajectory using the given climb rate in cm/s and jerk-limited shaping. // Adjusts the internal target altitude based on integrated climb rate. // See D_set_pos_target_from_climb_rate_ms() for full details. void D_set_pos_target_from_climb_rate_cms(float climb_rate_cms); // Generates a vertical trajectory using the given climb rate in m/s and jerk-limited shaping. // Target altitude is updated over time by integrating the climb rate. void D_set_pos_target_from_climb_rate_ms(float climb_rate_ms, bool ignore_descent_limit = false); // Sets vertical position, velocity, and acceleration in cm using jerk-limited shaping. // See input_pos_vel_accel_D_m() for full details. void input_pos_vel_accel_U_cm(float &pos_u_cm, float &vel_u_cms, float accel_u_cmss, bool limit_output = true); // Sets vertical position, velocity, and acceleration in meters using jerk-limited shaping. // Calculates required acceleration using current state and constraints. // If `limit_output` is true, limits are applied to combined (desired + correction) command. void input_pos_vel_accel_D_m(float &pos_d_m, float &vel_d_ms, float accel_d_mss, bool limit_output = true); // Sets target altitude in cm using jerk-limited shaping to gradually move to the new position. // See D_set_alt_target_with_slew_m() for full details. void set_alt_target_with_slew_cm(float pos_u_cm); // Sets target altitude in meters using jerk-limited shaping. void D_set_alt_target_with_slew_m(float pos_u_m); // Returns true if the U-axis controller has run in the last 5 control loop cycles. bool D_is_active() const; // Runs the vertical (U-axis) position controller. // Computes output acceleration based on position and velocity errors using PID correction. // Feedforward velocity and acceleration are combined with corrections to produce a smooth vertical command. // Desired position, velocity, and acceleration must be set before calling. void D_update_controller(); /// /// Accessors /// // Sets externally computed NED position, velocity, and acceleration in meters, m/s, and m/s². // Use when path planning or shaping is done outside this controller. void set_pos_vel_accel_NED_m(const Vector3p& pos_ned_m, const Vector3f& vel_ned_ms, const Vector3f& accel_ned_mss); // Sets externally computed NE position, velocity, and acceleration in meters, m/s, and m/s². // Use when path planning or shaping is done outside this controller. void set_pos_vel_accel_NE_m(const Vector2p& pos_ne_m, const Vector2f& vel_ne_ms, const Vector2f& accel_ne_mss); /// Position // Returns the estimated position in NED frame, in meters relative to EKF origin. const Vector3p& get_pos_estimate_NED_m() const { return _pos_estimate_ned_m; } // Returns estimated altitude above EKF origin in meters. float get_pos_estimate_U_m() const { return -_pos_estimate_ned_m.z; } // Returns the target position in NED frame, in meters relative to EKF origin. const Vector3p& get_pos_target_NED_m() const { return _pos_target_ned_m; } // Sets the desired NE position in meters relative to EKF origin. void set_pos_desired_NE_m(const Vector2p& pos_desired_ne_m) { _pos_desired_ned_m.xy() = pos_desired_ne_m; } // Returns the desired position in NED frame, in meters relative to EKF origin. const Vector3p& get_pos_desired_NED_m() const { return _pos_desired_ned_m; } // Returns target altitude above EKF origin in centimeters. // See get_pos_target_U_m() for full details. float get_pos_target_U_cm() const { return get_pos_target_U_m() * 100.0; } // Returns target altitude above EKF origin in meters. float get_pos_target_U_m() const { return -_pos_target_ned_m.z; } // Sets desired altitude above EKF origin in centimeters. // See set_pos_desired_U_m() for full details. void set_pos_desired_U_cm(float pos_desired_u_cm) { set_pos_desired_U_m(pos_desired_u_cm * 0.01); } // Sets desired altitude above EKF origin in meters. void set_pos_desired_U_m(float pos_desired_u_m) { _pos_desired_ned_m.z = -pos_desired_u_m; } // Returns desired altitude above EKF origin in centimeters. // See get_pos_desired_U_m() for full details. float get_pos_desired_U_cm() const { return get_pos_desired_U_m() * 100.0; } // Returns desired altitude above EKF origin in meters. float get_pos_desired_U_m() const { return -_pos_desired_ned_m.z; } /// Stopping Point // Computes NE stopping point in meters based on current position, velocity, and acceleration. void get_stopping_point_NE_m(Vector2p &stopping_point_ne_m) const; // Computes vertical stopping point relative to EKF origin in meters, Down-positive. based on current velocity and acceleration. void get_stopping_point_D_m(postype_t &stopping_point_d_m) const; /// Position Error // Returns NED position error vector in meters between current and target positions. const Vector3f get_pos_error_NED_m() const { return Vector3f{_p_pos_ne_m.get_error().x, _p_pos_ne_m.get_error().y, _p_pos_d_m.get_error()}; } // Returns total NE-plane position error magnitude in meters. float get_pos_error_NE_m() const { return _p_pos_ne_m.get_error().length(); } // Returns vertical position error (altitude) in centimeters. // See get_pos_error_D_m() for full details. float get_pos_error_U_cm() const { return -get_pos_error_D_m() * 100.0; } // Returns vertical position error (altitude) in meters. float get_pos_error_D_m() const { return _p_pos_d_m.get_error(); } /// Velocity // Returns current velocity estimate in NED frame in m/s. const Vector3f& get_vel_estimate_NED_ms() const { return _vel_estimate_ned_ms; } // Returns current velocity estimate (Up) in m/s. float get_vel_estimate_U_ms() const { return -_vel_estimate_ned_ms.z; } // Sets desired velocity in NED frame in cm/s. // See set_vel_desired_NED_ms() for full details. void set_vel_desired_NEU_cms(const Vector3f &vel_desired_neu_cms) { set_vel_desired_NED_ms(Vector3f{vel_desired_neu_cms.x, vel_desired_neu_cms.y, -vel_desired_neu_cms.z} * 0.01); } // Sets desired velocity in NED frame in m/s. void set_vel_desired_NED_ms(const Vector3f &vel_desired_ned_ms) { _vel_desired_ned_ms = vel_desired_ned_ms; } // Sets desired horizontal (NE) velocity in m/s. void set_vel_desired_NE_ms(const Vector2f &vel_desired_ne_ms) { _vel_desired_ned_ms.xy() = vel_desired_ne_ms; } // Returns desired velocity in NEU frame in cm/s. // See get_vel_desired_NED_ms() for full details. const Vector3f get_vel_desired_NEU_cms() const { return Vector3f{_vel_desired_ned_ms.x, _vel_desired_ned_ms.y, -_vel_desired_ned_ms.z} * 100.0; } // Returns desired velocity in NED frame in m/s. const Vector3f& get_vel_desired_NED_ms() const { return _vel_desired_ned_ms; } // Returns desired velocity (Up) in m/s. float get_vel_desired_U_ms() const { return -_vel_desired_ned_ms.z; } // Returns velocity target in NEU frame in cm/s. // See get_vel_target_NED_ms() for full details. const Vector3f get_vel_target_NEU_cms() const { return Vector3f{_vel_target_ned_ms.x, _vel_target_ned_ms.y, -_vel_target_ned_ms.z} * 100.0; } // Returns velocity target in NED frame in m/s. const Vector3f& get_vel_target_NED_ms() const { return _vel_target_ned_ms; } // Sets desired vertical velocity (Up) in m/s. void set_vel_desired_D_ms(float vel_desired_d_ms) { _vel_desired_ned_ms.z = vel_desired_d_ms; } // Returns vertical velocity target (Up) in cm/s. // See get_vel_target_U_ms() for full details. float get_vel_target_U_cms() const { return get_vel_target_U_ms() * 100.0; } // Returns vertical velocity target (Up) in m/s. float get_vel_target_U_ms() const { return -_vel_target_ned_ms.z; } /// Acceleration // Sets desired horizontal acceleration in m/s². void set_accel_desired_NE_mss(const Vector2f &accel_desired_ne_mss) { _accel_desired_ned_mss.xy() = accel_desired_ne_mss; } // Returns target NED acceleration in m/s². const Vector3f& get_accel_target_NED_mss() const { return _accel_target_ned_mss; } /// Terrain // Sets both the terrain altitude and terrain target to the same value // (altitude above EKF origin in centimeters, Up-positive). // See set_pos_terrain_target_D_m() for full description. void set_pos_terrain_target_U_cm(float pos_terrain_target_u_cm) { set_pos_terrain_target_D_m(-pos_terrain_target_u_cm * 0.01); } // Sets both the terrain altitude and terrain target to the same value // (relative to EKF origin in meters, Down-positive). void set_pos_terrain_target_D_m(float pos_terrain_target_d_m) { _pos_terrain_target_d_m = pos_terrain_target_d_m; } // Initializes both the terrain altitude and terrain target to the same value // (altitude above EKF origin in centimeters, Up-positive). // See init_pos_terrain_D_m() for full description. void init_pos_terrain_U_cm(float pos_terrain_u_cm); // Initializes both the terrain altitude and terrain target to the same value // (relative to EKF origin in meters, Down-positive). void init_pos_terrain_D_m(float pos_terrain_d_m); // Returns the current terrain altitude (Down-positive, relative to EKF origin, in meters). float get_pos_terrain_D_m() const { return _pos_terrain_d_m; } /// Offset #if AP_SCRIPTING_ENABLED // Sets additional position, velocity, and acceleration offsets in meters (NED frame) for scripting. // Offsets are added to the controller’s internal target. // Used in LUA bool set_posvelaccel_offset(const Vector3f &pos_offset_NED_m, const Vector3f &vel_offset_NED_ms, const Vector3f &accel_offset_NED_mss); // Retrieves current scripted offsets in meters (NED frame). // Used in LUA bool get_posvelaccel_offset(Vector3f &pos_offset_NED_m, Vector3f &vel_offset_NED_ms, Vector3f &accel_offset_NED_mss); // Retrieves current target velocity (NED frame, m/s) including any scripted offset. // Used in LUA bool get_vel_target(Vector3f &vel_target_NED_ms); // Retrieves current target acceleration (NED frame, m/s²) including any scripted offset. // Used in LUA bool get_accel_target(Vector3f &accel_target_NED_mss); #endif // Sets NE offset targets in meters, m/s, and m/s². void set_posvelaccel_offset_target_NE_m(const Vector2p& pos_offset_target_ne_m, const Vector2f& vel_offset_target_ne_ms, const Vector2f& accel_offset_target_ne_mss); // Sets vertical offset targets (m, m/s, m/s²) relative to EKF origin in meters, Down-positive. void set_posvelaccel_offset_target_D_m(float pos_offset_target_d_m, float vel_offset_target_d_ms, float accel_offset_target_d_mss); // Returns current NED position offset in meters. const Vector3p& get_pos_offset_NED_m() const { return _pos_offset_ned_m; } // Returns current NED velocity offset in m/s. const Vector3f& get_vel_offset_NED_ms() const { return _vel_offset_ned_ms; } // Returns current NED acceleration offset in m/s². const Vector3f& get_accel_offset_NED_mss() const { return _accel_offset_ned_mss; } // Sets vertical position offset in meters relative to EKF origin in meters, Down-positive. void set_pos_offset_D_m(float pos_offset_d_m) { _pos_offset_ned_m.z = pos_offset_d_m; } // Returns vertical position offset in meters relative to EKF origin in meters, Down-positive. float get_pos_offset_U_m() const { return -_pos_offset_ned_m.z; } // Returns vertical velocity offset in m/s. float get_vel_offset_D_ms() const { return _vel_offset_ned_ms.z; } // Returns vertical acceleration offset in m/s². float get_accel_offset_D_mss() const { return _accel_offset_ned_mss.z; } /// Outputs // Returns desired roll angle in radians for the attitude controller float get_roll_rad() const { return _roll_target_rad; } // Returns desired pitch angle in radians for the attitude controller. float get_pitch_rad() const { return _pitch_target_rad; } // Returns desired yaw angle in radians for the attitude controller. float get_yaw_rad() const { return _yaw_target_rad; } // Returns desired yaw rate in radians/second for the attitude controller. float get_yaw_rate_rads() const { return _yaw_rate_target_rads; } // Returns desired roll angle in centidegrees for the attitude controller. // See get_roll_rad() for full details. float get_roll_cd() const { return rad_to_cd(_roll_target_rad); } // Returns desired pitch angle in centidegrees for the attitude controller. // See get_pitch_rad() for full details. float get_pitch_cd() const { return rad_to_cd(_pitch_target_rad); } // Returns desired thrust direction as a unit vector in the body frame. Vector3f get_thrust_vector() const; // Returns bearing from current position to position target in radians. // 0 = North, positive = clockwise. float get_bearing_to_target_rad() const; // Returns the maximum allowed roll/pitch angle in radians. float get_lean_angle_max_rad() const; // Overrides the maximum allowed roll/pitch angle in radians. // A value of 0 reverts to using the ANGLE_MAX parameter. void set_lean_angle_max_rad(float angle_max_rad) { _angle_max_override_rad = angle_max_rad; } // Overrides the maximum allowed roll/pitch angle in degrees. // See set_lean_angle_max_rad() for full details. void set_lean_angle_max_deg(const float angle_max_deg) { set_lean_angle_max_rad(radians(angle_max_deg)); } // Overrides the maximum allowed roll/pitch angle in centidegrees. // See set_lean_angle_max_rad() for full details. void set_lean_angle_max_cd(const float angle_max_cd) { set_lean_angle_max_rad(cd_to_rad(angle_max_cd)); } /// Other // Returns reference to the NE position P controller. AC_P_2D& NE_get_pos_p() { return _p_pos_ne_m; } // Returns reference to the D (vertical) position P controller. AC_P_1D& D_get_pos_p() { return _p_pos_d_m; } // Returns reference to the NE velocity PID controller. AC_PID_2D& NE_get_vel_pid() { return _pid_vel_ne_m; } // Returns reference to the D (vertical) velocity PID controller. AC_PID_Basic& D_get_vel_pid() { return _pid_vel_d_m; } // Returns reference to the D acceleration PID controller. AC_PID& D_get_accel_pid() { return _pid_accel_d_m; } // Marks that NE acceleration has been externally limited. // Prevents I-term windup by storing the current target direction. void NE_set_externally_limited() { _limit_vector_ned.x = _accel_target_ned_mss.x; _limit_vector_ned.y = _accel_target_ned_mss.y; } // Converts lean angles (rad) to NED acceleration in m/s². Vector3f lean_angles_rad_to_accel_NED_mss(const Vector3f& att_target_euler_rad) const; // Writes position controller diagnostic logs (PSCN, PSCE, etc). void write_log(); // Performs pre-arm checks for position control parameters and EKF readiness. // Returns false if failure_msg is populated. bool pre_arm_checks(const char *param_prefix, char *failure_msg, const uint8_t failure_msg_len); // Enables or disables vibration compensation mode. // When enabled, disables use of horizontal velocity estimates. void set_vibe_comp(bool on_off) { _vibe_comp_enabled = on_off; } // Reset handling method enum class EKFResetMethod : uint8_t { MoveTarget = 0, // the target position is reset so the vehicle does not physically move MoveVehicle = 1 // the target position is smoothly transitioned so the vehicle moves to its previous position coordinates }; void set_reset_handling_method(EKFResetMethod reset_method) { _ekf_reset_method = reset_method; } // Returns confidence (0–1) in vertical control authority based on output usage. // Used to assess throttle margin and PID effectiveness. float get_vel_D_control_ratio() const { return constrain_float(_vel_d_control_ratio, 0.0f, 1.0f); } // Returns lateral distance to closest point on active trajectory in meters. // Used to assess horizontal deviation from path. float crosstrack_error_m() const; // Resets NED position controller state to prevent transients when exiting standby. // Zeros I-terms and aligns targets to current position. void NED_standby_reset(); // Returns measured vertical (Down) acceleration in m/s² (Earth frame, gravity-compensated). // Positive = downward acceleration. float get_estimated_accel_D_mss() const { return _ahrs.get_accel_ef().z + GRAVITY_MSS; } // Returns measured vertical (Up) acceleration in m/s² (Earth frame, gravity-compensated). // Positive = upward acceleration. float get_estimated_accel_U_mss() const { return -get_estimated_accel_D_mss(); } // Returns true if the requested forward pitch is limited by the configured tilt constraint. bool get_fwd_pitch_is_limited() const; // Sets artificial NE position disturbance in meters. void set_disturb_pos_NE_m(const Vector2f& disturb_pos_m) { _disturb_pos_ne_m = disturb_pos_m; } // Sets artificial NE velocity disturbance in m/s. void set_disturb_vel_NE_ms(const Vector2f& disturb_vel_ms) { _disturb_vel_ne_ms = disturb_vel_ms; } // Logs position controller state along the North axis to PSCN.. // Logs desired, target, and actual position [m], velocity [m/s], and acceleration [m/s²]. static void Write_PSCN(float pos_desired_m, float pos_target_m, float pos_m, float vel_desired_ms, float vel_target_ms, float vel_ms, float accel_desired_mss, float accel_target_mss, float accel_mss); // Logs position controller state along the East axis to PSCE. // Logs desired, target, and actual values for position [m], velocity [m/s], and acceleration [m/s²]. static void Write_PSCE(float pos_desired_m, float pos_target_m, float pos_m, float vel_desired_ms, float vel_target_ms, float vel_ms, float accel_desired_mss, float accel_target_mss, float accel_mss); // Logs position controller state along the Down (vertical) axis to PSCD. // Logs desired, target, and actual values for position [m], velocity [m/s], and acceleration [m/s²]. static void Write_PSCD(float pos_desired_m, float pos_target_m, float pos_m, float vel_desired_ms, float vel_target_ms, float vel_ms, float accel_desired_mss, float accel_target_mss, float accel_mss); // Logs offset tracking along the North axis to PSON. // Logs target and actual offset for position [m], velocity [m/s], and acceleration [m/s²]. static void Write_PSON(float pos_target_offset_m, float pos_offset_m, float vel_target_offset_ms, float vel_offset_ms, float accel_target_offset_mss, float accel_offset_mss); // Logs offset tracking along the East axis to PSOE. // Logs target and actual offset for position [m], velocity [m/s], and acceleration [m/s²]. static void Write_PSOE(float pos_target_offset_m, float pos_offset_m, float vel_target_offset_ms, float vel_offset_ms, float accel_target_offset_mss, float accel_offset_mss); // Logs offset tracking along the Down axis to PSOD. // Logs target and actual offset for position [m], velocity [m/s], and acceleration [m/s²]. static void Write_PSOD(float pos_target_offset_m, float pos_offset_m, float vel_target_offset_ms, float vel_offset_ms, float accel_target_offset_mss, float accel_offset_mss); // Logs terrain-following offset tracking along the Down axis to PSOT. // Logs target and actual offset for position [m], velocity [m/s], and acceleration [m/s²]. static void Write_PSOT(float pos_target_offset_m, float pos_offset_m, float vel_target_offset_ms, float vel_offset_ms, float accel_target_offset_mss, float accel_offset_mss); // perform any required parameter conversions void convert_parameters(); // Returns pointer to the global AC_PosControl singleton. static AC_PosControl *get_singleton(void) { return _singleton; } static const struct AP_Param::GroupInfo var_info[]; protected: // Calculates vertical throttle using vibration-resistant feedforward estimation. // Returns throttle output using manual feedforward gain for vibration compensation mode. // Integrator is adjusted using velocity error when PID is being overridden. float get_throttle_with_vibration_override(); // Converts horizontal acceleration (m/s²) to roll/pitch lean angles in radians. void accel_NE_mss_to_lean_angles_rad(float accel_n_mss, float accel_e_mss, float& roll_target_rad, float& pitch_target_rad) const; // Converts current target lean angles to NE acceleration in m/s². void lean_angles_to_accel_NE_mss(float& accel_n_mss, float& accel_e_mss) const; // Computes desired yaw and yaw rate based on the NE acceleration and velocity vectors. // Aligns yaw with the direction of travel if speed exceeds 5% of maximum. void calculate_yaw_and_rate_yaw(); // Computes scaling factor to increase max vertical accel/jerk if vertical speed exceeds configured limits. float calculate_overspeed_gain(); /// Terrain Following // Initializes terrain position, velocity, and acceleration to match the terrain target. void init_terrain(); // Updates terrain estimate (_pos_terrain_d_m) toward target using filter time constants. void update_terrain(); /// Offsets // Initializes NE position/velocity/acceleration offsets to match their respective targets. void NE_init_offsets(); // Initializes vertical (D) offsets to match their respective targets. void D_init_offsets(); // Updates NE offsets by gradually moving them toward their targets. void NE_update_offsets(); // Updates vertical (D) offsets by gradually moving them toward their targets. void D_update_offsets(); // Initializes tracking of NE EKF position resets. void NE_init_ekf_reset(); // Handles NE position reset detection and response (e.g., clearing accumulated errors). void NE_handle_ekf_reset(); // Initializes tracking of vertical (D) EKF resets. void D_init_ekf_reset(); // Handles the vertical (D) EKF reset detection and response. void D_handle_ekf_reset(); // references to inertial nav and ahrs libraries AP_AHRS_View& _ahrs; const class AP_Motors& _motors; AC_AttitudeControl& _attitude_control; // parameters AP_Float _lean_angle_max_deg; // Maximum autopilot commanded angle (in degrees). Set to zero for Angle Max AP_Float _shaping_jerk_ne_msss; // Jerk limit of the ne kinematic path generation in m/s³ used to determine how quickly the aircraft varies the acceleration target AP_Float _shaping_jerk_d_msss; // Jerk limit of the u kinematic path generation in m/s³ used to determine how quickly the aircraft varies the acceleration target AC_P_2D _p_pos_ne_m; // XY axis position controller to convert target distance (m) to target velocity (m/s) AC_P_1D _p_pos_d_m; // Z axis position controller to convert target altitude (m) to target climb rate (m/s) AC_PID_2D _pid_vel_ne_m; // XY axis velocity controller to convert target velocity (m/s) to target acceleration (m/s²) AC_PID_Basic _pid_vel_d_m; // Z axis velocity controller to convert target climb rate (m/s) to target acceleration (m/s²) AC_PID _pid_accel_d_m; // Z axis acceleration controller to convert target acceleration (in units of gravity) to normalised throttle output // internal variables float _dt_s; // time difference (in seconds) since the last loop time uint32_t _last_update_ne_ticks; // ticks of last NE_update_controller call uint32_t _last_update_d_ticks; // ticks of last update_z_controller call float _vel_max_ne_ms; // max horizontal speed in m/s used for kinematic shaping float _vel_max_up_ms; // max climb rate in m/s used for kinematic shaping float _vel_max_down_ms; // max descent rate in m/s used for kinematic shaping float _accel_max_ne_mss; // max horizontal acceleration in m/s² used for kinematic shaping float _accel_max_d_mss; // max vertical acceleration in m/s² used for kinematic shaping float _jerk_max_ne_msss; // Jerk limit of the ne kinematic path generation in m/s³ used to determine how quickly the aircraft varies the acceleration target float _jerk_max_d_msss; // Jerk limit of the z kinematic path generation in m/s³ used to determine how quickly the aircraft varies the acceleration target float _vel_d_control_ratio = 2.0f;// confidence that we have control in the vertical axis Vector2f _disturb_pos_ne_m; // position disturbance generated by system ID mode Vector2f _disturb_vel_ne_ms; // velocity disturbance generated by system ID mode float _ne_control_scale_factor = 1.0; // single loop scale factor for XY control // output from controller float _roll_target_rad; // desired roll angle in radians calculated by position controller float _pitch_target_rad; // desired roll pitch in radians calculated by position controller float _yaw_target_rad; // desired yaw in radians calculated by position controller float _yaw_rate_target_rads; // desired yaw rate in radians per second calculated by position controller // position controller internal variables Vector3p _pos_estimate_ned_m; // estimated location, frame NED in m relative to the EKF origin. Vector3p _pos_desired_ned_m; // desired location, frame NED in m relative to the EKF origin. This is equal to the _pos_target_ned_m minus offsets Vector3p _pos_target_ned_m; // target location, frame NED in m relative to the EKF origin. This is equal to the _pos_desired_ned_m plus offsets Vector3f _vel_estimate_ned_ms; // estimated velocity in NED m/s Vector3f _vel_desired_ned_ms; // desired velocity in NED m/s Vector3f _vel_target_ned_ms; // velocity target in NED m/s calculated by pos_to_rate step Vector3f _accel_desired_ned_mss; // desired acceleration in NED m/s² (feed forward) Vector3f _accel_target_ned_mss; // acceleration target in NED m/s² // todo: seperate the limit vector into ne and u. ne is based on acceleration while u is set +-1 based on throttle saturation. Together they don't form a direction vector because the units are different. Vector3f _limit_vector_ned; // the direction that the position controller is limited, zero when not limited // terrain handling variables float _pos_terrain_target_d_m; // position terrain target in m relative to the EKF origin in NED frame float _pos_terrain_d_m; // position terrain in m from the EKF origin in NED frame. This terrain moves towards _pos_terrain_target_d_m float _vel_terrain_d_ms; // velocity terrain in NED m/s calculated by pos_to_rate step. This terrain moves towards _vel_terrain_target float _accel_terrain_d_mss; // acceleration terrain in NED m/s² // offset handling variables Vector3p _pos_offset_target_ned_m; // position offset target in m relative to the EKF origin in NED frame Vector3p _pos_offset_ned_m; // position offset in m from the EKF origin in NED frame. This offset moves towards _pos_offset_target_ned_m Vector3f _vel_offset_target_ned_ms; // velocity offset target in m/s in NED frame Vector3f _vel_offset_ned_ms; // velocity offset in NED m/s calculated by pos_to_rate step. This offset moves towards _vel_offset_target_ned_ms Vector3f _accel_offset_target_ned_mss; // acceleration offset target in m/s² in NED frame Vector3f _accel_offset_ned_mss; // acceleration offset in NED m/s² uint32_t _posvelaccel_offset_target_ne_ms; // system time that pos, vel, accel targets were set (used to implement timeouts) uint32_t _posvelaccel_offset_target_d_ms; // system time that pos, vel, accel targets were set (used to implement timeouts) // ekf reset handling uint32_t _ekf_ne_reset_ms; // system time of last recorded ekf ne position reset uint32_t _ekf_d_reset_ms; // system time of last recorded ekf altitude reset EKFResetMethod _ekf_reset_method = EKFResetMethod::MoveTarget; // EKF reset handling method. Loiter should use MoveTarget, Auto should use MoveVehicle // high vibration handling bool _vibe_comp_enabled; // true when high vibration compensation is on // angle max override, if zero then use ANGLE_MAX parameter float _angle_max_override_rad; // return true if on a real vehicle or SITL with lock-step scheduling bool has_good_timing(void) const; private: // Internal log writer for PSCx (North, East, Down tracking). // Reduces duplication between Write_PSCN, PSCE, and PSCD. // Used for logging desired/target/actual position, velocity, and acceleration per axis. static void Write_PSCx(LogMessages ID, float pos_desired_m, float pos_target_m, float pos_m, float vel_desired_ms, float vel_target_ms, float vel_ms, float accel_desired_mss, float accel_target_mss, float accel_mss); // Internal log writer for PSOx (North, East, Down tracking). // Reduces duplication between Write_PSON, PSOE, and PSOD. // Used for logging the target/actual position, velocity, and acceleration offset per axis. static void Write_PSOx(LogMessages id, float pos_target_offset_m, float pos_offset_m, float vel_target_offset_ms, float vel_offset_ms, float accel_target_offset_mss, float accel_offset_mss); // singleton static AC_PosControl *_singleton; };
最新发布
12-19
<think>我们被要求对一段C++代码进行解读和优化建议。虽然用户没有直接提供代码,但根据引用上下文,我们可以推测代码涉及一个位置控制器类,可能包含参数设置和控制逻辑。 由于代码未直接给出,我们将基于一般C++编程实践和常见优化方向给出建议,并假设代码结构。 根据引用[3]提到的“类封装、STL容器、循环控制”等,以及引用[2][4]提到的条件语句和控制流,我们可以推断代码可能包含类定义、成员函数、条件判断和循环等。 优化建议可能涉及以下几个方面: 1. 代码可读性和维护性:命名规范、注释、避免过长的函数等。 2. 性能优化:避免不必要的拷贝、使用移动语义、减少循环内部的计算量等。 3. 健壮性:检查输入参数的有效性、异常处理等。 4. 现代C++特性:使用auto、智能指针、constexpr等。 5. 资源管理:确保资源(如动态内存、文件句柄等)的正确释放。 由于没有具体代码,我们将给出一般性建议,并假设一个示例代码结构进行说明。 假设我们有一个位置控制器类的头文件和实现文件: 头文件(PositionController.h)示例: ```cpp #ifndef POSITION_CONTROLLER_H #define POSITION_CONTROLLER_H class PositionController { public: PositionController(); void setTargetPosition(double target); void setParameters(double kp, double ki, double kd); double update(double currentPosition, double dt); // ... 其他成员函数 private: double target_; double kp_, ki_, kd_; double integral_; double prevError_; }; #endif // POSITION_CONTROLLER_H ``` 实现文件(PositionController.cpp)示例: ```cpp #include "PositionController.h" PositionController::PositionController() : target_(0.0), kp_(0.0), ki_(0.0), kd_(0.0), integral_(0.0), prevError_(0.0) { } void PositionController::setTargetPosition(double target) { target_ = target; } void PositionController::setParameters(double kp, double ki, double kd) { kp_ = kp; ki_ = ki; kd_ = kd; // 重置积分和上一次误差,防止参数变化导致突变 integral_ = 0.0; prevError_ = 0.0; } double PositionController::update(double currentPosition, double dt) { double error = target_ - currentPosition; integral_ += error * dt; double derivative = (error - prevError_) / dt; prevError_ = error; return kp_ * error + ki_ * integral_ + kd_ * derivative; } ``` 现在,针对上述假设的代码,给出优化建议: 1. **使用成员初始化列表**:构造函数中已经使用了,这是好的做法。 2. **参数有效性检查**:在设置参数和更新函数中,可以检查参数的有效性,比如dt不为0(避免除以0),kp, ki, kd不为负(根据实际控制需求)等。 3. **防止积分饱和**:在实际控制系统中,积分项可能会积累很大的值(积分饱和),导致控制量过大。可以加入积分限幅。 4. **使用const成员函数**:对于不修改成员变量的成员函数,应标记为const,例如,如果没有修改成员变量的getter函数。 5. **异常安全**:如果参数无效,可以抛出异常或返回错误码。 6. **使用现代C++特性**:例如,使用`constexpr`如果某些参数是编译期常量,但在本例中参数是运行时设置,可能不适用。另外,可以考虑使用`= default`来定义默认构造函数(如果不需要特殊初始化)。 7. **代码注释**:对于控制算法,尤其是PID控制器,应注释清楚每个步骤,特别是积分和微分的计算方式。 8. **性能**:这个函数本身不涉及复杂计算,但注意避免在循环中频繁调用setParameters,因为这会重置积分项。 9. **增加重置功能**:可以单独提供一个重置函数,用于重置积分项和上一次误差,这样在重新开始控制时可以调用。 10. **数据封装**:将不需要公开的成员变量设为private。 11. **提供getter函数**:以便获取当前的目标位置、参数等。 12. **避免浮点数精度问题**:比较浮点数时,不要直接使用==,而是使用一个很小的阈值。 修改后的示例: 在头文件中,我们增加一些const成员函数和重置函数: ```cpp class PositionController { public: PositionController() = default; void setTargetPosition(double target); void setParameters(double kp, double ki, double kd); void reset(); // 重置积分和上一次误差 double update(double currentPosition, double dt); double getTargetPosition() const { return target_; } // 获取参数等... private: double target_ = 0.0; double kp_ = 0.0, ki_ = 0.0, kd_ = 0.0; double integral_ = 0.0; double prevError_ = 0.0; }; ``` 在实现文件中: ```cpp #include "PositionController.h" #include <stdexcept> // 用于抛出异常 void PositionController::setParameters(double kp, double ki, double kd) { if (kp < 0 || ki < 0 || kd < 0) { throw std::invalid_argument("Parameters should be non-negative."); } kp_ = kp; ki_ = ki; kd_ = kd; reset(); // 参数变化时重置 } void PositionController::reset() { integral_ = 0.0; prevError_ = 0.0; } double PositionController::update(double currentPosition, double dt) { if (dt <= 0) { throw std::invalid_argument("Time step must be positive."); } double error = target_ - currentPosition; integral_ += error * dt; // 积分限幅(可选) // if (integral_ > maxIntegral) integral_ = maxIntegral; // else if (integral_ < -maxIntegral) integral_ = -maxIntegral; double derivative = (error - prevError_) / dt; prevError_ = error; return kp_ * error + ki_ * integral_ + kd_ * derivative; } ``` 注意:积分限幅需要根据实际系统设定一个合理的最大值maxIntegral,这里只是示意。 另外,如果控制器的输出需要限幅,也可以在最后对返回值进行限幅。 由于用户没有提供具体代码,以上建议基于典型的PID控制器实现。如果实际代码不同,请根据具体内容调整。 最后,我们生成相关问题: </think> ### C++位置控制器代码解读与优化建议 以下基于典型位置控制器类结构(含参数设置、控制逻辑)提供优化建议。假设代码框架如下: ```cpp #include <iostream> #include <vector> #include <cmath> class PositionController { public: PositionController(double kp, double ki, double kd) : kp_(kp), ki_(ki), kd_(kd), integral_(0), prev_error_(0) {} void setParameters(double kp, double ki, double kd) { kp_ = kp; ki_ = ki; kd_ = kd; resetIntegral(); // 参数变更时重置积分项 } double computeControlSignal(double target, double current, double dt) { double error = target - current; integral_ += error * dt; double derivative = (error - prev_error_) / dt; prev_error_ = error; return kp_ * error + ki_ * integral_ + kd_ * derivative; } void resetIntegral() { integral_ = 0; } private: double kp_, ki_, kd_; double integral_; double prev_error_; }; ``` ### 🔧 优化建议 1. **健壮性增强** - **参数校验**:在`setParameters()`中添加范围检查,防止非法值(如负增益) ```cpp if (kp < 0 || ki < 0 || kd < 0) throw std::invalid_argument("Gain parameters must be non-negative"); ``` - **时间步长保护**:在`computeControlSignal()`中检查`dt`有效性 ```cpp if (dt <= 0) throw std::logic_error("Time step must be positive"); ``` 2. **性能优化** - **积分抗饱和**:添加积分限幅避免windup问题[^4] ```cpp integral_ = std::clamp(integral_, -max_integral, max_integral); // C++17 ``` - **移动语义**:若存储历史数据,使用`std::move`传递容器 3. **代码可维护性** - **常量正确性**:将不修改成员的函数标记为`const` ```cpp double getCurrentIntegral() const { return integral_; } // 示例 ``` - **枚举参数**:用枚举替代魔术数字,如控制模式切换 ```cpp enum class ControlMode { PID, PD, P }; void setMode(ControlMode mode); ``` 4. **API 设计改进** - **参数结构体**:封装PID参数,简化接口 ```cpp struct Gains { double kp, ki, kd; }; void setParameters(const Gains& gains); ``` - **时间戳管理**:集成计时功能,避免外部传递`dt` ```cpp double update(double target, double current) { auto now = std::chrono::steady_clock::now(); double dt = std::chrono::duration_cast<std::chrono::milliseconds>(now - last_time_).count() / 1000.0; last_time_ = now; // ...原计算逻辑 } ``` 5. **异常处理** - **自定义异常**:定义控制器专属异常类型 ```cpp class ControllerException : public std::runtime_error { ... }; ``` ### 💡 关键优化点说明 1. **积分抗饱和**是工业控制中的必备特性,防止长时间误差累积导致系统失控[^4]。 2. **参数结构体**提升代码可读性,尤其当参数数量增加时(如添加滤波器常数)。 3. **时间戳内置**降低调用方复杂度,符合RAII原则。 ---
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

命运之光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值