arduino串行而不是解析命令
Arduino Serial not parsing commands
我正在使用processing(java(通过串行通信与控制两个伺服器和一个激光的arduino。一切都独立起作用,但是当我打开激光器时,使用处理时,我无法控制伺服器,直到将激光退回。故障排除我已经尝试过:
- 使用Arduino串行或油灰会导致Arduino按预期工作(伺服控制是独立于激光状态(
- 将Arduino放在串行桥配置中以监视什么串行命令处理正在发送到Arduino,它是预期(电机位置,激光状态(
- 我正在使用外部电源,所以我知道它不是Max-Current-Draw问题。(而且它有效
- 包括处理的延迟,冲洗Arduino Buffer。
- 使用了所有" n"," r"," r n"组合。
这是我的处理程序:
import processing.serial.*;
import org.gamecontrolplus.gui.*;
import org.gamecontrolplus.*;
import net.java.games.input.*;
String ard_ser = "/dev/ttyACM0";
ControlIO control;
ControlDevice stick;
float px, py;
int deg_x, deg_y;
boolean trailOn;
Serial myPort;
ArrayList<PVector> shadows = new ArrayList<PVector>();
ArrayList<PVector> trail = new ArrayList<PVector>();
public void setup() {
size(400, 400);
myPort = new Serial(this, Serial.list()[0],9600);
printArray(Serial.list());
// Initialise the ControlIO
control = ControlIO.getInstance(this);
// Find a device that matches the configuration file
stick = control.getMatchedDevice("joystick");
if (stick == null) {
println("No suitable device configured");
System.exit(-1); // End the program NOW!
}
// Setup a function to trap events for this button
stick.getButton("LASER").plug(this, "toggleLaser", ControlIO.ON_PRESS);
}
// Poll for user input called from the draw() method.
public void getUserInput() {
px = map(stick.getSlider("X").getValue(), -1, 1, 0, width);
deg_x = int(map(stick.getSlider("X").getValue(),-1,1,0,180));
py = map(stick.getSlider("Y").getValue(), -1, 1, 0, height);
deg_y = int(map(stick.getSlider("Y").getValue(),-1,1,0,180));
//stick.getButton("LASER").plug(this,"toggleLaser",ControlIO.);
}
// Event handler for the Laser button
public void toggleLaser() {
println("laser");
if (myPort.available()>0){
myPort.write("-1n");
}
//delay(1000);
return;
}
public void draw() {
getUserInput(); // Polling
background(255, 255, 240);
// Draw shadows
fill(0, 0, 255, 32);
noStroke();
for (PVector shadow : shadows)
ellipse(shadow.x, shadow.y, shadow.z, shadow.z);
if ( trail.size() > 1) {
stroke(132, 0, 0);
for (int n = 1; n < trail.size(); n++) {
PVector v0 = trail.get(n-1);
PVector v1 = trail.get(n);
line(v0.x, v0.y, v1.x, v1.y);
v0 = v1;
}
}
// Show position
noStroke();
fill(255, 64, 64, 64);
ellipse(px, py, 20, 20);
String position = str(deg_x)+','+str(deg_y)+'n';
if (myPort.available()>0){
//println(position);
myPort.write(position);
}
delay(10);
}
它具有一些可视化的可视化来监视电动机位置,并且基本上仅发送" mot_x_pos,mot_y_pos n"和一个" -1 n"以切换激光器。示例输出串行流:
90,90n
50,50n
-1n
Arduino代码解析流并控制电动机/激光器:
#include <Servo.h>
bool laser = true;
// true sets the value high (off for my transistor)
char val = 0;
const int laser_pin = 7;
int out1 = 9; //servo pins
int out2 = 11;
boolean newData= true;
Servo servo_x;
Servo servo_y;
int pos_x =0;
int pos_y =0;
int x_prev = 90;
int y_prev = 90;
void setup() {
Serial.begin(9600);
Serial.println("<Arduino is ready>");
servo_x.attach(out1);
servo_y.attach(out2);
pinMode(laser_pin, OUTPUT);
digitalWrite(laser_pin, laser); //turn laser off on startup
}
void loop(){
while(Serial.available()>0){
pos_x = Serial.parseInt();
pos_y = Serial.parseInt();
val = Serial.read(); // this catches the newline escape characters
if ( (pos_x<0) || (pos_y<0) ){
//toggle laser
if(laser){
laser = false;
}
else {laser = true;}
digitalWrite(laser_pin, laser);
}
else if( (val == 'n') || (val == 'r') ){
if(pos_x != x_prev){ //only write to the motors if something has changed
servo_x.write(pos_x);
x_prev = pos_x;
}
if(pos_y != y_prev){
servo_y.write(pos_y);
y_prev = pos_y;
}
}
}
}
任何建议将不胜感激,谢谢。
如果您提供了不起作用的命令顺序。
您的Arduino代码试图读取两个INT,但是您仅通过激光命令发送一个。那会摆脱同步(Parseint不在乎线的末端,很高兴去下一行(
尝试
- 让您的激光按钮发送" -1,-1",以便所有行上有两个数字
- 更好,为您的行创建一个更好的结构格式:从一封信开始,说是伺服或激光命令,然后阅读所需的内容,然后确保找到" n",然后重复。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 从命令行c++发送文本文件名
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 使用QProcess执行命令,并将结果存储在QStringList中
- 数组长度,为什么从命令行获取时不能使用它?
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- clang整洁10忽略了我的NOLINT命令
- 在子目录中使用target_sources()命令时用于单元测试(qtest)的项目结构
- 如何处理linux终端中带有负号(-)的C++中的命令行参数
- 我的Arduino串行命令行不起作用,我不知道为什么
- arduino串行而不是解析命令
- Arduino:AT命令-使用serial.Read()读取串行输出的最后一行
- 将存储在SD卡上的电话号码插入短信发送AT命令Arduino
- 如何在 Arduino 中读取 AT 命令的输出
- Arduino Yún 桥:简单命令失败
- Arduino不接受多个命令
- 在Arduino上执行多个函数/命令