주요 콘텐츠로 건너뛰기
버전: 최신

강제 피드백 예시

이 예제는 CPP HaplyHardwareAPI를 사용하여 포스 피드백 모드에서 Haply 하드웨어를 제어하는 방법을 보여줍니다. 전체 예제는 파일 끝에 있지만 주요 단계는 다음과 같습니다:

필요한 헤더 포함

#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

강제 피드백 명령 보내기

// In a loop
while(True) {
Haply::HardwareAPI::Devices::Inverse3::EndEffectorForceRequest request;
// x, y, z forces in N
request.force[0] = 0.0;
request.force[1] = 0.0;
request.force[2] = 0.0;

Haply::HardwareAPI::Devices::Inverse3::EndEffectorStateResponse response = inverse3.EndEffectorForce(request);
// Print the position and velocity
printf("Position: %f %f %f\n", response.position[0], response.position[1], response.position[2]);
printf("Velocity: %f %f %f\n", response.velocity[0], response.velocity[1], response.velocity[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

Haply::HardwareAPI::Devices::Inverse3::EndEffectorForceRequest request;
// x, y, z forces
request.force[0] = 0.0;
request.force[1] = 0.0;
request.force[2] = 0.0;

Haply::HardwareAPI::Devices::Inverse3::EndEffectorStateResponse
response = inverse3.EndEffectorForce(request);
printf("Position: %f %f %f\n", response.position[0],
response.position[1], response.position[2]);
printf("Velocity: %f %f %f\n", response.velocity[0],
response.velocity[1], response.velocity[2]);

std::this_thread::sleep_for(std::chrono::microseconds(100));
}
}