亞洲大學-創意設計學院人工智慧工作坊 KUKA工業機器手臂3D動畫模擬與操作 RobotSim
RobotCommandMessage.cs 程式碼
//RobotCommandMessage.cs
using RobotSim;
using UnityEngine;
using System;
public class RobotCommandMessage : RobotCommand
{
//顯示的訊息
public string Message = string.Empty;
//檢查是否未設定訊息
public override bool Check()
{
if (Message == string.Empty)
{
errorMassage = "未填入訊息";
return false;
}
else
{
return true;
}
}
public override int Execute()
{
//使用DebugConsole印出所設定的訊息
Debug.Log(Message);
//動作完成,執行下一行
return (line + 1);
}
public override string UpdateName()
{
////更新Gameobject在階層視窗內的名稱
return (gameObject.name = "MsgNotify(\"" + Message + "\")");
}
}
//Gripper.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gripper : MonoBehaviour
{
// Gripper程式會有2種模擬夾取
// 1.利用OnTriggerEnter自動取得在夾取範圍內的物件,夾取指令時將該物件的parent設為Gripper
// 2.以夾爪播放夾取動畫的方式移動夾爪,並利用Rigidbody產生夾取
//準備夾取的物件
public Transform readyGet;
//目前夾持的物件
public Transform holdingObject;
//夾取指令(將readyGet物件Parent設為Gripper)
public void Lock(Transform product)
{
if (holdingObject == null)
{
if (product)
{
product.transform.parent = transform;
holdingObject = product;
}
}
}
//傳回目前所夾持物
public Transform Unlock()
{
Transform returnObject = holdingObject;
holdingObject = null;//清空目前所持物
return returnObject;
}
//夾取readyGet物件
public void LockReadyGet()
{
Lock(readyGet);
}
//放開夾取物件
public void UnlockToWorld()
{
if (holdingObject)
{
holdingObject.parent = null;
}
holdingObject = null;//把手上拿著的東西丟到世界Root去
}
//偵測目前可夾取物
void OnTriggerEnter(Collider other)
{
readyGet = other.transform;
}
//移除目前圖夾取物
void OnTriggerExit(Collider other)
{
if (readyGet == other.transform)
{
readyGet = null;
}
}
}
//RobotCommandGripper.cs
using UnityEngine;
using RobotSim;
using System;
public class RobotCommandGripper : RobotCommand
{
//對應操作的夾爪
public Gripper gripper;
//夾爪動畫
public Animator animatorGripper;
//夾持命令
public bool Lock = false;
//檢查是否有設定好夾爪
public override bool Check()
{
if (gripper)
{
return true;
}
else
{
errorMassage = "Gripper is NULL";
return false;
}
}
//執行夾爪動作
public override int Execute()
{
if (Lock)
{
//夾取(以設定Parent方式)
gripper.LockReadyGet();
//夾取(播放動畫)
if (animatorGripper)
{
animatorGripper.speed = 1;
animatorGripper.Play("Lock", -1, 0);
}
}
else
{
//放開(以設定parent方式)
gripper.UnlockToWorld();
//放開(播放動畫)
if (animatorGripper)
{
animatorGripper.speed = 1;
animatorGripper.Play("UnLock", -1, 0);
}
}
//動作完成,執行下一行
return (line + 1);
}
public override string UpdateName()
{
//更新Gameobject在階層視窗內的名稱
return (gameObject.name = "GripperLock(" + Lock.ToString() + ")");
}
}