Ich arbeite an einem Projekt, wo ich ein Dual-Servo und ein Sonar hc-sr04 verwenden, um Dinge abzubilden. Ich bin immer noch am Anfang, arbeite aber mit einfachen Dingen wie dem Mappen einer Box. Ich nehme gerade die arduino seriellen Daten, wo ich habe (R, Theta, Phi) und das zu (x, y, z) und dann auf Matlab planend. Nach einem vollständigen Scan würde ich die TXT-Datei nach Matlab hochladen und ausführen. Ich möchte, dass es in Echtzeit ist. Das ist mein Arduino CodeVersuch, Real Time Plot Sensordaten von einem 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);
}
}
Dies ist mein Code. Mein Matlab-Code ist derzeit
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)
Ich weiß ein wenig über COM-Ports zu lesen und haben ein paar Szenarien gemacht, aber keiner von ihnen in Echtzeit gewesen. Jede Hilfe wäre willkommen.