GeekTrack/4.Software/GeekTrackSDK/Project/GeekTrack.cpp

142 lines
3.5 KiB
C++
Raw Normal View History

#include"stdafx.h"
#include"GeekTrackSDK.h"
#include<stdio.h>
#include<thread>
#include<winsock2.h>
#include<windows.h>
#pragma comment(lib,"ws2_32.lib")
/* max suppport diffrent device id. */
#define MAX_SUPPORT_DEV_NUM 9
struct GeekTrackNode_t {
EulerAngles_t Pose;
Quaternion_t Quat;
bool devOnline;
GeekTrackNode_t() {
memset(&Pose, 0, sizeof(EulerAngles_t));
memset(&Quat, 0, sizeof(Quaternion_t));
devOnline = false;
}
};
GeekTrackNode_t TrackNode[MAX_SUPPORT_DEV_NUM];
SOCKET sock;
sockaddr_in from, a;
WSADATA wsdata;
int fromlength = 0;
std::thread* t;
void GeekTrackUpdate() {
char buf[256];
int i = 0;
while (1)
{
//printf("...\n");
//<2F>ӹ㲥<D3B9><E3B2A5>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>ע<EFBFBD><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󶨵ĵ<F3B6A8B5>ַ<EFBFBD>ͽ<EFBFBD><CDBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD>ĵ<EFBFBD>ַ<EFBFBD>Dz<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
recvfrom(sock, buf, 256, 0, (struct sockaddr FAR*) &from, (int FAR*) & fromlength);
Sleep(10);
float q[4] = { 0.0f };
int id = 0;
//printf("%s\n", buf);
int ret = sscanf_s(buf, "%d %f %f %f %f\n", &id, &q[0], &q[1], &q[2], &q[3]);
if (ret > 0 && (id >= 0 && id < MAX_SUPPORT_DEV_NUM)) {
float yaw = 0.0f, pitch = 0.0f, roll = 0.0f;
2022-09-28 00:43:51 +08:00
//pitch = asin((-2.0 * ((double)q[3]*q[1] - (double)q[0]*q[2])));
//yaw = atan2((double)q[2]*q[1] + (double)q[0]*q[3], 0.5 - (double)q[2]*q[2] - (double)q[3]*q[3]);
//roll = atan2((double)q[2]*q[3] + (double)q[0]*q[1], 0.5 - (double)q[2]*q[2] - (double)q[1]*q[1]);
2022-09-28 00:43:51 +08:00
float w, x, y, z;
w = q[0]; x = q[1]; y = q[2]; z = q[3];
double PI = 3.1415926;
pitch = asin(-2.0f * (z * x - w * y)) * (180.0f / PI);
yaw = atan2(y * x + w * z, 0.5f - y * y - z * z) * (180.0f / PI);
roll = atan2(y * z + w * x, 0.5f - y * y - x * x) * (180.0f / PI);
//yaw *= (180.0f / 3.141592f);
//roll *= (180.0f / 3.141592f);
//pitch *= (180.0f / 3.141592f);
printf("yaw = %f, roll = %f, pitch = %f\n", yaw, roll, pitch);
TrackNode[id].Quat.w = q[0];
TrackNode[id].Quat.x = q[1];
TrackNode[id].Quat.y = q[2];
TrackNode[id].Quat.z = q[3];
TrackNode[id].Pose.x = pitch;
TrackNode[id].Pose.y = roll;
TrackNode[id].Pose.z = yaw;
TrackNode[id].devOnline = true;
}
ZeroMemory(buf, 256);
}
}
void GeekTrack_Init() {
static bool optval = false;
//<2F><><EFBFBD><EFBFBD>SOCKET<45><EFBFBD>汾Ϊ2.0
if (optval == true) {
return;
}
WSAStartup(0x0202, &wsdata);
optval = true;
//Ȼ<><C8BB><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׽<EFBFBD><D7BD>֣<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵĹ㲥<C4B9><E3B2A5>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>
a.sin_family = AF_INET;
a.sin_addr.s_addr = 0;
a.sin_port = htons(9000);
//from.sin_family = AF_INET;
//from.sin_addr.s_addr = INADDR_BROADCAST;
//from.sin_port = htons(5050);
fromlength = sizeof(SOCKADDR);
//<2F><>UDP<44><50>ʼ<EFBFBD><CABC><EFBFBD>׽<EFBFBD><D7BD><EFBFBD>
sock = socket(AF_INET, SOCK_DGRAM, 0);
// <20><><EFBFBD>ø<EFBFBD><C3B8>׽<EFBFBD><D7BD><EFBFBD>Ϊ<EFBFBD><EFBFBD><E3B2A5><EFBFBD>ͣ<EFBFBD>
//setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char FAR *)&optval, sizeof(optval));
// <20>Ѹ<EFBFBD><D1B8>׽<EFBFBD><D7BD>ְ<EFBFBD><D6B0><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ<EFBFBD><D6B7>
bind(sock, (sockaddr*)&a, sizeof(sockaddr_in));
t = new std::thread(GeekTrackUpdate);
}
EulerAngles_t GetEulerAngles(int devId) {
if (devId >= 0 && devId < MAX_SUPPORT_DEV_NUM) {
return TrackNode[devId].Pose;
}
EulerAngles_t Pose = {0.0f, 0.0f, 0.0f};
return Pose;
}
Quaternion_t GetQuaternion(int devId) {
if (devId >= 0 && devId < MAX_SUPPORT_DEV_NUM) {
return TrackNode[devId].Quat;
}
Quaternion_t Quat = { 0.0f, 0.0f, 0.0f, 0.0f };
return Quat;
}
int main()
{
GeekTrack_Init();
int devId = 0;
while (1) {
Sleep(30);
2022-09-28 00:43:51 +08:00
//printf("%d,%f,%f,%f\n", devId,TrackNode[devId].Pose.x, TrackNode[devId].Pose.y,
// TrackNode[devId].Pose.z);
}
system("pause");
return 0;
}