Idea for ultrasonic sonar scanner
I am planning to mount one (or more) Maxbotix range finders on a servomotor, so that I can sweep it back and forth and map out the shape of nearby objects.
To do this, I need a program in Processing that will run on a laptop, and a small program on Arduino to take the readings and report them back.
Read more to see v1 of code for the laptop display routine.
import processing.opengl.*;
// Sonar scan v1.1
// Program to plot values as a radar-like sweep
// Will ultimately take input from rangefinder on the Arduino
//
// Gian Pablo Villamil
// October 2, 2006
// Define constants
int maxReadings = 360;
float sweepAngleIncrement = TWO_PI/maxReadings ;
int numSweeps = 90 ; // number of sweep lines to draw
color whiteColor = color(255,255,255);
// Define variables
int curReading = 0 ;
int sweepRadius ;
float curSweepAngle = 0 ;
color sweepColor ;
int[] sonarReadings = new int[maxReadings];
void setup() {
size(640,480,OPENGL);
// size(screen.width,screen.height,OPENGL);
smooth();
frameRate(120);
sweepRadius = height/2;
// initialize the array of readings
for (int i = 0; i < maxReadings; i++) {
curSweepAngle = TWO_PI * i / maxReadings;
sonarReadings[i]=getReading(curSweepAngle);
}
}
void draw() {
background(0,0,0);
translate(width/2,height/2); // center the coordinate system
drawSweep();
curReading = (curReading + 1) % maxReadings;
curSweepAngle = TWO_PI * curReading / maxReadings;
sonarReadings[curReading]=getReading(curSweepAngle);
}
// draw current sweep, and trail of fading readings
void drawSweep() {
pushMatrix(); // save coordinates
rotate(TWO_PI*curReading/maxReadings);
for (int i = curReading ; i >= curReading - numSweeps ; i = i-1) {
if (i == curReading){
strokeWeight(3);
sweepColor = whiteColor; // draw the current reading in white
}
else {
strokeWeight(2);
sweepColor = color(0,(numSweeps-(curReading-i))*(255/numSweeps),0);
}
stroke(sweepColor); // stroke color fades from green to black
// draw a line by rotating the system by an increment, drawing line
int j = i ;
if (j < 0) {
j=j+maxReadings;
}
rotate(-sweepAngleIncrement);
line(0,0,0,sonarReadings[j]);
}
popMatrix(); // restore coordinates
}
// getReading function
// returns an integer "range" value for a given angle
int getReading(float readingAngle) {
return sweepRadius;
}