Qt绘制出思岚雷达扫描到的轮廓
Qt绘制出思岚雷达的扫描轮廓
1 配置思岚sdk的依赖库
可以参考我的qt pro文件的内容,按照实际路径添加
INCLUDEPATH += /home/kobe/QtProj/rplidar_sdk-release-v1.11.0/sdk/sdk/include
INCLUDEPATH += /home/kobe/QtProj/rplidar_sdk-release-v1.11.0/sdk/sdk/src
unix:!macx: LIBS += -L$$PWD/../../rplidar_sdk-release-v1.11.0/sdk/output/Linux/Release/ -lrplidar_sdk
INCLUDEPATH += $$PWD/../../rplidar_sdk-release-v1.11.0/sdk/output/Linux/Release
DEPENDPATH += $$PWD/../../rplidar_sdk-release-v1.11.0/sdk/output/Linux/Release
unix:!macx: PRE_TARGETDEPS += $$PWD/../../rplidar_sdk-release-v1.11.0/sdk/output/Linux/Release/librplidar_sdk.a
2 获取雷达的数据
其中重要的是这两句
// 抓取一个完整的0-360度的扫描数据
drv->grabScanData(buffer, countf);
// 按照扫描角度升序方式将数据排序
drv->ascendScanData(buffer, countf);
具体的功能可以参考思岚SDK手册
我的Askserialslot这个函数是在定时器是放在Qt的定时器中定时调用的。
//更新的的当前值
void MainWindow::Askserialslot()
{
if(comflagfront == COMOPEN)
{
rplidar_response_measurement_node_t buffer[1024];
int lengthcodef = sizeof(buffer[0]);
int lengthallf = sizeof(buffer);
size_t countf = lengthallf/lengthcodef;
drv->grabScanData(buffer, countf);
drv->ascendScanData(buffer, countf);
progressdatafront(buffer,countf);
lidarmain.__scanData(buffer,countf);
QImage lidarimg;
lidarmain.__draw(lidarimg);
ui->label->setPixmap(QPixmap::fromImage(lidarimg.scaled(ui->label->size(),Qt::KeepAspectRatio)));
}
if(comflagbehind == COMOPEN)
{
rplidar_response_measurement_node_t bufferbehind[1024];
int lengthcodeb = sizeof(bufferbehind[0]);
int lengthallb = sizeof(bufferbehind);
size_t countbehind = lengthallb/lengthcodeb;
// size_t countbehind = _countof(bufferbehind);
drvbehind->grabScanData(bufferbehind, countbehind);
drvbehind->ascendScanData(bufferbehind, countbehind);
progressdataback(bufferbehind,countbehind);
lidarmainbehind.__scanData(bufferbehind,countbehind);
QImage lidarimgbehind;
lidarmainbehind.__draw(lidarimgbehind);
ui->label_behind->setPixmap(QPixmap::fromImage(lidarimgbehind.scaled(ui->label_behind->size(),Qt::KeepAspectRatio)));
}
}
3 将获取的数据绘制出轮廓
scanData(buffer,countf)和 draw(lidarimg)这两个函数是实现具体的绘制功能,细节如下
/**
-
@brief 将扫描的原始数据转化为实际距离和角度,并将带有实际数据的点放到m_scanData里
-
@param buffer 雷达数据
-
@param count 雷达数据的个数
-
@param frequency 扫描的频率
*/
void LidarImage::__scanData(rplidar_response_measurement_node_t *buffer, size_t count)
{
m_scanData.clear();
for (int pos = 0; pos < (int)count; ++pos) {
scanDot dot;
if (!buffer[pos].distance_q2) continue;
dot.quality = (buffer[pos].sync_quality >> RPLIDAR_RESP_MEASUREMENT_QUALITY_SHIFT);
dot.angle = (buffer[pos].angle_q6_checkbit >> RPLIDAR_RESP_MEASUREMENT_ANGLE_SHIFT) / 64.0f;
dot.dist = buffer[pos].distance_q2 / 4.0f;
m_scanData.push_back(dot);
}
}
/**
- @brief 在图片上画出相应的点
*/
void LidarImage::__draw(QImage &lidarImage)
{
// lidarImage = QImage(m_picturePath);
lidarImage=QImage("//home//kobe//QtProj//lidarpic.jpg");
QPainter painter(&lidarImage);
int x, y;
double theta, rho;
int halfWidth = LidarImageWdith / 2;
int halfHeight = LidardarImageHeight / 2;
painter.setPen(QColor(255, 255, 255));
painter.setBrush(QColor(255,255,255));
painter.drawEllipse(halfWidth, halfHeight, 15, 15);//认为是雷达中心
for (int i = 0; i < m_scanData.size(); i++)
{
scanDot dot;
dot = m_scanData[i];
theta = dot.angle*PI / 180;
rho = dot.dist;
x = (int)(rho*sin(theta) / 20) + halfWidth;
y = (int)(-rho*cos(theta) / 20) + halfHeight;
painter.drawEllipse(x, y, 4, 4);
}
painter.end();
}
注意lidarImage这个路径下存储相应的底图。
4 最终实现效果
我是前后向各有一个雷达,底图是黑色
- 点赞
- 收藏
- 关注作者
评论(0)