UnityVR--Robotic Arm Scene 12-Simple Pipeline Application 4

Table of contents

1. Claws

2. Infrared sensor

3. Artifact generator

4. Summary


  The previous article has implemented the control of various movements of the robotic arm. This article implements the remaining components, such as grippers, sensors, and automatic placement of workpieces.

1. Claws

  I won’t go into details about the model adjustment of the hand claw. What needs to be set is the Rigidbody and Collider. You also need to determine the direction and distance of the hand claw opening and closing. For details, see the article Robotic Arm Scenario 3-Hand Claw .

  The main functions implemented are:

  1. When the collider on the claw is triggered, set the workpiece as a sub-object of the claw and move with the claw;

  2. When the gripper is opened, set the parent object of the workpiece to empty and send the zero return event to the robot arm;

  Here is the code for the handclaw:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Robot_ClawCtrl : MonoBehaviour
{
    public Transform finger1, finger2;//两个手指
    float MoveDis, OpenDis = 0.1f, CloseDis = 0.02f, curDir;
    //需要移动的距离,打开时的距离,闭合时的距离,当前距离
    public bool isHold = false; //是否抓到物体
    public Transform ClawTarget = null;  //手爪抓到的目标

    void Start()
    {
        EventManager.Instance.AddEvent(EventType.OnClawCtrl,this, OnClawControl);
    }
    public void OnClawControl(EventDataBase data)
    {
        EventDataClaw eventData = data as EventDataClaw;
        EClawType clawType = eventData.clawType;
        Transform target = eventData.target;
        float settime = eventData.waitTime;

        if(clawType==EClawType.Close)
           OnCloseClaw();    //如果闭合的时间有误,可以添加一个定时器

        if (clawType == EClawType.Open)  //添加了定时器的打开命令
        {
            if (timer <= settime)
                timer += Time.deltaTime;
            else
            {
                OnOpenClaw();
                timer = 0;
                return;
            }
        }
    }

    public void OnOpenClaw()
    {//打开手爪
        MoveDis = OpenDis;
        finger1.localPosition = new Vector3(0, 0, -OpenDis);
        finger2.localPosition = new Vector3(0, 0, OpenDis);
        if(ClawTarget!=null||isHold)
        {
            ClawTarget.transform.GetComponent<Rigidbody>().isKinematic = false;
            ClawTarget.transform.SetParent(null);
            isHold = false;
            //发送回零事件
            EventManager.Instance.SendEvent(EventType.OnArmCtrl, new EventDataArm
            {
                armType = EArmMoveType.Home,
                clawType = EClawType.Close,
                target = null,
                waitTime = 0
            });
            ClawTarget = null;
        }
    }

    public void OnCloseClaw()
    {//关闭手爪
        MoveDis = CloseDis;
        finger1.localPosition = new Vector3(0, 0, MoveDis);
        finger2.localPosition = new Vector3(0, 0, -MoveDis);
    }
    private void OnTriggerStay(Collider other)
    {
        ClawTarget = other.transform;
        //如果判断物体是工件
        //这里设置的是layer,判断tag也是可以的
        if (ClawTarget.gameObject.layer == LayerMask.NameToLayer("Load")&& ClawTarget!= null)
        {
            ClawTarget.transform.SetParent(transform);
            ClawTarget.transform.GetComponent<Rigidbody>().isKinematic = true;
            isHold = true;
            EventManager.Instance.SendEvent(EventType.OnArmCtrl, new EventDataArm 
            { armType = EArmMoveType.Put,target=ClawTarget,clawType=EClawType.Close,waitTime=0.1f});
        }
        else return;
    }

  You can hang the gripper script on the gripper node and put it together with its rigid body. Two moving fingers need to be specified. The two parameters IsHold and ClawTarget do not need to be assigned initial values. They are only used for reference during debugging.

2. Infrared sensor

  When a workpiece passes through the sensor, an event is sent to notify the conveyor belt to stop and the robot arm to grab. You can refer to the radiation detection in the laser door damage article. There are many other detection methods.

  1. Create two columns, RayEmitter and RayReciever, to indicate the locations of emitting and receiving rays. Set two empty nodes point1 and point2 under these two nodes as the real transmitting and receiving positions;

   2. Sensor code, the most important API of which is ray detection LineCast in the physical system. code show as below:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//传感器
public class RaySensor : MonoBehaviour
{
    public Transform emitter;
    public Transform receiver;
    void Start()
    {//使用工具包中的划线工具,画一条线段代表红外线
        Tools.DrawLine(transform, emitter.position, receiver.position, Color.cyan);
    }

    void FixedUpdate()
    {
        Physics.Linecast(emitter.position, receiver.position, out RaycastHit hit, 64);//64是Layer号
        if (hit.collider != null)
        {//当检测到工件时,发送传送带停止事件
            EventManager.Instance.SendEvent(EventType.OnConveyerCtrl, new EventDataConveyer
            {
                conveyerType = EConveyerType.Stop  //通知传送带停止
            });
            EventManager.Instance.SendEvent(EventType.OnArmCtrl, new EventDataArm
            {//发送机械臂抓取事件
                armType = EArmMoveType.Get,
                target = hit.transform,    //传输给机械臂抓取命令和目标
                clawType=EClawType.Close,  //发送手爪闭合的数据
                waitTime=0.2f  
            });
        }
        else
        {
            EventManager.Instance.SendEvent(EventType.OnConveyerCtrl, new EventDataConveyer
            {//传送带移动
                conveyerType = EConveyerType.Move
            });
        }
    }
}

3. Artifact generator

  Its function is to continuously place workpieces on the conveyor belt, and use the resource management tool Resload.cs written in ResourceManager--Resource Management to generate the workpieces placed in the Assets->Resources->BeltLoaders folder onto the conveyor belt.

   1. Settings in the scene: Place a model randomly as a generator. The key point is to place an empty node (pink) from which all artifacts are generated.

   2. Code LoadersManager of artifact generator:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Pool;

public class LoadersManager : MonoBehaviour
{//Dispenser管理器
 //挂在控制节点上,管理所有生成的传送带负载工件
    public GameObject[] loaders;  //所有的负载工件放入数组
    public float passedTime=0, loaderRate=10; //计时器和间隔放负载的时间
    public bool ifLoad = true;    //是否可以发放工件的标记

    void Update()
    {
        passedTime+= Time.deltaTime;
        if (loaders.Length>0 && passedTime>=loaderRate&&ifLoad)
        {//当数组中有工件物体&时间到&红外线发送信号
            GameObject loader =loaders[Random.Range(0,loaders.Length)]; //随机加载一个工件
            if (loader == null)
                Debug.Log("没有找到物体");
            else
            {
                var go=GameObject.Instantiate(loader); //实例化物体
                go.transform.SetParent(transform);
                go.transform.localPosition = Vector3.zero;
                go.SetActive(true);
                go.AddComponent<Loaders>(); //生成的每一个工件都要挂上Loaders脚本
                passedTime = 0;
            }
        }  
    }
}

  3. Workpiece script: When the above generator generates a workpiece, in addition to setting the initial position and parent node of the workpiece, it also needs to attach the Loaders.cs script to each workpiece. In the script, the workpiece needs to be added with a rigid body and Collision body, the code is as follows:

public class Loaders : MonoBehaviour
{//挂在每一个被抓取工件上
    private Rigidbody rig;
    void Start()
    {
        rig = gameObject.AddComponent<Rigidbody>();
        rig.useGravity = true;
        gameObject.GetComponent<MeshCollider>().convex = true;
        gameObject.layer = LayerMask.NameToLayer("Load");
    }

  4. Final effect:

4. Summary

  At this point, the conveyor belt scene has been built. This is just a simple example. Many situations are treated as the simplest model, and there are still problems such as cumulative errors and beat errors that need to be solved during the debugging process.

Guess you like

Origin blog.csdn.net/tangjieitc/article/details/131667455