Advertisement
Trainlover08

common.cpp

Oct 29th, 2024 (edited)
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.90 KB | None | 0 0
  1. // (common.cpp)
  2.  
  3.  
  4. #include "include.cpp"
  5.  
  6. // namespace wrapper for common functions
  7. namespace cmn {
  8.     class my_controller {
  9.     public:
  10.         // Make the class for the mappable controller layout.
  11.         // most of this is initialization and default constructor type of things
  12.         pros::controller_analog_e_t throttle_axis = pros::E_CONTROLLER_ANALOG_LEFT_Y;
  13.         pros::controller_analog_e_t steer_axis = pros::E_CONTROLLER_ANALOG_RIGHT_X;
  14.  
  15.         pros::controller_digital_e_t arm_pneumatic_button = pros::E_CONTROLLER_DIGITAL_DOWN;
  16.         pros::controller_digital_e_t wrist_pneumatic_button = pros::E_CONTROLLER_DIGITAL_DOWN;
  17.         pros::controller_digital_e_t intake_pneumatic_button = pros::E_CONTROLLER_DIGITAL_DOWN;
  18.         pros::controller_digital_e_t mogo_pneumatic_button = pros::E_CONTROLLER_DIGITAL_DOWN;
  19.         pros::controller_digital_e_t switch_controller_button = pros::E_CONTROLLER_DIGITAL_DOWN;
  20.         pros::controller_digital_e_t intake_motor_button = pros::E_CONTROLLER_DIGITAL_DOWN;
  21.         pros::controller_digital_e_t extake_motor_button = pros::E_CONTROLLER_DIGITAL_DOWN;
  22.         pros::controller_digital_e_t sort_color_button = pros::E_CONTROLLER_DIGITAL_DOWN;
  23.         pros::controller_digital_e_t load_arm_button = pros::E_CONTROLLER_DIGITAL_DOWN;
  24.         pros::controller_digital_e_t hold_ring_or_constant_scoring_button = pros::E_CONTROLLER_DIGITAL_DOWN;
  25.         pros::controller_digital_e_t score_held_ring_button = pros::E_CONTROLLER_DIGITAL_DOWN;
  26.         pros::controller_digital_e_t shift_button = pros::E_CONTROLLER_DIGITAL_X;
  27.         pros::controller_digital_e_t chopper_mech = pros::E_CONTROLLER_DIGITAL_DOWN;
  28.         pros::controller_digital_e_t screen_index_button = pros::E_CONTROLLER_DIGITAL_DOWN;
  29.         pros::controller_digital_e_t reverse = pros::E_CONTROLLER_DIGITAL_DOWN;
  30.         pros::Controller *controller;
  31.         bool is_master_controller = false;
  32.         int current_slave_screen_index = 0;
  33.     };
  34.  
  35.     class PID {
  36.         // a universal class for different PID controllers
  37.         public:
  38.             double kP = 0.0f;
  39.             double kD = 0.0f;
  40.             double kI = 0.0f;
  41.             double target = 0.0f;
  42.             double position = 0.0f;
  43.         private:
  44.             double error;
  45.             double x_1;
  46.             double x_2;
  47.             double integral;
  48.         public:
  49.             double run() {
  50.                 error = target - position;
  51.                 x_1 = x_2;
  52.                 x_2 = error;
  53.                 integral += error;
  54.                 return ((integral * kI) + (error * kP) + ((x_2 - x_1) * kD));
  55.             }
  56.     };
  57.  
  58.     class GUI {
  59.         // A class for printing and interaction with the brain
  60.     public:
  61.         int current_view = 0;
  62.         std::vector<pros::Motor> motor_list;
  63.         int selected_auton = 0;
  64.         std::vector<std::string> auton_name_list;
  65.  
  66.         void render () {
  67.             clear_screen();
  68.             switch (current_view){
  69.                 case 0:
  70.                     auton_select_screen();
  71.                     break;
  72.                 case 1:
  73.                     print_motor_temprature_screen();
  74.                     break;
  75.                 case 2:
  76.                     print_motor_detail_screen();
  77.                     break;
  78.                 case 3:
  79.                     current_view = 0;
  80.                     break;
  81.                 default:
  82.                     break;
  83.             }
  84.             press();
  85.         }
  86.  
  87.     private:
  88.         pros::Color get_temp(pros::Motor& motor) {
  89.             if(motor.get_temperature() < 55) {
  90.                 return pros::Color::green;
  91.             }else if(motor.get_temperature() >= 55 && motor.get_temperature() < 60){
  92.                 return pros::Color::yellow;
  93.             }else if(motor.get_temperature() >= 60 && motor.get_temperature() < 65){
  94.                 return pros::Color::red;
  95.             } else if (motor.get_temperature() >= 65) {
  96.                 return pros::Color::dark_red;
  97.             } else {
  98.                 return pros::Color::gray;
  99.             }
  100.         }
  101.  
  102.         // printing for all of the different screens
  103.  
  104.         // some of these use brute force (should be redone)
  105.         // others do it algorithmically (the good ones that things should be redone to)
  106.         void print_motor_temprature_screen() {
  107.             pros::screen::set_pen(pros::Color::white_smoke);
  108.             pros::screen::draw_line(360, 0, 360, 240);
  109.             pros::screen::draw_line(360, 120, 480, 120);
  110.  
  111.             pros::screen::draw_circle(420, 60, 46);
  112.             pros::screen::draw_circle(420, 180, 46);
  113.             pros::screen::draw_circle(300, 60, 46);
  114.             pros::screen::draw_circle(180, 60, 46);
  115.             pros::screen::draw_circle(60, 60, 46);
  116.             pros::screen::draw_circle(60, 180, 46);
  117.             pros::screen::draw_circle(180, 180, 46);
  118.             pros::screen::draw_circle(300, 180, 46);
  119.             pros::screen::set_pen(get_temp(motor_list[6]));
  120.             pros::screen::fill_circle(420, 60, 45);
  121.             pros::screen::set_pen(get_temp(motor_list[7]));
  122.             pros::screen::fill_circle(420, 180, 45);
  123.             pros::screen::set_pen(get_temp(motor_list[2]));
  124.             pros::screen::fill_circle(300, 60, 45);
  125.             pros::screen::set_pen(get_temp(motor_list[5]));
  126.             pros::screen::fill_circle(300, 180, 45);
  127.             pros::screen::set_pen(get_temp(motor_list[1]));
  128.             pros::screen::fill_circle(180, 60, 45);
  129.             pros::screen::set_pen(get_temp(motor_list[4]));
  130.             pros::screen::fill_circle(180, 180, 45);
  131.             pros::screen::set_pen(get_temp(motor_list[0]));
  132.             pros::screen::fill_circle(60, 60, 45);
  133.             pros::screen::set_pen(get_temp(motor_list[3]));
  134.             pros::screen::fill_circle(60, 180, 45);
  135.  
  136.             pros::screen::set_eraser(pros::Color::black);
  137.             pros::screen::set_pen(pros::Color::white_smoke);
  138.             pros::screen::print(pros::E_TEXT_SMALL, 17, 55, "L M 1 (%i)", motor_list[0].get_port());
  139.             pros::screen::print(pros::E_TEXT_SMALL, 137, 55, "L M 2 (%i)", motor_list[1].get_port());
  140.             pros::screen::print(pros::E_TEXT_SMALL, 257, 55, "L M 3 (%i)", motor_list[2].get_port());
  141.             pros::screen::print(pros::E_TEXT_SMALL, 17, 175, "R M 1 (%i)", motor_list[3].get_port());
  142.             pros::screen::print(pros::E_TEXT_SMALL, 137, 175, "R M 2 (%i)", motor_list[4].get_port());
  143.             pros::screen::print(pros::E_TEXT_SMALL, 257, 175, "R M 3 (%i)", motor_list[5].get_port());
  144.             pros::screen::print(pros::E_TEXT_SMALL, 377, 55, "Lower(%i)", motor_list[6].get_port());
  145.             pros::screen::print(pros::E_TEXT_SMALL, 377, 175, "Upper(%i)", motor_list[7].get_port());
  146.         }
  147.  
  148.         void clear_screen() {
  149.             pros::screen::set_eraser(pros::Color::black);
  150.             pros::screen::erase();
  151.             pros::screen::erase_rect(0,0,480,240);
  152.         }
  153.  
  154.         void print_motor_detail_screen () {
  155.             pros::screen::set_pen(pros::Color::white_smoke);
  156.             pros::screen::draw_line(360, 0, 360, 240);
  157.             pros::screen::draw_line(240, 0, 240, 240);
  158.             pros::screen::draw_line(120, 0, 120, 240);
  159.             pros::screen::draw_line(0, 120, 480, 120);
  160.             std::vector<std::string> stat_type {"Velocity ", "Temp ", "Torque ", "Power "};
  161.             std::vector<std::string> unit {"RPM", "C", "nm", "W"};
  162.  
  163.             for(int i = 0; i < 4; ++i){
  164.                 for(int  j = 0; j < 2; ++j){
  165.                     int velocity = motor_list[i + j].get_actual_velocity();
  166.                     int temprature = motor_list[i + j].get_temperature();
  167.                     int torque = std::roundf(motor_list[i + j].get_torque());
  168.                     int power = motor_list[i + j].get_power();
  169.                     std::vector<std::string> stat {std::to_string(velocity), std::to_string(temprature), std::to_string(torque), std::to_string(power)};
  170.                     for(int k = 1; k <= 4; ++k) {
  171.                         pros::screen::print(pros::E_TEXT_SMALL, (120 * i) + (8), (j * 120) + (k * 20), "%s%s%s", stat_type[k-1], stat[k-1], unit[k-1]);
  172.                     }
  173.                 }
  174.             }
  175.         }
  176.  
  177.         std::vector<int> is_selected() {
  178.             int x_1, y_1, x_2, y_2;
  179.             if((selected_auton % 2) == 0) {
  180.                 y_1 = 10;
  181.                 y_2 = 95;
  182.             } else {
  183.                 y_1 = 115;
  184.                 y_2 = 200;
  185.             }
  186.             x_1 = (((selected_auton - (selected_auton % 2)) / 2) * 120) + 10;
  187.             x_2 = x_1 + 100;
  188.  
  189.             return {x_1, y_1, x_2, y_2};
  190.         }
  191.  
  192.         void auton_select_screen() {
  193.             clear_screen();
  194.            
  195.             pros::screen::set_pen(pros::Color::purple);
  196.             pros::screen::fill_rect(is_selected()[0], is_selected()[1], is_selected()[2], is_selected()[3]);
  197.            
  198.             pros::screen::set_pen(pros::Color::light_blue);
  199.             for(int i = 0; i < 4; ++i) {
  200.                 for(int j = 0; j < 2; ++j) {
  201.                     pros::screen::fill_rect(((i * 120) + 20), ((j * 105) + 20), (((i + 1) * 120) - 20), (((j + 1) * 105) - 20));
  202.                 }
  203.             }
  204.  
  205.             pros::screen::set_pen(pros::Color::white_smoke);
  206.             pros::screen::draw_line(0, 211, 480, 211);
  207.             pros::screen::print(pros::E_TEXT_MEDIUM_CENTER, 240, 225, "Next Display ->");
  208.  
  209.             for(int k = 0; k < 4; ++k) {
  210.                 for(int a = 0; a < 2; ++a) {
  211.                     // this commented line below was causing a runtime error. I decided to come back to it later because it is not as critical as other things that needed to get done
  212.  
  213.                     //pros::screen::print(pros::E_TEXT_SMALL, ((k * 120) + 10), ((a * 120) + 55), "%s", auton_name_list[(k * 2) + a]);
  214.                 }
  215.             }
  216.         }
  217.  
  218.         void press() {
  219.             // a function that looks to see if the screen is being used as a touch screen based on different conditions
  220.  
  221.             pros::screen_touch_status_s_t status = pros::screen::touch_status();
  222.             if(status.touch_status == pros::E_TOUCH_PRESSED){
  223.                 if(!(current_view == 0)) {
  224.                     ++current_view;
  225.                 } else if(status.y > 215) {
  226.                     ++current_view;
  227.                 } else {
  228.                     selected_auton = (std::floor(status.x / 120) * 2) + (std::floor(status.y / 105));
  229.                 }
  230.                 if(current_view == 3) {
  231.                     current_view = 0;
  232.                 }
  233.             }
  234.         }
  235.     };
  236.  
  237.     class Tube {
  238.         // A default object as a constructor for the pneumatics system
  239.     public:
  240.         double length = 0; //in inches
  241.         double volume;
  242.         double init() {
  243.             volume = (std::pow((1.0f / 25.4), 2) * M_PI) * length; //in cubic inches
  244.             return volume;
  245.         }
  246.     };
  247.  
  248.     class Piston {
  249.         // again, another constructor class
  250.     public:
  251.         double stroke = 0; //in mm
  252.         double volume;
  253.         double init() {
  254.             volume = (std::pow((5.0f / 25.4), 2) * M_PI) * (stroke / 25.4); //in cubic inches
  255.             return volume;
  256.         }
  257.     };
  258.  
  259.     class Air_Tank {
  260.         // yet another constructor object
  261.     public:
  262.         std::string type; // default new type
  263.         double capacity;
  264.        
  265.         Air_Tank(std::string tank_type = "new") : type(tank_type) {
  266.             if (type == "legacy") {
  267.                 capacity = LEGACY_CAPACITY;
  268.             } else {
  269.                 capacity = NEW_CAPACITY;
  270.             }
  271.         }
  272.     private:
  273.         const double LEGACY_CAPACITY = 5.0; // not correct. need to find the exact value yet
  274.         const double NEW_CAPACITY = 9.154;
  275.     };
  276.  
  277.     class Pneumatics_Component{
  278.     public:
  279.         std::vector<double> volumes;
  280.         double total_component_volume; //in cubic inches
  281.  
  282.         void compute_system_volume () {
  283.             total_component_volume = 0;
  284.             for (int i = 0; i < volumes.size(); i++) {
  285.                 total_component_volume += volumes[i];
  286.             }
  287.         }
  288.     };
  289.  
  290.     class Pneumatics_System {
  291.         // the true pneumatics class, the one that gets used other than an initializer
  292.     public:
  293.         double air_pressure = 100.0f;
  294.         double true_air_pressure = 110.0f;
  295.         double system_volume;
  296.  
  297.         void system_init(std::vector<Pneumatics_Component> comp) {
  298.             system_volume = 0;
  299.             for (int i = 0; i < comp.size(); ++i) {
  300.                 system_volume += comp[i].total_component_volume;
  301.             }
  302.         }
  303.  
  304.         void cycled(Pneumatics_Component comp) {
  305.             double new_volume = system_volume + comp.total_component_volume;
  306.             true_air_pressure = true_air_pressure * (system_volume / new_volume);
  307.             if (true_air_pressure < 100) {
  308.                 air_pressure = true_air_pressure;
  309.             } else {
  310.                 air_pressure = 100;
  311.             }
  312.         }
  313.     };
  314.  
  315.     // a controller lcd class for spontainious match stats
  316.     class Controller_Print {
  317.     public:
  318.  
  319.         std::string my_time_to_string(unsigned int time) {
  320.         unsigned int minutes = time / 60;
  321.         unsigned int seconds = time % 60;
  322.         std::string minutes_str = std::to_string(minutes);
  323.         std::string seconds_str = std::to_string(seconds);
  324.         if (minutes < 10) {
  325.             minutes_str = "0" + minutes_str;
  326.         }
  327.         if (seconds < 10) {
  328.             seconds_str = "0" + seconds_str;
  329.         }
  330.         return minutes_str + ":" + seconds_str;
  331.     }
  332.    
  333.         std::vector<pros::Motor> cont_drivetrain;
  334.         unsigned int time;
  335.         Pneumatics_System p_sys;
  336.         float pressure_derivative, battery_derivative, temp_derivative;
  337.  
  338.         int get_pressure (Pneumatics_System p_sys) {
  339.             return std::round(p_sys.true_air_pressure);
  340.         }
  341.  
  342.         void init (my_controller& master, my_controller& slave) {
  343.             master.controller->clear();
  344.             slave.controller->clear();
  345.             pd_1 = p_sys.true_air_pressure;
  346.             bd_1 = pros::battery::get_capacity();
  347.             td_1 = get_current_temp();
  348.         }
  349.  
  350.         void print_master (my_controller master) {
  351.             // this commented line was found to be problematic because of refresh time
  352.  
  353.             //master.controller->clear();
  354.             master.controller->print(1, 8, "%s", my_time_to_string(time / 1000));
  355.         }
  356.  
  357.         void print_slave (my_controller slave) {
  358.             // this function is for printing to the slave controller. It shows the current stat and the derivitave for each stat
  359.  
  360.             //slave.controller->clear();
  361.             pd_2 = p_sys.true_air_pressure;
  362.             bd_2 = pros::battery::get_capacity();
  363.             pressure_derivative = (pd_2 - pd_1) / (time / 1000);
  364.             battery_derivative = ((bd_2 - bd_1) / (time / 1000)) * 100;
  365.             double current_temp = get_current_temp();
  366.             td_2 = current_temp;
  367.             temp_derivative = (td_2 - td_1) / (time / 1000);
  368.             if(slave.controller->get_digital(slave.screen_index_button)) {
  369.                 slave.current_slave_screen_index++;
  370.             }
  371.             switch(slave.current_slave_screen_index) {
  372.                 case 0:
  373.                     slave.controller->print(0, 5, "%i PSI", std::round(p_sys.true_air_pressure));
  374.                     slave.controller->print(1, 0, "%f/s", pressure_derivative);
  375.                     slave.controller->print(2, 5, "%s", my_time_to_string(-(pd_1 / 1000)/ pressure_derivative));
  376.                     break;
  377.                 case 1:
  378.                     slave.controller->print(0, 6, "%f%", pros::battery::get_capacity() * 100);
  379.                     slave.controller->print(1, 0, "%f/s", battery_derivative);
  380.                     slave.controller->print(2, 5, "%s", my_time_to_string(-((bd_1 * 100) / 1000) / battery_derivative));
  381.                     break;
  382.                 case 2:
  383.                     slave.controller->print(0, 6, "%iC", std::round(current_temp));
  384.                     slave.controller->print(1, 0, "%f/s", temp_derivative);
  385.                     slave.controller->print(2, 5, "%s", my_time_to_string(-(td_1 / 1000) / temp_derivative));
  386.                     break;
  387.                 case 3:
  388.                     slave.controller->print(1, 5, "%s", my_time_to_string(time / 1000));
  389.                     break;
  390.                 case 4:
  391.                     slave.current_slave_screen_index = 0;
  392.                     break;
  393.                 default:
  394.                     break;
  395.             }
  396.         }
  397.     private:
  398.         double pd_1, pd_2, bd_1, bd_2, td_1, td_2;
  399.  
  400.         double get_current_temp () {
  401.             return ((cont_drivetrain[0].get_temperature()) + (cont_drivetrain[1].get_temperature()) + (cont_drivetrain[2].get_temperature()) + (cont_drivetrain[3].get_temperature(), cont_drivetrain[4].get_temperature()) + (cont_drivetrain[5].get_temperature()) / 6);
  402.         }
  403.     };
  404. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement