Visual Servoing Platform  version 3.1.0
franka_execute_trajectory.cpp

An example showing how to execute a joint trajectory loaded from a CSV file.

This example is part of libfranka FCI C++ API: https://frankaemika.github.io/libfranka See https://frankaemika.github.io/docs for more details.

// Copyright (c) 2017 Franka Emika GmbH
// Use of this source code is governed by the Apache-2.0 license, see LICENSE
#include <cmath>
#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>
#include <visp3/core/vpConfig.h>
#ifdef VISP_HAVE_FRANKA
#include <franka/exception.h>
#include <franka/robot.h>
template <class T, size_t N> std::ostream &operator<<(std::ostream &ostream, const std::array<T, N> &array)
{
ostream << "[";
std::copy(array.cbegin(), array.cend() - 1, std::ostream_iterator<T>(ostream, ","));
std::copy(array.cend() - 1, array.cend(), std::ostream_iterator<T>(ostream));
ostream << "]";
return ostream;
}
int main(int argc, char **argv)
{
if (argc != 4) {
std::cerr << "Usage: " << argv[0] << " <robot-hostname> <trajectory-csv> <output>" << std::endl;
return -1;
}
std::cout << "Loading csv trajectory" << std::endl;
std::fstream csv_file_stream;
csv_file_stream.open(argv[2], std::fstream::in);
std::vector<std::array<double, 7> > samples;
while (csv_file_stream) {
std::array<double, 7> q;
char delimiter;
for (int i = 0; i < 7; i++) {
csv_file_stream >> q[i] >> delimiter;
}
samples.push_back(q);
}
std::cout << "Read " << samples.size() << " samples" << std::endl;
std::vector<franka::RobotState> states;
try {
franka::Robot robot(argv[1]);
// Set additional parameters always before the control loop, NEVER in the
// control loop! Set collision behavior.
robot.setCollisionBehavior(
{{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}},
{{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}},
{{20.0, 20.0, 20.0, 25.0, 25.0, 25.0}}, {{20.0, 20.0, 20.0, 25.0, 25.0, 25.0}},
{{20.0, 20.0, 20.0, 25.0, 25.0, 25.0}}, {{20.0, 20.0, 20.0, 25.0, 25.0, 25.0}});
// Set the joint impedance.
robot.setJointImpedance({{3000, 3000, 3000, 2500, 2500, 2000, 2000}});
size_t index = 0;
robot.control([&](const franka::RobotState &robot_state, franka::Duration time_step) -> franka::JointPositions {
states.push_back(robot_state);
index += time_step.toMSec();
if (index >= samples.size() - 1) {
return franka::MotionFinished(franka::JointPositions(samples.back()));
}
return samples[index];
});
} catch (const franka::ControlException &e) {
std::cout << e.what() << std::endl;
} catch (const franka::Exception &e) {
std::cout << e.what() << std::endl;
return -1;
}
std::cout << "Logging results to file: " << argv[3] << std::endl;
std::fstream output_stream;
output_stream.open(argv[3], std::fstream::out);
for (size_t s = 0; s < states.size(); s++) {
output_stream << "Sample: #" << s << std::endl;
output_stream << "q_d: \t" << samples[s] << std::endl;
output_stream << "Robot state:" << std::endl;
output_stream << "q: \t" << states[s].q << std::endl;
output_stream << "q_d: \t" << states[s].q_d << std::endl;
output_stream << "dq: \t" << states[s].dq << std::endl;
output_stream << "______" << std::endl;
}
return 0;
}
#else
int main() { std::cout << "This example needs libfranka to control Panda robot." << std::endl; }
#endif