Source Code: Produce graphical 3D Scan from Arduino/Servos in MATLAB
1: //PLOTOUTPUT.M
2: plotOutput(output, max_depth)
3: // plotOutput takes the serial outputs of the servo angles and distances from the Arduino, stored in a text
4: // file, and plots a 3D representation. 'output' is the name of the txt file,
5: //and max_depth is the distance of the object to the sensor
6:
7: data = csvread('output3.txt');
8:
9: tilt = data(:,1) .* (pi/180);
10: pan = data(:,2) .* (pi/180);
11: distance = data(:,3);
12:
13: for i = 1:length(distance)
14: if distance(i) > 1.5*max_depth
15: distance(i) = 0;
16: end
17: end
18:
19: [x,y,z] = sph2cart(pan, tilt, distance);
20:
21: plot3(x, y, z, '.')
22:
23: tri = delaunay(pan, tilt);
24: trisurf(tri, pan, tilt ,distance)
25:
26: xlabel('x coordinate (inches)')
27: ylabel('y coordinate (inches)')
28: zlabel('height (inches)')
29: end
1: //STUPIDOUTPUT.M
2: function stupidOutput(output, max_depth)
3: %stupidOutput is the not so smart predecesor of plotOutput. It reads the
4: %analog outputs from the scan of the pan servo to plot a 2D graph.
5:
6: data = csvread('output3.txt');
7:
8: pan = data(:,2) .* (pi/180);
9: distance = data(:,3);
10:
11: for i = 1:length(distance)
12: if distance(i) > 1.5*max_depth
13: distance(i) = 0;
14: end
15: end
16:
17: plot(pan, distance)
18:
19: xlabel('x coordinate (inches)')
20: ylabel('y coordinate (inches)')
21: end
1: // stupidSweep (Arduino)
2: #include <Servo.h>
3:
4: Servo pan_servo; // create servo object to control a servo
5:
6: const int PAN_PIN = 10;
7: const int IRpin = A0;
8:
9: int angle = 70; // variable to store the tilt servo angle
10: int pan = 35; // variable to store the pan angle
11: int IRscan; // IR Sensor variable
12:
13: void setup()
14: {
15: pan_servo.attach(PAN_PIN);
16: Serial.begin(9600);
17: }
18:
19: void loop()
20: {
21: pan_servo.write(pan); // set pan direction to zero
22: for(pan = 35; pan < 65; pan += 1) // goes from 0 degrees to 180 degrees
23: { // in steps of 1 degree
24: pan_servo.write(pan); // tell servo to go to position in variable 'pan'
25: IRscan = analogRead(IRpin);
26: Serial.print(pan);
27: Serial.print(",");
28: Serial.print(IRscan);
29: Serial.println();
30: delay(15); // waits 15ms for the servo to reach the position
31: }
32:
33: delay(1000000); //stop
34: }
1: //PAN-TILT-FULL (Arduino)
2: #include <Servo.h>
3: Servo pan_servo; // create servo object to control a servo
4: Servo tilt_servo; // create servo object to control a servo
5: // a maximum of eight servo objects can be created
6: const int PAN_PIN = 10;
7: const int TILT_PIN = 9;
8: const int IRpin = A0;
9:
10: int angle = 0; // variable to store the tilt servo angle
11: int pan = 0; // variable to store the pan angle
12: int IRscan; // IR Sensor variable
13:
14: void setup()
15: {
16: pan_servo.attach(PAN_PIN);
17: tilt_servo.attach(TILT_PIN);
18: Serial.begin(9600);
19: }
20:
21: void loop()
22: {
23: tilt_servo.write(55); // set servo angle to zero
24: pan_servo.write(35); // set pan direction to zero
25: for(pan = 35; pan < 65; pan += 1) // pans from 0 to 180 in 18 steps
26: {
27: for(angle = 55; angle < 85; angle += 1) // goes from 0 degrees to 180 degrees
28: { // in steps of 1 degree
29: tilt_servo.write(angle); // tell servo to go to position in variable 'angle'
30: IRscan = analogRead(IRpin);
31: Serial.print(pan);
32: Serial.print(",");
33: Serial.print(angle);
34: Serial.print(",");
35: Serial.print(.000000002*pow(IRscan, 4) - .000003*pow(IRscan, 3) + .0021*pow(IRscan, 2) - 0.6417*IRscan + 90.778);
36: Serial.println();
37: delay(30); // waits 15ms for the servo to reach the position
38: }
39: pan_servo.write(pan); // tell servo to go to position in variable 'pan'
40: delay(15); // waits 15ms for the servo to reach the position
41: }
42: delay(1000000); //stop
43: }
1: // Code used to calibrate; converts analog values to inches
2:
3: const int IRpin = A0;
4:
5: int IRscan; // IR Sensor variable
6:
7: void setup()
8: {
9: Serial.begin(9600);
10: }
11:
12: void loop()
13: {
14: IRscan = analogRead(IRpin);
15: Serial.print(.000000002*pow(IRscan, 4) - .000003*pow(IRscan, 3) + .0021*pow(IRscan, 2) - 0.6417*IRscan + 90.778);
16: Serial.println();
17: delay(200);
18: }