unity设备动作代码解析(4):XZ取放代码解析

勇哥注:

轴的移动很简单,就是Translate。

整个XZ取放如果想自动连在一起运动,在协程里应用StartCoroutine进行顺序动作的定义即可。



下面这个例子中,还用到了上节所讲的夹子代码。

image.png

(图1)


Safe Height  

Take Product Depth  取料时Z轴的深度

Release Product Length  放料时的深度,因为是放到另一个拉带上,所以要单独控制深度。

Release Product Length  这个当前设置的是3个横向位置,它是一个数组。

Feed In 是自动运行的开关。

Working是防重复操作。

Finished

Lift Time  横向移动的速度

Carrying Time 竖向取料的运动速度

Delta Time

Product Index  这个是横向移动位置的那个数组的指针,在自动运行时它会自动累计。

image.png

(国2)




源码:


Lifting是横向移动的协程,Carrying是上下移动的协程。

Working协程定义的自动运行时的动作序列。

最后还定义了一些调试功能,1夹子动作  2横向移动  3上下动作


public class Lift_Function : MonoBehaviour
{
    public GameObject lifter;
    public ConveyorBelt conveyorBelt_Function;

    public float safeHeight= 0.18f;
    public float takeProductDepth= 0.202f;
    public float releaseProductDepth= 0.3f;

    public float[] releaseProductLength;
         
    public bool feedIn;
    public bool working;
    public bool finished;

    public float liftTime = 2;
    public float carryingTime = 2;
    public float deltaTime = 0.1f;

    public int productIndex = 0;


    Clip_Function clipper;

    [Range(-1, 1)]
    int moving_V ;

    [Range(-1, 1)]
    int moving_H; 

    private void Start()
    {
        clipper = GetComponent<Clip_Function>();
        working = false;
        finished = true;
        feedIn = false;
    }

    IEnumerator Lifting()
    {
        if (working == false)
        {
            if (moving_V == 0)
            {
                moving_V = 1;
            }
            else
            {
                moving_V = -moving_V;
            }

            working = true;

            float tempDepth;
            if (lifter.transform.localPosition.z == 0)
            {
                tempDepth = takeProductDepth;
            }
            else
            {
                tempDepth = releaseProductDepth;
            }

            for (int i = 0; i < 60; i++)
            {
                lifter.transform.Translate(new Vector3(0, -moving_V * tempDepth / 60, 0));
                yield return new WaitForSecondsRealtime(liftTime / 60);
            }
            working = false;
        }        
    }


    IEnumerator Carrying()
    {
        if (working == false)
        {
            if (moving_H == 0)
            {
                moving_H = 1;
            }
            else
            {
                moving_H = -moving_H;
            }

            if (lifter.transform.localPosition.y > safeHeight)
            {
                working = true;
                for (int i = 0; i < 60; i++)
                {
                    lifter.transform.Translate(new Vector3(0, 0, -moving_H * releaseProductLength[productIndex] / 60));
                    yield return new WaitForSecondsRealtime(carryingTime / 60);
                }
                working = false;
            }
        }
    }

    IEnumerator Working()
    {
        feedIn = false;

        yield return StartCoroutine("Lifting");
        yield return StartCoroutine(clipper.ClipFunction());
        yield return StartCoroutine("Lifting");
        yield return StartCoroutine("Carrying");
        yield return StartCoroutine("Lifting");
        yield return StartCoroutine(clipper.ClipFunction());
        yield return StartCoroutine("Lifting");
        yield return StartCoroutine("Carrying");

        feedIn = true;

        if (productIndex < releaseProductLength.Length-1)
        {
            productIndex += 1;
        }
        else
        {
            productIndex = 0;
            feedIn = false;
            finished = true;
        }
    }


    private void OnTriggerStay(Collider other)
    {
        conveyorBelt_Function.feedIn = false;

        if (feedIn && (!working))
        {
            StartCoroutine("Working");
        }
    }

    private void OnTriggerExit(Collider other)
    {
        conveyorBelt_Function.feedIn = true;
    }


    #region debug
    void LiftWorking()
    {
        if(working==false)  StartCoroutine("Lifting");        
    }

    void CarryWorking()
    {
        if (working == false)  StartCoroutine("Carrying");
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha2) )
        {
            LiftWorking();
        }

        if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            CarryWorking();
        }
    }
    #endregion

}




本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:
本帖最后由 勇哥,很想停止 于 2024-10-22 09:04:16 编辑

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«    2025年6月    »
1
2345678
9101112131415
16171819202122
23242526272829
30
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 点击查阅微信群二维码
  • 扫描加勇哥的非标自动化群,验证答案:C#/C++/VB勇哥的非标自动化群
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:
  • 留言板:

Powered By Z-BlogPHP 1.7.2

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864