Skip to content

Commit 3b74e68

Browse files
author
KhalilSelyan
committed
feat: added color interpolation for speed limiter's donut according to current speed
Signed-off-by: KhalilSelyan <khalil@leodrive.ai>
1 parent c64093c commit 3b74e68

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

common/autoware_overlay_rviz_plugin/autoware_vehicle_overlay_rviz_plugin/include/speed_limit_display.hpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <rviz_common/properties/int_property.hpp>
2424
#include <rviz_common/ros_topic_display.hpp>
2525

26+
#include "autoware_auto_vehicle_msgs/msg/velocity_report.hpp"
2627
#include <tier4_planning_msgs/msg/velocity_limit.hpp>
2728

2829
#include <OgreColourValue.h>
@@ -38,9 +39,11 @@ class SpeedLimitDisplay
3839
SpeedLimitDisplay();
3940
void drawSpeedLimitIndicator(QPainter & painter, const QRectF & backgroundRect);
4041
void updateSpeedLimitData(const tier4_planning_msgs::msg::VelocityLimit::ConstSharedPtr msg);
42+
void updateSpeedData(const autoware_auto_vehicle_msgs::msg::VelocityReport::ConstSharedPtr & msg);
4143

4244
private:
43-
float current_limit; // Internal variable to store current gear
45+
float current_limit; // Internal variable to store current gear
46+
float current_speed_; // Internal variable to store current speed
4447
QColor gray = QColor(194, 194, 194);
4548
};
4649

common/autoware_overlay_rviz_plugin/autoware_vehicle_overlay_rviz_plugin/src/speed_limit_display.cpp

+51-7
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
namespace autoware_vehicle_overlay_rviz_plugin
3535
{
3636

37-
SpeedLimitDisplay::SpeedLimitDisplay() : current_limit(0.0)
37+
SpeedLimitDisplay::SpeedLimitDisplay() : current_limit(0.0), current_speed_(0.0)
3838
{
3939
std::string package_path =
4040
ament_index_cpp::get_package_share_directory("autoware_vehicle_overlay_rviz_plugin");
@@ -55,31 +55,75 @@ void SpeedLimitDisplay::updateSpeedLimitData(
5555
current_limit = msg->max_velocity;
5656
}
5757

58+
void SpeedLimitDisplay::updateSpeedData(
59+
const autoware_auto_vehicle_msgs::msg::VelocityReport::ConstSharedPtr & msg)
60+
{
61+
try {
62+
float speed = msg->longitudinal_velocity;
63+
current_speed_ = speed;
64+
} catch (const std::exception & e) {
65+
// Log the error
66+
std::cerr << "Error in processMessage: " << e.what() << std::endl;
67+
}
68+
}
69+
5870
void SpeedLimitDisplay::drawSpeedLimitIndicator(QPainter & painter, const QRectF & backgroundRect)
5971
{
6072
// Enable Antialiasing for smoother drawing
6173
painter.setRenderHint(QPainter::Antialiasing, true);
6274
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
6375

64-
// #C2C2C2
65-
painter.setPen(QPen(gray, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
66-
painter.setBrush(QBrush(gray, Qt::SolidPattern));
76+
// Convert color_min and color_max to QColor
77+
QColor colorMin = QColor("#FF9999");
78+
QColor colorMax = QColor("#FF3333");
79+
80+
// Calculate the ratio between the current speed and the speed limit
81+
double speed_to_limit_ratio = current_speed_ / current_limit;
82+
83+
QColor borderColor = gray;
84+
85+
if (speed_to_limit_ratio > 0.7) {
86+
// Adjust the interpolation to start more gradually
87+
// Calculate a new factor that slows down the transition
88+
double adjusted_speed_to_limit_ratio = (speed_to_limit_ratio - 0.7) / (1.0 - 0.7);
89+
double smoothFactor =
90+
pow(adjusted_speed_to_limit_ratio, 2); // Use a power to make it interpolate slower
91+
92+
int r = std::min(
93+
std::max(
94+
0, colorMin.red() + static_cast<int>((colorMax.red() - colorMin.red()) * smoothFactor)),
95+
255);
96+
int g = std::min(
97+
std::max(
98+
0,
99+
colorMin.green() + static_cast<int>((colorMax.green() - colorMin.green()) * smoothFactor)),
100+
255);
101+
int b = std::min(
102+
std::max(
103+
0, colorMin.blue() + static_cast<int>((colorMax.blue() - colorMin.blue()) * smoothFactor)),
104+
255);
105+
106+
borderColor = QColor(r, g, b);
107+
}
67108

68109
// Define the area for the outer circle
69110
QRectF outerCircleRect = backgroundRect;
70111
outerCircleRect.setWidth(backgroundRect.width() - 30);
71112
outerCircleRect.setHeight(backgroundRect.width() - 30);
72113
outerCircleRect.moveTopLeft(QPointF(backgroundRect.left() + 15, backgroundRect.top() + 10));
73114

115+
// Now use borderColor for drawing
116+
painter.setPen(QPen(borderColor, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
117+
painter.setBrush(borderColor);
118+
// Draw the outer circle for the speed limit indicator
119+
painter.drawEllipse(outerCircleRect);
120+
74121
// Define the area for the inner circle
75122
QRectF innerCircleRect = outerCircleRect;
76123
innerCircleRect.setWidth(outerCircleRect.width() / 1.25);
77124
innerCircleRect.setHeight(outerCircleRect.height() / 1.25);
78125
innerCircleRect.moveCenter(outerCircleRect.center());
79126

80-
// Draw the outer circle
81-
painter.drawEllipse(outerCircleRect);
82-
83127
painter.setRenderHint(QPainter::Antialiasing, true);
84128
QColor colorFromHSV;
85129
colorFromHSV.setHsv(0, 0, 0); // Hue, Saturation, Value

0 commit comments

Comments
 (0)