From 7bf206c45442d3bb90b4ae271da96d5f9e069adb Mon Sep 17 00:00:00 2001 From: Carter Date: Tue, 17 Nov 2026 21:10:18 -0500 Subject: [PATCH] I uncoommented all the code to use the lift with the rotation sensor. Next I need to make it more usable and intuitive to setup. --- include/EZ-Template/mechanisms.hpp | 3 +- .../rotation_mechanisms/rotation_calc.hpp | 9 +- include/setup/initialize.hpp | 2 +- .../rotation_mechanisms/lift_rotation.cpp | 139 ++++++++++++++++++ src/main.cpp | 22 ++- 5 files changed, 154 insertions(+), 21 deletions(-) diff --git a/include/EZ-Template/mechanisms.hpp b/include/EZ-Template/mechanisms.hpp index 363da47..d7b2cfc 100644 --- a/include/EZ-Template/mechanisms.hpp +++ b/include/EZ-Template/mechanisms.hpp @@ -57,7 +57,8 @@ namespace ez { * * \param controlPeriodM * The current control period were in - * + * \param controllerM + * * */ void motor_control(enum ControlPeriodM, enum ControllerM, enum ButtonsM, enum MotorGroups, float fw_motor_speed, float rev_motor_speed); diff --git a/include/EZ-Template/rotation_mechanisms/rotation_calc.hpp b/include/EZ-Template/rotation_mechanisms/rotation_calc.hpp index c43c454..4f29ecf 100644 --- a/include/EZ-Template/rotation_mechanisms/rotation_calc.hpp +++ b/include/EZ-Template/rotation_mechanisms/rotation_calc.hpp @@ -2,7 +2,6 @@ #include #include "api.h" -//#include "main.h" #include "pros/adi.hpp" #include "pros/rotation.hpp" @@ -79,10 +78,6 @@ namespace ez{ class LiftMech { private: std::vector liftStates; // Array the lift uses to move. - std::vector firstRange; - std::vector secondRange; - std::vector thirdRange; - std::vector fourthRange; int numLiftStates; float liftKP; @@ -101,10 +96,10 @@ namespace ez{ void cycle_states(); void reset_state(); void lift_control(); // Computes the velocity for the lift to move at. - int lift_range_finder(); // Finds the ranges for the arm to use + //int lift_range_finder(); // Finds the ranges for the arm to use void set_lift_kp(float _liftKP); - void set_ranges(std::vector _firstRange, std::vector _secondRange, std::vector _thirdRange, std::vector _fourthRange); + //void set_ranges(std::vector _firstRange, std::vector _secondRange, std::vector _thirdRange, std::vector _fourthRange); int get_lift_state(); int get_num_lift_states(); diff --git a/include/setup/initialize.hpp b/include/setup/initialize.hpp index e0b0057..8fcd9c3 100644 --- a/include/setup/initialize.hpp +++ b/include/setup/initialize.hpp @@ -38,7 +38,7 @@ inline ez::Piston PlaceHolder({'G',true}); * * [2] - Color sensors */ -inline pros::Rotation liftRotation({0}); +inline pros::Rotation liftRotation({18}); inline pros::Rotation armRotation({0}); inline pros::Rotation wristRotation({0}); diff --git a/src/EZ-Template/rotation_mechanisms/lift_rotation.cpp b/src/EZ-Template/rotation_mechanisms/lift_rotation.cpp index e38da72..9ebf4fd 100644 --- a/src/EZ-Template/rotation_mechanisms/lift_rotation.cpp +++ b/src/EZ-Template/rotation_mechanisms/lift_rotation.cpp @@ -2,6 +2,145 @@ #include "EZ-Template/rotation_mechanisms/rotation_calc.hpp" using namespace ez; + +LiftMech::LiftMech() { + liftHeight = 0; + liftError = 0; + liftVelocity = 0; + + liftStates = {0, 0, 0, 0}; +} +LiftMech::LiftMech(std::initializer_list _liftStates, int _numLiftStates) { + liftStates = _liftStates; + numLiftStates = _numLiftStates; + currLiftState = 0; + liftTarget = 0; + set_lift_kp(0.0173f); +} + +void LiftMech::lift_driver_control(ControllerR currController, ButtonsR buttonUsed) { + switch (currController) + { + case ControllerR::slave: + + switch (buttonUsed) + { + case ButtonsR::arrow_buttons: + if (master.get_digital(DIGITAL_RIGHT)) { + lift.move(127); + } + else if (master.get_digital(DIGITAL_DOWN)) { + lift.move(-127); + } + break; + case ButtonsR::Y_B_buttons: + if (master.get_digital(DIGITAL_Y)) { + lift.move(127); + } + else if (master.get_digital(DIGITAL_B)) { + lift.move(-127); + } + break; + case ButtonsR::left_shoulder_buttons: + if (master.get_digital(DIGITAL_L1)) { + lift.move(127); + } + else if (master.get_digital(DIGITAL_L2)) { + lift.move(-127); + } + break; + case ButtonsR::right_shoulder_buttons: + if (master.get_digital(DIGITAL_R1)) { + lift.move(127); + } + else if (master.get_digital(DIGITAL_R2)) { + lift.move(-127); + } + break; + default: + break; + } + break; + case ControllerR::master: + + switch (buttonUsed) + { + case ButtonsR::arrow_buttons: + if (slave.get_digital(DIGITAL_RIGHT)) { + cycle_states(); + } + else if (slave.get_digital(DIGITAL_DOWN)) { + reset_state(); + } + break; + case ButtonsR::Y_B_buttons: + if (slave.get_digital(DIGITAL_Y)) { + cycle_states(); + } + else if (slave.get_digital(DIGITAL_B)) { + reset_state(); + } + break; + case ButtonsR::left_shoulder_buttons: + if (slave.get_digital(DIGITAL_L1)) { + cycle_states(); + } + else if (slave.get_digital(DIGITAL_L2)) { + reset_state(); + } + break; + case ButtonsR::right_shoulder_buttons: + if (slave.get_digital(DIGITAL_R1)) { + cycle_states(); + } + else if (slave.get_digital(DIGITAL_R2)) { + reset_state(); + } + break; + default: + break; + } + break; + default: + break; + } +} + +void LiftMech::cycle_states() { + currLiftState ++; + + if (!liftStates.empty()) { + if (currLiftState == numLiftStates) { + currLiftState = 0; + } + liftTarget = liftStates[currLiftState]; + } +} +void LiftMech::reset_state() { + currLiftState = 0; + + if (!liftStates.empty()) { + liftTarget = liftStates[currLiftState]; + } +} + +void LiftMech::lift_control() { + liftError = liftTarget - liftRotation.get_position(); + liftVelocity = get_lift_kp() * liftError; + lift.move(liftVelocity); +} + +void LiftMech::set_lift_kp(float _liftKp) { + liftKP = _liftKp; +} +int LiftMech::get_lift_state() { + if (!liftStates.empty()) { + return liftStates[currLiftState]; + } +} +int LiftMech::get_num_lift_states() {return numLiftStates;} +float LiftMech::get_lift_kp() {return liftKP;} + /* ArmMech armObjRange; diff --git a/src/main.cpp b/src/main.cpp index 5f4fdc7..cc9313d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -39,12 +39,12 @@ namespace ez { }; } // Lift constructor -/*LiftMech liftObj( +LiftMech liftObj( - {0, 63194, 94246, 139878}, 4); + {0, }, 4); // Wrist constructor -WristMech wristObj( +/*WristMech wristObj( {0, -14529}, 2);*/ @@ -73,15 +73,13 @@ void initialize() { {139878, 150000} ); - armRotation.reset_position(); + armRotation.reset_position();*/ liftRotation.reset_position(); - wristRotation.reset_position();*/ + //wristRotation.reset_position(); /*pros::Task liftControlTasck([&]{ while (true) { - armObj.arm_control(); liftObj.lift_control(); - wristObj.wrist_control(); pros::delay(10); } });*/ @@ -354,14 +352,14 @@ void opcontrol() { } else { wrist.brake(); - } + }*/ //armObj.arm_driver_control(ControllerR::master, ButtonsR::left_shoulder_buttons); //liftObj.lift_driver_control(ControllerR::master, ButtonsR::right_shoulder_buttons); //armObj.arm_driver_control(ControllerR::slave, ButtonsR::left_shoulder_buttons); //liftObj.lift_driver_control(ControllerR::slave, ButtonsR::right_shoulder_buttons); //wristObj.wrist_driver_control(ControllerR::slave, ButtonsR::Y_B_buttons); - //wristObj.rotate_wrist(90);*/ + //wristObj.rotate_wrist(90); chassis.opcontrol_arcade_standard(ez::SPLIT); // Standard split arcade // chassis.opcontrol_tank(); // Tank control @@ -370,10 +368,10 @@ void opcontrol() { // chassis.opcontrol_arcade_flipped(ez::SINGLE); // Flipped single arcade // Terminal debugging - /*std::cout << "Arm rotation sensor value: " << "\033[31m" << armRotation.get_position() << " cdg" << "mm\033[0m" << std::endl; + //std::cout << "Arm rotation sensor value: " << "\033[31m" << armRotation.get_position() << " cdg" << "mm\033[0m" << std::endl; std::cout << "Lift rotation sensor value: " << "\033[32m" << liftRotation.get_position() << " cdg" << "mm\033[0m" << std::endl; - std::cout << "Wrist rotation sensor value: " << "\033[33m" << wristRotation.get_position() << " cdg" << "mm\033[0m" << std::endl; - std::cout << "Arm limit switch value: " << "\033[34m" << armReset.get_value() << "mm\033[0m" << std::endl;*/ + //std::cout << "Wrist rotation sensor value: " << "\033[33m" << wristRotation.get_position() << " cdg" << "mm\033[0m" << std::endl; + //std::cout << "Arm limit switch value: " << "\033[34m" << armReset.get_value() << "mm\033[0m" << std::endl;*/ ///pros::delay(250); pros::delay(ez::util::DELAY_TIME); // This is used for timer calculations! Keep this ez::util::DELAY_TIME