34
34
namespace autoware_vehicle_overlay_rviz_plugin
35
35
{
36
36
37
- SpeedLimitDisplay::SpeedLimitDisplay () : current_limit(0.0 )
37
+ SpeedLimitDisplay::SpeedLimitDisplay () : current_limit(0.0 ), current_speed_( 0.0 )
38
38
{
39
39
std::string package_path =
40
40
ament_index_cpp::get_package_share_directory (" autoware_vehicle_overlay_rviz_plugin" );
@@ -55,31 +55,75 @@ void SpeedLimitDisplay::updateSpeedLimitData(
55
55
current_limit = msg->max_velocity ;
56
56
}
57
57
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
+
58
70
void SpeedLimitDisplay::drawSpeedLimitIndicator (QPainter & painter, const QRectF & backgroundRect)
59
71
{
60
72
// Enable Antialiasing for smoother drawing
61
73
painter.setRenderHint (QPainter::Antialiasing, true );
62
74
painter.setRenderHint (QPainter::SmoothPixmapTransform, true );
63
75
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
+ }
67
108
68
109
// Define the area for the outer circle
69
110
QRectF outerCircleRect = backgroundRect;
70
111
outerCircleRect.setWidth (backgroundRect.width () - 30 );
71
112
outerCircleRect.setHeight (backgroundRect.width () - 30 );
72
113
outerCircleRect.moveTopLeft (QPointF (backgroundRect.left () + 15 , backgroundRect.top () + 10 ));
73
114
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
+
74
121
// Define the area for the inner circle
75
122
QRectF innerCircleRect = outerCircleRect;
76
123
innerCircleRect.setWidth (outerCircleRect.width () / 1.25 );
77
124
innerCircleRect.setHeight (outerCircleRect.height () / 1.25 );
78
125
innerCircleRect.moveCenter (outerCircleRect.center ());
79
126
80
- // Draw the outer circle
81
- painter.drawEllipse (outerCircleRect);
82
-
83
127
painter.setRenderHint (QPainter::Antialiasing, true );
84
128
QColor colorFromHSV;
85
129
colorFromHSV.setHsv (0 , 0 , 0 ); // Hue, Saturation, Value
0 commit comments