This example shows how to retrieve data from an ATI F/T sensor connected to a DAQ board. He we have 2 threads:
#include <iostream>
#include <visp3/core/vpIoTools.h>
#include <visp3/core/vpTime.h>
#include <visp3/gui/vpPlot.h>
#include <visp3/sensor/vpForceTorqueAtiSensor.h>
#if defined(VISP_HAVE_THREADS)
#include <thread>
#include <mutex>
typedef enum { BIAS_DONE, UNBIAS_DONE, TO_BIAS, TO_UNBIAS } BiasState;
#ifdef ENABLE_VISP_NAMESPACE
#endif
#ifndef DOXYGEN_SHOULD_SKIP_THIS
typedef struct
{
double timestamp;
BiasState bias_state;
} t_shared_data;
#endif
void scopeFunction(std::mutex &mutex_data, std::mutex &mutex_state, t_shared_data &s_shared_data, bool &s_state_stop)
{
#ifdef VISP_HAVE_DISPLAY
vpPlot scope(2, 700, 700, 100, 200,
"ATI F/T sensor data");
scope.initGraph(0, 3);
scope.initGraph(1, 3);
scope.setTitle(0, "Forces (N)");
scope.setTitle(1, "Torques (Nm)");
scope.setLegend(0, 0, "x");
scope.setLegend(0, 1, "y");
scope.setLegend(0, 2, "z");
scope.setLegend(1, 0, "x");
scope.setLegend(1, 1, "y");
scope.setLegend(1, 2, "z");
#else
(void)mutex_data;
(void)s_shared_data;
#endif
t_shared_data shared_data;
#ifdef VISP_HAVE_DISPLAY
bool click = false;
#else
#endif
do {
#ifdef VISP_HAVE_DISPLAY
{
std::lock_guard<std::mutex> lock(mutex_data);
shared_data.ft = s_shared_data.ft;
shared_data.timestamp = s_shared_data.timestamp;
shared_data.bias_state = s_shared_data.bias_state;
}
scope.plot(0, shared_data.timestamp, force);
scope.plot(1, shared_data.timestamp, torque);
if (shared_data.bias_state == BIAS_DONE)
else if (shared_data.bias_state == UNBIAS_DONE)
else if (shared_data.bias_state == TO_BIAS)
else if (shared_data.bias_state == TO_UNBIAS)
if (shared_data.bias_state == BIAS_DONE)
shared_data.bias_state = TO_UNBIAS;
else if (shared_data.bias_state == UNBIAS_DONE)
shared_data.bias_state = TO_BIAS;
{
std::lock_guard<std::mutex> lock(mutex_data);
s_shared_data.bias_state = shared_data.bias_state;
}
}
#endif
}
#ifdef VISP_HAVE_DISPLAY
#else
#endif
{
std::lock_guard<std::mutex> lock(mutex_state);
s_state_stop = true;
}
std::cout << "End of scope thread" << std::endl;
}
int main(int argc, char **argv)
{
#if defined(VISP_HAVE_ATIDAQ) && defined(VISP_HAVE_COMEDI)
#ifdef VISP_HAVE_VIPER850_DATA
(void)argc;
(void)argv;
std::string calibfile = std::string(VISP_VIPER850_DATA_PATH) + std::string("/ati/FT17824.cal");
std::cout << "ATI F/T calib file \"" << calibfile << "\" doesn't exist";
return EXIT_FAILURE;
}
#else
if (argc != 2) {
std::cout << "Usage: " << argv[0] << " <ATI calibration file FT*.cal]>" << std::endl;
return EXIT_FAILURE;
}
std::string calibfile(argv[1]);
#endif
std::cout << "ATI F/T sensor characteristics: \n" << ati << std::endl;
std::cout << "Data recording in progress..." << std::endl;
std::mutex mutex_data;
std::mutex mutex_state;
bool state_stop = false;
bool s_state_stop = false;
t_shared_data shared_data;
t_shared_data s_shared_data;
std::thread thread_scope(&scopeFunction, std::ref(mutex_data), std::ref(mutex_state), std::ref(s_shared_data), std::ref(s_state_stop));
std::string file("recorded-ft-sync.txt");
std::ofstream f(file.c_str());
do {
double timestamp = loop_time - start_time;
{
std::lock_guard<std::mutex> lock(mutex_data);
shared_data.bias_state = s_shared_data.bias_state;
}
if (shared_data.bias_state == TO_BIAS) {
std::cout << "Bias sensor" << std::endl;
std::cout << "Unbias sensor" << std::endl;
shared_data.bias_state = BIAS_DONE;
}
else if (shared_data.bias_state == TO_UNBIAS) {
shared_data.bias_state = UNBIAS_DONE;
}
{
std::lock_guard<std::mutex> lock(mutex_data);
s_shared_data.ft = ft;
s_shared_data.timestamp = timestamp;
s_shared_data.bias_state = shared_data.bias_state;
}
{
std::lock_guard<std::mutex> lock(mutex_state);
state_stop = s_state_stop;
}
f << timestamp <<
" " << ft.
t() << std::endl;
} while (!state_stop);
thread_scope.join();
f.close();
std::cout << "Data recorded in " << file << std::endl;
#else
(void)argc;
(void)argv;
std::cout << "You should install comedi and build atidaq to enable this test..." << std::endl;
#endif
return EXIT_SUCCESS;
}
#else
int main()
{
std::cout << "You should build this test with threading capabilities..." << std::endl;
return EXIT_SUCCESS;
}
#endif
Implementation of column vector and the associated operations.
vpColVector extract(unsigned int r, unsigned int colsize) const
static const vpColor blue
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
vpColVector getForceTorque() const
void setCalibrationFile(const std::string &calibfile, unsigned short index=1)
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()