【发布时间】:2017-04-29 07:54:04
【问题描述】:
我正在做一个项目,该项目使用处理从文件中读取数据,然后使用串行将这些数据发送到 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,但它不起作用然后我尝试了这个。
【问题讨论】:
-
您是否验证过这些值已在 arduino 端成功传递和解析? (您可以编写一些基本的测试文件来通过 arduino 上的状态机来展示值的准确性)
标签: arduino processing robotics