arduino串行而不是解析命令

Arduino Serial not parsing commands

本文关键字:命令 arduino      更新时间:2023-10-16

我正在使用processing(java(通过串行通信与控制两个伺服器和一个激光的arduino。一切都独立起作用,但是当我打开激光器时,使用处理时,我无法控制伺服器,直到将激光退回。故障排除我已经尝试过:

  1. 使用Arduino串行或油灰会导致Arduino按预期工作(伺服控制是独立于激光状态(
  2. 将Arduino放在串行桥配置中以监视什么串行命令处理正在发送到Arduino,它是预期(电机位置,激光状态(
  3. 我正在使用外部电源,所以我知道它不是Max-Current-Draw问题。(而且它有效
  4. 包括处理的延迟,冲洗Arduino Buffer。
  5. 使用了所有" 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,-1",以便所有行上有两个数字
  2. 更好,为您的行创建一个更好的结构格式:从一封信开始,说是伺服或激光命令,然后阅读所需的内容,然后确保找到" n",然后重复。