From 60b04be5e0618549a3c94fdb4df03982b41b9f84 Mon Sep 17 00:00:00 2001 From: Chi Huu Huynh <73843190+Chi-EEE@users.noreply.github.com> Date: Thu, 11 Jan 2024 15:08:11 +0000 Subject: [PATCH] Improve CarConsole code Move wheel debug components to DebugWheelRenderer class --- .../src/car/display/CarConsole.cpp | 132 ++----------- app/raspberry_pi/src/car/display/CarConsole.h | 2 + .../display/component/DebugWheelRenderer.cxx | 186 ++++++++++++++++++ app/raspberry_pi/xmake.lua | 2 +- 4 files changed, 201 insertions(+), 121 deletions(-) create mode 100644 app/raspberry_pi/src/car/display/component/DebugWheelRenderer.cxx diff --git a/app/raspberry_pi/src/car/display/CarConsole.cpp b/app/raspberry_pi/src/car/display/CarConsole.cpp index f9f0e881..72b627ca 100644 --- a/app/raspberry_pi/src/car/display/CarConsole.cpp +++ b/app/raspberry_pi/src/car/display/CarConsole.cpp @@ -228,123 +228,19 @@ namespace car::display { auto lidar_motor_checkbox_component = Checkbox(&lidar_motor_status, &lidar_motor_enabled, lidar_motor_checkbox_option); #pragma endregion -#pragma region Wheels - static constexpr int DEFAULT_REAR_WHEEL_SPEED = 0; - static constexpr int DEFAULT_FRONT_WHEEL_ANGLE = 90; - - int previous_rear_wheels_speed_slider_value = DEFAULT_REAR_WHEEL_SPEED; - - int rear_wheels_speed_slider_value = DEFAULT_REAR_WHEEL_SPEED; - int rear_left_wheel_speed_slider_value = DEFAULT_REAR_WHEEL_SPEED; - int rear_right_wheel_speed_slider_value = DEFAULT_REAR_WHEEL_SPEED; - - auto rear_wheel_speed_slider = Slider("Rear Wheels Speed:", &rear_wheels_speed_slider_value, 0, 100, 1); - auto rear_left_wheel_speed_slider = Slider("Left Rear Wheel Speed:", &rear_left_wheel_speed_slider_value, 0, 100, 1); - auto rear_right_wheel_speed_slider = Slider("Right Rear Wheel Speed:", &rear_right_wheel_speed_slider_value, 0, 100, 1); - - static constexpr auto REAR_WHEEL_DIRECTION_FORWARD_MESSAGE = "Rear Wheel Direction: Forward"; - static constexpr auto REAR_WHEEL_DIRECTION_BACKWARD_MESSAGE = "Rear Wheel Direction: Backward"; - - nod::signal rear_wheel_direction_signal; - - bool rear_wheel_direction_debounce = false; - std::string rear_wheel_direction_status = REAR_WHEEL_DIRECTION_FORWARD_MESSAGE; - bool rear_wheel_direction = true; - auto rear_wheel_direction_checkbox_option = CheckboxOption::Simple(); - rear_wheel_direction_checkbox_option.on_change = [&] - { - if (rear_wheel_direction_debounce) { - rear_wheel_direction = !rear_wheel_direction; - return; - } - rear_wheel_direction_debounce = true; - if (rear_wheel_direction) { - rear_wheel_direction_status = REAR_WHEEL_DIRECTION_FORWARD_MESSAGE; - } - else { - rear_wheel_direction_status = REAR_WHEEL_DIRECTION_BACKWARD_MESSAGE; - } - rear_wheel_direction_signal(rear_wheel_direction); - }; - - rear_wheel_direction_signal.connect([&](bool direction) + DebugWheelRenderer debug_wheel_renderer; + debug_wheel_renderer.getRearWheelDirectionSignal().connect([&](bool direction) { if (direction) { this->car_system->setRearWheelDirectionToForwards(); - rear_wheel_direction_status = REAR_WHEEL_DIRECTION_FORWARD_MESSAGE; } else { this->car_system->setRearWheelDirectionToBackwards(); - rear_wheel_direction_status = REAR_WHEEL_DIRECTION_BACKWARD_MESSAGE; } - rear_wheel_direction_debounce = false; } ); - auto rear_wheel_direction_checkbox_component = Checkbox(&rear_wheel_direction_status, &rear_wheel_direction, rear_wheel_direction_checkbox_option); - - int previous_front_wheels_angle_slider_value = DEFAULT_FRONT_WHEEL_ANGLE; - - int front_wheels_angle_slider_value = DEFAULT_FRONT_WHEEL_ANGLE; - int front_left_wheel_angle_slider_value = DEFAULT_FRONT_WHEEL_ANGLE; - int front_right_wheel_angle_slider_value = DEFAULT_FRONT_WHEEL_ANGLE; - - auto front_wheels_angle_slider = Slider("Front Wheels Angle:", &front_wheels_angle_slider_value, 0, 180, 1); - auto front_left_wheel_angle_slider = Slider("Left Front Wheel Angle:", &front_left_wheel_angle_slider_value, 0, 180, 1); - auto front_right_wheel_angle_slider = Slider("Right Front Wheel Angle:", &front_right_wheel_angle_slider_value, 0, 180, 1); - - auto rear_wheel_menu_entry = MenuEntry("Rear Wheel:") | bold; - auto front_wheel_menu_entry = MenuEntry("Front Wheel:") | bold; - - auto slider_container = - Container::Horizontal({ - Container::Vertical({ - rear_wheel_menu_entry, - rear_wheel_speed_slider, - rear_left_wheel_speed_slider, - rear_right_wheel_speed_slider, - rear_wheel_direction_checkbox_component, - }), - Container::Vertical({ - front_wheel_menu_entry, - front_wheels_angle_slider, - front_left_wheel_angle_slider, - front_right_wheel_angle_slider, - }) - }); - auto wheel_settings_renderer = Renderer(slider_container, [&] { - return - hbox({ - vbox({ - rear_wheel_menu_entry->Render(), - separator(), - rear_wheel_speed_slider->Render(), - separator(), - rear_left_wheel_speed_slider->Render(), - rear_right_wheel_speed_slider->Render(), - separator(), - text("Left Rear Wheel Speed: " + std::to_string(rear_left_wheel_speed_slider_value)), - text("Right Rear Wheel Speed: " + std::to_string(rear_right_wheel_speed_slider_value)), - separator(), - rear_wheel_direction_checkbox_component->Render(), - }) | xflex, - separator(), - vbox({ - front_wheel_menu_entry->Render(), - separator(), - front_wheels_angle_slider->Render(), - separator(), - front_left_wheel_angle_slider->Render(), - front_right_wheel_angle_slider->Render(), - separator(), - text("Left Front Wheel Angle: " + std::to_string(front_left_wheel_angle_slider_value)), - text("Right Front Wheel Angle: " + std::to_string(front_right_wheel_angle_slider_value)), - }) | xflex, - separator(), - }); - }); - -#pragma endregion + auto wheel_settings_renderer = debug_wheel_renderer.element(); #pragma endregion @@ -411,19 +307,15 @@ namespace car::display { // main thread). Using `screen.Post(task)` is threadsafe. screen.Post([&] { - if (previous_front_wheels_angle_slider_value != front_wheels_angle_slider_value) { - previous_front_wheels_angle_slider_value = front_wheels_angle_slider_value; - front_left_wheel_angle_slider_value = front_wheels_angle_slider_value; - front_right_wheel_angle_slider_value = front_wheels_angle_slider_value; - this->car_system->setFrontLeftWheelAngle({ front_left_wheel_angle_slider_value * 1.0f }); - this->car_system->setFrontRightWheelAngle({ front_right_wheel_angle_slider_value * 1.0f }); - } - if (previous_rear_wheels_speed_slider_value != rear_wheels_speed_slider_value) { - previous_rear_wheels_speed_slider_value = rear_wheels_speed_slider_value; - rear_left_wheel_speed_slider_value = rear_wheels_speed_slider_value; - rear_right_wheel_speed_slider_value = rear_wheels_speed_slider_value; - this->car_system->setRearLeftWheelSpeed({ rear_left_wheel_speed_slider_value }); - this->car_system->setRearRightWheelSpeed({ rear_right_wheel_speed_slider_value }); + if (debug_enabled) { + if (debug_wheel_renderer.updateFrontWheels()) { + this->car_system->setFrontLeftWheelAngle({ debug_wheel_renderer.getFrontLeftWheelAngleSliderValue() * 1.0f }); + this->car_system->setFrontRightWheelAngle({ debug_wheel_renderer.getFrontRightWheelAngleSliderValue() * 1.0f }); + } + if (debug_wheel_renderer.updateRearWheels()) { + this->car_system->setRearLeftWheelSpeed({ debug_wheel_renderer.getRearLeftWheelSpeedSliderValue() }); + this->car_system->setRearRightWheelSpeed({ debug_wheel_renderer.getRearRightWheelSpeedSliderValue() }); + } } this->car_system->update(); } diff --git a/app/raspberry_pi/src/car/display/CarConsole.h b/app/raspberry_pi/src/car/display/CarConsole.h index e8587486..1d40726c 100644 --- a/app/raspberry_pi/src/car/display/CarConsole.h +++ b/app/raspberry_pi/src/car/display/CarConsole.h @@ -15,9 +15,11 @@ #include #include "../system/CarSystem.h" +#include "component/DebugWheelRenderer.cxx" using namespace car::system; using namespace ftxui; +using namespace car::display::component; namespace car::display { static const ButtonOption animated_button_style = ButtonOption::Animated(); diff --git a/app/raspberry_pi/src/car/display/component/DebugWheelRenderer.cxx b/app/raspberry_pi/src/car/display/component/DebugWheelRenderer.cxx new file mode 100644 index 00000000..b4b2a949 --- /dev/null +++ b/app/raspberry_pi/src/car/display/component/DebugWheelRenderer.cxx @@ -0,0 +1,186 @@ +#ifndef DEBUGWHEELRENDERER_CXX +#define DEBUGWHEELRENDERER_CXX + +#pragma once + +#include + +#include +#include +#include +#include // ftxui::Loop + +using namespace ftxui; + +namespace car::display::component { + class DebugWheelRenderer { + private: + static constexpr int DEFAULT_REAR_WHEEL_SPEED = 0; + static constexpr int DEFAULT_FRONT_WHEEL_ANGLE = 90; + + static constexpr auto REAR_WHEEL_DIRECTION_FORWARD_MESSAGE = "Rear Wheel Direction: Forward"; + static constexpr auto REAR_WHEEL_DIRECTION_BACKWARD_MESSAGE = "Rear Wheel Direction: Backward"; + public: + DebugWheelRenderer() { + + } + + ftxui::Component element() + { + this->rear_wheel_speed_slider = Slider("Rear Wheels Speed:", &this->rear_wheels_speed_slider_value, 0, 100, 1); + this->rear_left_wheel_speed_slider = Slider("Left Rear Wheel Speed:", &this->rear_left_wheel_speed_slider_value, 0, 100, 1); + this->rear_right_wheel_speed_slider = Slider("Right Rear Wheel Speed:", &this->rear_right_wheel_speed_slider_value, 0, 100, 1); + + this->front_wheels_angle_slider = Slider("Front Wheels Angle:", &this->front_wheels_angle_slider_value, 0, 180, 1); + this->front_left_wheel_angle_slider = Slider("Left Front Wheel Angle:", &this->front_left_wheel_angle_slider_value, 0, 180, 1); + this->front_right_wheel_angle_slider = Slider("Right Front Wheel Angle:", &this->front_right_wheel_angle_slider_value, 0, 180, 1); + + this->rear_wheel_menu_entry = MenuEntry("Rear Wheel:") | bold; + this->front_wheel_menu_entry = MenuEntry("Front Wheel:") | bold; + + auto rear_wheel_direction_checkbox_option = CheckboxOption::Simple(); + rear_wheel_direction_checkbox_option.on_change = [&] + { + if (this->rear_wheel_direction_debounce) { + this->rear_wheel_direction = !this->rear_wheel_direction; + return; + } + this->rear_wheel_direction_debounce = true; + if (this->rear_wheel_direction) { + this->rear_wheel_direction_status = REAR_WHEEL_DIRECTION_FORWARD_MESSAGE; + } + else { + this->rear_wheel_direction_status = REAR_WHEEL_DIRECTION_BACKWARD_MESSAGE; + } + this->rear_wheel_direction_signal(this->rear_wheel_direction); + this->rear_wheel_direction_debounce = false; + }; + + this->rear_wheel_direction_checkbox_component = Checkbox(&this->rear_wheel_direction_status, &this->rear_wheel_direction, rear_wheel_direction_checkbox_option); + + this->slider_container = + Container::Horizontal({ + Container::Vertical({ + this->rear_wheel_menu_entry, + this->rear_wheel_speed_slider, + this->rear_left_wheel_speed_slider, + this->rear_right_wheel_speed_slider, + this->rear_wheel_direction_checkbox_component, + }), + Container::Vertical({ + this->front_wheel_menu_entry, + this->front_wheels_angle_slider, + this->front_left_wheel_angle_slider, + this->front_right_wheel_angle_slider, + }) + } + ); + + return Renderer(this->slider_container, [&] { + return + hbox({ + vbox({ + this->rear_wheel_menu_entry->Render(), + separator(), + this->rear_wheel_speed_slider->Render(), + separator(), + this->rear_left_wheel_speed_slider->Render(), + this->rear_right_wheel_speed_slider->Render(), + separator(), + text("Left Rear Wheel Speed: " + std::to_string(this->rear_left_wheel_speed_slider_value)), + text("Right Rear Wheel Speed: " + std::to_string(this->rear_right_wheel_speed_slider_value)), + separator(), + this->rear_wheel_direction_checkbox_component->Render(), + }) | xflex, + separator(), + vbox({ + this->front_wheel_menu_entry->Render(), + separator(), + this->front_wheels_angle_slider->Render(), + separator(), + this->front_left_wheel_angle_slider->Render(), + this->front_right_wheel_angle_slider->Render(), + separator(), + text("Left Front Wheel Angle: " + std::to_string(this->front_left_wheel_angle_slider_value)), + text("Right Front Wheel Angle: " + std::to_string(this->front_right_wheel_angle_slider_value)), + }) | xflex, + separator(), + }); + }); + } + + bool updateFrontWheels() { + if (this->previous_front_wheels_angle_slider_value != this->front_wheels_angle_slider_value) { + this->previous_front_wheels_angle_slider_value = this->front_wheels_angle_slider_value; + this->front_left_wheel_angle_slider_value = this->front_wheels_angle_slider_value; + this->front_right_wheel_angle_slider_value = this->front_wheels_angle_slider_value; + return true; + } + return false; + } + + bool updateRearWheels() { + if (this->previous_rear_wheels_speed_slider_value != this->rear_wheels_speed_slider_value) { + this->previous_rear_wheels_speed_slider_value = this->rear_wheels_speed_slider_value; + this->rear_left_wheel_speed_slider_value = this->rear_wheels_speed_slider_value; + this->rear_right_wheel_speed_slider_value = this->rear_wheels_speed_slider_value; + return true; + } + return false; + } + + nod::signal& getRearWheelDirectionSignal() { + return this->rear_wheel_direction_signal; + } + + const int& getFrontLeftWheelAngleSliderValue() const { + return this->front_left_wheel_angle_slider_value; + } + + const int& getFrontRightWheelAngleSliderValue() const { + return this->front_right_wheel_angle_slider_value; + } + + const int& getRearLeftWheelSpeedSliderValue() const { + return this->rear_left_wheel_speed_slider_value; + } + + const int& getRearRightWheelSpeedSliderValue() const { + return this->rear_right_wheel_speed_slider_value; + } + + private: + nod::signal rear_wheel_direction_signal; + + int previous_rear_wheels_speed_slider_value = DEFAULT_REAR_WHEEL_SPEED; + int rear_wheels_speed_slider_value = DEFAULT_REAR_WHEEL_SPEED; + int rear_left_wheel_speed_slider_value = DEFAULT_REAR_WHEEL_SPEED; + int rear_right_wheel_speed_slider_value = DEFAULT_REAR_WHEEL_SPEED; + + int previous_front_wheels_angle_slider_value = DEFAULT_FRONT_WHEEL_ANGLE; + int front_wheels_angle_slider_value = DEFAULT_FRONT_WHEEL_ANGLE; + int front_left_wheel_angle_slider_value = DEFAULT_FRONT_WHEEL_ANGLE; + int front_right_wheel_angle_slider_value = DEFAULT_FRONT_WHEEL_ANGLE; + + bool rear_wheel_direction_debounce = false; + std::string rear_wheel_direction_status = REAR_WHEEL_DIRECTION_FORWARD_MESSAGE; + bool rear_wheel_direction = true; + + Component rear_wheel_speed_slider; + Component rear_left_wheel_speed_slider; + Component rear_right_wheel_speed_slider; + + Component rear_wheel_direction_checkbox_component; + + Component front_wheels_angle_slider; + Component front_left_wheel_angle_slider; + Component front_right_wheel_angle_slider; + + Component rear_wheel_menu_entry; + Component front_wheel_menu_entry; + + Component slider_container; + }; +} + +#endif \ No newline at end of file diff --git a/app/raspberry_pi/xmake.lua b/app/raspberry_pi/xmake.lua index 4d90a817..485240c7 100644 --- a/app/raspberry_pi/xmake.lua +++ b/app/raspberry_pi/xmake.lua @@ -47,7 +47,7 @@ target("raspberry_pi") add_packages("ftxui") - add_requires("opencv") + add_packages("opencv") add_packages("rplidar") add_packages("pca9685") add_packages("tb6612")