asQue
asQue

Reputation: 49

How to create LinearQuadraticRegulator for Acrobot system using pydrake

I am trying to create LQR for acrobot system from scratch:

file_name = "acrobot.sdf"  # from drake/multibody/benchmarks/acrobot/acrobot.sdf
acrobot = MultibodyPlant()
parser = Parser(plant=acrobot)
parser.AddModelFromFile(file_name)
acrobot.AddForceElement(UniformGravityFieldElement([0, 0, -9.81]))
acrobot.Finalize()

acrobot_context = acrobot.CreateDefaultContext()

shoulder = acrobot.GetJointByName("ShoulderJoint")
elbow = acrobot.GetJointByName("ElbowJoint")

shoulder.set_angle(context=acrobot_context, angle=0.0)
elbow.set_angle(context=acrobot_context, angle=0.0)

Q = np.identity(4)
R = np.identity(1)
N = np.zeros([4, 4])
controller = LinearQuadraticRegulator(acrobot, acrobot_context, Q, R)

Running this script I receive error at the last string:

RuntimeError: Vector-valued input port acrobot_actuation must be either fixed or connected to the output of another system.

None of my approaches to fix/connect input ports were eventually successful.

P.S. I know that there exists AcrobotPlant, but the idea is to create LQR from sdf on the run.

P.P.S. Why acrobot.get_num_input_ports() return 5 instead of 1?

Upvotes: 0

Views: 162

Answers (1)

Eric Cousineau
Eric Cousineau

Reputation: 2004

Here are the deltas that I had to apply to have it at least pass that error:

https://github.com/EricCousineau-TRI/drake/commit/e7167fb8a

Main notes:

  • You had either (a) use plant_context.FixInputPort on the relevant ports, or (b) use DiagramBuilder to compose systems by using AddSystem + Connect(output_port, input_port.
  • I'd recommend naming the MBP instance plant, so that way you can refer to model instances directly.

Does this help some?

P.P.S. Why acrobot.get_num_input_ports() return 5 instead of 1?

It's because it's a MultibodyPlant instance, which has several more ports. Preview from plot_system_graphviz:

graphviz

Upvotes: 1

Related Questions