我正在开发一个项目,该项目使用处理从文件中读取数据,然后使用serial将这些数据发送到arduino Uno板,然后将这些值提供给arduino braccio Braccio.ServoMovment()函数以移动机械臂根据他们。然而,无论何时我运行代码,我都会在函数中得到错误的值,并且手臂以奇怪的方式移动,我已经测试了相同的代码并试图点亮一个led,并且由于某种原因它运行良好。
import processing.serial.*;
import java.io.*;
int mySwitch=0;
int counter=0;
String [] subtext;
Serial myPort;
void setup(){
//Create a switch that will control the frequency of text file reads.
//When mySwitch=1, the program is setup to read the text file.
//This is turned off when mySwitch = 0
mySwitch=1;
//Open the serial port for communication with the Arduino
//Make sure the COM port is correct
myPort = new Serial(this, "COM5", 9600);
myPort.bufferUntil('\n');
}
void draw() {
// print(" the switch " + mySwitch);
if (mySwitch>0){
/*The readData function can be found later in the code.
This is the call to read a CSV file on the computer hard-drive. */
readData("C:/Users/Tareq/Desktop/data.txt");
/*The following switch prevents continuous reading of the text file, until
we are ready to read the file again. */
mySwitch=0;
}
/*Only send new data. This IF statement will allow new data to be sent to
the arduino. */
if(counter<subtext.length){
/* Write the next number to the Serial port and send it to the Arduino
There will be a delay of half a second before the command is
sent to turn the LED off : myPort.write('0'); */
// print(subtext[counter]);
delay(5000);
myPort.write(subtext[counter]);
print(subtext[counter]);
delay(2000);
//Increment the counter so that the next number is sent to the arduino.
counter++;
} else{
//If the text file has run out of numbers, then read the text file again in 5 seconds.
delay(5000);
mySwitch=1;
}
}
/* The following function will read from a CSV or TXT file */
void readData(String myFileName){
File file=new File(myFileName);
BufferedReader br=null;
try{
br=new BufferedReader(new FileReader(file));
String text=null;
/* keep reading each line until you get to the end of the file */
while((text=br.readLine())!=null){
/* Spilt each line up into bits and pieces using a comma as a separator */
subtext = splitTokens(text,",");
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
try {
if (br != null){
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Arduino代码
#include <Braccio.h>
#include <Servo.h>
#include<SoftwareSerial.h>
Servo base;
Servo shoulder;
Servo elbow;
Servo wrist_rot;
Servo wrist_ver;
Servo gripper;
String stringData[6] ;
int intData[6] ;
void setup() {
Serial.begin(9600);
Braccio.begin();
pinMode(1, OUTPUT); // Set pin as OUTPUT
pinMode(2, OUTPUT); // Set pin as OUTPUT
pinMode(3, OUTPUT); // Set pin as OUTPUT
pinMode(4, OUTPUT); // Set pin as OUTPUT
pinMode(5, OUTPUT); // Set pin as OUTPUT
pinMode(6, OUTPUT); // Set pin as OUTPUT
pinMode(7, OUTPUT); // Set pin as OUTPUT
pinMode(8, OUTPUT); // Set pin as OUTPUT
pinMode(9, OUTPUT); // Set pin as OUTPUT
}
void loop() {
for(int y=0;y<6;y++){
while(Serial.available()== 0){}
stringData[y]= Serial.readString();
if(stringData[y]=="90")
intData[y]=90;
else if(stringData[y]=="120")
intData[y]=120;
else if(stringData[y]=="180")
intData[y]=180;
else if(stringData[y]=="45")
intData[y]=45;
else if(stringData[y]=="160")
intData[y]=160;
else if(stringData[y]=="170")
intData[y]=170;
else if(stringData[y]=="165")
intData[y]=165;
else if(stringData[y]=="60")
intData[y]=60;
else if(stringData[y]=="30")
intData[y]=30;
else if(stringData[y]=="110")
intData[y]=110;
else if(stringData[y]=="80")
intData[y]=80;
else if(stringData[y]=="155")
intData[y]=155;
}
Braccio.ServoMovement(20 , intData[0], intData[1], intData[2] , intData[3] ,intData[4] , intData[5]);
}
注意:从中读取的文件是一个记事本,其中包含用“,”150,30,60之类的数字分隔的数字,用于我的代码我用6进行测试 有人可能会说它很奇怪为什么发送字符串然后将它映射到一个int而不是直接发送int,我先尝试了int然后它没有用,然后我试了这个。
答案 0 :(得分:0)
我会尝试在以下罪魁祸首之间隔离这个问题:
你能让机器人从arduino中的静态路径做可重复的事情(即硬编码arduino中的值并检查你是否知道机器人是如何被驱动的并且它正在工作)
您是否在arduino和处理之间干净地传递数据?为了测试这个,创建一些数值的对象/数组,并将它们从处理转移到arduino,并让arduino验证它是否收到了预期的结果。
您的文件是否正确无误。阅读文件并将其写入处理控制台或其他内容,以确认您拥有自己的想法。