The sonar sensor of the NXT can measure the distance to objects. So this sensor can make a map of the surroundings of the brick.
To make a map the sonar needs to be turned around 360 degrees, while measuring the distance of the objects. Because the sensor needs to be attached with the cord, this turning is most easily done by rotating the whole brick, with its wheels. These wheels can provide a measure for the angle rotated, if you don't have a compass sensor.
Then the data need to be analyzed. Writing a file from a C++ prog (with bluetooth conenction to the NXT, steering it around its axis too) is really easy:
add some headers:
#include
ofstream out1;
out1.open("result.txt",ios::app);
//in the while loop:
out1 << motorD << "***"<< sonarD << "***"<< endl;
//writing "***" between the values, to get a nice split of the string into an array
//and close it after stopping the measurements:
out1.close();
Then you have a textfile, how to unwrap the data into a map? For the moment I used FLASH CS3 for this. It can be done with OpenGL too. In the FLASH the data are loaded and the string is split into an array using the "a"'a between the values. Then the angle (coming from the motor sensor) is used to put the dots (from the sonar) at the indicated distance from the NXT, using a bit of mathematic, sine and cosine: abstract: (can be used like this everywhere) centre: 300,200 ff: scale factor for the radius = distance from the centre results: array containing angle1, distance1, angle2, distance2, etc Math.PI/180 : needed to get from degrees to radians so a point becomes: (300 - ff* results[i+1]*Math.sin(-results[i] * Math.PI/180 ), 200 + - ff* results[i+1]*Math.cos(results[i] * Math.PI/180 )) Actually this is going from rectangular to "polar" coordinates, turning a rectangle around a point. There is a filter of this in Photoshop, quite fun to play with it, in 17th century art, painters were doing this trick making paintings which could only be seen through a circular mirror wrapped around a stick! They called this anamorphoses...

In the picture you see the result: the test environment and the dots.
You can observe that the sonar sees slightly "bigger" objects then the objects we see. Also the rounded object can pose a problem, the sonar works with signals reflected from the object, so how does he see this circle? The NXT was going around several times before this image was built.
Some more experiments are needed with different materials and shapes to get a real notion of his viewing capabilities.
The next step is to steer to another position, make a second map, and trying to relate these two maps.
work in progress!
//-----------------------------------------------------------------------------------------------------------------------------------------------------
When I found the fantom Lib possibilities the map could be made immediately using the bluetooth JAVA connection.
The I2C sensors and the DIRECT COMMANDS cuased some problems but in the end it worked, using the compass and the sonar sensor.
The compass sensor has to be very horizontal, otherwise the degrees do not cover the circle evenly, as can be seen in the picture in the fantom lib blog.
0 comments:
Post a Comment