我正在开发一个项目,该项目从用户那里获取右手和左手的值,然后将数据发送到 Arduino。我目前的问题是 Arduino 没有收到我能说的任何信息。现在我们将其设置为 5V;如果它收到“2”或“3”并且设置为 0V。当我们在 Arduino 和 Kinect 上运行程序时,我们从未看到引脚的输出降至 0V。
我拥有的完整代码是:
#pragma region Includes
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include <XnOS.h>
#include <GL/glut.h>
#include <math.h>
#include <XnCppWrapper.h>
using namespace xn;
#pragma endregion
#pragma region Defines
//---------------------------------------------------------------------------
// Defines
//---------------------------------------------------------------------------
#define SAMPLE_XML_PATH "../Data/SamplesConfig.xml"
// Pose to Use Added
#define POSE_TO_USE "Psi"
#pragma endregion
#pragma region Global Nodes
//---------------------------------------------------------------------------
// Globals
//---------------------------------------------------------------------------
// Nodes Added for Skeletal Tracking
UserGenerator g_UserGenerator;
#pragma endregion
#pragma region Functions
// Functions Added for Skeletal Tracking
void XN_CALLBACK_TYPE User_NewUser(xn::UserGenerator& generator, XnUserID nId, void* pCookie)
{
printf("New User: %d\n", nId);
g_UserGenerator.GetPoseDetectionCap().StartPoseDetection(POSE_TO_USE, nId);
}
void XN_CALLBACK_TYPE User_LostUser(xn::UserGenerator& generator, XnUserID nId, void* pCookie)
{}
void XN_CALLBACK_TYPE Pose_Detected(xn::PoseDetectionCapability& pose, const XnChar* strPose, XnUserID nId, void* pCookie)
{
printf("Pose %s for user %d\n", strPose, nId);
g_UserGenerator.GetPoseDetectionCap().StopPoseDetection(nId);
g_UserGenerator.GetSkeletonCap().RequestCalibration(nId, TRUE);
}
void XN_CALLBACK_TYPE Calibration_Start(xn::SkeletonCapability& capability, XnUserID nId, void* pCookie)
{
printf("Starting calibration for user %d\n", nId);
}
void XN_CALLBACK_TYPE Calibration_End(xn::SkeletonCapability& capability, XnUserID nId, XnBool bSuccess, void* pCookie)
{
if (bSuccess)
{
printf("User calibrated\n");
g_UserGenerator.GetSkeletonCap().StartTracking(nId);
}
else
{
printf("Failed to calibrate user %d\n", nId);
g_UserGenerator.GetPoseDetectionCap().StartPoseDetection(POSE_TO_USE, nId);
}
}
#pragma endregion
int main(int argc, char* argv[])
{
//System::IO::Ports::SerialPort^ serialPort1;
//serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
//serialPort1->PortName="COM3";
XnStatus nRetVal = XN_STATUS_OK;
Context context;
nRetVal = context.Init();
// TODO: check error code
// Create the user generator
nRetVal = g_UserGenerator.Create(context);
// TODO: check error code
XnCallbackHandle h1, h2, h3;
g_UserGenerator.RegisterUserCallbacks(User_NewUser, User_LostUser, NULL, h1);
g_UserGenerator.GetPoseDetectionCap().RegisterToPoseCallbacks(Pose_Detected, NULL, NULL, h2);
g_UserGenerator.GetSkeletonCap().RegisterCalibrationCallbacks(Calibration_Start, Calibration_End, NULL, h3);
// Set the profile
g_UserGenerator.GetSkeletonCap().SetSkeletonProfile(XN_SKEL_PROFILE_ALL);
// Start generating
nRetVal = context.StartGeneratingAll();
// TODO: check error code
printf("Start Pose:\n");
while (TRUE)
{
// Update to next frame
nRetVal = context.WaitAndUpdateAll();
// TODO: check error code
// Extract hand position of each tracked user
XnUserID aUsers[1];
XnUInt16 nUsers = 1;
g_UserGenerator.GetUsers(aUsers, nUsers);
for (int i = 0; i < nUsers; ++i)
{
if (g_UserGenerator.GetSkeletonCap().IsTracking(aUsers[i]))
{
//serialPort1->Open();
//Get head position
/*XnSkeletonJointPosition Head;
g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(aUsers[i], XN_SKEL_HEAD, Head);
printf("%d: (%f,%f,%f) [%f]\n", aUsers[i], Head.position.X, Head.position.Y, Head.position.Z, Head.fConfidence);*/
//Get right hand position
XnSkeletonJointPosition RightHand;
g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(aUsers[i], XN_SKEL_LEFT_HAND, RightHand);
printf("%d: (%f,%f,%f) [%f]\n", aUsers[i], RightHand.position.X, RightHand.position.Y, RightHand.position.Z, RightHand.fConfidence);
//http://stackoverflow.com/questions/13547574/visual-studio-2010-arduino-serial-communication
int portspeed;
int centerpix;
int xmid;
HANDLE hDevice = CreateFile("COM7",GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,0);
DCB lpTest;
GetCommState(hDevice,&lpTest);
lpTest.BaudRate = CBR_9600;
lpTest.ByteSize = 8;
lpTest.Parity = NOPARITY;
lpTest.StopBits = ONESTOPBIT;
SetCommState(hDevice,&lpTest);
DWORD btsIO;
if (RightHand.position.X<0)
{
char test[] = "2";
WriteFile(hDevice,test,strlen(test),&btsIO,NULL);
printf("Right ");
}
else
{
char test[] = "3";
WriteFile(hDevice,test,strlen(test),&btsIO,NULL);
printf("Left");
}
FlushFileBuffers(hDevice);
}
else
{
printf("Not tracking.\n");
}
}
}
// Clean up
context.Shutdown();
return 0;
}`
Arduino的代码是:
int incomingByte = 0; // For incoming serial data
void setup()
{
Serial.begin(9600); // Opens serial port, sets data rate to 9600 bit/s
}
void loop()
{
// Send data only when you receive data:
//if (Serial.available() > 0)
//{
incomingByte = Serial.read();
if (incomingByte==50) //if =2
analogWrite(9,0);
//else
// analogWrite(9,0);
if (incomingByte==51) //if =3
analogWrite(9,255);
//else
// analogWrite(9,0);
delay(3000);
//}
//else
// analogWrite(9,255);
}
每次 Kinect 接收到用户右手的新值时,此代码当前都处于 while 循环中。
有什么我可以在我的代码中修复或使用不同的代码吗?