[英]Attempting to Real Time Plot sensor data from an Arduino
我正在一個項目中,我正在使用雙伺服和一個聲納hc-sr04來映射事物。 我仍處於起步階段,但正在處理一些簡單的事情,例如映射一個盒子。 我目前正在獲取我有(R,theta,phi)的arduino串行數據,並將其轉換為(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端口有所了解,並做了一些場景,但是沒有一個是實時的。 任何幫助,將不勝感激。
一些可能會幫助您的事情。
在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發送的串行數據。 您可以在此處找到一些信息。
您的目標必須是將我提到的兩個部分集成到與您先前的代碼一起使用的代碼中。
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.