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