【问题标题】:Why cos() need rad on my calculator need only degrees为什么我的计算器上的 cos() 需要 rad 只需要度数
【发布时间】:2020-03-09 13:25:44
【问题描述】:

请给我的 Arduino 狗提供此代码
但它不起作用,因为我没有将角度转换为 rad 进行 cos() 操作,但最后我需要获取角度(在 cos() 操作之后)。

const int b = 200; //Lengt
const int Pin = A0; //Pot pin
int c = 0; //Variable for Angle
int d = 0; //
int e = 0; //Final side size

void setup() {
  Serial.begin(9600);
}

void loop() {
c = analogRead(Pin);//Read potval write to variable c
c = map(c, 0, 1023, 0, 180);//Map potval to Angle
c = cos(c)//Calculate Cosinus
d = b - (b * c);//200 - (200 * c)
e = sqrt(d)//Calcute √e
Serial.print(e)//Print out final side a
}


//Example:
//d = 200 − (200 * 0,6560)
//d = 200 − 131,2 = 68.8
//e = √68.8
//e = 8.29

【问题讨论】:

  • Rad to degree 与 pi 有某种关系 :) 。 (pi 甚至是一个预定义的常数)。通常,int 计算是首选,但这里不是。 cos 返回一个介于 -1 和 +1 之间的数字,因此只有三个可能的 int 结果; ( -1, 0, 1 )

标签: math arduino trigonometry robot


【解决方案1】:

你的问题是“为什么?”

答案是:因为它在 C++ 中是这样定义的。

【讨论】:

    【解决方案2】:

    因为函数 cos() 被定义为接受以弧度表示的参数:https://www.arduino.cc/reference/en/language/functions/trigonometry/cos/

    弧度是角度的SI unit,大多数编程库都希望角度参数以弧度为单位。大多数科学计算器都有一个用于在弧度和度数(有时是梯度)之间切换的按钮。

    【讨论】:

    • 我宁愿写为什么因为我不能发布它可以有人帮助我请更正我的代码以工作
    • 变量 c、d、e 中的整数会丢失太多精度。使用浮点数!
    猜你喜欢
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-21
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多