测量样本
本示例展示了如何使用 CPP HaplyHardwareAPI 将 Inverse3 用作纯输入设备。 完整示例可在文件末尾找到,但主要步骤如下:
包括必要的标题
#include "HardwareAPI.h"
查找设备并打开连接
std::vector ports =
Haply::HardwareAPI::Devices::DeviceDetection::DetectInverse3s(); // List all the available devices
Haply::HardwareAPI::IO::SerialStream serial_stream(ports[0].c_str()); // Open the connection to the first device found
Haply::HardwareAPI::Devices::Inverse3 inverse3(&serial_stream; // Create the device object
Haply::HardwareAPI::Devices::Inverse3::DeviceInfoResponse response_to_wake = inverse3.DeviceWakeup(); // Get the device info
获取位置测量值
从 Inverse3 获取位置测量值有两种方法。 第一种是利用设备内置的运动学,第二种是利用计算机解析的运动学。
使用设备内置运动学
使用设备内置的运动学原理是获取位置测量值的最简单方法,它会自动调整底座的方向,并返回末端效应器的位置。
// In a loop
while(True) {
Haply::HardwareAPI::Devices::Inverse3::EndEffectorStateResponse
response = inverse3.GetEndEffectorPosition();
printf("Position: %f %f %f\n", response.position[0],
response.position[1], response.position[2]);
}
使用计算机上解析的运动学数据
由于我们使用 IMU 来获取底座的方向,因此在非常精确的应用中,它可能会导致位置测量的变化。 为了避免这种情况,可以在计算机上解决运动学问题,并手动设置底座的角度,使其与物理设置相匹配。
// Set the angles of the base, default position is upright
// Inverse3.AdjustAngles([angle0, angle1, angle2]);
// In a loop
while(True) {
Haply::HardwareAPI::Devices::Inverse3::EndEffectorStateResponse
response = inverse3.GetEndEffectorPosition(false);
printf("Position: %f %f %f\n", response.position[0],
response.position[1], response.position[2]);
}
完整示例
#include <string.h>
#include <chrono>
#include <iostream>
#include <iterator>
#include <string>
#include <thread>
#include "HardwareAPI.h"
int main(int argc, char* argv[])
{
char* portName;
if (argc < 2)
{
std::vector ports =
Haply::HardwareAPI::Devices::DeviceDetection::DetectInverse3s();
std::string portNames[256];
int nbport = ports.size();
#if defined _DEBUG
printf("Found %d ports: \n", nbport);
#endif
if (nbport > 0)
{
int index = nbport - 1;
#if defined(_WIN32) || defined(_WIN64)
portName = _strdup(ports[index].c_str());
#endif
#if defined(__linux__) || defined(__APPLE__)
portName = strdup(ports[index].c_str());
#endif
}
else
{
#if defined _DEBUG
std::cout << "No Inverse3 found" << std::endl;
#endif
return -1;
}
}
else
{
#if defined(_WIN32) || defined(_WIN64)
portName = _strdup(argv[1]); // argv1;
#endif
#if defined(__linux__)
portName = strdup(argv[1]); // argv1;
#endif
}
#if defined _DEBUG
printf("Using port %s\n", portName);
#endif
Haply::HardwareAPI::IO::SerialStream serial_stream(portName);
if (serial_stream.OpenDevice() < 0)
printf("unable to open serial stream for '%s'", portName);
Haply::HardwareAPI::Devices::Inverse3 inverse3(&serial_stream);
Haply::HardwareAPI::Devices::Inverse3::DeviceInfoResponse response_to_wake =
inverse3.DeviceWakeup();
#if defined _DEBUG
std::cout << std::endl << "Press ENTER to continue . . .";
std::cin.get();
#endif
while (true)
{
// This is the fast update loop
// To Use the Inverse3 as a pure measurement device, use the following
// functions Inverse3.GetEndEffectorPosition(); // for standard
// Kinematics For added precision, the kinematics can be resolved on the
// computer with the following functions
// Inverse3.GetEndEffectorPosition(false); for on-computer Kinematics
// When using the on-computer Kinematics, the angles of the base can be
// adjusted with the following function to match the physical setup:
// Inverse3.AdjustAngles([angle0, angle1, angle2]);
Haply::HardwareAPI::Devices::Inverse3::EndEffectorStateResponse
response = inverse3.GetEndEffectorPosition(false);
printf("Position: %f %f %f\n", response.position[0],
response.position[1], response.position[2]);
std::this_thread::sleep_for(std::chrono::microseconds(100));
}
}