usc2019-RobotSim

實踐-機器手臂程式設計

View on GitHub

RobotSim Project

RobotCommandMessage.cs Code

//RobotCommandMessage.cs
using RobotSim;
using UnityEngine;
using System;

public class RobotCommandMessage : RobotCommand
{
	//display message
	public string Message = string.Empty;
	
	//check message 
	public override bool Check()
	{
		if (Message == string.Empty)
		{
			errorMassage = "message empty";
			return false;
		}
		else
		{
			return true;
		}
	}

	public override int Execute()
	{
		//use DebugConsole to print message
		Debug.Log(Message);
		// go to next line
		return (line + 1);
	}

	public override string ExportDat()
	{
		//no message needed to be sent to robot dat files
		return string.Empty;
	}

	public override string ExportSrc()
	{
		//output MsgNotify("message")  to robot src file
		return tab + "MsgNotify(\"" + Message + "\")" + Environment.NewLine;
	}

	public override string UpdateName()
	{
		////rename Gameobject
		return (gameObject.name = "MsgNotify(\"" + Message + "\")");
	}
}

Image Image

Image Image Image

//Gripper.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Gripper : MonoBehaviour
{
	// Gripper code has two methods for gripping
	// 1.use OnTriggerEnter to catch objects within a default range,and the parent of the holding object is set up to Gripper
	// 2.use the gripper catching animation to move the gripper, and use Rigidbody function for Clipping

	//declare ready object: readyGet
	public Transform readyGet;
	//declare holding object: holdingObject
	public Transform holdingObject;

	//declare Lock() as catching command, and set readyGet's Parent as Gripper
	public void Lock(Transform product)
	{
		if (holdingObject == null)
		{
			if (product)
			{
				product.transform.parent = transform;
				holdingObject = product;
			}
		}
	}

	//declare Unlock() to return holding object
	public Transform Unlock()
	{
		Transform returnObject = holdingObject;
		holdingObject = null;//clear holding object

		return returnObject;
	}

	//declare LockReadyGet() for catching a ready object
	public void LockReadyGet()
	{
		Lock(readyGet);
	}
	//declare UnlockToWorld() to unlock the holding object
	public void UnlockToWorld()
	{
		if (holdingObject)
		{
			holdingObject.parent = null;
		}
		holdingObject = null;//put the holding object to World Root
	}
	//declare OnTriggerEnter for detecting object ready for gripping
	void OnTriggerEnter(Collider other)
	{
		readyGet = other.transform;
	}
	//declare OnTriggerExit
	void OnTriggerExit(Collider other)
	{
		if (readyGet == other.transform)
		{
			readyGet = null;
		}
	}
}

//RobotCommandGripper.cs
using UnityEngine;
using RobotSim;
using System;

public class RobotCommandGripper : RobotCommand
{
	//declare Gripper object: gripper
	public Gripper gripper;
	//Gripper animator
	public Animator animatorGripper;
	//declare boolen Lock and set as false
	public bool Lock = false;

	// Check()
	public override bool Check()
	{
		if (gripper)
		{
			return true;
		}
		else
		{
			errorMassage = "Gripper is NULL";
			return false;
		}
	}

	//Execute
	public override int Execute()
	{
		if (Lock)
		{
			//gripping
			gripper.LockReadyGet();
			//play gripping animation
			if (animatorGripper)
			{
				animatorGripper.speed = 1;
				animatorGripper.Play("Lock", -1, 0);
			}
		}
		else
		{
			//unlock gripper
			gripper.UnlockToWorld();
			//play un-lock animation
			if (animatorGripper)
			{
				animatorGripper.speed = 1;
				animatorGripper.Play("UnLock", -1, 0);
			}
		}

		return (line + 1);
	}

	public override string ExportDat()
	{
		//return nothing to Dat file
		return "";
	}

	public override string ExportSrc()
	{
		//return  GripperLock(true/false) to src file
		return tab + "GripperLock(" + Lock.ToString() + ");" + Environment.NewLine;
	}

	public override string UpdateName()
	{
		//rename Gameobject
		return (gameObject.name = "GripperLock(" + Lock.ToString() + ")");
	}

}

Image

Image

Image

Image Image

Image

Image

RobotSim export and import to WorkVisual Project

Image

Image Image

Image Image