2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
|
|
|
#include "frc/geometry/Rotation2d.h"
|
|
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
2019-11-02 13:35:03 -05:00
|
|
|
#include <wpi/json.h>
|
|
|
|
|
|
2020-08-06 23:57:39 -07:00
|
|
|
#include "units/math.h"
|
|
|
|
|
|
2019-07-24 02:57:39 -04:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2019-08-17 01:00:33 -04:00
|
|
|
Rotation2d::Rotation2d(units::radian_t value)
|
|
|
|
|
: m_value(value),
|
|
|
|
|
m_cos(units::math::cos(value)),
|
|
|
|
|
m_sin(units::math::sin(value)) {}
|
2019-07-24 02:57:39 -04:00
|
|
|
|
2020-07-03 21:58:19 -07:00
|
|
|
Rotation2d::Rotation2d(units::degree_t value)
|
|
|
|
|
: m_value(value),
|
|
|
|
|
m_cos(units::math::cos(value)),
|
|
|
|
|
m_sin(units::math::sin(value)) {}
|
|
|
|
|
|
2019-07-24 02:57:39 -04:00
|
|
|
Rotation2d::Rotation2d(double x, double y) {
|
|
|
|
|
const auto magnitude = std::hypot(x, y);
|
|
|
|
|
if (magnitude > 1e-6) {
|
|
|
|
|
m_sin = y / magnitude;
|
|
|
|
|
m_cos = x / magnitude;
|
|
|
|
|
} else {
|
|
|
|
|
m_sin = 0.0;
|
|
|
|
|
m_cos = 1.0;
|
|
|
|
|
}
|
2019-08-17 01:00:33 -04:00
|
|
|
m_value = units::radian_t(std::atan2(m_sin, m_cos));
|
2019-07-24 02:57:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Rotation2d Rotation2d::operator+(const Rotation2d& other) const {
|
|
|
|
|
return RotateBy(other);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Rotation2d& Rotation2d::operator+=(const Rotation2d& other) {
|
|
|
|
|
double cos = Cos() * other.Cos() - Sin() * other.Sin();
|
|
|
|
|
double sin = Cos() * other.Sin() + Sin() * other.Cos();
|
|
|
|
|
m_cos = cos;
|
|
|
|
|
m_sin = sin;
|
2019-08-17 01:00:33 -04:00
|
|
|
m_value = units::radian_t(std::atan2(m_sin, m_cos));
|
2019-07-24 02:57:39 -04:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Rotation2d Rotation2d::operator-(const Rotation2d& other) const {
|
|
|
|
|
return *this + -other;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Rotation2d& Rotation2d::operator-=(const Rotation2d& other) {
|
|
|
|
|
*this += -other;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Rotation2d Rotation2d::operator-() const { return Rotation2d(-m_value); }
|
|
|
|
|
|
2019-09-28 18:40:56 -04:00
|
|
|
Rotation2d Rotation2d::operator*(double scalar) const {
|
|
|
|
|
return Rotation2d(m_value * scalar);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-08 14:20:26 -04:00
|
|
|
bool Rotation2d::operator==(const Rotation2d& other) const {
|
|
|
|
|
return units::math::abs(m_value - other.m_value) < 1E-9_rad;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Rotation2d::operator!=(const Rotation2d& other) const {
|
|
|
|
|
return !operator==(other);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-24 02:57:39 -04:00
|
|
|
Rotation2d Rotation2d::RotateBy(const Rotation2d& other) const {
|
|
|
|
|
return {Cos() * other.Cos() - Sin() * other.Sin(),
|
|
|
|
|
Cos() * other.Sin() + Sin() * other.Cos()};
|
|
|
|
|
}
|
2019-11-02 13:35:03 -05:00
|
|
|
|
|
|
|
|
void frc::to_json(wpi::json& json, const Rotation2d& rotation) {
|
|
|
|
|
json = wpi::json{{"radians", rotation.Radians().to<double>()}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void frc::from_json(const wpi::json& json, Rotation2d& rotation) {
|
|
|
|
|
rotation = Rotation2d{units::radian_t{json.at("radians").get<double>()}};
|
|
|
|
|
}
|