我在代码中同时使用 OpenCV 库和 C++ NAOqi API。特别是,我需要使用setAngles()方法来设置机器人的头部关节。执行这些行:
//(1) set desired joint values on the desired joint names
pan = 0.0;
tilt = 0.0;
AL::ALValue names = AL::ALValue::array(PAN_JOINT,TILT_JOINT);
AL::ALValue angles = AL::ALValue::array(pan,tilt);
cout << "pan: " << pan << endl;
cout << "tilt: " << tilt << endl;
motionPtr->setAngles(names,angles,0.1f);
//(2) capture image from subscribed camera
AL::ALValue img = cameraPtr->getImageRemote(cameraClient);
imgHeader.data = (uchar*) img[6].GetBinary();
cameraPtr->releaseImage(cameraClient);
//(3) show image
imshow("test",imgHeader);
waitKey(30);
导致崩溃并出现以下错误:
在抛出 'AL::ALError' 的实例后调用终止 what(): ALMotion::setAngles ALBroker::methodCall: method: setAngles, params: [["HeadYaw", "HeadPitch"], [0, 0], 0 ] ALMotion::setAngles ALMotion::setAngles fractionMaxSpeed: 预期最大速度的一小部分在 0.0 和 1.0 之间
即它说的第三个参数setAngles()
不是预期的0.0和1.0之间的值。实际上,它将其读取为 0(可能转换为int
?)。当然,这不是真的,从代码中可以看出。“有趣”的方面是,如果我评论cv::imshow()
,代码正常工作并且不会引发错误。
有谁知道为什么会发生这种情况的可能原因?显然,在机器人上设置角度的操作与通过 OpenCV 显示图像的操作之间没有任何联系。你能帮助我吗?谢谢
更新:也许我忘了说这段代码在一个while循环中。无论如何,使用post
NAOqi 代理对象的属性,我使该方法setAngles
在并行线程中在后台运行。通过这种方式,图像可以正确显示并且不会引发错误。但是,机器人关节没有设置,机器人是静止的。cv::imshow
同样,如果未调用,则不会发生这种情况。这是包含整个 while 循环的完整函数。
//these are member variables of a class
double pan = 0.0;
double tilt = 0.0;
Mat imgHeader = Mat(320,240,CV8UC3);
bool headset = false;
//main loop
while(!headset){
motionPtr->setStiffnesses("Head", 1.0f);
int dtilt, dpan;
int res;
/**** Get inputs from joystick ***/
res = joy->readEv();
if(res != -1){
if(jse->type & JS_EVENT_BUTTON){
if((int)jse->number == X_BUTTON){//tilt down
dtilt = -1;
}
else if((int)jse->number == TRIANGLE_BUTTON){//tilt up
dtilt = 1;
}
else if((int)jse->number == START_BUTTON){//tilt down
(firstKeyIgnored) ? (headset = true) : (firstKeyIgnored = true) ;
}
else{
dtilt = 0;
}
}
updateTilt(dtilt); //<-- it just changes the value of the member variable `tilt`
}
AL::ALValue names = AL::ALValue::array(PAN_JOINT,TILT_JOINT);
AL::ALValue angles = AL::ALValue::array(pan,tilt);
int id;
id = motionPtr->post.setAngles(names,angles,0.1f);
//capture image from subscribed camera
ALimg = cameraPtr->getImageRemote(cameraClient);
imgHeader.data = (uchar*) ALimg[6].getObject();
imshow("test",imgHeader);
waitKey(30);//*/
}