当前位置:   article > 正文

Qt5 实现雷达及余晖扫描,探测发现障碍物距离及角度_雷达余晖算法

雷达余晖算法

Qt5 实现雷达及余晖扫描,探测发现障碍物距离及角度

本示例主要利用 QPainter实现雷达图及余晖扫描效果,不依赖其他第三方库。显示效果流畅、平滑,不卡顿,可以直接跨平台应用Windows、Android、iMac及iOS系统。

如下是在Android手机上的界面效果图:

源代码工程链接:

https://download.csdn.net/download/u011275054/10867488


1、总体说明

       该雷达模拟器主要通过QPainter分别绘制刻度线、刻度值、余晖扫描以及障碍物区域几个主要部分。

图中的红色区域为扫描到的障碍物部分,绿色区域为实时扫描部分,设定能够探测到的最大距离为180cm,

检测到障碍物后,显示雷达实时的旋转角度和探测到的距离。

下面逐一进行上代码说明

2、雷达主体部分的绘制

  1. void RadarSimulator::drawRadar(QPainter *painter)
  2. {
  3. painter->save();
  4. painter->setPen(pen_draw);
  5. painter->translate(width/2,0);
  6. double dOrigpos = height-height*0.074;
  7. painter->drawArc(-(width-width*0.0825)/2, dOrigpos-(width-width*0.0825)/2,(width-width*0.0825),(width-width*0.0825),0*16,180* 16);
  8. painter->drawArc(-(width-width*0.27)/2,dOrigpos-(width-width*0.27)/2,(width-width*0.27),(width-width*0.27),0*16,180* 16);
  9. painter->drawArc(-(width-width*0.479)/2,dOrigpos-(width-width*0.479)/2,(width-width*0.479),(width-width*0.479),0*16,180* 16);
  10. painter->drawArc(-(width-width*0.687)/2,dOrigpos-(width-width*0.687)/2,(width-width*0.687),(width-width*0.687),0*16,180* 16);
  11. painter->drawArc(-(width-width*0.887)/2,dOrigpos-(width-width*0.887)/2,(width-width*0.887),(width-width*0.887),0*16,180* 16);
  12. painter->restore();
  13. painter->save();
  14. painter->setPen(pen_draw);
  15. painter->translate(width/2,height-height*0.074);
  16. painter->drawLine(-width/2,0,width/2,0);
  17. double dRadius = width/2*0.96;
  18. painter->drawLine(0,0,(-dRadius)*qCos(qDegreesToRadians(30.0)), (-dRadius)*qSin(qDegreesToRadians(30.0)));
  19. painter->drawLine(0,0,(-dRadius)*qCos(qDegreesToRadians(60.0)), (-dRadius)*qSin(qDegreesToRadians(60.0)));
  20. painter->drawLine(0,0,(-dRadius)*qCos(qDegreesToRadians(90.0)), (-dRadius)*qSin(qDegreesToRadians(90.0)));
  21. painter->drawLine(0,0,(-dRadius)*qCos(qDegreesToRadians(120.0)),(-dRadius)*qSin(qDegreesToRadians(120.0)));
  22. painter->drawLine(0,0,(-dRadius)*qCos(qDegreesToRadians(150.0)),(-dRadius)*qSin(qDegreesToRadians(150.0)));
  23. painter->drawLine((-dRadius)*qCos(qDegreesToRadians(30.0)),0,dRadius,0);
  24. painter->restore();
  25. }

3、雷达实时扫描线部分:

  1. void RadarSimulator::drawLine()
  2. {
  3. QPainter *painter = new QPainter(this);
  4. painter->save();
  5. QPen pen;
  6. QColor color(30,250,60);
  7. pen.setWidthF(16);
  8. pen.setColor(color); //添加线的颜色
  9. painter->setPen(pen);
  10. painter->translate(width/2,height-height*0.074);
  11. // draws the line according to the angle
  12. //painter->drawLine(0,0,(height-height*0.12)*qCos(qDegreesToRadians((double)iAngle)),-(height-height*0.12)*qSin(qDegreesToRadians((double)iAngle)));
  13. if(bRecyle){
  14. veclines.clear();
  15. bRecyle = false;
  16. bNeedErase = true;
  17. }
  18. if(veclines.size() >= 30){
  19. veclines.pop_front();
  20. }
  21. QLineF line(0, 0,(height-height*0.12)*qCos(qDegreesToRadians((double)iAngle)),
  22. -(height-height*0.12)*qSin(qDegreesToRadians((double)iAngle)));
  23. veclines.push_back(line);
  24. //qDebug("***DrawLine:Angle:%d", iAngle);
  25. int line_cnt = veclines.size();
  26. for(int num = 0; num < line_cnt; num++)
  27. {
  28. float opc = 0.3;
  29. if(num != 0){
  30. opc = (float)2.0*num / line_cnt;
  31. if(opc <= 0.3)
  32. opc = 0.3;
  33. }
  34. painter->setOpacity(opc);
  35. painter->drawLine(veclines[num]);
  36. }
  37. // painter->drawLines(veclines);
  38. painter->restore();
  39. delete(painter);
  40. }

