It uses a Benewake TF Mini Plus Lidar module, a nema 8, a nema 17, two DRV8825s, a LM393 Infrared motor sensor, and a limit switch to generate 3d point cloud models, when I go to verify it, it gives me a whole bunch of errors
#include <AccelStepper.h>
#include <SoftwareSerial.h>
// Define stepper motor connections and parameters
#define DIR_PIN_TILT 6 // Direction pin for NEMA 8 tilt axis
#define STEP_PIN_TILT 7 // Step pin for NEMA 8 tilt axis
#define DIR_PIN_PAN 8 // Direction pin for NEMA 17 pan axis
#define STEP_PIN_PAN 9 // Step pin for NEMA 17 pan axis
#define LIMIT_SWITCH_PIN 10 // Pin for the limit switch
#define IR_SENSOR_PIN A0 // Pin for the LM393 Infrared sensor
#define STEPS_PER_REV_PAN 600 // Steps per revolution for NEMA 17 pan axis
#define RPM_TILT 60 // RPM for NEMA 8 tilt axis
// Define variables for stepper motors
AccelStepper stepperTilt(AccelStepper::DRIVER, STEP_PIN_TILT, DIR_PIN_TILT);
AccelStepper stepperPan(AccelStepper::DRIVER, STEP_PIN_PAN, DIR_PIN_PAN);
// Define software serial port for TF mini Plus LiDAR
SoftwareSerial lidarSerial(2, 3); // RX, TX
// Variables for LiDAR measurements
int dist; //actual distance measurements of LiDAR
int strength; //signal strength of LiDAR
int check; //save check value
int i;
int uart[9]; //save data measured by LiDAR
const int HEADER=0x59; //frame header of data package
// Variables for limit switch and IR sensor
bool panLimitSwitchState = false;
bool tiltIRSenssorState = false;
bool panHomeReached = false;
void setup() {
Serial.begin(9600); // Initialize serial port for debugging
lidarSerial.begin(115200); // Initialize serial port for TF mini Plus LiDAR
// Prompt user to enter "Run" to start LiDAR scan
Serial.println("Enter 'Run' to initiate LiDAR scan:");
}
void loop() {
// Check for user input
if (Serial.available() > 0) {
// Read user input
String userInput = Serial.readStringUntil('\n');
// Check if user input matches "Run"
if (userInput.trim() == "Run") {
// Set up motor parameters
stepperTilt.setMaxSpeed(RPM_TILT * STEPS_PER_REV_PAN / 60); // Convert RPM to steps per second
stepperTilt.setAcceleration(1000); // Set acceleration for smooth movement
stepperPan.setMaxSpeed(STEPS_PER_REV_PAN); // Max speed for NEMA 17 pan axis
stepperPan.setAcceleration(1000); // Set acceleration for smooth movement
// Initialize limit switch and IR sensor pins
pinMode(LIMIT_SWITCH_PIN, INPUT_PULLUP); // Set pin for limit switch as input with internal pull-up resistor
pinMode(IR_SENSOR_PIN, INPUT); // Set pin for IR sensor as input
// Homing procedure for pan axis
Serial.println("Homing the pan axis...");
while (!panHomeReached) {
stepperPan.runSpeed();
if (digitalRead(LIMIT_SWITCH_PIN) == LOW) {
stepperPan.setCurrentPosition(0);
panHomeReached = true;
}
}
stepperPan.stop();
Serial.println("Pan axis homed.");
// Homing procedure for tilt axis
Serial.println("Homing the tilt axis...");
while (digitalRead(IR_SENSOR_PIN) == HIGH) {
stepperTilt.runSpeed();
}
stepperTilt.stop();
stepperTilt.setCurrentPosition(0);
Serial.println("Tilt axis homed.");
// Break out of the input loop
break;
} else {
// Prompt the user again for valid input
Serial.println("Invalid input. Please enter 'Run' to initiate LiDAR scan:");
}
}
// Read data from LiDAR
if (lidarSerial.available()) { // Check if serial port has data input
if(lidarSerial.read() == HEADER) { // Assess data package frame header 0x59
uart[0]=HEADER;
if (lidarSerial.read() == HEADER) { // Assess data package frame header 0x59
uart[1] = HEADER;
for (i = 2; i < 9; i++) { // Save data in array
uart[i] = lidarSerial.read();
}
check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];
if (uart[8] == (check & 0xff)) { // Verify the received data as per protocol
dist = uart[2] + uart[3] * 256; // Calculate distance value
strength = uart[4] + uart[5] * 256; // Calculate signal strength value
Serial.print("Distance = ");
Serial.print(dist); // Output measured distance value of LiDAR
Serial.print('\t');
Serial.print("Signal Strength = ");
Serial.println(strength); // Output signal strength value
}
}
}
}
// Check if limit switch for Pan axis is pressed
panLimitSwitchState = digitalRead(LIMIT_SWITCH_PIN);
if (panLimitSwitchState == LOW) {
// Reset the position of the pan stepper motor to 0
stepperPan.setCurrentPosition(0);
panHomeReached = true;
}
// Check if IR sensor for Tilt axis detects an obstacle
tiltIRSenssorState = digitalRead(IR_SENSOR_PIN);
if (tiltIRSenssorState == HIGH) {
// Reset the position of the tilt stepper motor to 0
stepperTilt.setCurrentPosition(0);
}
// Move the tilt motor one step
stepperTilt.runSpeed();
// Move the pan motor one step when the tilt motor completes one revolution
if (stepperTilt.distanceToGo() == 0) {
if (!panHomeReached) {
stepperPan.runSpeed();
}
}
// Check if the pan motor has rotated 180 degrees
if (stepperPan.currentPosition() >= STEPS_PER_REV_PAN * 180 / 360) {
// Stop the pan motor
stepperPan.stop();
// Move the pan motor back to the home position
stepperPan.moveTo(0);
panHomeReached = false;
}
// Output angles of stepper motors to serial port
float tiltAngle = stepperTilt.currentPosition() * 360.0 / STEPS_PER_REV_PAN; // Calculate tilt angle
float panAngle = stepperPan.currentPosition() * 360.0 / STEPS_PER_REV_PAN; // Calculate pan angle
Serial.print("Tilt Angle: ");
Serial.print(tiltAngle);
Serial.print(" degrees, Pan Angle: ");
Serial.print(panAngle);
Serial.println(" degrees");
// If the pan motor has returned to the home position, restate the previous serial message
if (panHomeReached && stepperPan.distanceToGo() == 0) {
Serial.println("Enter 'Run' to initiate LiDAR scan:");
panHomeReached = false;
}
}