Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_CustomRotations/AP_CustomRotations.cpp
Views: 1798
#include "AP_CustomRotations_config.h"12#if AP_CUSTOMROTATIONS_ENABLED34#include "AP_CustomRotations.h"56#include <AP_InternalError/AP_InternalError.h>7#include <AP_Vehicle/AP_Vehicle_Type.h>8#include <AP_HAL/AP_HAL_Boards.h>9#include <AP_BoardConfig/AP_BoardConfig.h>1011const AP_Param::GroupInfo AP_CustomRotations::var_info[] = {1213// @Param: _ENABLE14// @DisplayName: Enable Custom rotations15// @Values: 0:Disable, 1:Enable16// @Description: This enables custom rotations17// @User: Standard18// @RebootRequired: True19AP_GROUPINFO_FLAGS("_ENABLE", 1, AP_CustomRotations, enable, 0, AP_PARAM_FLAG_ENABLE),2021// @Group: 1_22// @Path: AP_CustomRotations_params.cpp23AP_SUBGROUPINFO(params[0], "1_", 2, AP_CustomRotations, AP_CustomRotation_params),2425// @Group: 2_26// @Path: AP_CustomRotations_params.cpp27AP_SUBGROUPINFO(params[1], "2_", 3, AP_CustomRotations, AP_CustomRotation_params),2829AP_GROUPEND30};3132AP_CustomRotation::AP_CustomRotation(AP_CustomRotation_params &_params):33params(_params)34{35init();36}3738void AP_CustomRotation::init()39{40m.from_euler(radians(params.roll),radians(params.pitch),radians(params.yaw));41q.from_rotation_matrix(m);42}4344AP_CustomRotations::AP_CustomRotations()45{46AP_Param::setup_object_defaults(this, var_info);4748#if CONFIG_HAL_BOARD == HAL_BOARD_SITL49if (singleton != nullptr) {50AP_HAL::panic("AP_CustomRotations must be singleton");51}52#endif53singleton = this;54}5556void AP_CustomRotations::init()57{58if (enable == 0) {59return;60}6162// make sure all custom rotations are allocated63for (uint8_t i = 0; i < NUM_CUST_ROT; i++) {64AP_CustomRotation* rot = get_rotation(Rotation(i + ROTATION_CUSTOM_1));65if (rot == nullptr) {66AP_BoardConfig::allocation_error("Custom Rotations");67}68}69}7071void AP_CustomRotations::convert(Rotation r, float roll, float pitch, float yaw)72{73AP_CustomRotation* rot = get_rotation(r);74if (rot == nullptr) {75return;76}77if (!rot->params.roll.configured() && !rot->params.pitch.configured() && !rot->params.yaw.configured()) {78rot->params.roll.set_and_save(roll);79rot->params.pitch.set_and_save(pitch);80rot->params.yaw.set_and_save(yaw);81rot->init();82}83}8485void AP_CustomRotations::set(Rotation r, float roll, float pitch, float yaw)86{87AP_CustomRotation* rot = get_rotation(r);88if (rot == nullptr) {89return;90}91rot->params.roll.set(roll);92rot->params.pitch.set(pitch);93rot->params.yaw.set(yaw);94rot->init();95}9697void AP_CustomRotations::from_rotation(Rotation r, QuaternionD& q)98{99AP_CustomRotation* rot = get_rotation(r);100if (rot == nullptr) {101return;102}103q = rot->q.todouble();104}105106void AP_CustomRotations::from_rotation(Rotation r, Quaternion& q)107{108AP_CustomRotation* rot = get_rotation(r);109if (rot == nullptr) {110return;111}112q = rot->q;113}114115void AP_CustomRotations::rotate(Rotation r, Vector3d& v)116{117AP_CustomRotation* rot = get_rotation(r);118if (rot == nullptr) {119return;120}121v = (rot->m * v.tofloat()).todouble();122}123124void AP_CustomRotations::rotate(Rotation r, Vector3f& v)125{126AP_CustomRotation* rot = get_rotation(r);127if (rot == nullptr) {128return;129}130v = rot->m * v;131}132133AP_CustomRotation* AP_CustomRotations::get_rotation(Rotation r)134{135if (r < ROTATION_CUSTOM_1 || r >= ROTATION_CUSTOM_END) {136INTERNAL_ERROR(AP_InternalError::error_t::bad_rotation);137return nullptr;138}139const uint8_t index = r - ROTATION_CUSTOM_1;140if (rotations[index] == nullptr) {141rotations[index] = NEW_NOTHROW AP_CustomRotation(params[index]);142// make sure param is enabled if custom rotation is used143enable.set_and_save_ifchanged(1);144}145return rotations[index];146}147148// singleton instance149AP_CustomRotations *AP_CustomRotations::singleton;150151namespace AP {152153AP_CustomRotations &custom_rotations()154{155return *AP_CustomRotations::get_singleton();156}157158}159160#endif // AP_CUSTOMROTATIONS_ENABLED161162163