Main Page   Hardware Class Hierarchy   Hardware API     Mapping Class Hierarchy  Mapping API 

oSonarDV Class Reference

Controls a Devantech SRF04 Ranger sonar module. More...

#include <osonar.h>


Public Member Functions

 oSonarDV (IOLINE channel, PORT port, IOLINE itpu)
 TPU channel / Control channel.

void ping (void)
 Activates the sonar sounding mechanism.

INT_MILLIMETER value (void)
 Returns last distance returned from sonar in millimeters. returns 0 if no sounding.


Detailed Description

Controls a Devantech SRF04 Ranger sonar module.

Operation
The oSonarDV uses the TPU programmable time accummulation (PTA) function to measure the time of sounding. The ping function will actually sit idle on the CPU and wait for the value to be calculated. Thus, you should expect this function to take about 10 or so milliseconds.
Note:
Since it returns millimeters, the range is about 30-3000.
There are essentially two ways one can implement the sonar. The first is to sound the sonar and just (loop) until you know its done (the way I choose). The second is to sound the sonar, and have a background routine check status periodically; unfortunately, there are two wait periods to account for. The first is the period that the control pin must be high before being lowered again. The second is of course waiting for the echo to return. I was tempted to use the background method, but it turns out it becomes complicated, and has other problems. Most importantly, I'm not convinced the time gained is worth the complication needed in the calling program to ensure the sonar has completed.
Remarks
Sound travels 1 inch every 73.746us (at 70F sea level) or 1mm every 2.9us. if I read it right, assuming an MRM using 16MHz, each system clock is 62.5 NANOSECONDS. We multiply "number of clocks" by 62.5 to obtain length of each TCR1 "tick". For instance, default for OOMRM is 32, thus 2000 ns (2us) each TCR1 "tick". Thus, multiply final PTA "ticks" by 2us and divide by 73.746 to obtain distance in inches. One final problem before we get the equation; we must obtain the relationship to the system clock. I'm using the 16MHz clock as baseline with 62.5 ns per "number of clocks", at 8MHz each clock is twice as long etc. thus clocks is 62.5ns*(16/clock_speed). and last, but not least, you divide the mess by 2 for no particular reason than it seems bloody necessary. If anyone can find my (or documentation) error please let me know. The final equation is:
 D= (T*62.5*(16/clock_speed)*TCR1)/(73746*2) (nanoseconds)
  where:
  D is distance in inches
  T is PTA count of TCR1 "ticks"
  clock_speed is 8,16,or 25
  TCR1 is "number of clocks" per TPU Ref. 2-2 (TCR1 divisor). Ref: OOMRM TPU TCR1 definition).
Sample Code
#include <osonar.h>
oSonarDV sonar(5,PORT_E,0);     // Port E channel 5, TPU port channel 0.
oGreenLED green;
int main()
{
  sonar.ping();
  if (sonar.value() > 50)
  {
    green.on();
  }
  exit(0);
}


Constructor & Destructor Documentation

oSonarDV IOLINE  channel,
PORT  port,
IOLINE  itpu
 

TPU channel / Control channel.

Parameters:
channel Channel for the pulse trigger input.
port Port for the pulse trigger input. (TPU not supported)
itpu Channel for the echo output on the TPU port.


The documentation for this class was generated from the following file:
Generated on Mon Oct 8 19:32:45 2007 for OOMRM Hardware API by doxygen1.3