Hello,
I wanted to create a LiDar/tof Scanner using the Arduino, a stepper-motor for the horizontal rotation and a servo for the vertical rotation. I'm using the VL53L0X(From Adafruit, with the VL53L0X library from Adafruit) as the tof sensor, the Pololu A4988 as the Stepper driver. However, as I wanted to get the first measurements done, I noticed, that the stepper isn't rotating as intended an it seemed to skip some steps and it is faster for a few steps only to slow down for the following steps. The steps were taken one at a time 100 times(1.8° per step), and after each step the measurement was taken, however, instead of doing a 180° rotation the stepper did around 45° but wasn't precise anymore. I commented the out the measurement function and the motor did turn perfectly 180°. After I tried it with Steppers and failed, I tried using servos, however, now the Arduino seems to reset every time it gets to the measurement function.
Here is the script:
#include "Adafruit_VL53L0X.h"
Adafruit_VL53L0X tof = Adafruit_VL53L0X();
#include <Servo.h>
Servo myservo;
int x[181];
int y[181];
int winkel = 0;
float Winkel_rad = 0;
float Pi = 3.1415;
void setup(){
Serial.begin(9600);
Serial.println("Setup");
}
void loop(){
myservo.attach(7);
winkel = 0;
myservo.write(winkel);
for (int i=0; i < 180; i++){
myservo.write(winkel);
delay(1000);
int distance = readDistanceTimeOfFlight();
Serial.println(distance);
Winkel_rad = winkel * (Pi / 180.0);
x[i] = distance * cos(Winkel_rad);
y[i] = distance * sin(Winkel_rad);
winkel = winkel + 1;
}
Serial.print("x = [");
for (int i = 0; i < 180; ++i) {
Serial.print(x[i]);
Serial.print(", ");
}
Serial.println("]");
Serial.print("y = [");
for (int i = 0; i < 180; ++i) {
Serial.print(y[i]);
Serial.print(", ");
}
Serial.println("]");
}
float readDistanceTimeOfFlight()
{
VL53L0X_RangingMeasurementData_t measure;
tof.rangingTest(&measure, false);
if (measure.RangeStatus != 4)
return measure.RangeMilliMeter * 0.001f;
else
return 0;
}
For the Stepper motor I replaced my servo.write() with(and removed all the servo stuff and added the needed variables):
digitalWrite(dirArm, HIGH); // Dreh - Richtung
digitalWrite(stepperArm, HIGH);
delayMicroseconds(1000); // Geschwindigkeit
digitalWrite(stepperArm, LOW);
delayMicroseconds(1000);