我正在开展一个项目,我正在使用双伺服和一个声纳hc-sr04来映射事物。我还处于起步阶段,但我正在处理一些简单的事情,比如绘制一个盒子。我目前正在使用arduino串行数据(R,theta,phi)并将其转换为(x,y,z),然后在matlab上绘图。完整扫描后,我会将txt文件上传到matlab并运行它。我希望这是实时的。 这是我的arduino代码
#include <Servo.h>
#include <NewPing.h>
Servo myservo,myservo2; // create servo object to control a servo
#define TRIGGER_PIN 7
#define ECHO_PIN 2
#define MAX_DISTANCE 200
NewPing s1(TRIGGER_PIN,ECHO_PIN,MAX_DISTANCE);
double smoothedValue1,smoothedValue2;
float filterValue;
const int numReadings = 100;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int myCounter = 0;
int Counter = 0;
int upper=2;
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(12); // attaches the servo on pin 9 to the servo object
myservo2.attach(11);
myservo.write(pos);
myservo2.write(pos);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
Serial.begin(9600);
}
void loop() {
if(myCounter<182/upper)
{
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
total = total - readings[readIndex];
int Input1=s1.ping_cm();
if (Input1 == 0 || Input1 >100)
{
Input1=100;
}
readings[readIndex] = Input1;
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
filterValue=.2;
average = total / numReadings;
smoothedValue1 = (average * (1 - filterValue)) + (smoothedValue1 * filterValue);
smoothedValue2 = (smoothedValue1 * (1 - filterValue)) + (smoothedValue2 * filterValue);
// send it to the computer as ASCII digits
// send it to the computer as ASCII digits
Serial.print(smoothedValue2);
Serial.print("\t");
Serial.print(pos);
Serial.print('\t');
Serial.println(Counter);
delay (30);
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
total = total - readings[readIndex];
int Input1=s1.ping_cm();
if (Input1 == 0 || Input1 >100)
{
Input1=100;
}
readings[readIndex] = Input1;
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
average = total / numReadings;
filterValue=.2;
smoothedValue1 = (average * (1 - filterValue)) + (smoothedValue1 * filterValue);
smoothedValue2 = (smoothedValue1 * (1 - filterValue)) + (smoothedValue2 * filterValue);
// send it to the computer as ASCII digits
Serial.print(smoothedValue2);
Serial.print("\t");
Serial.print(pos);
Serial.print('\t');
Serial.println(Counter);
delay (30);
}
myCounter = myCounter + 1;
Counter=Counter+upper;
myservo2.write(Counter);
}
}
这是我的代码。我的Matlab代码目前是
clc;clear;
[A,B,C]=textread('CoolTerm Capture 2016-08-01 13-49-13.asc','','headerlines',6);
[A];
[X]=A.*sind(B).*cosd(C);
[Y]=A.*sind(B).*sind(C);
[Z]=A.*cosd(B);
scatter3(X,Z,Y,3)
我对阅读COM端口知之甚少,并且做了一些场景,但没有一个是实时的。任何帮助,将不胜感激。
答案 0 :(得分:0)
有些事情可能会对你有所帮助。
在Matlab中,你可以使用hold all
在同一个图中绘图,这样你就可以为你的情节添加点或曲线等等。这个简短的例程说明了我所说的:
plot(0,0, '*r');
xlim([0 100]);
ylim([0 100]);
hold all
for x=1:100
plot(x,x,'*r');
pause(1)
end
另一方面,对于与arduino的通信,您可以使用Matlab中提供的串口通信,并直接在Matlab中读取Arduino发送的串行数据。您可以找到一些信息here。
您的目标必须是将我提到的两个部分集成到一个与您之前的代码一起使用的代码中。
希望它有所帮助。