📄 global.cpp
字号:
{
CString strTXData; //向串口发送的数据
strTXData = "e"; //发送字符'e'给机器人,机器人即停止运行
CIRExpPlatformDlg *pdlgTemp = (CIRExpPlatformDlg *)theApp.m_pMainWnd;
pdlgTemp->m_ctrlComm.SetOutput(COleVariant(strTXData));//发送ASCII字符数据
theApp.nLastMotiontype = STOP;
theApp.nDirection = STOP;
theApp.strCarState= "停止";
}
/*================================================================================
*函数名: void SearchCornerEdge()
*函数参数: 无
*函数功能描述: 搜索拐角处边缘
*返回值: void
*==================================================================================*/
void SearchCornerEdge()
{
int r, g, b, i, j;
BOOL bFoundEdge=FALSE; //标识有没有发现边缘,TRUE--已发现,FALSE--未发现
int rEdge,gEdge,bEdge; //边缘所在点的RGB三分量
int flag=0;
int nlineLeftEdge=0;
int nlineRightEdge=0;
theApp.bLeftEdge= FALSE; //机器人未遇到左列边缘
theApp.bRightEdge= FALSE; //机器人未遇到右列边缘
//边缘为黄色,rEdge,gEdge,bEdge分别为其RGB分量
rEdge=255;
gEdge=255;
bEdge=0;
for (i = 0; i < HEIGHT; i=i+5) //行
{
int xLeftTemp=0,yLeftTemp=0;
int xRightTemp=0,yRightTemp=0;
int tempCntLeftEdge=0;
int tempCntRightEdge=0;
int temp1=0;
int temp2=0;
bFoundEdge=FALSE;
flag=0;
for (j = 0; j < WIDTH; j++) //列
{
r = theApp.tempColor[(HEIGHT - i - 1) * WIDTH * 3 + j * 3 + 2];
g = theApp.tempColor[(HEIGHT - i - 1) * WIDTH * 3 + j * 3 + 1];
b = theApp.tempColor[(HEIGHT - i - 1) * WIDTH * 3 + j * 3 + 0];
if(r == rEdge && g == gEdge && b == bEdge) //边缘
{
if((bFoundEdge == FALSE) &&(flag==0))
{
bFoundEdge = TRUE;
xLeftTemp=j;
yLeftTemp=i;
}
else if((bFoundEdge == FALSE) && (flag==1) )
{
temp2=j;
if(temp2-temp1>30) //需要调整
{
bFoundEdge = TRUE;
xRightTemp=j;
yRightTemp=i;
}
else
break;
}
if((bFoundEdge == TRUE) && (j>WIDTH-6) && (flag==0))
{
tempCntLeftEdge=j-xLeftTemp;
if(tempCntLeftEdge>theApp.nLeftRightEdgeParm1)
nlineLeftEdge++;
break;
}
else if((bFoundEdge == TRUE) && (j>WIDTH-6) && (flag==1))
{
tempCntRightEdge=j-xRightTemp;
if(tempCntRightEdge>theApp.nLeftRightEdgeParm1)
nlineRightEdge++;
break;
}
}
else
{
if(bFoundEdge == TRUE && flag==0)
{
temp1=j;
tempCntLeftEdge=j-xLeftTemp;
if(tempCntLeftEdge>=theApp.nLeftRightEdgeParm1)
{
bFoundEdge = FALSE;
flag=1;
nlineLeftEdge++;
}
else
break;
}
else if(bFoundEdge == TRUE && flag==1)
{
tempCntRightEdge=j-xRightTemp;
if(tempCntRightEdge>=theApp.nLeftRightEdgeParm1)
{
bFoundEdge = FALSE;
flag=2;
nlineRightEdge++;
}
else
break;
}
}
if( flag==2 )
break;
}
}
if(nlineLeftEdge<theApp.nLeftRightEdgeParm2/5)
theApp.bLeftEdge=FALSE;
else
theApp.bLeftEdge=TRUE;
if(nlineRightEdge<theApp.nLeftRightEdgeParm2/5)
theApp.bRightEdge=FALSE;
else
theApp.bRightEdge=TRUE;
}
/*================================================================================
*函数名: void SearchEdge()
*函数参数: 无
*函数功能描述: 搜索除拐角处以外的边缘
*返回值: void
*==================================================================================*/
void SearchEdge()
{
int r, g, b, i, j;
BOOL bFoundEdge=FALSE; //标识有没有发现边缘,TRUE--已发现,FALSE--未发现
int rEdge,gEdge,bEdge; //边缘所在点的RGB三分量
int xyCenterEdgeLine[50][2]={0}; //隔五行扫描行边缘,每行中心点坐标
int xyCenterEdgeColumn[50][2]={0}; //隔五行扫描列边缘,每行中心点坐标
int lineEdgeArray=0; //满足行边缘条件的行数
int columnEdgeArray=0; //满足行边缘条件的列数
theApp.bLeftEdge= FALSE; //机器人未遇到左边缘
theApp.bRightEdge= FALSE; //机器人未遇到右列边缘
theApp.bTopEdge = FALSE; //机器人未遇到上边缘
theApp.bBottomEdge=FALSE; //机器人未遇到下边缘
int nCntEdgeLine=0; //满足条件的行边缘行数,初始化为0
int nCntEdgeColumn=0; //满足条件的列边缘列数,初始化为0
//边缘为黄色,rEdge,gEdge,bEdge分别为其RGB分量
rEdge=255;
gEdge=255;
bEdge=0;
for (i = 0; i < HEIGHT; i=i+5) //行
{
int xTemp=0,yTemp=0;
bFoundEdge=FALSE;
int tempCntEdge=0;
for (j = 0; j < WIDTH; j++) //列
{
r = theApp.tempColor[(HEIGHT - i - 1) * WIDTH * 3 + j * 3 + 2];
g = theApp.tempColor[(HEIGHT - i - 1) * WIDTH * 3 + j * 3 + 1];
b = theApp.tempColor[(HEIGHT - i - 1) * WIDTH * 3 + j * 3 + 0];
if(r == rEdge && g == gEdge && b == bEdge) //边缘
{
++tempCntEdge;
if(bFoundEdge == FALSE)
{
bFoundEdge = TRUE;
xTemp=j;
yTemp=i;
}
if((tempCntEdge >= theApp.nLineEdgeParm1) && (bFoundEdge == TRUE) && (j > WIDTH - 6) && (j < WIDTH - 2)) // 由于噪音的影响,tempcolor的边缘值是不准确的
{
nCntEdgeLine += 1; //nCntEdge机器人视野范围内所有边缘线行数
xyCenterEdgeLine[lineEdgeArray][0]=(j+xTemp)/2;
xyCenterEdgeLine[lineEdgeArray][1]=(i+yTemp)/2;
lineEdgeArray++;
break;
}
else if((tempCntEdge >= theApp.nColumnEdgeParm1) &&(tempCntEdge <theApp.nLineEdgeParm1) && (bFoundEdge == TRUE) && (j > WIDTH - 6) && (j < WIDTH - 2)) // 至少减一,tempcolor的边缘值是不准确的
{
nCntEdgeColumn += 1; //nCntEdge机器人视野范围内所有边缘线行数
xyCenterEdgeColumn[columnEdgeArray][0]=(j+xTemp)/2;
xyCenterEdgeColumn[columnEdgeArray][1]=(i+yTemp)/2;
columnEdgeArray++;
break;
}
}
else
{
if((tempCntEdge >= theApp.nLineEdgeParm1) && (bFoundEdge == TRUE))
{
nCntEdgeLine += 1; //nCntEdge机器人视野范围内所有边缘线行数
xyCenterEdgeLine[lineEdgeArray][0]=(j+xTemp)/2;
xyCenterEdgeLine[lineEdgeArray][1]=(i+yTemp)/2;
lineEdgeArray++;
break;
}
else if((tempCntEdge >= theApp.nColumnEdgeParm1) &&(tempCntEdge <theApp.nLineEdgeParm1) && (bFoundEdge == TRUE))
{
nCntEdgeColumn += 1; //nCntEdge机器人视野范围内所有边缘线行数
xyCenterEdgeColumn[columnEdgeArray][0]=(j+xTemp)/2;
xyCenterEdgeColumn[columnEdgeArray][1]=(i+yTemp)/2;
columnEdgeArray++;
break;
}
bFoundEdge = FALSE;
tempCntEdge = 0;
}
}
}
if((nCntEdgeLine<theApp.nLineEdgeparm2/5) && (nCntEdgeColumn<theApp.nColumnEdgeParm2/5)) //100->50机器人视野范围内边缘行数少于阀值10,列数少于阀值100
theApp.bFlagEdge=FALSE; //没有遇到边缘
else
{
theApp.bFlagEdge=TRUE;
if(nCntEdgeLine>=theApp.nLineEdgeparm2/5)
{
int nCntTopLine=0;
int nCntBottomLine=0;
int tempLine=0;
theApp.deltaxyEdgeLine=0;
for(int k=0;k<lineEdgeArray-1;k++)
{
theApp.deltaxyEdgeLine=theApp.deltaxyEdgeLine+(xyCenterEdgeLine[k][0]-xyCenterEdgeLine[k+1][0])/(xyCenterEdgeLine[k+1][1]-xyCenterEdgeLine[k][1]);
//>0,边缘方向是:/
//<0,边缘方向是:\
tempLine++;
if(xyCenterEdgeLine[k][1]<=YROBOT/2) //上行边缘
{
nCntTopLine++;
if(nCntTopLine>lineEdgeArray*0.5)
{
theApp.bTopEdge = TRUE;
break;
}
}
else if(xyCenterEdgeLine[k][1]>YROBOT/2) //下行边缘
{
nCntBottomLine++;
if(nCntBottomLine>lineEdgeArray*0.5)
{
theApp.bBottomEdge = TRUE;
break;
}
}
}
if(tempLine !=0)
theApp.deltaxyEdgeLine=theApp.deltaxyEdgeLine/tempLine; //平均斜率
}
if(nCntEdgeColumn>=theApp.nColumnEdgeParm2/5)
{
int nCntLeftColumn=0;
int nCntRightColumn=0;
int tempColumn=0;
theApp.deltaxyEdgeColumn=0;
for(int k=0;k<columnEdgeArray-1;k++)
{
theApp.deltaxyEdgeColumn=theApp.deltaxyEdgeColumn+(xyCenterEdgeColumn[k][0]-xyCenterEdgeColumn[k+1][0])/(xyCenterEdgeColumn[k+1][1]-xyCenterEdgeColumn[k][1]);
//>0,边缘方向是:/
//<0,边缘方向是:\
tempColumn++;
if(xyCenterEdgeColumn[k][0]<XROBOT) //左列边缘
{
nCntLeftColumn++;
if(nCntLeftColumn>columnEdgeArray*0.5)
{
theApp.bLeftEdge = TRUE;
break;
}
}
else
if(xyCenterEdgeColumn[k][0]>XROBOT) //右列边缘
{
nCntRightColumn++;
if(nCntRightColumn>columnEdgeArray*0.5)
{
theApp.bRightEdge = TRUE;
break;
}
}
}
if(tempColumn !=0)
theApp.deltaxyEdgeColumn=theApp.deltaxyEdgeColumn/tempColumn; //平均斜率
}
}
}
/*================================================================================
*函数名: void SearchObjectAndObstacle()
*函数参数: 无
*函数功能描述: 搜索目标物和障碍物函数
*返回值: void
*==================================================================================*/
void SearchObjectAndObstacle()
{
int r, g, b, i, j;
int tempCntObject = 0; //每列目标点个数
int tempCntObstacle=0; //每列障碍物点个数
int rObject,gObject,bObject; //目标物所在点的RGB三分量
int rObstacle,gObstacle,bObstacle; //障碍物所在点的RGB三分量
BOOL bFoundObject = FALSE; //标识有没有发现目标,TRUE--已发现,FALSE--未发现
BOOL bFoundObstacle =FALSE; //标识有没有发现障碍物,TRUE--已发现,FALSE--未发现
theApp.bFlagObject =FALSE; //机器人未发现目标物
theApp.bFlagObstacle=FALSE; //机器人未发现障碍物
int nCntObject = 0; //满足条件的目标物行数,初始化为0
int nCntObstacle=0; //满足条件的障碍物行数,初始化为0
if(theApp.colObject==RED) //目标物是红色
{
rObject=255;
gObject=0;
bObject=0;
}
else if(theApp.colObject==BLUE) //目标物是蓝色
{
rObject=0;
gObject=0;
bObject=255;
}
if(theApp.colObstacle==RED) //障碍物是红色
{
rObstacle=255;
gObstacle=0;
bObstacle=0;
}
else
if(theApp.colObstacle==BLUE) //障碍物是蓝色
{
rObstacle=0;
gObstacle=0;
bObstacle=255;
}
for (i = 0; i < HEIGHT; ++i) //行
{
int xTemp=0,yTemp=0;
bFoundObject=FALSE;
bFoundObstacle =FALSE;
tempCntObject=0;
tempCntObstacle=0;
for (j = 0; j < WIDTH; ++j) //列
{
r = theApp.tempColor[(HEIGHT - i - 1) * WIDTH * 3 + j * 3 + 2];
g = theApp.tempColor[(HEIGHT - i - 1) * WIDTH * 3 + j * 3 + 1];
b = theApp.tempColor[(HEIGHT - i - 1) * WIDTH * 3 + j * 3 + 0];
if(r == rObject && g == gObject && b == bObject) //目标物
{
++tempCntObject;
if(bFoundObject == FALSE)
{
bFoundObject = TRUE;
xTemp = j;
yTemp = i;
}
if(tempCntObject > theApp.nObjectParm1 && bFoundObject == TRUE && j > WIDTH - 6 && j < WIDTH - 2) // 至少减一,tempcolor的边缘值是不准确的
{
nCntObject += 1;
theApp.xCoordinate += (j + xTemp)/2;
theApp.yCoordinate += (i + xTemp)/2;
break;
}
}
else
{
if(tempCntObject > theApp.nObjectParm1 && bFoundObject == TRUE)
{
nCntObject += 1;
theApp.xCoordinate += (j + xTemp)/2;
theApp.yCoordinate += (i + yTemp)/2;
break;
}
bFoundObject = FALSE;
tempCntObject = 0;
}
if(r == rObstacle && g == gObstacle && b == bObstacle) //障碍物
{
++tempCntObstacle;
if(bFoundObstacle == FALSE)
bFoundObstacle = TRUE;
if(tempCntObstacle > theApp.nObstacleParm1 && bFoundObstacle == TRUE && j > WIDTH - 6 && j < WIDTH - 2) // 至少减一,tempcolor的边缘值是不准确的
{
nCntObstacle += 1;
break;
}
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -