Hallo Zusammen,
ich möchte mit einem VL53L0X das am nähesten an einem bewegten Wagen befindliche Objekt anfahren. Dazu fahre ich die vorgegebene Wegstrecke ab und nehme jede 1/4 Umdrehung des Schrittmotors eine Abstandsmessung vor.
D.h. die Bewegung ist nicht kontinuierlich. Gibt es eine einfache Möglichkeit die Messung während der Bewegung des Schrittmotor vorzunehmen?
Arduino UNO, A4988 Schrittmotortreiber und VL53L0X Time-of-Flight Lasersensor
//////////////////////////////////////////
//VL53L0X
//////////////////////////////////////////
#include <Wire.h>
#include <VL53L0X.h>
VL53L0X sensor;
#define HIGH_SPEED
//#define HIGH_ACCURACY
//////////////////////////////////////////
//Stepper
//////////////////////////////////////////
#include <AccelStepper.h>
// Define a stepper and the pins it will use
AccelStepper stepper(1,9,8); // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
//////////////////////////////////////////
//Variablendefinition
//////////////////////////////////////////
int posmatrixx[40] = {};
int posmatrixy[40] = {};
int pos = 0;
void setup() {
Serial.begin(9600);
Wire.begin();
sensor.init();
sensor.setTimeout(500);
sensor.startContinuous();
stepper.setMaxSpeed(500*16);
stepper.setAcceleration(5000);
#if defined HIGH_SPEED
// reduce timing budget to 20 ms (default is about 33 ms)
sensor.setMeasurementTimingBudget(20000);
#elif defined HIGH_ACCURACY
// increase timing budget to 200 ms
sensor.setMeasurementTimingBudget(200000);
#endif
}
void loop() {
for(int n=1; n<=40; n++){
//stepper.moveTo(200*16*n);
stepper.setSpeed(200*16);
posmatrixy[n] = sensor.readRangeSingleMillimeters();
posmatrixx[n] = stepper.currentPosition();
stepper.runToNewPosition(10*16*n);
Serial.println(posmatrixx[n]);
Serial.println(posmatrixy[n]);
}
delay(2000);
pos = getMin(posmatrixy, 40);
Serial.println("MinPos");
Serial.println(posmatrixx[pos]);
stepper.runToNewPosition(posmatrixx[pos]);
delay(2000);
}
int getMin(int* array, int size){
int minIndex = 0;
int min = array[minIndex];
for (int i=1; i<size; i++){
if (array[i] < min){
minIndex = i;
min = array[i];
}
}
return minIndex;
}