Skip to content
TheChatArchiveThe Chat Archive
Geminigemini-2.5-flash-lite-preview-06-17-thinking· GoogleHugging Face LMArena Human Preference 140K

i have a problem in simscape simulink. I am building the inverted pendulum which starts…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Metallama-3.3-70b-instruct’s answer.
  1. User

    i have a problem in simscape simulink. I am building the inverted pendulum which starts from down position, swings up and balance at top vertical position. For now the swing up position works well but it got issues in balancing in the top vertical position. I use the transform sensor to get the angle (from 0 to pi to 0) and speed (negative and positive numbers depending on the direction). The angle is causing the difficulties in inverting the PID regulation (*-1) because now when it crosses the top vertical point the angle is still positive (now the desired position for angle when pendulum is top vertical is pi) and the amplifier is not inverting itself. I thought about solutions, maybe theres a way to measure the angle from 0 to 2pi or -pi to pi (ideal) to omit the repeating of the angle. Or maybe theres some way to use some flip flop? when the desired positionis crossed to invert the control, but then standing still in the desired position would cause errors.

  2. Gemini Assistant

    Okay, this is a classic problem when dealing with systems that have unstable equilibrium points and angle wrapping! Your intuition about the angle representation is spot on. The issue often lies in how the controller interprets the angle when it passes the vertical point (π radians).

    Let's break down the problem and potential solutions:

    1. The Root Cause: Angle Wrapping and Unstable Equilibrium

    • Angle Wrapping: Sensors measuring angles often output a value within a specific range, like [0, π] or [-π/2, π/2] or [0, 2π]. When the pendulum swings past the top vertical (π), the sensor might wrap around (e.g., π + ε becomes ε, or π + ε becomes π - ε depending on the exact range and calculation).
    • Setpoint vs. Actual Angle: Your target is π. If the angle becomes π + 0.1, and the sensor wraps it to 0.04, your error calculation (setpoint - actual_angle) becomes π - 0.04, which is large and positive. The controller then likely pushes the cart in the wrong direction.
    • Unstable Equilibrium: Balancing at the top (π) is fundamentally different from the stable equilibrium at the bottom (0). The system wants to fall away from π. A controller tuned for the stable point (0) and simply inverted often lacks the necessary damping and responsiveness for the unstable point.

    2. Solutions

    Here are several approaches, starting with the most recommended:

    A. Ensure Continuous Angle Representation (Recommended)

    The most robust solution is to work with an angle that is continuous and doesn't wrap unexpectedly within the range relevant to the controller. The range [-π, π] is often ideal, where:

    • 0 radians = Downward vertical (stable equilibrium)

    • π radians = Upward vertical (unstable equilibrium)

    • -π radians = Upward vertical (same position as π, just opposite direction of measurement)

    • How to achieve this:

      • Check your Sensor: Examine the configuration of your Transform Sensor or whichever block is providing the angle. Can it be configured to output in [-π, π]? Often, using atan2(y, x) where y is the pendulum's vertical component and x is its horizontal component (relative to the pivot) gives this naturally. If your sensor is calculating angle based on atan(x/y), that could be problematic near y=0.

      • Integrate Angular Velocity: If your sensor provides reliable angular velocity (theta_dot), you can integrate it to get the angle. Start with an initial angle (e.g., 0) and integrate theta_dot over time. This inherently produces a continuous angle. Be mindful of potential drift if the velocity measurement isn't perfect, though for typical pendulum simulations, this is often very stable.

        [Integrator Block] -- Output Angle --> [PID Block]
              ^
              | [Angular Velocity from Sensor]
        
      • Unwrapping Block (If Sensor is Fixed): If you cannot change the sensor output range (e.g., it always outputs 0 to π), you need a block before the PID controller to "unwrap" the angle.

        Logic:

        1. Take the sensor's angle output.
        2. Compare it to the previous angle output.
        3. If the current angle jumps suddenly (e.g., from π to 0, or 0 to π), it indicates a wrap-around.
        4. If current_angle < previous_angle and previous_angle was near π (e.g., > π - threshold), add 2π to current_angle.
        5. If current_angle > previous_angle and previous_angle was near 0 (e.g., < threshold), subtract 2π from current_angle.
        6. Otherwise, the current_angle is the unwrapped angle.
        7. Update previous_angle for the next step.

        Simulink Implementation: This can be done using Unit Delay (to store previous angle), Relational Operator, Sum, Gain, and Switch blocks. It's a bit more complex but feasible.

    B. PID Controller Tuning and Structure for Unstable Equilibrium

    Regardless of the angle representation, balancing at π requires careful tuning.

    • Setpoint: Set the PID's setpoint to π.
    • Error Calculation: Ensure the error is calculated correctly based on your chosen angle representation. If using [-π, π], the error is π - angle.
    • Tuning:
      • P Gain (Kp): This is the most critical. You need enough P gain to counteract the pendulum falling, but too much will cause oscillations.
      • D Gain (Kd): Essential for damping. It should primarily act on the angular velocity (or the derivative of the error). A negative Kd term fed by angular velocity helps stabilize. The control law often involves -Kd * angular_velocity. Check your PID block's configuration – does it compute Kd based on derivative(error) or directly use angular_velocity? For this unstable point, you often need a significant Kd term acting on velocity.
      • I Gain (Ki): Use with caution. It helps eliminate steady-state error but can destabilize the system if too high, especially around an unstable point. You might not need it, or only a very small value.
    • Use Simulink's PID Tuner: If you haven't already, try using the PID Tuner app in Simulink. Simulate the swing-up, and once it's near the top, activate the tuner while the system is supposed to be balancing. It can help find appropriate gains.
    • Controller Input: Ensure your PID controller is receiving both the angle and the angular velocity (as the derivative term). The control logic for balancing often depends heavily on both.

    C. State Feedback Control (More Advanced/Robust)

    Often, a simple PID isn't sufficient for robust balancing. State feedback control is more common:

    • State Vector: Define the system's state as x = [angle; angular_velocity]. You might also include cart position and velocity if it's a cart-pole system.
    • Control Law: The goal is to drive the state towards the desired equilibrium ([π; 0]). A common linear control law is u = -K * (current_state - desired_state), where K is a gain vector [K1, K2].
      • u = -K1 * (angle - π) - K2 * (angular_velocity - 0)
      • This structure naturally incorporates feedback based on both angle and velocity. The PID controller is essentially trying to approximate this form.
    • LQR (Linear Quadratic Regulator): A systematic way to find the optimal gains K is using LQR, which minimizes a cost function balancing state deviation and control effort. You would need to linearize your system dynamics around the equilibrium point (π, 0) to use LQR.

    D. Gain Scheduling (If Necessary)

    If the optimal control strategy changes drastically between the swing-up phase and the balancing phase, you could consider gain scheduling. This involves using different PID controllers (or different PID gains) depending on the current angle. However, this adds complexity and might be overkill if a well-tuned PID or state feedback works.

    Summary and Action Plan

    1. Prioritize Angle Representation: Verify your angle measurement. Aim for a continuous representation, ideally in the range [-π, π]. If your sensor doesn't provide this, implement an unwrapping block. This is the most likely culprit.
    2. Use the Correct Setpoint: Set the angle setpoint to π.
    3. Tune the PID: Focus on the Kp and Kd gains. Ensure Kd is providing damping, likely based on angular velocity. Use the PID Tuner tool.
    4. Consider State Feedback: If PID tuning proves difficult or unreliable, explore implementing a state feedback controller (like LQR).

    Your idea about a "flip flop" is conceptually related to gain scheduling or switching controllers, but directly implementing it based on crossing π is tricky. Ensuring the angle is represented correctly and tuning the PID/controller for the specific unstable equilibrium point is the standard and most effective approach.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.