PCSDK提供的dll基于c#。如果其他语言要获取机器人信息或者写入数据,使用webservice更方便。 webservice相关介绍参见:https://developercenter.robotstudio.com/api/rwsApi/ 例如,希望在c++获取当前机器人位置,可以使用以下代码:
#include <iostream> #include <string> #include <curl/curl.h> #include <nlohmann/json.hpp>
using json = nlohmann::json;
size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) { ((std::string*)userp)->append((char*)contents, size * nmemb); return size * nmemb; }
int main() { CURL* curl; CURLcode res;
curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init();
if (curl) { std::string readBuffer;
std::string url = "http://127.0.0.1/rw/motionsystem ... arget/?json=1";
std::string userpwd = "Default User:robotics";
struct curl_slist* headers = NULL; curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); curl_easy_setopt(curl, CURLOPT_USERPWD, userpwd.c_str());
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
if (res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); } else { try { json j = json::parse(readBuffer); json state = j["_embedded"]["_state"][0]; std::string x = state["x"]; std::string y = state["y"]; std::string z = state["z"]; std::string q1 = state["q1"]; std::string q2 = state["q2"]; std::string q3 = state["q3"]; std::string q4 = state["q4"];
Quaternion q; q.w = std::stod(q1); q.x = std::stod(q2); q.y = std::stod(q3); q.z = std::stod(q4); EulerAngles angles = ToEulerAngles(q);
std::cout << "机器人位置:" << std::endl; std::cout << "x: " << x << ", y: " << y << ", z: " << z << std::endl; std::cout << "rz: " << angles.yaw << ", ry: " << angles.pitch << ", rx: " <<angles.roll << std::endl; std::cout << "q1: " << q1 << ", q2: " << q2 << ", q3: " << q3 << ", q4: " << q4 << std::endl; } catch (const json::parse_error& e) { std::cerr << "Error parsing JSON: " << e.what() << std::endl; } }
curl_slist_free_all(headers); curl_easy_cleanup(curl); }
curl_global_cleanup();
return 0; }
struct Quaternion { double w, x, y, z; };
struct EulerAngles { double roll, pitch, yaw; };
Quaternion ToQuaternion(double yaw, double pitch, double roll) { double cy = cos(yaw * 0.5); double sy = sin(yaw * 0.5); double cp = cos(pitch * 0.5); double sp = sin(pitch * 0.5); double cr = cos(roll * 0.5); double sr = sin(roll * 0.5); Quaternion q; q.w = cy * cp * cr + sy * sp * sr; q.x = cy * cp * sr - sy * sp * cr; q.y = sy * cp * sr + cy * sp * cr; q.z = sy * cp * cr - cy * sp * sr; return q; }
EulerAngles ToEulerAngles(Quaternion q) { EulerAngles angles; double M_PI = 3.14159265;
double sinr_cosp = 2 * (q.w * q.x + q.y * q.z); double cosr_cosp = 1 - 2 * (q.x * q.x + q.y * q.y); angles.roll = std::atan2(sinr_cosp, cosr_cosp)/ M_PI * 180;
double sinp = 2 * (q.w * q.y - q.z * q.x); if (std::abs(sinp) >= 1) angles.pitch = std::copysign(M_PI / 2, sinp)/ M_PI * 180; else angles.pitch = std::asin(sinp)/M_PI * 180;
double siny_cosp = 2 * (q.w * q.z + q.x * q.y); double cosy_cosp = 1 - 2 * (q.y * q.y + q.z * q.z); angles.yaw = std::atan2(siny_cosp, cosy_cosp)/M_PI*180;
return angles; }
********************************
|