使用Leap Motion控制工业步进电机

本文关键字Leap Motion、Unity3D、Arduino、工业步进电机。

我使用的arduino型号是arduino uno r3,使用配套TB6600驱动板+J-4218HB401步进电机。Unity3D与Arduino使用串口通信。

Unity3D代码如下,新建脚本SerialPortTest :

using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System.Threading;
public class SerialPortTest : MonoBehaviour
{

//Setup parameters to connect to Arduino

public static SerialPort sp = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);
public static string strIn;
public string message;
public bool isShow = false;
public bool isTimer = false;
public float timer = 4.0f;

void Start()
{
    OpenConnection();
  //  InvokeRepeating("kk", 1, 1);
}

void Update()
{
    if (Input.GetKey(KeyCode.A))
    {
        sp.Write(1.ToString());
    }
    if (Input.GetKey(KeyCode.Space))
    {
        // istrue = true;
        sp.Write(0.ToString());
    }

    
    //Read incoming data
    //   strIn = sp.ReadLine();
    // print(strIn);
    //You can also send data like this
    //   sp.Write("2");
}
//Function connecting to Arduino

public void shun()
{
    if (isShow==false)
    {
        sp.Write(1.ToString());
        print("sp.Write(1.ToString())");
        isTimer = true;
    }
}
public void ni() {
    if (isShow==false)
    {
        sp.Write(0.ToString());
        print("sp.Write(0.ToString())");
        isTimer = true;
    }
}

public void OpenConnection()
{
    if (sp != null)
    {
        if (sp.IsOpen)
        {
            sp.Close();
            message = "Closing port, because it was already open!";
        }
        else
        {
            sp.Open();  // opens the connection
            sp.ReadTimeout = 50;  // sets the timeout value before reporting error
            message = "Port Opened!";
        }
    }
    else
    {
        if (sp.IsOpen)
        {
            print("Port is already open");
        }
        else
        {
            print("Port == null");
        }
    }              
}


void OnApplicationQuit()
{
    sp.Close();
}

}

Arduino 代码:

const int PUL = 2; //PUL+ 接Pin 2, PUL-接GND
const int DIR = 3; //DIR+ 接Pin 3, DIR-接GND
const int PUL1 = 4; //PUL+ 接Pin 4, PUL-接GND
const int DIR1 = 5; //DIR+ 接Pin 5, DIR-接GND
boolean isWork= false;

void setup() {
pinMode(PUL, OUTPUT);
pinMode(DIR, OUTPUT);
pinMode(PUL1, OUTPUT);
pinMode(DIR1, OUTPUT);
Serial.begin(9600);
}

void loop() {
if (Serial.available() > 0) {
char incomingByte = Serial.read();
switch(incomingByte){
case '0' :
if(isWork){
break;
}
//============逆时针转向:
digitalWrite(DIR, HIGH);
digitalWrite(DIR1, HIGH);
delay(5);
GenPulse(1000, 500);
//============顺时针转向:
digitalWrite(DIR, LOW);
digitalWrite(DIR1, LOW);
delay(5);
GenPulse(1000, 500);
break;
case '1':
if(isWork){
break;
}
//============逆时针转向:
digitalWrite(DIR, HIGH);
digitalWrite(DIR1, HIGH);
delay(5);
GenPulse_(1000, 500);
//============顺时针转向:
digitalWrite(DIR, LOW);
digitalWrite(DIR1, LOW);
delay(5);
GenPulse_(1000, 500);
break;
}
}
}
void GenPulse(unsigned int PulseNum, unsigned int Feq) { //PulseNum:步数
isWork==true;
for (unsigned int i = 0; i < PulseNum; i++) { //Feq:频率
digitalWrite(PUL, HIGH);
delayMicroseconds(500000 / Feq);
digitalWrite(PUL, LOW);
delayMicroseconds(500000 / Feq);

}
isWork ==false;
}

void GenPulse_(unsigned int PulseNum, unsigned int Feq) { //PulseNum:步数
isWork==true;
for (unsigned int i = 0; i < PulseNum; i++) { //Feq:频率
digitalWrite(PUL1, HIGH);
delayMicroseconds(500000 / Feq);
digitalWrite(PUL1, LOW);
delayMicroseconds(500000 / Feq);

}
isWork ==false;
}


接线图


使用Leap Motion控制工业步进电机_第1张图片
接线图.jpg

本文旨在纪录自己的一些学习经验,未经作者允许,禁止转载。

你可能感兴趣的:(使用Leap Motion控制工业步进电机)