Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        3年前关闭。
                                                                                            
                
        
我正在尝试计算theta在0-2 pi范围内的方向,以指示放置在0度,120和240度标记处的三个麦克风的合成矢量的方向。所有的麦克风都在工作,但是以某种方式我似乎无法获得正确的度值。有人可以看一下我的代码吗?

void detDirection(){

//Mic1
float x0 =0;
float y0 =volts[0];
//Serial.println(x0);
// Serial.println(y0);
//Mic2
float x1 =volts[1]*cos(120.0/360 * 2*Pi);
float y1 =volts[1]*sin(120.0/360 * 2*Pi);
// Serial.println(x1);
//Serial.println(y1);
//Mic3
float x2 =volts[2]*cos(240.0/360 * 2*Pi);
float y2 = volts[2]*sin(240.0/360 * 2*Pi);
//Serial.println(x2);
//Serial.println(y2);
//Calculate resultant
float sumX = x0 + x1 + x2;
float sumY = y0 + y1 + y2;
//Serial.println(sumX);
//Serial.println(sumY);
float resultant = pow(pow(sumX,2)+pow(sumY,2),0.5);
float degree = atan2(sumY,sumX);
float fixDegree= 0 ;
//Fix degree
if(!isNeg(sumX) && !isNeg(sumY)){
  fixDegree = degree;
}
else if(isNeg(sumX) && !isNeg(sumY)){
  fixDegree = Pi - degree;
}
else if(isNeg(sumX) && isNeg(sumY)){
  fixDegree = Pi+ degree;
}
else if (!isNeg(sumX) && isNeg(sumY)){
  fixDegree = 2*Pi - degree;
}

String text = "";
text.concat(resultant);
text.concat(" ");
text.concat(fixDegree);
text.concat(" ");
//Serial.println(text);
}

最佳答案

好吧...我不确定,但是...如果你写

//Mic2
float x1 =volts[1]*cos(120.0/360 * 2*Pi);
float y1 =volts[1]*sin(120.0/360 * 2*Pi);




//Mic3
float x2 =volts[2]*cos(240.0/360 * 2*Pi);
float y2 = volts[2]*sin(240.0/360 * 2*Pi);


我想您打算使用Mic1(x0y0)是

//Mic1
float x0 =volts[0]*cos(0.0/360 * 2*Pi);
float y0 =volts[0]*sin(0.0/360 * 2*Pi);


那是

//Mic1
float x0 =volts[0]*cos(0.0);
float y0 =volts[0]*sin(0.0);


并且,记住cos(0.0)是1,而sin(0.0)是0,

//Mic1
float x0 =volts[0];
float y0 =0.0;


但是在你的代码中我看到

//Mic1
float x0 =0;
float y0 =volts[0];


简要地说,我怀疑您已将x0y0切换了。

附注:对不起,我的英语不好。

10-01 20:26
查看更多