joint_velocity_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 (velocity_joint_interface_ == nullptr) {
19  ROS_ERROR(
20  "JointVelocityExampleController: Error getting velocity joint interface from hardware!");
21  return false;
22  }
23 
24  std::string arm_id;
25  if (!node_handle.getParam("arm_id", arm_id)) {
26  ROS_ERROR("JointVelocityExampleController: Could not get parameter arm_id");
27  return false;
28  }
29 
30  std::vector<std::string> joint_names;
31  if (!node_handle.getParam("joint_names", joint_names)) {
32  ROS_ERROR("JointVelocityExampleController: Could not parse joint names");
33  }
34  if (joint_names.size() != 7) {
35  ROS_ERROR_STREAM("JointVelocityExampleController: Wrong number of joint names, got "
36  << joint_names.size() << " instead of 7 names!");
37  return false;
38  }
39  velocity_joint_handles_.resize(7);
40  for (size_t i = 0; i < 7; ++i) {
41  try {
45  "JointVelocityExampleController: Exception getting joint handles: " << ex.what());
46  return false;
47  }
48  }
49 
50  auto state_interface = robot_hardware->get<franka_hw::FrankaStateInterface>();
51  if (state_interface == nullptr) {
52  ROS_ERROR("JointVelocityExampleController: Could not get state interface from hardware");
53  return false;
54  }
55 
56  try {
57  auto state_handle = state_interface->getHandle(arm_id + "_robot");
58 
59  std::array<double, 7> q_start{{0, -M_PI_4, 0, -3 * M_PI_4, 0, M_PI_2, M_PI_4}};
60  for (size_t i = 0; i < q_start.size(); i++) {
61  if (std::abs(state_handle.getRobotState().q_d[i] - q_start[i]) > 0.1) {
63  "JointVelocityExampleController: Robot is not in the expected starting position for "
64  "running this example. Run `roslaunch franka_example_controllers move_to_start.launch "
65  "robot_ip:=<robot-ip> load_gripper:=<has-attached-gripper>` first.");
66  return false;
67  }
68  }
71  "JointVelocityExampleController: Exception getting state handle: " << e.what());
72  return false;
73  }
74 
75  return true;
76 }
77 
80 }
81 
83  const ros::Duration& period) {
84  elapsed_time_ += period;
85 
86  ros::Duration time_max(8.0);
87  double omega_max = 0.1;
88  double cycle = std::floor(
89  std::pow(-1.0, (elapsed_time_.toSec() - std::fmod(elapsed_time_.toSec(), time_max.toSec())) /
90  time_max.toSec()));
91  double omega = cycle * omega_max / 2.0 *
92  (1.0 - std::cos(2.0 * M_PI / time_max.toSec() * elapsed_time_.toSec()));
93 
94  for (auto joint_handle : velocity_joint_handles_) {
95  joint_handle.setCommand(omega);
96  }
97 }
98 
100  // WARNING: DO NOT SEND ZERO VELOCITIES HERE AS IN CASE OF ABORTING DURING MOTION
101  // A JUMP TO ZERO WILL BE COMMANDED PUTTING HIGH LOADS ON THE ROBOT. LET THE DEFAULT
102  // BUILT-IN STOPPING BEHAVIOR SLOW DOWN THE ROBOT.
103 }
104 
105 } // namespace franka_example_controllers
106 
std::string arm_id
bool init(hardware_interface::RobotHW *robot_hardware, ros::NodeHandle &node_handle) override
#define ROS_ERROR(...)
std::array< std::string, 7 > joint_names
void update(const ros::Time &, const ros::Duration &period) override
const char * what() const noexcept override
bool getParam(const std::string &key, std::string &s) const
std::vector< hardware_interface::JointHandle > velocity_joint_handles_
JointHandle getHandle(const std::string &name)
#define ROS_ERROR_STREAM(args)
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
hardware_interface::VelocityJointInterface * velocity_joint_interface_


franka_example_controllers
Author(s): Franka Emika GmbH
autogenerated on Mon Sep 19 2022 03:06:01