File size: 2,981 Bytes
495f681
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <string>

#include <medrct/types/joint_state.hh>
#include <medrct/log.hh>
#include <medrct/config.hh>

#include <medrct_default_controller/config.hh>
#include <medrct_default_controller/default_controller_factory.hh>
#include <medrct_default_controller/cartesian_teleop_controller.hh>
#include <medrct_default_controller/joint_teleop_controller.hh>

namespace medrct
{
namespace controller
{

Controller::Ptr DefaultControllerFactory::create(
    const medrct::stream::StreamFactory& stream_factory,
    YAML::Node controller_config) const
{
  std::string class_name = GetValue<std::string>(
      GetYamlNode(controller_config, "loader"), "create_class_name");
  std::string controller_name =
      GetValue<std::string>(controller_config, "name");

  YAML::Node input_config = GetYamlNode(controller_config, "input");
  if (class_name == "JointMimicController" ||
      class_name == "JointIncrementController")
  {
    JointTeleopControllerConfig jtcc;
    JointTeleopControllerConfig::FromYaml(
        jtcc, controller_config, stream_factory);
    if (class_name == "JointMimicController")
    {
      auto jmc = std::make_shared<JointMimicController>();
      if (!jmc->init(jtcc))
        throw std::invalid_argument(
            "Failed to init JointMimicController with name: " +
            jtcc.controller_name);
      return jmc;
    }
    // else Must be JointIncrementController
    auto jic = std::make_shared<JointIncrementController>();
    if (!jic->init(jtcc))
      throw std::invalid_argument(
          "Failed to init JointIncrementController with name: " +
          jtcc.controller_name);
    return jic;
  }

  if (class_name == "CartesianIncrementController")
  {
    CartesianIncrementControllerConfig cic_cfg;
    CartesianIncrementControllerConfig::FromYaml(
        cic_cfg, controller_config, stream_factory);
    Transform tf;
    cic_cfg.forward_kinematics->computeFK(tf, {0.0, 0.0, 0.0, 0.0, 0.0, 0.0});
    auto cic = std::make_shared<CartesianIncrementController>();
    if (!cic->init(cic_cfg))
      throw std::invalid_argument(
          "Failed to init CartesianIncrementController with name: " +
          cic_cfg.controller_name);
    return cic;
  }
  else if (class_name == "CartesianFollowerController")
  {
    CartesianFollowerControllerConfig cfcc;
    CartesianFollowerControllerConfig::FromYaml(
        cfcc, controller_config, stream_factory);
    auto cfc = std::make_shared<CartesianFollowerController>();
    if (!cfc->init(cfcc))
      throw std::invalid_argument(
          "Failed to init CartesianFollowerController with name: " +
          cfcc.controller_name);
    return cfc;
  }
  throw std::invalid_argument(
      "No valid controller specified with [create_class_name]: " + class_name);
  return nullptr;
}

} // namespace controller
} // namespace medrct

// clang-format off
MEDRCT_ADD_PLUGIN_SECTIONED(medrct::controller::DefaultControllerFactory, DefaultControllerFactory, DCtrlF)
// clang-format on