Hola, saludos soy nueva en esta plataforma.
Estoy realizando un escáner 3D para un proyecto por medio de un sensor óptico de referencia Sharp GP2Y0A41SK0F y 2 motores paso a paso NEMA 17, sin embargo al momento de correr el programa los valores que me arroja en el monitor serie NO SON CORRECTOS ya que los verifico en el programa Meshlab que sirve para formar la nube de puntos del objeto a escanear y no me forma la imagen que necesito.
Adjunto el código por si alguien me puede ayudar con algún error que vea, de antemano muchas gracias me seria de muchísima ayuda
int scan_amount = 40; //Amaunt of scans for each point. The result is the mean. This would increase the delay for each scan.
int z_axis_height = 1200; //in cm //Maximum height of the scaned file
int step_delay = 3000; //in us //Delay for each step for the stepper motor in microseconds
float z_layer_height = 0.5; //in mm //Layer height. The amount of mm for each layer.
int lead_screw_rotations_per_cm = 8; //How many rotations needs the lead screw to make in order to make 1cm.
int steps_per_rotation_for_motor = 50; //Steps that the motor needs for a full rotation.
int distance_to_center = 9; //In cm. Distance from sensor to the turntable center in cm
//I/O
//Turntable driver pins
int dir_r = 7;
int step_r = 8;
int enable_r = 9;
//Z-axis driver pins
int dir_z = 5;
int step_z = 3;
int enable_z = 6;
int scan_changed = 0; //Scan process was changed
float distancia = 0; //Measured distance
float angle = 0; //Rotation angle for each loop (0º-360º)
float x = 0; //X, Y and Z coordinate
float y = 0;
float z = 0;
int z_loop = 0; //variable used for the z-axis motor rotation
int r_loop = 0; //variable used for the turntable motor rotation
float measured_analog = 0; //Analog read from the distance sensor
float analog = 0; //Analog MEAN
float RADIANS = 0.0; //Angle in radians. We calculate this value later in Setup loop
int steps_z_height = 0; //Variable used for the amount of steps in z-axis
void setup() {
Serial.begin(9600);
pinMode(A3, INPUT);
pinMode(dir_z, OUTPUT);
pinMode(step_z, OUTPUT);
pinMode(enable_z, OUTPUT);
pinMode(dir_r, OUTPUT);
pinMode(step_r, OUTPUT);
pinMode(enable_r, OUTPUT);
digitalWrite(enable_z,HIGH);//disable the z_azis driver
digitalWrite(enable_r,HIGH);//disable the z_azis driver
//Calculate variables
RADIANS = (3.141592 / 180.0) * (360/steps_per_rotation_for_motor);
steps_z_height = (z_layer_height * steps_per_rotation_for_motor * lead_screw_rotations_per_cm)/10;
}
void loop() {
if(z < z_axis_height)
{
for(int loop_cont = 0 ; loop_cont < steps_per_rotation_for_motor; loop_cont++)
{
//getDistance();
digitalWrite(enable_r,LOW); //enable the turntable driver
digitalWrite(dir_r,LOW); //turntable spin to right
digitalWrite(step_r,HIGH); //make a step
delay(5);
delayMicroseconds(step_delay);
digitalWrite(step_r,LOW);
delay(5);
delayMicroseconds(step_delay);
angle = angle + RADIANS;
}
angle = 0; //Reset the angle value for next rotation
/*My threaded rod needs 8 full rotations for 1cm. A full rotation is 200 steps in my case.
We need 1600 for 1cm. So, we need 80 for 0.5mm. The amount is calulated automaically.
Just change the variables at the beginning if you want*/
while(z_loop < steps_z_height)
{
digitalWrite(enable_z,LOW); //enable the z_azis driver
digitalWrite(dir_z,LOW); //z_azis spin to right
digitalWrite(step_z,HIGH); //z_azis make a step
delayMicroseconds(step_delay);
digitalWrite(step_z,LOW);
delayMicroseconds(step_delay);
z_loop = z_loop+1; //Increase the loop by 1
}
z = z + z_layer_height; //Increase the made z-height by 1 unit
z_loop = 0; //Reset the z-axis rotation variable
//}//end of if z_height
}
double getDistance();
for (int aa=0; aa < scan_amount; aa++)
{
measured_analog= analogRead(A3);
delay(2);
analog = analog + measured_analog;
}
distancia = analog/scan_amount;
analog=0;
measured_analog = 0;
distancia = map(distancia,0.0,1023.0,0.0,3.3);
distancia = -5.40274*pow(distancia,3)+28.4823*pow(distancia,2)-49.7115*distancia+31.3444;
distancia = distance_to_center - distancia;
y = (sin(angle) * distancia);
x = (cos(angle) * distancia);
Serial.print(y); Serial.print(",");
Serial.print(x); Serial.print(",");
Serial.println(z); Serial.print("");
// Serial.println(distancia);
Serial.println(angle);
// Serial.println(" cm ");
delay(500);
}
//Function that maps the value in a float format
float map(float fval, float val_in_min, float val_in_max, float val_out_min, float val_out_max)
{
return (fval - val_in_min) * (val_out_max - val_out_min) / (val_in_max - val_in_min) + val_out_min;
}


