动态卡尺胶路检测

动态卡尺胶路检测

1. 示例效果

  • 使用了三个卡尺工具、一个线段工具。
  • 这种方法可以检测胶路最常见的缺陷:断胶和胶宽等
    在这里插入图片描述

2. 代码

#region namespace imports
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.ToolBlock;
using Cognex.VisionPro3D;
using Cognex.VisionPro.ImageProcessing;
using Cognex.VisionPro.Caliper;
using Cognex.VisionPro.Dimensioning;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  
  //1.变量定义
  CogGraphicCollection my_gc = new CogGraphicCollection();
  CogGraphicLabel res = new CogGraphicLabel();
  #endregion

  /// <summary>
  /// Called when the parent tool is run.
  /// Add code here to customize or replace the normal run behavior.
  /// </summary>
  /// <param name="message">Sets the Message in the tool's RunStatus.</param>
  /// <param name="result">Sets the Result in the tool's RunStatus</param>
  /// <returns>True if the tool should run normally,
  ///          False if GroupRun customizes run behavior</returns>
  public override bool GroupRun(ref string message, ref CogToolResultConstants result)
  {
    // To let the execution stop in this script when a debugger is attached, uncomment the following lines.
    // #if DEBUG
    // if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
    // #endif


    // Run each tool using the RunTool function
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);

    //===2.1 工具获取、获取参数、初始化等
    //初始化方向起点位置
    CogCaliperTool start = mToolBlock.Tools["StartPoint"] as CogCaliperTool; 
    start.Run();
    //初始化方向终点位置
    CogCaliperTool end = mToolBlock.Tools["EndPoint"] as CogCaliperTool; 
    end.Run();
    //动态卡尺,不断地利用该卡尺得到下一个点的位置
    CogCaliperTool autoCaliper = mToolBlock.Tools["AutoPoint"] as CogCaliperTool; 
    //动态卡尺的矩形框
    CogRectangleAffine rec = autoCaliper.Region as CogRectangleAffine;
    //线段工具用来计算角度
    CogCreateSegmentTool segment = mToolBlock.Tools["CogCreateSegmentTool1"] as CogCreateSegmentTool;
    //获取参数
    int d = Convert.ToInt32(mToolBlock.Inputs["Distance"].Value);
    int totalNum = Convert.ToInt32(mToolBlock.Inputs["totalNum"].Value);
    int range = Convert.ToInt32(mToolBlock.Inputs["range"].Value);
    //清空图形集合
    my_gc.Clear();
    //实例化一个多变形对象
    CogPolygon p = new CogPolygon();
    res.SetXYText(100, 100, "OK");
    res.Font = new Font("宋体",20);
    
    
    //===2.2 循环生成指定数量的点
    for (int i = 0;  i < totalNum;  i++)
    {
      if(i == 0)
      {
        //第一个点使用搜索方向的起点
        p.AddVertex(start.Results[0].PositionX, start.Results[0].PositionX, i);
        continue;
      }
      else if(i == 1)
      {
        //第二个点使用搜索方向的终点
        p.AddVertex(start.Results[0].PositionX, end.Results[0].PositionX, i);
        continue;
      }
      else
      {
        //第三个以上的路径点
        //设置线段起点和终点,运行线段工具,最终得到上一个线段的角度
        segment.Segment.StartX = p.GetVertexX(i - 2);
        segment.Segment.StartY = p.GetVertexY(i - 2);
        segment.Segment.EndX = p.GetVertexX(i - 1);
        segment.Segment.EndY = p.GetVertexY(i - 1);
        segment.Run();
        //通过上一个线段的角度、路径之间的距离、上一个点的坐标,算出下一个点的坐标,并将算出的坐标作为动态卡尺的中心点,卡尺角度与线段垂直
        rec.CenterX = p.GetVertexX(i - 1) + Math.Round(Math.Cos(segment.Segment.Rotation), 5) * d;
        rec.CenterY = p.GetVertexY(i - 1) + Math.Round(Math.Sin(segment.Segment.Rotation), 5) * d;
        //将动态卡尺的角度设为与线段垂直,然后运行
        rec.Rotation = CogMisc.DegToRad(90 + Convert.ToInt32(CogMisc.RadToDeg(segment.Segment.Rotation)));
        autoCaliper.Run();
        
        //自动卡尺运行结果判断
        if (autoCaliper.Results.Count != 0)
        {
          //如果动态卡尺找到了,就直接存进多边形
          p.AddVertex(autoCaliper.Results[0].PositionX, autoCaliper.Results[0].PositionY, i);
        }
        else
        {
          //如果没有找到,则以线段为基准,向左旋转一定角度遍历运行
          
          //----旋转搜索----
          //初始角度为上一个线段的角度
          int angle = Convert.ToInt32(CogMisc.RadToDeg(segment.Segment.Rotation));
          int j;
          for (j = 0; j < range; j+=5)
          {
            if(j <= range / 2)
            {
              //右转角度计算
              angle = angle + j;
            }
            else
            {
             //左转角度计算
              angle = angle - (j - range / 2);
            }
            rec.CenterX = p.GetVertexX(i - 1) + Math.Round(Math.Cos((angle * Math.PI) / 180), 5) * d;//5是返回值中的小数位数
            rec.CenterY = p.GetVertexX(i - 1) + Math.Round(Math.Sin((angle * Math.PI) / 180), 5) * d;
            autoCaliper.Run();
            if(autoCaliper.Results.Count != 0)
            {
              p.AddVertex(autoCaliper.Results[0].PositionX, autoCaliper.Results[0].PositionY, i);
              break;
            }
            
            //如果还没有找到,退出
            if(j >= range)
            {
              res.SetXYText(100, 100, "NG");
              break;
            }    
          }
        }
      }
      
      //生成每一段路径上的指示箭头
      CogLineSegment LineArrow1 = new CogLineSegment();
      LineArrow1.Color = CogColorConstants.Yellow;
      LineArrow1.LineWidthInScreenPixels = 2;
      LineArrow1.SetStartEnd(p.GetVertexX(i - 1), p.GetVertexY(i - 1), autoCaliper.Results[0].PositionX, autoCaliper.Results[0].PositionY);
      LineArrow1.EndPointAdornment = CogLineSegmentAdornmentConstants.SolidArrow;
      my_gc.Add(LineArrow1);
    }
    
    return false;
  }

  #region When the Current Run Record is Created
  /// <summary>
  /// Called when the current record may have changed and is being reconstructed
  /// </summary>
  /// <param name="currentRecord">
  /// The new currentRecord is available to be initialized or customized.</param>
  public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord)
  {
  }
  #endregion

  #region When the Last Run Record is Created
  /// <summary>
  /// Called when the last run record may have changed and is being reconstructed
  /// </summary>
  /// <param name="lastRecord">
  /// The new last run record is available to be initialized or customized.</param>
  public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
  {
    //3.图形输出
    foreach (ICogGraphic x in my_gc)
    {
      mToolBlock.AddGraphicToRunRecord(x, lastRecord, "CogImageConvertTool1.InputImage", "");
    }
    mToolBlock.AddGraphicToRunRecord(res, lastRecord, "CogImageConvertTool1.InputImage", "");
  }
  #endregion

  #region When the Script is Initialized
  /// <summary>
  /// Perform any initialization required by your script here
  /// </summary>
  /// <param name="host">The host tool</param>
  public override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host)
  {
    // DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
    base.Initialize(host);


    // Store a local copy of the script host
    this.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));
  }
  #endregion

}


视频链接:Visonpro动态卡尺自动路径(胶线路径)搜索

  • 7
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MechMaster

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值