jc-bao / quad-drake

Apache License 2.0
0 stars 0 forks source link

Diagram is not connected when set up a LQR controller #1

Open jc-bao opened 10 months ago

jc-bao commented 10 months ago

When running lqr = LinearQuadraticRegulator(quadrotor_diagram, context, Q, R) to setup the LQR controller, I got the error "RuntimeError: InputPort::Eval(): required InputPort[0] (propcommand) of System :: (Diagram) is not connected".

I have connected the 'prop_command' to the system input with

    quadrotor_builder.ExportInput(props.get_command_input_port(), "prop_command")
    quadrotor_builder.ExportOutput(quadrotor_plant.get_state_output_port(), "state")

How can I resolve this issue?

jc-bao commented 10 months ago

🤖

The error message suggests that the input port named "prop_command" is not connected when you try to set up the Linear Quadratic Regulator (LQR) controller. This could happen for a variety of reasons, and without the complete code, it's hard to pinpoint the exact issue. However, here are some general troubleshooting steps you can follow:

Check the Context

Make sure that the context you are passing to LinearQuadraticRegulator is correctly set up for the quadrotor_diagram. The context should be created from the diagram itself:

context = quadrotor_diagram.CreateDefaultContext()

Check the Connections

Make sure that all the necessary connections are made before you build the diagram. The order of operations can sometimes matter. For example, you should connect all subsystems and ports before calling Build() on the diagram builder.

Check the Input and Output Port Names

Double-check that the names of the input and output ports match exactly with what you expect. Typos or incorrect string names can lead to issues.

Check the System Before LQR

Before you run the LQR line, try to simulate or otherwise evaluate your quadrotor_diagram to make sure that it's fully operational. If it's not, the LQR setup is likely to fail.

Check the LQR Inputs

Make sure that the matrices Q and R are of the correct dimensions and are compatible with your system's state and input dimensions.

Debugging

You can also use debugging techniques to step through the code and inspect the state of the quadrotor_diagram and context just before the LQR line that's causing the error. This can often provide clues as to what's going wrong.

Example

Here's a simplified example to illustrate the order of operations:

from pydrake.all import DiagramBuilder, LinearQuadraticRegulator

# Create a diagram builder
quadrotor_builder = DiagramBuilder()

# Add your quadrotor plant and other systems
quadrotor_plant = ...
props = ...

# Connect systems
quadrotor_builder.Connect(...)

# Export inputs and outputs
quadrotor_builder.ExportInput(props.get_command_input_port(), "prop_command")
quadrotor_builder.ExportOutput(quadrotor_plant.get_state_output_port(), "state")

# Build the diagram
quadrotor_diagram = quadrotor_builder.Build()

# Create a context for the diagram
context = quadrotor_diagram.CreateDefaultContext()

# Set up the LQR controller
Q = ...
R = ...
lqr = LinearQuadraticRegulator(quadrotor_diagram, context, Q, R)

If you've tried all these steps and are still encountering issues, you may need to delve deeper into the specifics of your implementation.

jc-bao commented 10 months ago

check context

::_ Context (of a Diagram)
---------------------------
12 total discrete states in 1 groups
70 total numeric parameters in 7 groups
8 total abstract parameters

::_::plant Context
-------------------
Time: 0
States:
  1 discrete state groups with
     12 states
       0 0 0 0 0 0 0 0 0 0 0 0

Parameters:
  7 numeric parameter groups with
     10 parameters
       nan nan nan nan nan nan nan nan nan nan
     10 parameters
                      0.775                    0                    0                    0 0.001935483870967742 0.003225806451612903 0.004516129032258064                    0                    0                    0
     10 parameters
       0 0 0 0 0 0 0 0 0 0
     10 parameters
       0 0 0 0 0 0 0 0 0 0
     10 parameters
       0 0 0 0 0 0 0 0 0 0
     10 parameters
       0 0 0 0 0 0 0 0 0 0
     10 parameters
       0 0 0 0 0 0 0 0 0 0
  7 abstract parameters

::_::scene_graph Context
-------------------------
Time: 0
Parameters:
  1 abstract parameters

Compare with other systems:

::_ Context (of a Diagram)
---------------------------
12 total continuous states
292 total numeric parameters in 27 groups
17 total abstract parameters

::_::plant Context
-------------------
Time: 0
States:
  12 continuous states
    0 0 0 0 0 0 0 0 0 0 0 0

Parameters:
  27 numeric parameter groups with
     10 parameters
       nan nan nan nan nan nan nan nan nan nan
     10 parameters
       1 0 0 0 1 1 1 0 0 0
     10 parameters
       1 0 0 0 1 1 1 0 0 0
     10 parameters
       1 0 0 0 1 1 1 0 0 0
     10 parameters
       1 0 0 0 1 1 1 0 0 0
     10 parameters
       1 0 0 0 1 1 1 0 0 0
     10 parameters
       1 0 0 0 1 1 1 0 0 0
     10 parameters
       1 0 0 0 1 1 1 0 0 0
     10 parameters
       1 0 0 0 1 1 1 0 0 0
     10 parameters
       1 0 0 0 1 1 1 0 0 0
     10 parameters
       1 0 0 0 1 1 1 0 0 0
     10 parameters
       1 0 0 0 1 1 1 0 0 0
     10 parameters
                      0.775                    0                    0                    0 0.001935483870967742 0.003225806451612903 0.004516129032258064                    0                    0                    0
     10 parameters
         0   0   0   0 0.4 0.4 0.4   0   0   0
     10 parameters
         0   0   0   0 0.4 0.4 0.4   0   0   0
     10 parameters
         0   0   0   0 0.4 0.4 0.4   0   0   0
     12 parameters
       1 0 0 0 1 0 0 0 1 0 0 0
     12 parameters
       1 0 0 0 1 0 0 0 1 0 0 0
     12 parameters
       1 0 0 0 1 0 0 0 1 0 0 0
     12 parameters
       1 0 0 0 1 0 0 0 1 0 0 0
     12 parameters
       1 0 0 0 1 0 0 0 1 0 0 0
     12 parameters
       1 0 0 0 1 0 0 0 1 0 0 0
     12 parameters
       1 0 0 0 1 0 0 0 1 0 0 0
     12 parameters
       1 0 0 0 1 0 0 0 1 0 0 0
     12 parameters
       1 0 0 0 1 0 0 0 1 0 0 0
     12 parameters
       1 0 0 0 1 0 0 0 1 0 0 0
     12 parameters
       1 0 0 0 1 0 0 0 1 0 0 0
  16 abstract parameters

::_::scene_graph Context
-------------------------
Time: 0
Parameters:
  1 abstract parameters

Cannot see the difference. Only one more discrete group.

Possible issue: the context value is different.

Minor problem resolved: by changing the time_step from 0.01 to 0.0, the system becomes continuous again.

jc-bao commented 10 months ago

Connections

The input and output is connected.

image
jc-bao commented 10 months ago

✔️ Check without LQR

Run the simulation without LQR.

Initially, not work. By setting input port with certain value, the system can run.

quadrotor_diagram.get_input_port().FixValue(context, [thrust/4.0]*4)