The program begins by configuring various registers. The PIC is configured to use its internal RC oscillator for the clock source, the code sets its speed to 8 MHz. It configures all inputs to digital and the appropriate inputs and outputs per the table above. TIMER1 is configured to increment at a 1:1 ratio with the instruction clock, which is one quarter of the processor frequency, 8 MHz. Thus, TIMER1 increments every 0.5 µs. The RS232 UART is configured for asynchronous serial operation at 57.14 kbit/s, eight-bit data, with no parity. Figure 31 shows a flow chart that describes the microprocessor’s program. The program’s flow chart is continued on Figure 32.
The program uses a 20 to 35 ms time period as the primary building block for all activities because the RC servo must receive its positioning signal at an interval on the
place within a regular 20 to 40 ms period or time block. The main loop begins by moving the servo sensor head to its initial position. The subroutine SubSendServoPulse is called to send the servo a high logic pulse. The pulse’s duration is proportional to the angular position desired. The subroutine uses the value of the position variable in a case- switch statement to choose the length of the high pulse. The RC servo signal must be repeated after roughly 20 ms, and the program calls the subDEL20 subroutine to produce the delay. If the position is the initial position, then a longer delay consisting of eight iterations of the servo pulse and 20 ms delay is provided in the program to allow the detector head extra time to move from its final position to the initial position, an angle of 72º. Based on the Futaba S2003 specification rotation rate of 261º/s, one would expect the rotation from the last position to the starting, initial position to require 276 ms [35]. When the servo is only required to move 18º between sonar firings, the servo pulse and 20 ms delay is repeated just three times because the smaller angular displacement requires less time for the servo to complete.
After the servo moves to the correct angular position, the program commands a sonar ranging. Each of the four sonar transducers is triggered sequentially beginning with sonar number four. The appropriate INIT signal output is taken to a high logic state, causing the corresponding SensComp 6500 ranging module to trigger its sonar ranging cycle, and the transducer transmits sixteen pulses. The PIC’s enhanced capture and compare module is configured to trigger on the rising edge of the combined ECHO signal and the associated interrupt flags are cleared. Then TIMER1’s count is cleared and the counter started.
The SensComp 6500 module has a default 2.38 ms delay after transmitting the sixteen sonar pulses to prevent confusing the transducer’s ring down as a return echo [24]. Assuming the speed of sound in air equals 343 m/s, this limits the minimum range to 80 cm. It was decided that this minimum range was unacceptably large for the expected operating environment. At the risk of detecting the ring down as echo the PIC overrides the default echo blanking by sending the blanking inhibit BINH to a high logic state after 1 ms. The 1 ms BINH delay has been observed to provide a 20 cm minimum range.
Figure 32. Continuation of Program Flowchart.
Then the PIC begins a loop that checks the interrupt flags for either receipt of an echo or a TIMER1 counter overrun. If the SensComp 6500 ranging module detects a valid echo, it sends its ECHO signal high. The four module’s ECHO signals are combined via a XOR gate. If the combined echo transitions from a low to high logic state, both of TIMER1’s counter bytes are saved, and then TIMER1 is allowed to finish
counting up until overflow. This ensures the process’ duration takes 33 ms regardless of the length of time between sonar firing and reception of the echo, and it guarantees the RC servo pulse will be sent again after an appropriate delay.
The most significant byte is tested to determine if the counter value indicates receipt of an echo from a target at a range that exceeds the limits of the reportable range. These TOF values are set to a literal value 0x02 to flag the data as no contact. The most significant byte and least significant byte of the counter variables are combined to make a one-byte value of the count. The 27 bit through the 214 are retained, and the byte is saved to an array whose index corresponds uniquely to the combination of sonar number and servo position using the relation below.
4(5array index= −position) (4+ −sonar number)
The variable SonarNum is decremented to allow the PIC to complete a sonar ranging with each of the sonar modules from four to one. The inner loop is repeated. If decrementing the variable results in a value of zero, then all sonars have been fired and the position variable is decremented. The position loop is repeated until decrementing the position variable results in a value equal to zero. Then the position value is reset to its initial value, five, and the outermost loop begins again with the sensor head being commanded to move to the initial position.