Sonar Code Translation
JWatts
Posts: 3
Hi everyone,
I recently built a sonar sensor based on the specifications listed at the following site http://www.reconnsworld.com/bs2sonar.html
I’ve tested the sensor with an oscilloscope and the hardware seems to be working perfectly fine.
The site also has the code listed which should load a debug window in the basic stamp compiler, and display distance readings (in 'units') from the circuit.
However, the code seems too outdated. It looks like PBasic 2.0?
Since I’m currently running a BS2sx together with a board of education, I’ve been trying to update the code for my module using the PBasic 2.5 syntax. I’m having problems updating the subroutines and was wondering if someone could help me fix and optimise the code so that it can work together with the BS2x module.
I’ve also attached the original BS2 code along with the current code I’m working on.
I would be grateful for any help in this matter. Thanks in advance!
Post Edited (JWatts) : 4/15/2005 12:58:30 PM GMT
I recently built a sonar sensor based on the specifications listed at the following site http://www.reconnsworld.com/bs2sonar.html
I’ve tested the sensor with an oscilloscope and the hardware seems to be working perfectly fine.
The site also has the code listed which should load a debug window in the basic stamp compiler, and display distance readings (in 'units') from the circuit.
However, the code seems too outdated. It looks like PBasic 2.0?
Since I’m currently running a BS2sx together with a board of education, I’ve been trying to update the code for my module using the PBasic 2.5 syntax. I’m having problems updating the subroutines and was wondering if someone could help me fix and optimise the code so that it can work together with the BS2x module.
I’ve also attached the original BS2 code along with the current code I’m working on.
I would be grateful for any help in this matter. Thanks in advance!
Post Edited (JWatts) : 4/15/2005 12:58:30 PM GMT
Comments
In progress code:
For optimization, I would first get rid of the array and the bubble sorting routine because it's unnecessarily consuming processor time and RAM. Instead of an array and all those swap variables, declare five variables, index, sample, sum, smallest, and biggest. Sum should be a word variable. Then, keep a running sum of the samples and an ongoing record of the largest and smallest and subtract them out at the end.
Each time through the Sonar FOR...NEXT loop, your code should decide whether sample is the smallest, biggest, or somewhere in-between. The first time though the loop, copy sample to sum, biggest, and smallest. The rest of the loop iterations should work like this: If the sample is in-between the smallest and biggest, just add it to the sum variable. If it's bigger than the current biggest, also set biggest equal to sample. If it's smaller than the current smallest, set smallest equal to sample. When the loop is done, subtract biggest and smallest from sum. Then, divide by the number of samples, which is the number of loop iterations minus two (nsamp-2) and you've got your average.
The best way to develop and test this averaging algorithm is with a small program and the DEBUGIN command. This will allow you to enter values that you want to average into the Debug Terminal's transmit windowpane, and of course, use the DEBUG command to display the results in the Debug Terminal's receive windowpane.