Advertisement
Guest User

test_urdf.cc

a guest
Jun 3rd, 2020
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. #include <memory>
  2. #include <string>
  3.  
  4. #include "fmt/ostream.h"
  5. #include <gflags/gflags.h>
  6.  
  7. #include "drake/common/drake_assert.h"
  8. #include "drake/common/find_resource.h"
  9. #include "drake/geometry/geometry_visualization.h"
  10. #include "drake/geometry/scene_graph.h"
  11. #include "drake/lcm/drake_lcm.h"
  12.  
  13. // #include "drake/systems/analysis/implicit_euler_integrator.h"
  14. // #include "drake/systems/analysis/runge_kutta3_integrator.h"
  15. // #include "drake/systems/analysis/semi_explicit_euler_integrator.h"
  16. #include "drake/systems/analysis/simulator.h"
  17. #include "drake/systems/analysis/simulator_gflags.h"
  18. #include "drake/systems/analysis/simulator_print_stats.h"
  19. #include "drake/systems/framework/diagram_builder.h"
  20.  
  21. #include "drake/multibody/parsing/parser.h"
  22. #include "drake/multibody/plant/multibody_plant.h"
  23. #include "drake/multibody/tree/revolute_joint.h"
  24. #include "drake/multibody/tree/revolute_joint.h"
  25.  
  26. #include "drake/math/rotation_matrix.h"
  27.  
  28.  
  29.  
  30. // #include "drake/systems/primitives/constant_vector_source.h"
  31.  
  32.  
  33. DEFINE_double(
  34. mbp_discrete_update_period, 1.0E-3,
  35. "The fixed-time step period (in seconds) of discrete updates for the "
  36. "multibody plant modeled as a discrete system. Strictly positive. "
  37. "Set to zero for a continuous plant model.");
  38.  
  39. DEFINE_double(target_realtime_rate, 1.0,
  40. "Playback speed. See documentation for "
  41. "Simulator::set_target_realtime_rate() for details.");
  42.  
  43.  
  44. namespace drake {
  45. namespace examples {
  46. namespace test_urdf {
  47. namespace {
  48.  
  49. using math::RigidTransformd;
  50.  
  51. using geometry::SceneGraph;
  52. using geometry::SourceId;
  53. using geometry::Sphere;
  54. using lcm::DrakeLcm;
  55. using Eigen::Vector3d;
  56. using Eigen::VectorXd;
  57. using drake::multibody::MultibodyPlant;
  58. using multibody::Parser;
  59. using multibody::RevoluteJoint;
  60.  
  61. using multibody::CoulombFriction;
  62.  
  63.  
  64. int do_main(){
  65. // Build a generic multibody plant.
  66. systems::DiagramBuilder<double> builder;
  67.  
  68. SceneGraph<double>& scene_graph = *builder.AddSystem<SceneGraph>();
  69. scene_graph.set_name("scene_graph");
  70.  
  71. MultibodyPlant<double>& plant =
  72. *builder.AddSystem<MultibodyPlant>(FLAGS_mbp_discrete_update_period);
  73. plant.RegisterAsSourceForSceneGraph(&scene_graph);
  74.  
  75. Parser parser(&plant);
  76. std::string full_name =
  77. FindResourceOrThrow("drake/examples/ddr_sdf/model_ddrcustom.urdf");
  78. // FindResourceOrThrow("drake/examples/ddr_sdf/pioneer2dx/model.sdf");
  79.  
  80. parser.AddModelFromFile(full_name);
  81.  
  82. // Now the model is complete.
  83. // plant.WeldFrames(plant.world_frame(), plant.GetFrameByName("chassis"));
  84.  
  85. const double kPadMinorRadius = 6e-3; // 6 mm.
  86. Eigen::Vector3d p_FS;
  87. p_FS(0) = 0;
  88. p_FS(1) = 0;
  89. p_FS(2) = 0;
  90.  
  91. // Pose of the sphere frame S in the finger frame F.
  92. const RigidTransformd X_FS(p_FS);
  93.  
  94. auto& base_link = plant.GetBodyByName("base_link");
  95.  
  96. CoulombFriction<double> friction(0.5,0.5);
  97.  
  98. plant.RegisterCollisionGeometry(base_link, X_FS, Sphere(kPadMinorRadius),
  99. "collision" + std::to_string(1), friction);
  100.  
  101. // const Vector4<double> red(0.8, 0.2, 0.2, 1.0);
  102. // plant.RegisterVisualGeometry(base_link, X_FS, Sphere(kPadMinorRadius),
  103. // "collision" + std::to_string(1), friction);
  104.  
  105.  
  106. fmt::print("Test Point 1\n");
  107.  
  108.  
  109.  
  110.  
  111. plant.Finalize();
  112.  
  113. // Set how much penetration (in meters) we are willing to accept.
  114. plant.set_penetration_allowance(1e-5);
  115. plant.set_stiction_tolerance(1e-2);
  116.  
  117. // There are two actuators. Both are revolute joints
  118. DRAKE_DEMAND(plant.num_actuators() == 2);
  119. DRAKE_DEMAND(plant.num_actuated_dofs() == 2);
  120.  
  121.  
  122. // Sanity check on the availability of the optional source id before using it.
  123. DRAKE_DEMAND(plant.geometry_source_is_registered());
  124.  
  125. fmt::print("Test Point 2\n");
  126.  
  127. builder.Connect(
  128. plant.get_geometry_poses_output_port(),
  129. scene_graph.get_source_pose_port(plant.get_source_id().value()));
  130.  
  131. builder.Connect(
  132. scene_graph.get_query_output_port(),
  133. plant.get_geometry_query_input_port());
  134.  
  135. // geometry::ConnectDrakeVisualizer(&builder, scene_graph);
  136.  
  137. fmt::print("Test Point 3\n");
  138.  
  139. auto diagram = builder.Build();
  140.  
  141. DrakeLcm lcm;
  142. geometry::ConnectDrakeVisualizer(&builder, scene_graph, &lcm);
  143.  
  144. // Create a context for this system:
  145. std::unique_ptr<systems::Context<double>> diagram_context =
  146. diagram->CreateDefaultContext();
  147. diagram->SetDefaultContext(diagram_context.get());
  148. systems::Context<double>& plant_context =
  149. diagram->GetMutableSubsystemContext(plant, diagram_context.get());
  150.  
  151. fmt::print("Test Point 4\n");
  152.  
  153. const Eigen::VectorXd tau = Eigen::VectorXd::Zero(plant.num_actuated_dofs());
  154. fmt::print("tau = {}\n", tau);
  155. plant.get_actuation_input_port().FixValue(&plant_context, tau);
  156. // plant.get_input_port(0).FixValue(&plant_context, Vector1d(0.0));
  157.  
  158. fmt::print("Test Point 5\n");
  159.  
  160.  
  161. // systems::Simulator<double> simulator(*diagram);
  162. systems::Simulator<double> simulator(*diagram, std::move(diagram_context));
  163.  
  164. fmt::print("Test Point 6\n");
  165.  
  166. simulator.set_target_realtime_rate(FLAGS_target_realtime_rate);
  167. fmt::print("Test Point 7\n");
  168. simulator.get_mutable_context().SetAccuracy(1e-4);
  169. fmt::print("Test Point 8\n");
  170.  
  171. simulator.Initialize();
  172. simulator.AdvanceTo(10);
  173. fmt::print("Test Point 9\n");
  174.  
  175. // Add a line break before simulator statistics.
  176. fmt::print("\n");
  177.  
  178. systems::PrintSimulatorStatistics(simulator);
  179. fmt::print("Test Point 10\n");
  180.  
  181.  
  182. return 0;
  183. }
  184.  
  185.  
  186.  
  187. } //namespace
  188.  
  189. } //test_urdf
  190. } // example
  191. } // drake
  192.  
  193.  
  194. int main(int argc, char* argv[]) {
  195. gflags::SetUsageMessage(
  196. "Demo used to exercise MultibodyPlant's contact modeling in a gripping "
  197. "scenario. SceneGraph is used for both visualization and contact "
  198. "handling. "
  199. "Launch drake-visualizer before running this example.");
  200. gflags::ParseCommandLineFlags(&argc, &argv, true);
  201. return drake::examples::test_urdf::do_main();
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement