弧度 — 另一种角度单位Radians — Another Unit for Angles
4.1 为什么要学第二种单位?4.1 Why Learn a Second Unit?
我们平时用"度"来表示角度,这是给人看的 -- 0 度、90 度、180 度,很直觉。We normally use "degrees" for angles — it's intuitive for humans: 0°, 90°, 180°.
但 C++ 代码里的 sin() 和 cos() 函数不接受度,它们只接受另一种单位:弧度。But in C++ code, the sin() and cos() functions don't accept degrees — they only accept another unit: radians.
写 sin(90) 不会得到 1。因为 C++ 的 sin() 会把 90 当作 90 弧度(大约是 5156 度),结果完全不对!Writing sin(90) won't give you 1. C++'s sin() treats 90 as 90 radians (about 5156 degrees) — a completely wrong result!
正确写法:sin(90 * M_PI / 180) 或者 sin(1.5708)Correct way: sin(90 * M_PI / 180) or sin(1.5708)
而且弧度还有一个超好用的公式(下面会讲)。所以弧度不只是"另一种写法",它在数学上有独特的优势。Plus, radians have a super useful formula (explained below). So radians aren't just "another way of writing" — they have unique mathematical advantages.
π(读作 "派")≈ 3.14159,是一个数学常数。π (pronounced "pie") ≈ 3.14159, is a mathematical constant.
它的含义:任何圆的周长 ÷ 直径 = π。不管圆有多大多小,这个比值永远是 π。Its meaning: circumference of any circle ÷ diameter = π. No matter how big or small the circle, this ratio is always π.
记住 π ≈ 3.14 就够用了。代码里写 M_PI 就是 π。Just remember π ≈ 3.14. In code, M_PI represents π.
4.2 弧度的定义4.2 Definition of Radians
换算关系很简单:The conversion is simple:
半圈 = 180 度 = π 弧度(约 3.14)Half circle = 180° = π radians (about 3.14)
四分之一圈 = 90 度 = π/2 弧度(约 1.57)Quarter circle = 90° = π/2 radians (about 1.57)
换算公式:Conversion formula:
在代码里,通常定义一个宏或常量来做转换:In code, you typically define a macro or constant for the conversion:
// 度转弧度
double toRad(double deg) {
return deg * M_PI / 180.0;
}
// 弧度转度
double toDeg(double rad) {
return rad * 180.0 / M_PI;
}
4.3 弧度的杀手级应用4.3 The Killer App of Radians
弧度有一个非常简洁的公式:Radians have a beautifully simple formula:
只有用弧度,这个公式才成立。用度不行。This formula only works with radians, not degrees.
举个例子:半径 10cm 的圆弧,转了 90 度(= π/2 弧度),弧长是多少?Example: an arc with radius 10cm, turned 90 degrees (= π/2 radians). What's the arc length?
- 弧长 = 10 × π/2 = 10 × 1.5708 = 15.7cmArc length = 10 × π/2 = 10 × 1.5708 = 15.7cm
这个公式在定位轮里非常重要 -- 定位轮的轮子转过的弧长,就等于半径乘以转过的弧度。这是定位轮测距的数学基础。This formula is crucial for tracking wheels — the arc length traveled by the wheel equals the radius times the radians turned. This is the mathematical foundation of tracking wheel distance measurement.
准备好了!You're Ready!
你已经掌握了理解定位轮需要的所有数学工具。回顾一下:You've mastered all the math tools needed to understand tracking wheels. Let's review:
- 坐标 = 用两个数字描述位置(x 是左右,y 是前后)Coordinates = describe position with two numbers (x is left-right, y is front-back)
- 角度 = 描述机器人朝哪个方向Angles = describe which direction the robot is facing
- sin / cos = 把"斜着走"拆成"往右多少 + 往前多少"sin / cos = split "diagonal movement" into "how far right + how far forward"
- 弧度 = 代码里使用的角度单位,弧长 = 半径 × 弧度Radians = the angle unit used in code; arc length = radius × radians
这四个工具组合在一起,就能理解定位轮的全部数学原理了。These four tools combined let you understand all the math behind tracking wheels.