今天主机被同事借走,由于github上未上传最新的工程文件,Hexo博客暂时停载
今日主要编写U3D C#脚本,完整机械手的运动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Axis1 : MonoBehaviour {
// Start is called before the first frame update
void Start () {
}
// Update is called once per frame
void Update () {
}
public void left () {
transform.Rotate (0, -50 * Time.deltaTime, 0, Space.Self);
}
public void right () {
transform.Rotate (0, 50 * Time.deltaTime, 0, Space.Self);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class buttonOptions : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler {
public bool invokeOnce = false;
private bool hadInvoke = false;
public float interval = 0.1f;
private bool isPointerDown = false;
private float recordTime;
public UnityEvent onPress = new UnityEvent ();
public UnityEvent onRelease = new UnityEvent ();
// Start is called before the first frame update
void Start () {
}
// Update is called once per frame
void Update () {
if (invokeOnce && hadInvoke) return;
if (isPointerDown) {
onPress.Invoke ();
hadInvoke = true;
if ((Time.time - recordTime) > interval) {
onPress.Invoke ();
hadInvoke = true;
}
}
}
public void OnPointerDown (PointerEventData eventDate) {
isPointerDown = true;
recordTime = Time.time;
}
public void OnPointerUp (PointerEventData eventDate) {
isPointerDown = false;
hadInvoke = false;
onRelease.Invoke ();
}
public void OnPointerExit (PointerEventData eventDate) {
isPointerDown = false;
hadInvoke = false;
onRelease.Invoke ();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GeneralJoint : MonoBehaviour {
private string objectName;
private int joint_number;
GameObject Mcamera;
Main mainScript;
float[] temp_angle = { 0, 0, 0 };
int axis_number; //该关节的旋转轴,0代表x轴,1代表y轴,2代表z轴,均为本体轴
public int Axis_number {
get {
return axis_number;
}
set {
axis_number = Axis_number;
}
}
//读取该关节的当前关节转角
public float read_tempangle () {
return temp_angle[Axis_number];
}
// 通过控制函数控制该关节的转角
public void angleMove (float ang) {
if (ang > 180) {
while (ang > 180) {
ang -= 360;
}
} else if (ang < -180) {
while (ang < -180) {
ang += 360;
}
}
temp_angle[Axis_number] = ang;
transform.localRotation = Quaternion.Euler (temp_angle[0], temp_angle[1], temp_angle[2]);
}
// Use this for initialization
void Start () {
Mcamera = GameObject.Find ("Main Camera");
objectName = gameObject.name;
joint_number = int.Parse (objectName.Substring (7, 1));
mainScript = Mcamera.GetComponent<Main> ();
axis_number = mainScript.axis_number[joint_number];
}
// Update is called once per frame
void Update () {
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Main : MonoBehaviour {
#region definition
//将机械臂的组件关联到下面的数组中
GameObject[] jointArray = new GameObject[6];
int steps = 100; //每次运动的步数,这个步数乘以fixupdate每帧的时间就是一次运动需要的时间
int cnt = 0; //计数器
bool moveTag = false; //运动标志
//各关节转动轴编号,0绕x轴转,1绕y轴转,2绕z轴转
public int[] axis_number = new int[] { 1, 2, 2, 2, 1, 2 };
private float[] jointAngleRealTime = new float[6]; //存储实时的角度
private float[] targetAngle = new float[6]; //目标角度
private float[] sliderAngle = new float[6] { 90, 90, 90, 90, 90, 90 }; //滑条上的角度
private float[] deltaAngle = new float[6]; //单步变化角度
private float[] currentAngle = new float[6]; //当前步数的关节角度
#endregion
//将场景的物体关联到该脚本中
void InitObject () {
string objName;
objName = string.Format ("S_Axis_0");
jointArray[0] = GameObject.Find (objName);
objName = string.Format ("L_Axis_1");
jointArray[1] = GameObject.Find (objName);
objName = string.Format ("U_Axis_2");
jointArray[2] = GameObject.Find (objName);
objName = string.Format ("R_Axis_3");
jointArray[3] = GameObject.Find (objName);
objName = string.Format ("B_Axis_4");
jointArray[4] = GameObject.Find (objName);
objName = string.Format ("T_Axis_5");
jointArray[5] = GameObject.Find (objName);
}
public void setAngle (float[] angle) //控制角度转动
{
jointArray[0].transform.GetComponent<GeneralJoint> ().angleMove (angle[0]);
jointArray[1].transform.GetComponent<GeneralJoint> ().angleMove (angle[1]);
jointArray[2].transform.GetComponent<GeneralJoint> ().angleMove (angle[2]);
jointArray[3].transform.GetComponent<GeneralJoint> ().angleMove (angle[3]);
jointArray[4].transform.GetComponent<GeneralJoint> ().angleMove (angle[4]);
jointArray[5].transform.GetComponent<GeneralJoint> ().angleMove (angle[5]);
updateJointAngle ();
}
void updateJointAngle () //更新关节实时角度显示
{
for (int i = 0; i < 6; i++) {
jointAngleRealTime[i] = jointArray[i].GetComponent<GeneralJoint> ().read_tempangle ();
}
}
//设置各关节的目标角度
void SetTargetAngle () {
for (int i = 0; i < targetAngle.Length; i++) {
targetAngle[i] = sliderAngle[i] - 90;
}
}
//重置各关节的目标角度
void ReSetTargetAngle () {
for (int i = 0; i < targetAngle.Length; i++) {
targetAngle[i] = 0;
}
sliderAngle = new float[6] { 90, 90, 90, 90, 90, 90 };
}
//设置关节单步变化角度
void getDeltaAngle () {
for (int i = 0; i < deltaAngle.Length; i++) {
deltaAngle[i] = (targetAngle[i] - jointAngleRealTime[i]) / steps;
}
}
//获得当前该步关节的角度
void getCurrentAngle () {
for (int i = 0; i < 6; i++) {
currentAngle[i] = jointAngleRealTime[i] + deltaAngle[i];
}
}
// Use this for initialization
void Start () {
InitObject ();
setAngle (new float[6] { 0, 0, 0, 0, 0, 0 });
}
void FixedUpdate () {
if (moveTag) {
if (cnt < 100) {
getCurrentAngle ();
setAngle (currentAngle);
cnt++;
} else {
setAngle (targetAngle);
moveTag = false;
}
}
}
void OnGUI () {
//简单的操作界面
if (GUI.Button (new Rect (10, 40, 50, 30), "Move")) {
SetTargetAngle ();
getDeltaAngle ();
cnt = 0;
moveTag = true;
}
if (GUI.Button (new Rect (10, 90, 50, 30), "Reset")) {
ReSetTargetAngle ();
getDeltaAngle ();
cnt = 0;
moveTag = true;
}
if (GUI.Button (new Rect (10, 140, 50, 30), "Exit")) {
UnityEditor.EditorApplication.isPlaying = false;
// Application.Quit ();
}
// GUI.Label (new Rect (55, 125, 150, 500), "操作说明:先通过滑条调整各个关节的目标角度,然后按“move”按钮开始运动");
GUI.BeginGroup (new Rect (50, 20, 400, 200));
sliderAngle[0] = GUI.HorizontalSlider (new Rect (55, 25, 100, 20), sliderAngle[0], 0, 180);
GUI.Label (new Rect (55, 5, Screen.width, 20), "joint0 value: " + ((int) sliderAngle[0] - 90) + " degree");
sliderAngle[1] = GUI.HorizontalSlider (new Rect (55, 55, 100, 20), sliderAngle[1], 0, 180);
GUI.Label (new Rect (55, 35, Screen.width, 20), "joint1 value: " + ((int) sliderAngle[1] - 90) + " degree");
sliderAngle[2] = GUI.HorizontalSlider (new Rect (55, 85, 100, 20), sliderAngle[2], 0, 180);
GUI.Label (new Rect (55, 65, Screen.width, 20), "joint2 value: " + ((int) sliderAngle[2] - 90) + " degree");
sliderAngle[3] = GUI.HorizontalSlider (new Rect (205, 25, 100, 20), sliderAngle[3], 0, 180);
GUI.Label (new Rect (205, 5, Screen.width, 20), "joint3 value: " + ((int) sliderAngle[3] - 90) + " degree");
sliderAngle[4] = GUI.HorizontalSlider (new Rect (205, 55, 100, 20), sliderAngle[4], 0, 180);
GUI.Label (new Rect (205, 35, Screen.width, 20), "joint4 value: " + ((int) sliderAngle[4] - 90) + " degree");
sliderAngle[5] = GUI.HorizontalSlider (new Rect (205, 85, 100, 20), sliderAngle[5], 0, 180);
GUI.Label (new Rect (205, 65, Screen.width, 20), "joint5 value: " + ((int) sliderAngle[5] - 90) + " degree");
GUI.EndGroup ();
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class ReadInfor : MonoBehaviour {
//文本中每行的内容
ArrayList infoall;
// Start is called before the first frame update
void Start () {
string pathTemp = "";
if (Application.platform == RuntimePlatform.Android) {
pathTemp = "/storage/emulated/0/DCIM"; //Android设备目录
} else {
// pathTemp = Application.streamingAssetsPath; //streaming 文件夹
pathTemp = "D:/U3D"; //本地测试目录
//System.Environment.CurrentDirectory //工程目录
}
// Debug.Log ("pathTemp " + pathTemp);
//得到文本中每一行的内容
infoall = LoadFile (pathTemp, "config.txt");
for (int i = 0; i < infoall.Count; i++) {
Debug.Log (infoall[i]);
// Debug.Log (SplitInforStr (infoall[i].ToString ()));
// Debug.Log ("100 -x = " + (100 - SplitInforInt (infoall[i].ToString ())).ToString ());
}
}
/**
* path:读取文件的路径
* name:读取文件的名称
*/
ArrayList LoadFile (string path, string name) {
//使用流的形式读取
StreamReader sr = null;
try {
sr = File.OpenText (path + "//" + name);
} catch (Exception e) {
//路径与名称未找到文件则直接返回空
Debug.Log (e);
return null;
}
string line;
ArrayList arrlist = new ArrayList ();
while ((line = sr.ReadLine ()) != null) {
//一行一行的读取
//将每一行的内容存入数组链表容器中
arrlist.Add (line);
}
//关闭流
sr.Close ();
//销毁流
sr.Dispose ();
//将数组链表容器返回
return arrlist;
}
// 返回字符串
string SplitInforStr (string strTemp) {
string[] str = new string[2];
str = strTemp.Split ('/');
return str[0];
}
/// 返回int型
int SplitInforInt (string strTemp) {
string[] str = new string[2];
str = strTemp.Split ('/');
try { //防止读取文失败影响正常运行
return Convert.ToInt32 (str[1]);
} catch (Exception ex) {
Debug.Log (ex);
return 0;
}
}
}