如何使用处理代码和 Arduino UNO 板控制伺服系统?

问题描述 投票:0回答:1

首先,我要声明的是,我不是代码编写者(尽管我对 LabVIEW 非常熟悉)。我的背景是激光和光学系统设计和开发。

目前,我正在尝试将伺服系统集成到用于远程大气测绘激光雷达的光学组件中。光学元件(称为 TR-Swich)对于保持“发射激光”脉冲流与反向散射“返回”光之间的对准至关重要。为了确保长期对准并补偿光学安装座热漂移和冲击漂移,连接到光学安装座俯仰和偏航执行器的伺服系统将允许我们的客户根据需要调整 TR-Swich,从而保持最佳信号返回。 由于各种限制(时间、空间、易于集成到现有硬件等),我希望使用伺服系统,它体积小且可靠,并且可以使用 Arduino Uno 板轻松控制。我已经证明它们可以很好地移动和设置执行器的位置;现在我正在尝试让 GUI 界面正常工作...希望这就是你们所有人进来的地方... 由于我是 Arduino 板代码(以及相关的处理代码)的新手,我四处窥探并发现了 hackster.io 上发布的代码(早在 2020 年)——这与我正在寻找的内容很接近。该代码是由engineerkid(他的黑客用户名)编写的。

我复制了他的 Arduino 板和处理 GUI 的代码,但我无法让它工作。我(通过黑客)联系了他,但没有收到回复。我给他发的消息是,

嗨,工程师小子,

首先,我要感谢您发布示例!它非常接近我使用伺服系统调整光学安装座的最终需要。我复制了您的 Arduino 和处理草图代码,它已经接近工作,但有些东西不太正确。

伺服系统(实际上与您在示例中使用的伺服系统相同)移动到中心位置,并且使用处理代码基于鼠标位置的显示工作得很好。不幸的是,伺服系统不响应鼠标的移动。请注意,目前我只使用一个伺服系统 - 是否需要两个伺服系统才能使代码正常工作?

在这个前提下,我删除了与其中一个舵机相关的代码,但仍然无法实现任何运动。我浏览了一些有关 Arduino 和处理代码之间的通信问题的信息,并发现了以下评论(可能相关),“

Arduino 中的串行监视器就像一个单独的终端程序,因此它和您的处理草图正在竞争与 Arduino 的相同串行连接。“我们将非常感谢您提供的任何帮助!谢谢!史蒂夫

他的Arduino代码是:

#include <Servo.h> char tiltChannel=0, panChannel=1; Servo servoTilt, servoPan; char serialChar=0; void setup() { servoTilt.attach(9); // The Tilt servo is attached to pin 9. servoPan.attach(10); // The Pan servo is attached to pin 10. servoTilt.write(90); // Initially put the servos both servoPan.write(90); // at 90 degrees. Serial.begin(57600); // Set up a serial connection for 57,600 bit/s. } void loop() { while(Serial.available() <=0); //Wait for a character on the serial port. serialChar = Serial.read(); //Copy the character from the serial port to the variable if(serialChar == tiltChannel){ //Check to see if the character is the servo ID for the tilt servo while(Serial.available() <=0); //Wait for the second command byte from the serial port. servoTilt.write(Serial.read()); //Set the tilt servo position to the value of the second command byte received on the serial port } else if(serialChar == panChannel){ //Check to see if the initial serial character was the servo ID for the pan servo. while(Serial.available() <= 0); //Wait for the second command byte from the serial port. servoPan.write(Serial.read()); //Set the pan servo position to the value of the second command byte received from the serial port. } //If the character is not the pan or tilt servo ID, it is ignored. }

他的处理代码是:

import processing.serial.*;
Serial port; // The serial port we will be using
int xpos=90; // set x servo's value to mid point (0-180)
int ypos=90; // set y servo's value to mid point (0-180)

void setup()
{
  size(360, 360);
  frameRate(100);
  String arduinoPort = Serial.list()[0];
  port = new Serial(this, arduinoPort, 57600);
}

void draw()
{
  fill(175);
  rect(0,0,360,360);
  fill(255,0,0); //rgb value so RED
  rect(180, 175, mouseX-180, 10); //xpos, ypos, width, height
  fill(0,255,0); // and GREEN
  rect(175, 180, 10, mouseY-180);
  update(mouseX, mouseY);
}

void update(int x, int y)
{
  //Calculate servo position from mouseX
  xpos= x/2;
  ypos = y/2;
  //Output the servo position ( from 0 to 180)
  port.write(xpos+"x");
  port.write(ypos+"y");
}

如果您想查看他的代码以及他设置的非常酷的示例,该页面是:

使用 Arduino 和处理进行伺服电机控制

如上所述,我通过删除平移伺服控制来简化代码,但仍然无法使用代码的倾斜部分从伺服获得任何响应。它似乎确实与通信瓶颈有关,我只是不确定需要添加哪些小代码片段来防止冲突。 顺便说一句,通信出现故障的一个迹象是 Arduino 板上的 LED 串行通信灯。在使用(或尝试使用处理 GUI)之前,我复制了已发布的用于仅使用 Arduino 代码移动伺服器的代码,并且能够将伺服器定位在任何所需的角度(从 0 到 180 度)。

我注意到,每次上传代码(这会将伺服器设置为任何选定的硬编码角度值)时,LED 会随着新代码的上传而闪烁并来回切换。现在有了 GUI,只有 1 个 LED 持续亮着……这可能不是一个好兆头……

Java 代码不会发送您的 Arduino 草图所期望的内容(反之亦然)。

arduino processing communication servo
1个回答
0
投票

serialChar = Serial.read(); if(serialChar == tiltChannel){

我建议在Arduino端进行更改

char tiltChannel='x', panChannel='y';

并将Java端修改为

port.write("x");
port.write(xpos);

其他舵机也一样。

并确保每个 Java 
port.write

仅发送一个字节。


© www.soinside.com 2019 - 2024. All rights reserved.