RoBoard魔人的機器人日誌

2013/6/7

[練習] AX-12輪胎 簡易控制程式

在很久之前本魔有介紹過AX-12轉動的方法

而今天我要介紹的是AX-12的全轉模式

基本上在官方的文件中有提到喔!

Address 0x20,0x21 
Moving Speed. Sets the angular velocity of the output moving to the Goal Position. Setting this value to its maximum value of 0x3ff moves the output with an angular velocity of 114 RPM, provided that there is enough power supplied (The lowest velocity is when this value is set to 1. When set to 0, the velocity is the largest possible for the supplied voltage, e.g. no velocity control is applied.)

沒錯的   所以我們只要將Operating Angle Limit設定為0    它就會進入全轉模式

而官方文件中也有寫到Operating Angle Limit 的Address為0x06,0x07,0x08,0x09

在這之後我們只要控制轉動速度    就可以讓馬達旋轉囉!!

那就讓我們來看看程式碼吧!!

首先看看介面的設計 在此本魔是使用C#來做的


應該是不會很難理解的

下面的上下左右button是可以讓輪胎轉動   直到按下stop button

上面的 speed則是可以調整轉速

keyboard mode則是可以讓我們使用WSAD來控制輪胎

那我們來看看本魔實做的辦法吧   

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RoBoIO_DotNet;
namespace controller_AX12
{
    public partial class Form1 : Form
    {
        public byte[] ax01 = new byte[11]{ 0xff, 0xff, 0x0f, 0x07, 0x03, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00 };
        public byte[] ax02 = new byte[11]{ 0xff, 0xff, 0x12, 0x07, 0x03, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00 };
        public byte[] read = new byte[6];
        public int run;
        public Form1()
        {
            InitializeComponent();
            groupBox1.Enabled = false;
            checkBox1.Enabled = false;
            run = 0;
            
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //RoBoIO.com3_DisableTurboMode();
            RoBoIO.com3_Close();
        }
        private void Start_Click(object sender, EventArgs e)
        {
            if (!groupBox1.Enabled){
                if (!RoBoIO.roboio_SetRBVer(RoBoIO.RB_100RD))
                    MessageBox.Show("roboio_SetRBVer fail");
                if (!RoBoIO.com3_Init(RoBoIO.COM_HDUPLEX))
                    MessageBox.Show("com3_Init fail");
                if (!RoBoIO.com3_SetFormat(RoBoIO.COM_BYTESIZE8, RoBoIO.COM_STOPBIT1, RoBoIO.COM_NOPARITY))
                    MessageBox.Show("com3_SetFormat fail");
                if (!RoBoIO.com3_SetBaud(RoBoIO.COMBAUD_115200BPS))
                    MessageBox.Show("com3_SetBaud fail");
                //RoBoIO.com3_EnableTurboMode();
                groupBox1.Enabled = true;
                checkBox1.Enabled = true;
                Start.Enabled = false;
                ax02[10] = BitConverter.GetBytes(~(ax02[2] + ax02[3] + ax02[4] + ax02[5] + ax02[6] + ax02[7] + ax02[8] + ax02[9]))[0];
                ax01[10] = BitConverter.GetBytes(~(ax01[2] + ax01[3] + ax01[4] + ax01[5] + ax01[6] + ax01[7] + ax01[8] + ax01[9]))[0];
                if (!RoBoIO.com3_ServoTRX(ax01, 11, read, 6))
                    MessageBox.Show("com3_ServoTRX fail read back = " + read[0] + " " + read[1] + " " + read[2] + " " + read[3] + " " + read[4] + " " + read[5]);
                if (!RoBoIO.com3_ServoTRX(ax02, 11, read, 6))
                    MessageBox.Show("com3_ServoTRX fail read back = " + read[0] + " " + read[1] + " " + read[2] + " " + read[3] + " " + read[4] + " " + read[5]);
                ax01[3] = 0x05;
                ax01[5] = 0x20;
                ax02[3] = 0x05;
                ax02[5] = 0x20;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            xUp();
        }
        private void xUp() {
            int speed = 10;
            if (run != 0)
            {
                Stop(5);
                speed = 5;
            }
            int v = speedbar.Value;
            run = 1;
            for (int i = 0; i < v % 0x3ff; i = i + v / speed)
            {
                ax01[6] = BitConverter.GetBytes(0x00ff & (i + 0x400))[0];
                ax01[7] = BitConverter.GetBytes((0xff00 & (i + 0x400)) / 0xff)[0];
                ax02[6] = BitConverter.GetBytes(0x00ff & i)[0];
                ax02[7] = BitConverter.GetBytes((0xff00 & i) / 0xff)[0];
                ax02[8] = BitConverter.GetBytes(~(ax02[2] + ax02[3] + ax02[4] + ax02[5] + ax02[6] + ax02[7]))[0];
                ax01[8] = BitConverter.GetBytes(~(ax01[2] + ax01[3] + ax01[4] + ax01[5] + ax01[6] + ax01[7]))[0];
                if (!RoBoIO.com3_ServoTRX(ax01, 9, read, 6))
                    MessageBox.Show("com3_ServoTRX fail read back = " + read[0] + " " + read[1] + " " + read[2] + " " + read[3] + " " + read[4] + " " + read[5]);
                if (!RoBoIO.com3_ServoTRX(ax02, 9, read, 6))
                    MessageBox.Show("com3_ServoTRX fail read back = " + read[0] + " " + read[1] + " " + read[2] + " " + read[3] + " " + read[4] + " " + read[5]);
                RoBoIO.delay_ms(25);
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            xdown();
        }
        private void xdown() {
            int speed = 10;
            if (run != 0)
            {
                Stop(5);
                speed = 5;
            }
            int v = speedbar.Value;
            run = 2;
            for (int i = 0; i < v % 0x3ff; i = i + v / speed)
            {
                ax01[6] = BitConverter.GetBytes(0x00ff & i)[0];
                ax01[7] = BitConverter.GetBytes((0xff00 & i) / 0xff)[0];
                ax02[6] = BitConverter.GetBytes(0x00ff & (i + 0x400))[0];
                ax02[7] = BitConverter.GetBytes((0xff00 & (i + 0x400)) / 0xff)[0];
                ax02[8] = BitConverter.GetBytes(~(ax02[2] + ax02[3] + ax02[4] + ax02[5] + ax02[6] + ax02[7]))[0];
                ax01[8] = BitConverter.GetBytes(~(ax01[2] + ax01[3] + ax01[4] + ax01[5] + ax01[6] + ax01[7]))[0];
                if (!RoBoIO.com3_ServoTRX(ax01, 9, read, 6))
                    MessageBox.Show("com3_ServoTRX fail read back = " + read[0] + " " + read[1] + " " + read[2] + " " + read[3] + " " + read[4] + " " + read[5]);
                if (!RoBoIO.com3_ServoTRX(ax02, 9, read, 6))
                    MessageBox.Show("com3_ServoTRX fail read back = " + read[0] + " " + read[1] + " " + read[2] + " " + read[3] + " " + read[4] + " " + read[5]);
                RoBoIO.delay_ms(25);
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            xleft();
        }
        private void xleft() {
            int speed = 10;
            if (run != 0)
            {
                Stop(5);
                speed = 5;
            }
            int v = speedbar.Value;
            run = 3;
            for (int i = 0; i < v % 0x3ff; i = i + v / speed)
            {
                ax01[6] = BitConverter.GetBytes(0x00ff & (i + 0x400))[0];
                ax01[7] = BitConverter.GetBytes((0xff00 & (i + 0x400)) / 0xff)[0];
                ax02[6] = BitConverter.GetBytes(0x00ff & (i + 0x400))[0];
                ax02[7] = BitConverter.GetBytes((0xff00 & (i + 0x400)) / 0xff)[0];
                ax02[8] = BitConverter.GetBytes(~(ax02[2] + ax02[3] + ax02[4] + ax02[5] + ax02[6] + ax02[7]))[0];
                ax01[8] = BitConverter.GetBytes(~(ax01[2] + ax01[3] + ax01[4] + ax01[5] + ax01[6] + ax01[7]))[0];
                if (!RoBoIO.com3_ServoTRX(ax01, 9, read, 6))
                    MessageBox.Show("com3_ServoTRX fail read back = " + read[0] + " " + read[1] + " " + read[2] + " " + read[3] + " " + read[4] + " " + read[5]);
                if (!RoBoIO.com3_ServoTRX(ax02, 9, read, 6))
                    MessageBox.Show("com3_ServoTRX fail read back = " + read[0] + " " + read[1] + " " + read[2] + " " + read[3] + " " + read[4] + " " + read[5]);
                RoBoIO.delay_ms(25);
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            xright();
        }
        private void xright() {
            int speed = 10;
            if (run != 0)
            {
                Stop(5);
                speed = 5;
            }
            int v = speedbar.Value;
            run = 4;
            for (int i = 0; i < v % 0x3ff; i = i + v / speed)
            {
                ax01[6] = BitConverter.GetBytes(0x00ff & i)[0];
                ax01[7] = BitConverter.GetBytes((0xff00 & i) / 0xff)[0];
                ax02[6] = BitConverter.GetBytes(0x00ff & i)[0];
                ax02[7] = BitConverter.GetBytes((0xff00 & i) / 0xff)[0];
                ax02[8] = BitConverter.GetBytes(~(ax02[2] + ax02[3] + ax02[4] + ax02[5] + ax02[6] + ax02[7]))[0];
                ax01[8] = BitConverter.GetBytes(~(ax01[2] + ax01[3] + ax01[4] + ax01[5] + ax01[6] + ax01[7]))[0];
                if (!RoBoIO.com3_ServoTRX(ax01, 9, read, 6))
                    MessageBox.Show("com3_ServoTRX fail read back = " + read[0] + " " + read[1] + " " + read[2] + " " + read[3] + " " + read[4] + " " + read[5]);
                if (!RoBoIO.com3_ServoTRX(ax02, 9, read, 6))
                    MessageBox.Show("com3_ServoTRX fail read back = " + read[0] + " " + read[1] + " " + read[2] + " " + read[3] + " " + read[4] + " " + read[5]);
                RoBoIO.delay_ms(25);
            }
        }
        private void button5_Click(object sender, EventArgs e)
        {
            Stop(10);
        }
        private void Stop(int speed)
        {
            if (run != 0)
            {
                int v = speedbar.Value;
                switch (run)
                {
                    case 1:
                        for (int i = v % 0x3ff; i > 0; i = i - v / speed)
                        {
                            ax01[6] = BitConverter.GetBytes(0x00ff & (i + 0x3ff))[0];
                            ax01[7] = BitConverter.GetBytes((0xff00 & (i + 0x3ff)) / 0xff)[0];
                            ax02[6] = BitConverter.GetBytes(0x00ff & i)[0];
                            ax02[7] = BitConverter.GetBytes((0xff00 & i) / 0xff)[0];
                            ax02[8] = BitConverter.GetBytes(~(ax02[2] + ax02[3] + ax02[4] + ax02[5] + ax02[6] + ax02[7]))[0];
                            ax01[8] = BitConverter.GetBytes(~(ax01[2] + ax01[3] + ax01[4] + ax01[5] + ax01[6] + ax01[7]))[0];
                            if (!RoBoIO.com3_ServoTRX(ax01, 9, read, 6))
                                MessageBox.Show("com3_ServoTRX fail read back = " + read[0] + " " + read[1] + " " + read[2] + " " + read[3] + " " + read[4] + " " + read[5]);
                            if (!RoBoIO.com3_ServoTRX(ax02, 9, read, 6))
                                MessageBox.Show("com3_ServoTRX fail read back = " + read[0] + " " + read[1] + " " + read[2] + " " + read[3] + " " + read[4] + " " + read[5]);
                            RoBoIO.delay_ms(25);
                        }
                        break;
                    case 2:
                        for (int i = v % 0x3ff; i > 0; i = i - v / speed)
                        {
                            ax01[6] = BitConverter.GetBytes(0x00ff & i)[0];
                            ax01[7] = BitConverter.GetBytes((0xff00 & i) / 0xff)[0];
                            ax02[6] = BitConverter.GetBytes(0x00ff & (i + 0x3ff))[0];
                            ax02[7] = BitConverter.GetBytes((0xff00 & (i + 0x3ff)) / 0xff)[0];
                            ax02[8] = BitConverter.GetBytes(~(ax02[2] + ax02[3] + ax02[4] + ax02[5] + ax02[6] + ax02[7]))[0];
                            ax01[8] = BitConverter.GetBytes(~(ax01[2] + ax01[3] + ax01[4] + ax01[5] + ax01[6] + ax01[7]))[0];
                            if (!RoBoIO.com3_ServoTRX(ax01, 9, read, 6))
                                MessageBox.Show("com3_ServoTRX fail read back = " + read[0] + " " + read[1] + " " + read[2] + " " + read[3] + " " + read[4] + " " + read[5]);
                            if (!RoBoIO.com3_ServoTRX(ax02, 9, read, 6))
                                MessageBox.Show("com3_ServoTRX fail read back = " + read[0] + " " + read[1] + " " + read[2] + " " + read[3] + " " + read[4] + " " + read[5]);
                            RoBoIO.delay_ms(25);
                        }
                        break;
                    case 3:
                        for (int i = v % 0x3ff; i > 0; i = i - v / speed)
                        {
                            ax01[6] = BitConverter.GetBytes(0x00ff & (i + 0x3ff))[0];
                            ax01[7] = BitConverter.GetBytes((0xff00 & (i + 0x3ff)) / 0xff)[0];
                            ax02[6] = BitConverter.GetBytes(0x00ff & (i + 0x3ff))[0];
                            ax02[7] = BitConverter.GetBytes((0xff00 & (i + 0x3ff)) / 0xff)[0];
                            ax02[8] = BitConverter.GetBytes(~(ax02[2] + ax02[3] + ax02[4] + ax02[5] + ax02[6] + ax02[7]))[0];
                            ax01[8] = BitConverter.GetBytes(~(ax01[2] + ax01[3] + ax01[4] + ax01[5] + ax01[6] + ax01[7]))[0];
                            if (!RoBoIO.com3_ServoTRX(ax01, 9, read, 6))
                                MessageBox.Show("com3_ServoTRX fail read back = " + read[0] + " " + read[1] + " " + read[2] + " " + read[3] + " " + read[4] + " " + read[5]);
                            if (!RoBoIO.com3_ServoTRX(ax02, 9, read, 6))
                                MessageBox.Show("com3_ServoTRX fail read back = " + read[0] + " " + read[1] + " " + read[2] + " " + read[3] + " " + read[4] + " " + read[5]);
                            RoBoIO.delay_ms(25);
                        }
                        break;
                    case 4:
                        for (int i = v % 0x3ff; i > 0; i = i - v / speed)
                        {
                            ax01[6] = BitConverter.GetBytes(0x00ff & i)[0];
                            ax01[7] = BitConverter.GetBytes((0xff00 & i) / 0xff)[0];
                            ax02[6] = BitConverter.GetBytes(0x00ff & i)[0];
                            ax02[7] = BitConverter.GetBytes((0xff00 & i) / 0xff)[0];
                            ax02[8] = BitConverter.GetBytes(~(ax02[2] + ax02[3] + ax02[4] + ax02[5] + ax02[6] + ax02[7]))[0];
                            ax01[8] = BitConverter.GetBytes(~(ax01[2] + ax01[3] + ax01[4] + ax01[5] + ax01[6] + ax01[7]))[0];
                            if (!RoBoIO.com3_ServoTRX(ax01, 9, read, 6))
                                MessageBox.Show("com3_ServoTRX fail read back = " + read[0] + " " + read[1] + " " + read[2] + " " + read[3] + " " + read[4] + " " + read[5]);
                            if (!RoBoIO.com3_ServoTRX(ax02, 9, read, 6))
                                MessageBox.Show("com3_ServoTRX fail read back = " + read[0] + " " + read[1] + " " + read[2] + " " + read[3] + " " + read[4] + " " + read[5]);
                            RoBoIO.delay_ms(25);
                        }
                        break;
                }
                run = 0;
            }
        }
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                groupBox1.Enabled = false;
            }
            else {
                groupBox1.Enabled = true;
            }
        }
        private void checkBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (checkBox1.Checked)
            {
                if (e.KeyCode == Keys.W && run != 1)
                    xUp();
                else if (e.KeyCode == Keys.S && run != 2)
                    xdown();
                else if (e.KeyCode == Keys.A && run != 3)
                    xleft();
                else if (e.KeyCode == Keys.D && run != 4)
                    xright();
            }
        }
        private void checkBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if(checkBox1.Checked){
                if (run != 0)
                Stop(10);
            }
        }
    }
}

Share:

0 留言:

張貼留言

技術提供:Blogger.

追蹤者