Arduino uno とサーボ シールドでロボット アームを作成しました。アームは USB を使用して C# Visual Studio で制御します。現在、アームには6つのサーボが取り付けられており、電源は5V、2Aで使用されています。アームにデータを送ることで制御できます。問題は、少数のデータが送信された後、サーボ アームが制御を失い、ランダムに移動することです。
*UPDATE 5V、5A電源を使用し、サーボの数を3つに減らします。サーボにいくつかのデータが送信された後、ロボットはまだ誤動作/ランダムに動きます.(ロボットは数秒間誤動作し、必要な位置に戻ります. (私は 6 つのサーボを持っています。それぞれ 3 つのサーボを試しましたが、同じ問題が発生しました。)
これは私のArduinoコードです
#include <Servo.h>
Servo myservoA;
Servo myservoB;
Servo myservoC;
Servo myservoD;
Servo myservoE;
Servo myservoF;
int i,pos,myspeed,myshow,COM0,MOVE=0;
int sea,seb,sec,sed,see,sef;
static int v=0;
byte Readbytes [10];
byte byte0, byte1,byte2,byte3,byte4,byte5,byte6,byte7;
byte newpos0, newpos1,newpos2,newpos3,newpos4,newpos5,newpos6,newpos7;
byte oldpos0, oldpos1,oldpos2,oldpos3,oldpos4,oldpos5,oldpos7,oldpos8;
String mycommand=""; /// Serial capture #auto: automatic operation
#com: computer serial port control #stop: standstill
static int mycomflag=2; // #auto:2 automatic operation , #com: 1 computer
serial port control #stop:0 standstill
void setup()
{
pinMode(13,INPUT);
pinMode(12,INPUT);
Serial.begin(9600);
myshow=0;
mycomflag=2; // the ARM default state: 2 automatic operation
// 1 pc
myservoA.attach(3);
myservoB.attach(5);
myservoC.attach(6);
myservoD.attach(9);
myservoE.attach(10);
myservoF.attach(11);
myservoA.write(10); //0A Control wrist rotation
myservoB.write(20); //14
myservoC.write(10); //0A
myservoD.write(20); //3C
myservoE.write(30); //64
myservoF.write(5); //4B Control waist
oldpos0=10;oldpos1=20;oldpos2=10;oldpos3=20;oldpos4=30;oldpos5=5;
newpos0=10;newpos1=20;newpos2=10;newpos3=20;newpos4=30;newpos5=5;
}
void loop()
{
if (Serial.available()>0)
{
i=0;int bufferLimit=9;
while(Serial.available()>0 && i < bufferLimit)
{
Readbytes[i]= Serial.read();
i++;
}
newpos0=(Readbytes[0]);
newpos1=(Readbytes[1]);
newpos2=(Readbytes[2]);
newpos3=(Readbytes[3]);
newpos4=(Readbytes[4]);
newpos5=(Readbytes[5]);
newpos6=(Readbytes[6]);
newpos7=(Readbytes[7]);
COM0=1;
}
delay(100);//Required for all bytes to be read at SP2 before sending
//at SP2 again
if (COM0==1&&(newpos5==2||newpos5==24||newpos5==80))
{
//Serial.write(Readbytes,i);//To Freezer
//Serial.write(Readbytes[0]);
//Serial.write(Readbytes[1]);
Serial.write(newpos0);
Serial.write(newpos1);
Serial.write(newpos2);
Serial.write(newpos3);
Serial.write(newpos4);
Serial.write(newpos5);
Serial.write(newpos6);
Serial.write(newpos7);
COM0=0;
MOVE=1;
}
if(MOVE==1)
{
myspeed=800;
for(pos = 0; pos <=myspeed; pos += 1)
{
myservoA.write(int(map(pos,1,myspeed,oldpos0,newpos0)));
myservoB.write(int(map(pos,1,myspeed,oldpos1,newpos1)));
myservoC.write(int(map(pos,1,myspeed,oldpos2,newpos2)));
myservoD.write(int(map(pos,1,myspeed,oldpos3,newpos3)));
myservoE.write(int(map(pos,1,myspeed,oldpos4,newpos4)));
myservoF.write(int(map(pos,1,myspeed,oldpos5,newpos5)));
delay(1);
}
MOVE=0;
oldpos0=newpos0;
oldpos1=newpos1;
oldpos2=newpos2;
oldpos3=newpos3;
oldpos4=newpos4;
oldpos5=newpos5;
}
これは私のVisual Studio WINFORMコードです
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RoboAnimate
{
public partial class Form1 : Form
{
string RxString;
string c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15,
c16, c17;
string fourbytesHexStr;
UInt32 fourbytesHex = 0;
byte A, B, C, D, E, F,timer1val;
bool do1=false, do2 = false, do3 = false, do4 = false, do5 = false, do6
= false, sequence1 = true;
private void timer1_Tick(object sender, EventArgs e)
{
timer1val++;
textBox4.Text = timer1val.ToString();
if (timer1val == 2) Do1();
if (timer1val == 5) Do2();
if (timer1val == 7) Do3();
if (timer1val == 9) Do4();
if (timer1val == 11) Do5();
if (timer1val == 13) Do6();
if (timer1val == 15) Do7();
if (timer1val == 17) Do8();
if (timer1val == 19) Do7();
if (timer1val == 21) Do6();
if (timer1val == 23) Do5();
if (timer1val == 25) Do4();
if (timer1val == 27) Do3();
if (timer1val == 29) Do2();
if (timer1val == 32)
{
Do1();
sequence1 = false;
timer1.Enabled = false;
timer1val = 0;
}
}
byte whichdo;
private void Form1_Load(object sender, EventArgs e)
{
}
private void Pos2_Click(object sender, EventArgs e)
{
// Gripper
// A B C D E F
byte[] M1bytesToSend = new byte[8] { 160, 30, 10, 20, 30, 24, 0x91,
0xCA };
serialPort1.Write(M1bytesToSend, 0, 8);
}
private void Pos3_Click(object sender, EventArgs e)
{
// Gripper
// A B C D E F
byte[] M1bytesToSend = new byte[8] { 160, 10, 10, 20, 30, 24, 0x91,
0xCA };
serialPort1.Write(M1bytesToSend, 0, 8);
}
private void Pos4_Click(object sender, EventArgs e)
{
// Gripper
// A B C D E F
byte[] M1bytesToSend = new byte[8] { 160, 10, 30, 60, 40, 24, 0x91,
0xCA };
serialPort1.Write(M1bytesToSend, 0, 8);
}
private void Pos5_Click(object sender, EventArgs e)
{
// Gripper
// A B C D E F
byte[] M1bytesToSend = new byte[8] { 160, 60, 30, 60, 40, 24, 0x91,
0xCA };
serialPort1.Write(M1bytesToSend, 0, 8);
}
private void Pos6_Click(object sender, EventArgs e)
{
// Gripper
// A B C D E F
byte[] M1bytesToSend = new byte[8] { 90, 60, 30, 60, 40, 24, 0x91,
0xCA };
serialPort1.Write(M1bytesToSend, 0, 8);
}
private void Pos7_Click(object sender, EventArgs e)
{
// Gripper
// A B C D E F
byte[] M1bytesToSend = new byte[8] { 90, 100, 60, 100, 40, 80, 0x91,
0xCA };
serialPort1.Write(M1bytesToSend, 0, 8);
}
private void Pos8_Click(object sender, EventArgs e)
{
// Gripper
// A B C D E F
byte[] M1bytesToSend = new byte[8] { 95, 90, 130, 100, 40, 80, 0x91,
0xCA };
serialPort1.Write(M1bytesToSend, 0, 8);
}
private void ScrollA_Scroll(object sender, ScrollEventArgs e)
{
ServoA.Text = ScrollA.Value.ToString();
}
private void ScrollB_Scroll(object sender, ScrollEventArgs e)
{
ServoB.Text = ScrollB.Value.ToString();
}
private void ScrollC_Scroll(object sender, ScrollEventArgs e)
{
ServoC.Text = ScrollC.Value.ToString();
}
private void ScrollD_Scroll(object sender, ScrollEventArgs e)
{
ServoD.Text = ScrollD.Value.ToString();
}
private void ScrollE_Scroll(object sender, ScrollEventArgs e)
{
ServoE.Text = ScrollE.Value.ToString();
}
private void ScrollF_Scroll(object sender, ScrollEventArgs e)
{
ServoF.Text = ScrollF.Value.ToString();
}
private void Send_Click(object sender, EventArgs e)
{
A = Convert.ToByte(ServoA.Text);
B = Convert.ToByte(ServoB.Text);
C = Convert.ToByte(ServoC.Text);
D = Convert.ToByte(ServoD.Text);
E = Convert.ToByte(ServoE.Text);
F = Convert.ToByte(ServoF.Text);
// A B C D E
//byte[] M1bytesToSend = new byte[8] { 0x4A, 0x14, 0x0A, 0x3C, 0x64,
0x4B, 0x91, 0xCA };
byte[] M1bytesToSend = new byte[8] { A, B, C, D, E, F, 0x91, 0xCA };
serialPort1.Write(M1bytesToSend, 0, 8);
}
private void serialPort1_DataReceived(object sender,
SerialDataReceivedEventArgs e)
{
Thread.Sleep(50);
int i;
int bytes = serialPort1.BytesToRead;
byte[] buffer = new byte[bytes];
string[] hex = new string[bytes];
serialPort1.Read(buffer, 0, bytes);
RxString = ByteArrayToHexString(buffer);
for (i = 0; i < bytes; ++i)
{
hex[i] = buffer[i].ToString("X");
}
textBox2.Text = RxString;
textBox3.Text = bytes.ToString();
if (bytes == 9)
{
c1 = (hex[0]).PadLeft(2, '0');
c2 = (hex[1]).PadLeft(2, '0');
c3 = (hex[2]).PadLeft(2, '0');
c4 = (hex[3]).PadLeft(2, '0');
c5 = (hex[4]).PadLeft(2, '0');
c6 = (hex[5]).PadLeft(2, '0');
c7 = (hex[6]).PadLeft(2, '0');
c8 = (hex[7]).PadLeft(2, '0');
c9 = (hex[8]).PadLeft(2, '0');
fourbytesHexStr = string.Concat(c1, c2, c3, c4, c5, c6, c7, c8,
c9);
fourbytesHexStr = fourbytesHexStr.PadRight(8, '0');
textBox1.Text = fourbytesHexStr;
textBox3.Text = bytes.ToString();
serialPort1.DiscardInBuffer();
}
if (bytes == 8)
{
c1 = (hex[0]).PadLeft(2, '0');
c2 = (hex[1]).PadLeft(2, '0');
c3 = (hex[2]).PadLeft(2, '0');
c4 = (hex[3]).PadLeft(2, '0');
c5 = (hex[4]).PadLeft(2, '0');
c6 = (hex[5]).PadLeft(2, '0');
c7 = (hex[6]).PadLeft(2, '0');
c8 = (hex[7]).PadLeft(2, '0');
fourbytesHexStr = string.Concat(c1, c2, c3, c4, c5, c6, c7, c8);
fourbytesHexStr = fourbytesHexStr.PadRight(8, '0');
textBox1.Text = fourbytesHexStr;
textBox3.Text = bytes.ToString();
serialPort1.DiscardInBuffer();
}
if (bytes == 7)
{
c1 = (hex[0]).PadLeft(2, '0');
c2 = (hex[1]).PadLeft(2, '0');
c3 = (hex[2]).PadLeft(2, '0');
c4 = (hex[3]).PadLeft(2, '0');
c5 = (hex[4]).PadLeft(2, '0');
c6 = (hex[5]).PadLeft(2, '0');
c7 = (hex[6]).PadLeft(2, '0');
fourbytesHexStr = string.Concat(c1, c2, c3, c4, c5, c6, c7);
fourbytesHexStr = fourbytesHexStr.PadRight(8, '0');
textBox1.Text = fourbytesHexStr;
textBox3.Text = bytes.ToString();
serialPort1.DiscardInBuffer();
}
}
private string ByteArrayToHexString(byte[] buffer)
{
StringBuilder sb = new StringBuilder(buffer.Length * 3);
foreach (byte b in buffer)
sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, '
'));
return sb.ToString().ToUpper();
}
private void Pos1_Click(object sender, EventArgs e)
{
// Gripper
// A B C D E F
byte[] M1bytesToSend = new byte[8] { 115, 95, 80, 21, 31, 2, 0x91,
0xCA };
serialPort1.Write(M1bytesToSend, 0, 8);
}
private void Open_Click(object sender, EventArgs e)
{
serialPort1.PortName = "COM6";
serialPort1.BaudRate = 9600;
serialPort1.DataBits = 8;
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;
serialPort1.Open();
if (serialPort1.IsOpen) PortStatus.Text = "PORT Opened...Click to
Start DAQ";
Close.Enabled = true;
Open.Enabled = false;
}
public Form1()
{
InitializeComponent();
whichdo = 100;
}
private void Animate1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void Do1()
{
byte[] M1bytesToSend = new byte[8] { 115, 95, 80, 21, 31, 2, 0x91,
0xCA };
serialPort1.Write(M1bytesToSend, 0, 8);
}
private void Do2()
{
byte[] M2bytesToSend = new byte[8] { 160, 30, 10, 20, 30, 24, 0x91,
0xCA };
serialPort1.Write(M2bytesToSend, 0, 8);
}
private void Do3()
{
byte[] M3bytesToSend = new byte[8] { 160, 10, 10, 20, 30, 24, 0x91,
0xCA };
serialPort1.Write(M3bytesToSend, 0, 8);
}
private void Do4()
{
byte[] M4bytesToSend = new byte[8] { 160, 10, 30, 60, 40, 24, 0x91,
0xCA };
serialPort1.Write(M4bytesToSend, 0, 8);
}
private void Do5()
{
byte[] M5bytesToSend = new byte[8] { 160, 60, 30, 60, 40, 24, 0x91,
0xCA };
serialPort1.Write(M5bytesToSend, 0, 8);
}
private void Do6()
{
byte[] M6bytesToSend = new byte[8] { 90, 60, 30, 60, 40, 24, 0x91,
0xCA };
serialPort1.Write(M6bytesToSend, 0, 8);
}
private void Do7()
{
byte[] M7bytesToSend = new byte[8] { 90, 100, 60, 100, 40, 80, 0x91,
0xCA };
serialPort1.Write(M7bytesToSend, 0, 8);
}
private void Do8()
{
byte[] M8bytesToSend = new byte[8] { 95, 90, 130, 100, 40, 80, 0x91,
0xCA };
serialPort1.Write(M8bytesToSend, 0, 8);
}
}
}