Search code examples
drake

pydrake- Error when connecting IiwaCommandSender to LcmPublisherSystem


I have the following lines in my program-

lcm = DrakeLcm() 

lcm_command_sender = builder.AddSystem(IiwaCommandSender())

lcm_publisher = builder.AddSystem(LcmPublisherSystem.Make(channel="IIWA_COMMAND", lcm_type=lcmt_iiwa_command, lcm=lcm, publish_period=0.01))

//Connect controller to command sender
builder.Connect(controller.GetOutputPort("joint_positions"), lcm_command_sender.get_position_input_port())

//Connect lcm command sender to publisher
builder.Connect(lcm_command_sender.GetOutputPort("lcmt_iiwa_command"), lcm_publisher.get_input_port())

Upon running, the last line gives the following runtime error-

RuntimeError: DiagramBuilder::Connect: Mismatched value types while connecting output port lcmt_iiwa_command of System lcm_command_sender (type drake::lcmt_iiwa_command) to input port lcm_message of System LcmPublisherSystem(IIWA_COMMAND) (type drake::pydrake::Object)

I am not sure why this happens, since the documentation specifically says that the output port of IiwaCommandSender should be connected to the input port of LcmPublisherSystem, and both of them are of the type AbstractValue. What is it that I'm missing?


Solution

  • In the code above, the output port of the IiwaCommandSender is of type "drake::lcmt_iiwa_command" (a C++ class wrapped in Python), but the input port of the LcmPublisherSystem is of type "drake.lcmt_iiwa_command" (a Python class). Those are two different runtime types, even though they both represent an LCM message.

    In this case, you should set use_cpp_serializer=True when calling LcmPublisherSystem.Make so that the input port is the C++ type, instead of the Python type.

    https://drake.mit.edu/pydrake/pydrake.systems.lcm.html#pydrake.systems.lcm.LcmPublisherSystem.Make