changeset 155:a5289fe64ef6 main

Four sonar sensors, with fixed frequency cycle. Code improvements.
author Bob Cook <bob@bobcookdev.com>
date Sat, 08 Sep 2012 13:17:03 -0700
parents 7374d1cd677f
children d4d3c3228208
files main/robots/odr-sonar-front/main.cpp main/robots/odr-sonar-front/sonar.cpp
diffstat 2 files changed, 27 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/main/robots/odr-sonar-front/main.cpp	Sun Aug 26 15:35:05 2012 -0700
+++ b/main/robots/odr-sonar-front/main.cpp	Sat Sep 08 13:17:03 2012 -0700
@@ -244,7 +244,6 @@
     //--    Initialize the sonar subsystem.
 
     sonar_init();
-    sonar_start();
 
     //--    When we sleep we want the "idle" mode e.g. wake up on any interrupt.
 
--- a/main/robots/odr-sonar-front/sonar.cpp	Sun Aug 26 15:35:05 2012 -0700
+++ b/main/robots/odr-sonar-front/sonar.cpp	Sat Sep 08 13:17:03 2012 -0700
@@ -40,8 +40,18 @@
 #include "packages/common/util/misc.h"
 
 // ----------------------------------------------------------------------------------------
+//  To reduce the frequency of the sensor pings, we number them with gaps between each.
+//  See the TIMER1_OVF interrupt code to see how this is used.
 
-volatile uint8_t   g_curr_sensor; // 0 = left, 1 = center left, 2 = center right, 3 = right
+static const uint8_t sensor_index_offset = 6;
+
+static const uint8_t sensor_index_left         = ( 0 * sensor_index_offset );
+static const uint8_t sensor_index_center_left  = ( 1 * sensor_index_offset );
+static const uint8_t sensor_index_center_right = ( 2 * sensor_index_offset );
+static const uint8_t sensor_index_right        = ( 3 * sensor_index_offset );
+static const uint8_t sensor_index_max_value    = ( 4 * sensor_index_offset );
+
+volatile uint8_t   g_curr_sensor_index;
 
 volatile uint16_t  g_sensor_left;               // pin PD6
 volatile uint16_t  g_sensor_center_left;        // pin PD5
@@ -82,33 +92,31 @@
     //--    We need to advance to the next sensor then trigger an output pulse. After
     //      the trigger we switch the pin to an input for sensing the hold-off period
     //      and the reception of the final result.
-    //
-    //      To reduce the frequency, we skip 1, 3, 5 and 7.
     
-    g_curr_sensor++;
-    g_curr_sensor %= 8;
+    g_curr_sensor_index++;
+    g_curr_sensor_index %= sensor_index_max_value;
 
-    switch ( g_curr_sensor )
+    switch ( g_curr_sensor_index )
     {
-        case 0:
+        case sensor_index_left:
             // switch PORTD.6 to an output, set it high
             DDRD  |= ( 1 << PD6 );
             PORTD |= ( 1 << PD6 );
             break;
 
-        case 2:
+        case sensor_index_center_left:
             // switch PORTD.5 to an output, set it high
             DDRD  |= ( 1 << PD5 );
             PORTD |= ( 1 << PD5 );
             break;
 
-        case 4:
+        case sensor_index_center_right:
             // switch PORTB.6 to an output, set it high
             DDRB  |= ( 1 << PB6 );
             PORTB |= ( 1 << PB6 );
             break;
 
-        case 6:
+        case sensor_index_right:
             // switch PORTB.7 to an output, set it high
             DDRB  |= ( 1 << PB7 );
             PORTB |= ( 1 << PB7 );
@@ -117,9 +125,9 @@
 
     spinwait_delay_us( 5 );
 
-    switch ( g_curr_sensor )
+    switch ( g_curr_sensor_index )
     {
-        case 0:
+        case sensor_index_left:
             // set PORTD.6 low then switch it to an input
             PORTD &= ~( 1 << PD6 );
             DDRD  &= ~( 1 << PD6 );
@@ -127,7 +135,7 @@
             PCMSK2 |= ( 1 << PCINT22 );
             break;
 
-        case 2:
+        case sensor_index_center_left:
             // set PORTD.5 low then switch it to an input
             PORTD &= ~( 1 << PD5 );
             DDRD  &= ~( 1 << PD5 );
@@ -135,7 +143,7 @@
             PCMSK2 |= ( 1 << PCINT21 );
             break;
 
-        case 4:
+        case sensor_index_center_right:
             // set PORTB.6 low then switch it to an input
             PORTB &= ~( 1 << PB6 );
             DDRB  &= ~( 1 << PB6 );
@@ -143,7 +151,7 @@
             PCMSK0 |= ( 1 << PCINT6 );
             break;
 
-        case 6:
+        case sensor_index_right:
             // set PORTB.7 low then switch it to an input
             PORTB &= ~( 1 << PB7 );
             DDRB  &= ~( 1 << PB7 );
@@ -159,7 +167,7 @@
 {
     //--    Pin-change interrupt, could be PORTB.6 (center right) or PORTB.7 (right).
     
-    if ( g_curr_sensor == 4 ) // PORTB.6
+    if ( g_curr_sensor_index == sensor_index_center_right )
     {
         if ( ( PINB & ( 1 << PB6 ) ) != 0 )
         {
@@ -176,7 +184,7 @@
             PCMSK0 &= ~( 1 << PCINT6 );
         }
     }
-    else // PORTB.7
+    else // sensor_index_right
     {
         if ( ( PINB & ( 1 << PB7 ) ) != 0 )
         {
@@ -201,7 +209,7 @@
 {
     //--    Pin-change interrupt, could be PORTD.5 (center left) or PORTD.6 (left).
     
-    if ( g_curr_sensor == 0 ) // PORTD.6
+    if ( g_curr_sensor_index == sensor_index_left )
     {
         if ( ( PIND & ( 1 << PD6 ) ) != 0 )
         {
@@ -218,7 +226,7 @@
             PCMSK2 &= ~( 1 << PCINT22 );
         }
     }
-    else // PORTD.5
+    else // sensor_index_center_left
     {
         if ( ( PIND & ( 1 << PD5 ) ) != 0 )
         {