joint_position_example_controller.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 Franka Emika GmbH
2 // Use of this source code is governed by the Apache-2.0 license, see LICENSE
4 
5 #include <cmath>
6 
11 #include <ros/ros.h>
12 
14 
16  ros::NodeHandle& node_handle) {
18  if (position_joint_interface_ == nullptr) {
19  ROS_ERROR(
20  "JointPositionExampleController: Error getting position joint interface from hardware!");
21  return false;
22  }
23  std::vector<std::string> joint_names;
24  if (!node_handle.getParam("joint_names", joint_names)) {
25  ROS_ERROR("JointPositionExampleController: Could not parse joint names");
26  }
27  if (joint_names.size() != 7) {
28  ROS_ERROR_STREAM("JointPositionExampleController: Wrong number of joint names, got "
29  << joint_names.size() << " instead of 7 names!");
30  return false;
31  }
32  position_joint_handles_.resize(7);
33  for (size_t i = 0; i < 7; ++i) {
34  try {
38  "JointPositionExampleController: Exception getting joint handles: " << e.what());
39  return false;
40  }
41  }
42 
43  std::array<double, 7> q_start{{0, -M_PI_4, 0, -3 * M_PI_4, 0, M_PI_2, M_PI_4}};
44  for (size_t i = 0; i < q_start.size(); i++) {
45  if (std::abs(position_joint_handles_[i].getPosition() - q_start[i]) > 0.1) {
47  "JointPositionExampleController: Robot is not in the expected starting position for "
48  "running this example. Run `roslaunch franka_example_controllers move_to_start.launch "
49  "robot_ip:=<robot-ip> load_gripper:=<has-attached-gripper>` first.");
50  return false;
51  }
52  }
53 
54  return true;
55 }
56 
58  for (size_t i = 0; i < 7; ++i) {
59  initial_pose_[i] = position_joint_handles_[i].getPosition();
60  }
62 }
63 
65  const ros::Duration& period) {
66  elapsed_time_ += period;
67 
68  double delta_angle = M_PI / 16 * (1 - std::cos(M_PI / 5.0 * elapsed_time_.toSec())) * 0.2;
69  for (size_t i = 0; i < 7; ++i) {
70  if (i == 4) {
71  position_joint_handles_[i].setCommand(initial_pose_[i] - delta_angle);
72  } else {
73  position_joint_handles_[i].setCommand(initial_pose_[i] + delta_angle);
74  }
75  }
76 }
77 
78 } // namespace franka_example_controllers
79 
std::vector< hardware_interface::JointHandle > position_joint_handles_
#define M_PI
std::array< std::string, 7 > joint_names
void update(const ros::Time &, const ros::Duration &period) override
hardware_interface::PositionJointInterface * position_joint_interface_
bool init(hardware_interface::RobotHW *robot_hardware, ros::NodeHandle &node_handle) override
#define M_PI_2
JointHandle getHandle(const std::string &name)
bool getParam(const std::string &key, std::string &s) const
PLUGINLIB_EXPORT_CLASS(moveit_fake_controller_manager::MoveItFakeControllerManager, moveit_controller_manager::MoveItControllerManager)
#define ROS_ERROR_STREAM(args)
#define ROS_ERROR(...)


franka_example_controllers
Author(s): Franka Emika GmbH
autogenerated on Fri Oct 23 2020 03:47:17