使用鼠标arduino通过pc控制两个伺服器

时间:2016-02-13 18:44:31

标签: c# c++ arduino

我想通过PC控制两个伺服器,使用鼠标x-y坐标。光标的x-y坐标发送到串行端口:

对于arduino部分,我得到6个字符串并将其分成2部分。然后将这些部分转换为整数值并发送到arduino引脚以设置伺服位置:

 #include <Servo.h> 
    String readString, servo1, servo2;
    Servo myservo1;  // create servo object to control a servo 
    Servo myservo2;

    void setup() {
    Serial.begin(9600);
    myservo1.attach(6);  //the pin for the servo control 
    myservo2.attach(7);
    }

    void loop() {}

    void serialEvent() {
    while (Serial.available()) {
    delay(2);  
    if (Serial.available() >0) {
     char c = Serial.read();  //gets one byte from serial buffer
     readString += c; //makes the string readString
    } 
    }

    if (readString.length() >0) {
     Serial.println(readString); //see what was received

     // expect a string like 07002100 containing the two servo positions      
     servo1 = readString.substring(0, 3); //get the first three characters
     servo2 = readString.substring(3, 6); //get the next three characters 

     Serial.println(servo1);  //print ot serial monitor to see results
     Serial.println(servo2);

     int n1; //declare as number  
     int n2;

     char carray1[6]; //magic needed to convert string to a number 
     servo1.toCharArray(carray1, sizeof(carray1));
     n1 = atoi(carray1); 

     char carray2[6];
     servo2.toCharArray(carray2, sizeof(carray2));
     n2 = atoi(carray2); 

     myservo1.write(n1); //set servo position 
     myservo2.write(n2);
   readString="";
 } 
}

但是,代码非常慢。我需要非常缓慢地移动鼠标以使伺服器移动。从50度到170度的瞬间移动需要1秒才能移动伺服。在这种情况下,你能提供更好的选择来控制两个伺服系统吗?

只控制一个伺服系统,它可以立即移动伺服,没有任何滞后:

#include <Servo.h>

Servo x;

int xval;
void setup() {
  Serial.begin(9600);
  x.attach(9);
}

void loop() {

}
void serialEvent() {
  xval = Serial.parseInt();
  if(xval!=0) {
    x.write(xval);
  }
}

C#中的代码:

public partial class Form1 : Form
    {
        SerialPort port;
        public Form1()
        {
            InitializeComponent();
            init();
        }
        private void init()
        {
            port = new SerialPort();
            port.PortName = "COM1";
            port.BaudRate = 9600;
            //porty = new SerialPort();

            try
            {
                port.Open();
            }
            catch (Exception e1)
            {

                MessageBox.Show(e1.Message);
            }
        }
        int x = 0, y = 0;
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            Graphics g = CreateGraphics();
            Pen p = new Pen(Color.Navy);
            Pen erase = new Pen(Color.White);
            x = e.X; y = e.Y;
            double curWidth = this.Width / 180;
            double x2 = Math.Round(x / curWidth);

            double curHeight = this.Height / 180;
            double y2 = Math.Round(y / curHeight);

            label1.Text = x2.ToString(); label2.Text = y2.ToString();

            string valx = x2.ToString();
            string valy = y2.ToString();
            while(valx.Length < 3)
            {
                valx = '0' + valx;
            }
            while(valy.Length < 3)
            {
                valy = '0' + valy;
            }
            string valsum = valx+valy;
            label3.Text = valsum.ToString();
            if (port.IsOpen)
            {
                port.WriteLine(valsum);
            }
        }
    }

在上面的代码中,我采用x和y坐标并将其转换为大约180范围。将值连接到一个字符串后,通过串口发送它。

1 个答案:

答案 0 :(得分:0)

为了提高代码的性能,您可以消除延迟并使用最大波特率(115200)。为了简单起见,我在下面压缩了你的Arduino代码:

#include <Servo.h> 

// create 2 servos x and y
Servo x;   
Servo y;

void setup() {
    Serial.begin(115200);
    x.attach(6);   
    y.attach(7);
}

void loop() {
    //empty
}

void serialEvent() {
   //set servo position.
   if (Serial.available()>0) { 
       //Receives 2 angles separated per a comma
       x.write(Serial.parseInt()); //Returns first angle
       y.write(Serial.parseInt()); //Returns second angle
       Serial.read(); //Read the '\n'. I am not sure if it is necessary.
   } 
}

在C#源代码中也提高波特率:

port.BaudRate = 115200;   

另外,我建议你通过串口发送用逗号分隔的角度。所以,而不是:

while(valx.Length < 3) {
    valx = '0' + valx;
}
while(valy.Length < 3) {
    valy = '0' + valy;
}
string valsum = valx+valy;
label3.Text = valsum.ToString();
if (port.IsOpen) {
    port.WriteLine(valsum);
}     

替换为:

if (port.IsOpen) {
    port.WriteLine(valx + ',' + valy); //Note that WriteLine appends a '\n' character in the end.
}