HoloLib
High-performance holonomic (X-Drive) control library for VEX V5
Loading...
Searching...
No Matches
modular_lift.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Eigen/Core"
4#include "pros/motors.hpp"
5#include <atomic>
6#include <optional>
7
8class ModularLift;
9
10/**
11 * @brief Configuration for a single lift motor.
12 */
14 int port; /**< Motor port number */
15 pros::MotorGear gearset; /**< Motor internal gearset */
16};
17
18/**
19 * @brief Enum defining the physical mechanism of the lift.
20 */
21enum class LiftMechanism {
22 CASCADE,
24 SIX_BAR,
26};
27
28/**
29 * @brief Configuration constants and physical properties of the lift.
30 */
31struct LiftConfig {
32 float gear_ratio; /**< External gear ratio applied to the motors */
33 float arm_length; /**< Length of the lift arm */
34 float arm_mass_kg; /**< Mass of the arm mechanism in kilograms */
35 float payload_mass_kg; /**< Mass of the payload being lifted in kilograms */
36 float kG_base; /**< Base gravity feed-forward gain */
37 float tolerance; /**< Acceptable error tolerance for considering a move finished */
38 Eigen::Matrix<float, 1, 2> K; /**< State-feedback gain matrix [kP, kD] */
39 float spool_radius = 1.0f; /**< Radius of the spool (for cascade/elevator mechanisms) */
40};
41
42/**
43 * @brief A modular controller for various lift mechanisms.
44 *
45 * Handles background task processing, feed-forward calculations based on physics,
46 * and LQR/state-feedback control for accurate positioning.
47 */
49public:
50 /**
51 * @brief Construct a new ModularLift object.
52 *
53 * @param m_configs Vector of motor configurations for the lift.
54 * @param t The type of lift mechanism (e.g. FOUR_BAR, CASCADE).
55 * @param c The configuration containing physical constants and control gains.
56 */
57 ModularLift(const std::vector<LiftMotorConfig>& m_configs, LiftMechanism t, LiftConfig c);
58
59 /**
60 * @brief Destroy the ModularLift object.
61 *
62 * Cleans up the background task and associated resources.
63 */
65
66 /**
67 * @brief Convert raw motor degrees into lift arm radians (or meters for cascade).
68 *
69 * @param motor_degrees The raw sensor value in degrees.
70 * @return float The calculated position.
71 */
72 float getLiftRadians(float motor_degrees);
73
74 /**
75 * @brief Set the mass of the current payload to update feed-forward calculations dynamically.
76 *
77 * @param mass_kg Mass of the payload in kilograms.
78 */
79 void setPayload(float mass_kg);
80
81 /**
82 * @brief Calculate the required feed-forward voltage to hold the lift at its current position.
83 *
84 * @param current_motor_degrees The current position of the lift.
85 * @return float The calculated feed-forward voltage (in millivolts or specific units).
86 */
87 float calculateFeedforward(float current_motor_degrees);
88
89 /**
90 * @brief Command the lift to move to a target position.
91 *
92 * @param target_motor_degrees The target position in motor degrees.
93 */
94 void moveTo(float target_motor_degrees);
95
96 /**
97 * @brief Cancel the current movement command.
98 */
99 void cancel();
100
101 /**
102 * @brief Block the current thread until the lift reaches its target position.
103 */
105
106 /**
107 * @brief Check if the lift is currently trying to reach a target position.
108 *
109 * @return true if running, false otherwise.
110 */
111 bool isRunning();
112
113private:
114 std::vector<pros::Motor> motors;
115 LiftMechanism type;
116 LiftConfig config;
117
118 std::atomic<bool> is_running;
119 std::atomic<bool> cancel_request;
120 std::atomic<bool> is_settled;
121
122 std::optional<pros::Task> task = std::nullopt;
123 pros::Mutex target_mutex;
124 float target_position = 0.0f;
125
126 void startTask();
127 void controlLoopImpl();
128};
A modular controller for various lift mechanisms.
void setPayload(float mass_kg)
Set the mass of the current payload to update feed-forward calculations dynamically.
float calculateFeedforward(float current_motor_degrees)
Calculate the required feed-forward voltage to hold the lift at its current position.
float getLiftRadians(float motor_degrees)
Convert raw motor degrees into lift arm radians (or meters for cascade).
bool isRunning()
Check if the lift is currently trying to reach a target position.
ModularLift(const std::vector< LiftMotorConfig > &m_configs, LiftMechanism t, LiftConfig c)
Construct a new ModularLift object.
void cancel()
Cancel the current movement command.
~ModularLift()
Destroy the ModularLift object.
void waitUntilDone()
Block the current thread until the lift reaches its target position.
void moveTo(float target_motor_degrees)
Command the lift to move to a target position.
LiftMechanism
Enum defining the physical mechanism of the lift.
Configuration constants and physical properties of the lift.
float arm_length
Length of the lift arm.
float payload_mass_kg
Mass of the payload being lifted in kilograms.
float tolerance
Acceptable error tolerance for considering a move finished.
float gear_ratio
External gear ratio applied to the motors.
Eigen::Matrix< float, 1, 2 > K
State-feedback gain matrix [kP, kD].
float spool_radius
Radius of the spool (for cascade/elevator mechanisms)
float arm_mass_kg
Mass of the arm mechanism in kilograms.
float kG_base
Base gravity feed-forward gain.
Configuration for a single lift motor.
int port
Motor port number.
pros::MotorGear gearset
Motor internal gearset.