gpt4 book ai didi

c# - 使用鼠标arduino通过PC控制两个伺服系统

转载 作者:太空宇宙 更新时间:2023-11-04 13:20:46 25 4
gpt4 key购买 nike

我想通过 PC 使用鼠标 x-y 坐标控制两个 Helm 机。光标x-y坐标发送到串口:

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

 #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="";
}
}

但是,代码很慢。我需要非常缓慢地移动鼠标以使 Helm 机移动。从比方说从 50 度到 170 度的即时移动需要 1 秒来移动伺服。在这种情况下,您能否提供更好的选择来控制两个伺服系统?

只控制一个 Helm 机效果很好,它可以立即移动 Helm 机,没有任何滞后:

#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 的范围。在我将这些值连接成一个字符串以通过串行端口发送它之后。

最佳答案

为了提高代码的性能,您可以删除延迟并使用最大波特率 (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.
}

关于c# - 使用鼠标arduino通过PC控制两个伺服系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35383873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com