GKD.RoboCtrl
RoboMaster Linux 电控:异步 IO、设备驱动与机器人控制
载入中...
搜索中...
未找到
mecanum.hpp
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <cmath>
6
7#include "utils/utils.hpp"
8
9namespace roboctrl::utils::kinematics {
10
12 fp32 left_front {};
13 fp32 right_front {};
14 fp32 left_rear {};
15 fp32 right_rear {};
16};
17
18inline mecanum_wheel_speeds inverse_mecanum(
19 vectorf velocity, fp32 rotate_speed, fp32 max_wheel_speed)
20{
22 .left_front = velocity.x - velocity.y - rotate_speed,
23 .right_front = velocity.x + velocity.y + rotate_speed,
24 .left_rear = velocity.x + velocity.y - rotate_speed,
25 .right_rear = velocity.x - velocity.y + rotate_speed};
26 const fp32 peak = std::max({std::fabs(result.left_front), std::fabs(result.right_front),
27 std::fabs(result.left_rear), std::fabs(result.right_rear)});
28 if (peak > max_wheel_speed && peak > 0.0f) {
29 const fp32 scale = max_wheel_speed / peak;
30 result.left_front *= scale; result.right_front *= scale;
31 result.left_rear *= scale; result.right_rear *= scale;
32 }
33 return result;
34}
35
37inline std::array<fp32, 4> mecanum_motor_targets(
38 vectorf velocity, fp32 rotate_speed, fp32 max_wheel_speed,
39 const std::array<int, 4>& directions)
40{
41 if (!std::isfinite(velocity.x) || !std::isfinite(velocity.y) ||
42 !std::isfinite(rotate_speed) || !std::isfinite(max_wheel_speed) || max_wheel_speed <= 0.0f ||
43 !std::all_of(directions.begin(), directions.end(), [](int value) { return value == -1 || value == 1; }))
44 return {};
45 const auto wheels = inverse_mecanum(velocity, rotate_speed, max_wheel_speed);
46 return {wheels.left_front * directions[0], wheels.right_front * directions[1],
47 wheels.left_rear * directions[2], wheels.right_rear * directions[3]};
48}
49
50} // namespace roboctrl::utils::kinematics
常用数值与字节工具集合。
float fp32
单精度浮点别名。
Definition utils.hpp:22