load("//tools/lint:lint.bzl", "add_lint_tests")
load(
    "//tools/skylark:drake_cc.bzl",
    "drake_cc_googletest",
    "drake_cc_library",
    "drake_cc_package_library",
)

package(
    default_visibility = ["//visibility:public"],
)

drake_cc_package_library(
    name = "kuka_iiwa",
    visibility = ["//visibility:public"],
    deps = [
        ":build_iiwa_control",
        ":iiwa_command_receiver",
        ":iiwa_command_sender",
        ":iiwa_constants",
        ":iiwa_driver",
        ":iiwa_driver_functions",
        ":iiwa_status_receiver",
        ":iiwa_status_sender",
        ":sim_iiwa_driver",
    ],
)

drake_cc_library(
    name = "iiwa_constants",
    srcs = ["iiwa_constants.cc"],
    hdrs = ["iiwa_constants.h"],
    deps = [
        "//common:essential",
        "@fmt",
    ],
)

drake_cc_library(
    name = "iiwa_command_receiver",
    srcs = ["iiwa_command_receiver.cc"],
    hdrs = ["iiwa_command_receiver.h"],
    deps = [
        ":iiwa_constants",
        "//common:essential",
        "//lcmtypes:lcmtypes_drake_cc",
        "//systems/framework:leaf_system",
        "//systems/lcm:lcm_pubsub_system",
    ],
)

drake_cc_library(
    name = "iiwa_command_sender",
    srcs = ["iiwa_command_sender.cc"],
    hdrs = ["iiwa_command_sender.h"],
    deps = [
        ":iiwa_constants",
        "//common:essential",
        "//lcmtypes:lcmtypes_drake_cc",
        "//systems/framework:leaf_system",
    ],
)

drake_cc_library(
    name = "iiwa_status_receiver",
    srcs = ["iiwa_status_receiver.cc"],
    hdrs = ["iiwa_status_receiver.h"],
    deps = [
        ":iiwa_constants",
        "//common:essential",
        "//lcmtypes:lcmtypes_drake_cc",
        "//systems/framework:leaf_system",
    ],
)

drake_cc_library(
    name = "iiwa_status_sender",
    srcs = ["iiwa_status_sender.cc"],
    hdrs = ["iiwa_status_sender.h"],
    deps = [
        ":iiwa_constants",
        "//common:essential",
        "//lcmtypes:lcmtypes_drake_cc",
        "//systems/framework:leaf_system",
    ],
)

drake_cc_library(
    name = "iiwa_driver",
    srcs = ["iiwa_driver.cc"],
    hdrs = ["iiwa_driver.h"],
    deps = [
        "//common:essential",
        "//common:name_value",
    ],
)

drake_cc_library(
    name = "iiwa_driver_functions",
    srcs = ["iiwa_driver_functions.cc"],
    hdrs = ["iiwa_driver_functions.h"],
    interface_deps = [
        ":iiwa_driver",
        "//common:essential",
        "//multibody/parsing:model_instance_info",
        "//multibody/plant",
        "//systems/framework:diagram_builder",
        "//systems/lcm:lcm_buses",
    ],
    deps = [
        ":build_iiwa_control",
        "//manipulation/util:make_arm_controller_model",
        "//systems/primitives:shared_pointer_system",
    ],
)

drake_cc_library(
    name = "sim_iiwa_driver",
    srcs = ["sim_iiwa_driver.cc"],
    hdrs = ["sim_iiwa_driver.h"],
    deps = [
        ":iiwa_constants",
        "//common:essential",
        "//multibody/plant",
        "//systems/controllers:inverse_dynamics_controller",
        "//systems/framework:diagram_builder",
        "//systems/primitives:adder",
        "//systems/primitives:demultiplexer",
        "//systems/primitives:discrete_derivative",
        "//systems/primitives:first_order_low_pass_filter",
    ],
)

drake_cc_library(
    name = "build_iiwa_control",
    srcs = ["build_iiwa_control.cc"],
    hdrs = ["build_iiwa_control.h"],
    deps = [
        ":iiwa_command_receiver",
        ":iiwa_constants",
        ":iiwa_status_sender",
        ":sim_iiwa_driver",
        "//multibody/plant",
        "//systems/framework:diagram_builder",
        "//systems/lcm",
        "//systems/primitives:demultiplexer",
        "//systems/primitives:gain",
        "@eigen",
    ],
)

# === test/ ===

drake_cc_googletest(
    name = "iiwa_constants_test",
    deps = [":iiwa_constants"],
)

drake_cc_googletest(
    name = "iiwa_command_receiver_test",
    deps = [
        ":iiwa_command_receiver",
        "//common/test_utilities:eigen_matrix_compare",
    ],
)

drake_cc_googletest(
    name = "iiwa_command_sender_test",
    deps = [
        ":iiwa_command_sender",
        "//common/test_utilities:eigen_matrix_compare",
        "//common/test_utilities:limit_malloc",
    ],
)

drake_cc_googletest(
    name = "iiwa_status_receiver_test",
    deps = [
        ":iiwa_status_receiver",
        "//common/test_utilities:eigen_matrix_compare",
        "//common/test_utilities:limit_malloc",
    ],
)

drake_cc_googletest(
    name = "iiwa_status_sender_test",
    deps = [
        ":iiwa_status_sender",
        "//common/test_utilities:eigen_matrix_compare",
    ],
)

drake_cc_googletest(
    name = "build_iiwa_control_test",
    data = [
        "@drake_models//:iiwa_description",
    ],
    deps = [
        ":build_iiwa_control",
        ":iiwa_constants",
        "//lcm",
        "//manipulation/util:make_arm_controller_model",
        "//multibody/parsing:model_instance_info",
        "//multibody/parsing:parser",
        "//multibody/plant",
        "//systems/analysis:simulator",
        "//systems/framework:diagram_builder",
        "//systems/lcm",
        "//systems/primitives:constant_vector_source",
        "//systems/primitives:shared_pointer_system",
    ],
)

drake_cc_googletest(
    name = "iiwa_driver_functions_test",
    data = [
        "//manipulation/util:test_directives",
        "@drake_models//:iiwa_description",
        "@drake_models//:wsg_50_description",
    ],
    deps = [
        ":iiwa_driver_functions",
        "//common:find_resource",
        "//common/test_utilities:expect_throws_message",
        "//lcm:drake_lcm_params",
        "//manipulation/util:zero_force_driver_functions",
        "//multibody/parsing:parser",
        "//multibody/parsing:process_model_directives",
        "//multibody/plant",
        "//systems/analysis:simulator",
        "//systems/framework:diagram_builder",
        "//systems/lcm:lcm_config_functions",
    ],
)

drake_cc_googletest(
    name = "sim_iiwa_driver_test",
    data = [
        "@drake_models//:iiwa_description",
    ],
    deps = [
        ":iiwa_constants",
        ":sim_iiwa_driver",
        "//multibody/parsing",
        "//multibody/plant",
        "//systems/framework/test_utilities:scalar_conversion",
    ],
)

add_lint_tests()