4、绘制扫描障碍物区域部分:

  1. void RadarSimulator::drawObject()
  2. {
  3. QPainter *painter = new QPainter(this);
  4. painter->save();
  5. QPen pen;
  6. QColor color(255,10,10);
  7. pen.setWidthF(16);
  8. pen.setColor(color); //添加线的颜色
  9. painter->setPen(pen);
  10. painter->translate(width/2,height-height*0.074); // moves the starting coordinats to new location
  11. float pixsDistance = iDistance*((height-height*0.1666)*1/180); // covers the distance from the sensor from cm to pixels
  12. // limiting the range to 40 cms
  13. if(iDistance<180 && iDistance > 0)
  14. {
  15. if(bNeedErase){
  16. vecObjlines.clear();
  17. bNeedErase = false;
  18. }
  19. QLineF line(pixsDistance*qCos(qDegreesToRadians((double)iAngle)),
  20. -pixsDistance*qSin(qDegreesToRadians((double)iAngle)),
  21. (width-width*0.538)*qCos(qDegreesToRadians((double)iAngle)),
  22. -(width-width*0.538)*qSin(qDegreesToRadians((double)iAngle)));
  23. vecObjlines.push_back(line);
  24. }
  25. int line_cnt = vecObjlines.size();
  26. for(int num = 0; num < line_cnt; num++)
  27. {
  28. float opc = 0.3;
  29. if(num != 0){
  30. opc = (float)2.0*num / line_cnt;
  31. if(opc <= 0.3)
  32. opc = 0.3;
  33. }
  34. painter->setOpacity(opc);
  35. painter->drawLine(vecObjlines[num]);
  36. }
  37. // painter->drawLines(vecObjlines);
  38. painter->restore();
  39. delete(painter);
  40. }

5、绘制检测到的障碍物距离、角度数值部分

  1. void RadarSimulator::drawText(QPainter *painter)
  2. {
  3. painter->save();
  4. painter->setBrush(Qt::black);
  5. painter->drawRect(0,height-height*0.070,width,height*0.070);
  6. painter->restore();
  7. painter->save();
  8. QString noObject;
  9. if(iDistance>180 || iDistance <= 2) {
  10. noObject = "超出范围";
  11. }
  12. else {
  13. noObject = "已探测到";
  14. }
  15. painter->setPen(pen_draw);
  16. painter->setFont(font_txt);
  17. painter->drawText(width-width*0.4754, height-height*0.0833, "20cm");
  18. painter->drawText(width-width*0.3854, height-height*0.0833, "60cm");
  19. painter->drawText(width-width*0.281, height-height*0.0833, "100cm");
  20. painter->drawText(width-width*0.177, height-height*0.0833, "140cm");
  21. painter->drawText(width-width*0.0799, height-height*0.0833, "180cm");
  22. painter->setFont(font_Reslt);
  23. painter->drawText(width-width*0.915, height-height*0.0260, "目标: " + noObject);
  24. QString angle_ = QString::number(iAngle);
  25. painter->drawText(width-width*0.53, height-height*0.0260, "角度: " + angle_ +" °");
  26. QString dis = QString::number(iDistance);
  27. painter->drawText(width-width*0.30, height-height*0.0260, "距离: " + dis + " cm");
  28. painter->restore();
  29. painter->save();
  30. painter->setPen(pen_draw);
  31. painter->setFont(font_txt);
  32. painter->translate((width-width*0.5094)+width/2*qCos(qDegreesToRadians(30.0)),(height-height*0.0700)-width/2*qSin(qDegreesToRadians(30.0)));
  33. painter->rotate(-qDegreesToRadians(-60.0));
  34. painter->drawText(0, 0, "30°");
  35. painter->restore();
  36. painter->save();
  37. painter->setPen(pen_draw);
  38. painter->setFont(font_txt);
  39. painter->translate((width-width*0.509)+width/2*qCos(qDegreesToRadians(60.0)),(height-height*0.0650)-width/2*qSin(qDegreesToRadians(60.0)));
  40. painter->rotate(-qDegreesToRadians(-30.0));
  41. painter->drawText(0,0, "60°");
  42. painter->restore();
  43. painter->save();
  44. painter->setPen(pen_draw);
  45. painter->setFont(font_txt);
  46. painter->translate((width-width*0.507)+width/2*qCos(qDegreesToRadians(90.0)),(height-height*0.0433)-width/2*qSin(qDegreesToRadians(90.0)));
  47. painter->rotate(qDegreesToRadians(0.0));
  48. painter->drawText(0,0, "90°");
  49. painter->restore();
  50. painter->save();
  51. painter->setPen(pen_draw);
  52. painter->setFont(font_txt);
  53. painter->translate(width-width*0.503+width/2*qCos(qDegreesToRadians(120.0)),(height-height*0.06129)-width/2*qSin(qDegreesToRadians(120.0)));
  54. painter->rotate(qDegreesToRadians(-30.0));
  55. painter->drawText(0,0, "120°");
  56. painter->restore();
  57. painter->save();
  58. painter->setPen(pen_draw);
  59. painter->setFont(font_txt);
  60. painter->translate((width-width*0.5104)+width/2*qCos(qDegreesToRadians(150.0)),(height-height*0.0574)-width/2*qSin(qDegreesToRadians(150.0)));
  61. painter->rotate(qDegreesToRadians(-60.0));
  62. painter->drawText(0,0, "150°");
  63. painter->restore();
  64. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/170637
推荐阅读
相关标签
  

闽ICP备14008679号