浙江省中控杯高校机器人巡回赛参考程序

源代码在线查看: unit1.cpp

软件大小: 434 K
上传用户: huajunun
关键词: 机器人 参考程序
下载地址: 免注册下载 普通下载 VIP

相关代码

				//---------------------------------------------------------------------------
				
				#include 
				#pragma hdrstop
				 #include 
				#include 
				#include "Unit1.h"
				
				//---------------------------------------------------------------------------
				#pragma package(smart_init)
				#pragma link "MSCommLib_OCX"
				#pragma resource "*.dfm"
				TForm1 *Form1;
				HWND    hWndC;
				//---------------------------------------------------------------------------
				__fastcall TForm1::TForm1(TComponent* Owner)
				        : TForm(Owner)
				{
				}
				//---------------------------------------------------------------------------
				
				void __fastcall TForm1::FormCreate(TObject *Sender)
				{
				  MyCom->RThreshold=18;
				  MyCom->CommPort=5;
				  MyCom->InputMode=   comInputModeBinary;
				  if(MyCom->PortOpen==false)
				         { MyCom->PortOpen=true;}
				
				}
				//---------------------------------------------------------------------------
				
				void __fastcall TForm1::Button6Click(TObject *Sender)
				{
				   if(MyCom->PortOpen)
				      MyCom->PortOpen =false;
				}
				//---------------------------------------------------------------------------
				void __fastcall TForm1::MyComComm(TObject *Sender)
				{
				  OleVariant   RxBuff;
				   unsigned char buff[128];
				  if(MyCom->CommEvent!=comEvReceive)
				      return;
				  int BuffPtr=MyCom->InBufferCount;
				
				  if(BuffPtr>0)
				     { RxBuff= MyCom->Input;
				       int j=RxBuff.ArrayHighBound(1);
				       for(int i=0;i				        buff[i]=RxBuff.GetElement(i);
				     }
				
				    MyCom->InputLen=0;
				
				    if(BuffPtr>0)
				      { if(buff[0]==0xfa)
				         {
				         }
				      }
				}
				//---------------------------------------------------------------------------
				void __fastcall TForm1::Button3Click(TObject *Sender)
				{
				  //---------------------------------
				   if(MyCom->PortOpen)
				      { unsigned char redata[8]={0xf5,0x5f,0x01,0x60,0x00,0x00,0x00,0x00};
				
				        if(MyCom->PortOpen)
				          { OleVariant   TxBuff;
				
				          TxBuff=VarArrayCreate(OPENARRAY(int,(0,7)),varByte);
				          for(int i=0;i				            TxBuff.PutElement(redata[i],i);
				          MyCom->Output=   TxBuff;
				          }
				       }
				}
				//---------------------------------------------------------------------------
				void __fastcall TForm1::Button1Click(TObject *Sender)
				{
				         if(MyCom->PortOpen)
				      { unsigned char redata[8]={0xf5,0x5f,0x03,0x20,0x00,0x00,0x00,0x00};
				
				        if(MyCom->PortOpen)
				          { OleVariant   TxBuff;
				
				          TxBuff=VarArrayCreate(OPENARRAY(int,(0,7)),varByte);
				          for(int i=0;i				            TxBuff.PutElement(redata[i],i);
				          MyCom->Output=   TxBuff;
				          }
				       }
				}
				//---------------------------------------------------------------------------
				void __fastcall TForm1::Button2Click(TObject *Sender)
				{
				     if(MyCom->PortOpen)
				      { unsigned char redata[8]={0xf5,0x5f,0x04,0x30,0x00,0x00,0x00,0x00};
				
				        if(MyCom->PortOpen)
				          { OleVariant   TxBuff;
				
				          TxBuff=VarArrayCreate(OPENARRAY(int,(0,7)),varByte);
				          for(int i=0;i				            TxBuff.PutElement(redata[i],i);
				          MyCom->Output=   TxBuff;
				          }
				       }
				}
				//---------------------------------------------------------------------------
				void __fastcall TForm1::Button4Click(TObject *Sender)
				{
				    if(MyCom->PortOpen)
				      { unsigned char redata[8]={0xf5,0x5f,0x02,0x00,0x00,0x00,0x00,0x00};
				
				        if(MyCom->PortOpen)
				          { OleVariant   TxBuff;
				
				          TxBuff=VarArrayCreate(OPENARRAY(int,(0,7)),varByte);
				          for(int i=0;i				            TxBuff.PutElement(redata[i],i);
				          MyCom->Output=   TxBuff;
				          }
				       }
				}
				//---------------------------------------------------------------------------
				void __fastcall TForm1::Button7Click(TObject *Sender)
				{
				    if(MyCom->PortOpen)
				      { unsigned char redata[8]={0xf5,0x5f,0x05,0x00,0x00,0x00,0x00,0x00};
				
				        if(MyCom->PortOpen)
				          { OleVariant   TxBuff;
				
				          TxBuff=VarArrayCreate(OPENARRAY(int,(0,7)),varByte);
				          for(int i=0;i				            TxBuff.PutElement(redata[i],i);
				          MyCom->Output=   TxBuff;
				          }
				       }
				}
				//---------------------------------------------------------------------------
				
				void __fastcall TForm1::Button8Click(TObject *Sender)
				{
				  hWndC = capCreateCaptureWindowA("Capture Window",WS_CHILD | WS_VISIBLE ,
				   Image1->Left,Image1->Top,Image1->Width,Image1->Height,Form1->Handle,0);
				
				 hWndC = capCreateCaptureWindowA("Capture Window",WS_CHILD | WS_VISIBLE ,
				   Image1->Left,Image1->Top,Image1->Width,Image1->Height,Form1->Handle,0);
				
				 if (hWndC != 0)
				 {
				
				  SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
				  SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0);
				  SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0);
				  SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);
				  SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0);
				  SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0);
				  SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0);
				  SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0);
				
				}
				}
				//---------------------------------------------------------------------------
				
				
				void __fastcall TForm1::Timer1Timer(TObject *Sender)
				{
				
				   Image2->Canvas->CopyRect(Image2->ClientRect, Canvas, Image1->BoundsRect);
				     int iw;
				    int ih;
				    int red;
				    int green;
				    int blue;
				    int w1,h1,w2,h2,w3,h3;//设定三个坐标参数
				    int a,b,d,h;
				
				    for(iw=0;iw				    {
				        for(ih=0;ih				        {
				                 //将像素的RGB 分别取出
				               red=Image2->Canvas->Pixels[iw][ih]&0x0000ff;
				               green=(Image2->Canvas->Pixels[iw][ih]>>8)&0x0000ff;
				               blue=(Image2->Canvas->Pixels[iw][ih]>>16)&0x0000ff;
				  int rgb=red+blue+green+1;
				               int red0=100*red/rgb;
				               int green0=100*green/rgb;
				               int blue0=100*blue/rgb;
				                float cy=0.229*(float)red+0.587*(float)green+0.114*(float)blue;
				                float cu=(-1*0.147)*(float)red-0.289*(float)green+0.436*(float)blue;
				                float cv=0.615*(float)red-0.515*(float)green-0.100*(float)blue;
				               //三值化
				
				               if(blue0>39&&(blue>230&&red				               {                 Image3->Canvas->Pixels[iw][ih]=0xff0000;w2=iw;h2=ih;}//将蓝点坐标取出作为车头                }
				               else if(green>254&&cu				               {                       Image3->Canvas->Pixels[iw][ih]=0x00ff00; w1=iw;h1=ih;}//将绿点坐标取出作为车身                  }
				               else  if(red0>40&&(red>230&&blue				                  {                                     //红
				                                   Image3->Canvas->Pixels[iw][ih]=0x0000ff; w3=iw;h3=ih;}//将红点坐标取出作为终点                  }
				
				        }
				    }
				                 a=(h3-h1)/(w3-w1); //车身与终点连线斜率;
				                 b=w2-w1;     //假定纵轴刚好经过车身坐标的坐标系,则b为在该坐标系内车头横坐标;
				                 h=a*b+h1;   //在假定坐标系内车头对应在车身与终点直线上的纵坐标;
				                 d=sqrt((w3-w2)*(w3-w2)+(h3-h2)*(h3-h2));    //车头与终点的距离
				              if(d>30)       //车头的距离大于30
				              {
				               if(((h2-h)20))    //终点与车身是否在一条直线上;
				                  {
				                     if((h2-h)>20)   //车头在连线右方
				                         {                //左转
				
				                          if(MyCom->PortOpen)
				                              { unsigned char redata[8]={0xf5,0x5f,0x03,0x10,0x00,0x00,0x00,0x00};
				
				                            if(MyCom->PortOpen)
				                               { OleVariant   TxBuff;
				
				                                   TxBuff=VarArrayCreate(OPENARRAY(int,(0,7)),varByte);
				                                         for(int i=0;i				                                       TxBuff.PutElement(redata[i],i);
				                                  MyCom->Output=   TxBuff;
				                              }
				                           }
				                      }
				              else                     //车头在连线左方则右转
				              {
				
				              if(MyCom->PortOpen)
				                { unsigned char redata[8]={0xf5,0x5f,0x04,0x10,0x00,0x00,0x00,0x00};
				
				                  if(MyCom->PortOpen)
				                     { OleVariant   TxBuff;
				
				                        TxBuff=VarArrayCreate(OPENARRAY(int,(0,7)),varByte);
				                       for(int i=0;i				                     TxBuff.PutElement(redata[i],i);
				                       MyCom->Output=   TxBuff;
				                     }
				                   }
				               }
				             }
				                 else
				                  {
				                   if(MyCom->PortOpen)                 //前进
				                        { unsigned char redata[8]={0xf5,0x5f,0x01,0x20,0x00,0x00,0x00,0x00};
				
				                                if(MyCom->PortOpen)
				                                { OleVariant   TxBuff;
				
				                                        TxBuff=VarArrayCreate(OPENARRAY(int,(0,7)),varByte);
				                                         for(int i=0;i				                                        TxBuff.PutElement(redata[i],i);
				                                        MyCom->Output=   TxBuff;
				                                 }
				                        }
				                }
				
				              }
				          else                   //距离				          {
				
				    if(MyCom->PortOpen)
				      { unsigned char redata[8]={0xf5,0x5f,0x05,0x00,0x00,0x00,0x00,0x00};
				
				        if(MyCom->PortOpen)
				          { OleVariant   TxBuff;
				
				          TxBuff=VarArrayCreate(OPENARRAY(int,(0,7)),varByte);
				          for(int i=0;i				            TxBuff.PutElement(redata[i],i);
				          MyCom->Output=   TxBuff;
				          }
				       }
				
				          }
				
				
				    
				
				
				}
				//---------------------------------------------------------------------------
				
				void __fastcall TForm1::Button9Click(TObject *Sender)
				{
				   Timer1->Enabled=true;
				}
				//---------------------------------------------------------------------------
				
				
							

相关资源