Visual Servoing Platform  version 3.1.0
franka_generate_joint_position_motion.cpp
1 // Copyright (c) 2017 Franka Emika GmbH
2 // Use of this source code is governed by the Apache-2.0 license, see LICENSE
3 #include <cmath>
4 #include <iostream>
5 
6 #include <visp3/core/vpConfig.h>
7 
8 #ifdef VISP_HAVE_FRANKA
9 
10 #include <franka/exception.h>
11 #include <franka/robot.h>
12 
25 int main(int argc, char **argv)
26 {
27  if (argc != 2) {
28  std::cerr << "Usage: ./generate_joint_position_motion <robot-hostname>" << std::endl;
29  return -1;
30  }
31 
32  try {
33  franka::Robot robot(argv[1]);
34 
35  // Set additional parameters always before the control loop, NEVER in the
36  // control loop! Set collision behavior.
37  robot.setCollisionBehavior(
38  {{20.0, 20.0, 18.0, 18.0, 16.0, 14.0, 12.0}}, {{20.0, 20.0, 18.0, 18.0, 16.0, 14.0, 12.0}},
39  {{20.0, 20.0, 18.0, 18.0, 16.0, 14.0, 12.0}}, {{20.0, 20.0, 18.0, 18.0, 16.0, 14.0, 12.0}},
40  {{20.0, 20.0, 20.0, 25.0, 25.0, 25.0}}, {{20.0, 20.0, 20.0, 25.0, 25.0, 25.0}},
41  {{20.0, 20.0, 20.0, 25.0, 25.0, 25.0}}, {{20.0, 20.0, 20.0, 25.0, 25.0, 25.0}});
42 
43  auto initial_position = robot.readOnce().q_d;
44  double time = 0.0;
45  robot.control([=, &time](const franka::RobotState &, franka::Duration time_step) -> franka::JointPositions {
46  time += time_step.toSec();
47 
48  double delta_angle = M_PI / 8 * (1 - std::cos(M_PI / 5.0 * time));
49 
50  franka::JointPositions output = {{initial_position[0], initial_position[1], initial_position[2],
51  initial_position[3] + delta_angle, initial_position[4] + delta_angle,
52  initial_position[5], initial_position[6] + delta_angle}};
53 
54  if (time >= 10.0) {
55  std::cout << std::endl << "Finished motion, shutting down example" << std::endl;
56  return franka::MotionFinished(output);
57  }
58  return output;
59  });
60  } catch (const franka::Exception &e) {
61  std::cout << e.what() << std::endl;
62  return -1;
63  }
64 
65  return 0;
66 }
67 
68 #else
69 int main() { std::cout << "This example needs libfranka to control Panda robot." << std::endl; }
70 #endif