UnityVR--Robotic Arm Scene 10-Simple Pipeline Application 2 (Download)

Table of contents

I. Introduction

2. Modification of event center

3. The robot arm joins the movement of DoTween

4. Control of robotic arm joints


I. Introduction

  The previous article has completed the construction of the assembly line. What we need to complete in this article is: 1. Modification of the event center; 2. DoTween motion modification of the robotic arm;

  This article is built on the basis of Event Center 2 and the robotic arm scenario . You need to first understand the basic knowledge related to it, understand the control of the robotic arm and mechanical claw, and the control of the inverse solution of the robotic arm. This article will build on these foundations. Modify the code.

2. Modification of event center

  1. EventType.cs

  Add event types for conveyor belts, robotic arms, and grippers to the EventType enumeration:

using UnityEngine;

public enum EventType 
{
    //其他事件类型先省略了
    //…………
    OnConveyerCtrl,
    OnArmCtrl,
    OnClawCtrl
}

  2. EventDataBase.cs

  Data that needs to be transmitted when adding robotic arm and gripper control to EventDataBase: (The space is limited, only the classes needed for this article are shown)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//管理委托要传入的参数
public class EventDataBase
{//父类
    
}

public class EventDataConveyer:EventDataBase
{//传送带需要传入移动或停止状态
    public EConveyerType conveyerType;  
}

public class EventDataArm:EventDataBase
{
    public EArmMoveType armType;  //机械臂的运动类型
    public Transform target;      //机械臂抓取目标,由传感器传入
    public EClawType clawType;    //如果需要的话,发送手爪状态命令
    public float waitTime;        //发送手爪命令时,同时发送让手爪等待的时间

}

public class EventDataClaw:EventDataBase
{
    public EClawType clawType;   //发送手爪命令
    public Transform target;     //手爪的目标,一般不太需要用到
    public float waitTime;       
}

public enum EConveyerType
{
    Move,Stop
}
public enum EArmMoveType
{
    Get,Up,Put,Home    //机械臂的状态:取东西,抬起,放东西,回零
}
public enum EClawType
{
    Open,Close
}

  With these event types and data defined, event sending can then be used to control the movement of the robotic arm and gripper.

3. The robot arm joins the movement of DoTween

  The previous robot arm movements were all hard cuts, with at most an interpolation operation added, but the operation was also shaky. Here is a DoTween plug-in, which extends the Rotate method of Transform and uses the DoTween curve to make the movement smoother:

  Use the four-element rotation API in DoTween to achieve rotation around different coordinate axes

public static TweenerCore<Quaternion, Quaternion, NoOptions> DOLocalRotateQuaternion(this Transform target, Quaternion endValue, float duration);

  Transform extension code is as follows:

using UnityEngine;
using DG.Tweening;//别忘记调用DoTween
using System;

public static class TransformExt
{
    public static void RotateX(this Transform transform, float x, float time, Action action)
    {
        transform.DOLocalRotateQuaternion(Quaternion.AngleAxis(x, Vector3.right), time).OnComplete(() => action());
    }
    public static void RotateY(this Transform transform, float y, float duration, Action action)
    {
        transform.DOLocalRotateQuaternion(Quaternion.AngleAxis(y, Vector3.up), time).OnComplete(() => action());
    }
    public static void RotateZ(this Transform transform, float z, float duration, Action action)
    {
        transform.DOLocalRotateQuaternion(Quaternion.AngleAxis(z, Vector3.forward), time).OnComplete(() => action());
    }
}

4. Control of robotic arm joints

  1. Put the code for joint rotation first: (refer to the article on robotic arm control (2) here )

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//机械臂单个关节点的旋转控制
//把它挂在每一个关节节点上,并选择关节的旋转方向

public enum RotateAxis
{
    x,y,z
}
public class Robot_JointCtrl : MonoBehaviour
{
    public RotateAxis axis;
    public float rotateTime;
    public float currentAngle=0;  //记录角度

    public void SetAngle(float angle, Action action)
    {
        if(currentAngle==angle)
        {
            action();
        }
        else
        {
            switch (axis)
            {
                case RotateAxis.x:
                    transform.RotateX(angle, rotateTime, action);
                    currentAngle = transform.rotation.eulerAngles.x;
                    break;
                case RotateAxis.y:
                    transform.RotateY(angle, rotateTime, action);
                    currentAngle = transform.rotation.eulerAngles.y;
                    break;
                case RotateAxis.z:
                    transform.RotateZ(angle, rotateTime, action);
                    currentAngle = transform.rotation.eulerAngles.y;
                    break;
            }
        }
    }

  Hang this script on each robot arm joint that needs to control rotation, and rotate the rotation axis and rotation time:

   2. Test it and give it a try (if you don’t need to test, you can skip steps 2 and 3)

  The control code of the robotic arm, Test.cs, traverses and executes all the joints with the Robot_JointCtrl script:

public class Test : MonoBehaviour
{
    public Robot_JointCtrl[] joints;
    //旋转的数据
    float[] home = { 0, 0, 0, 0, 0};     //回零动作
    float[] get = { -42.7, 9.12, -115, 90,0 }; //抓取动作
    float[] put = { 50, 40, -118, -90, 0 };  // 放置动作

    public void MoveJoints(float[] angle, int i)
    {//遍历所有活动关节
        joints[i].SetAngle(angle[i], () =>
        {
            Debug.Log(i + "关节已经完成移动");
            if (i == joints.Length-1)
            {
                Debug.Log("所有关节完成旋转");
                return ;
            }
            i++;
            MoveJoints(angle, i);
        });
    }

    void Update()
    {//测试MoveJoints方法
        if (Input.GetKeyDown(KeyCode.A))
        {
             MoveJoints(get,0);
        }
        if (Input.GetKeyDown(KeyCode.B))
        {
            MoveJoints(put,0);
        }
        if (Input.GetKeyDown(KeyCode.C))
        {
            MoveJoints(home,0);
        }
    }
}

  3. Hang the above test script on the root node of the robotic arm, and put the corresponding rotating joint into the array. The rotation result will not be released.

 (This article has a test demo download)

 The event sending and IK automatic operation of the robotic arm will be written in sections in the next article, otherwise the code will be too long.

  

Guess you like

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