Project

General

Profile

Revision fd73d758

IDfd73d7589ef076f3d6b164031d18f5827a18a8d4

Added by Thomas Mullins over 11 years ago

Changed range sensor to read on-demand

View differences:

scout_avr/src/range.cpp
17 17
struct range_t {
18 18
  unsigned int start; // timer value on rising edge
19 19
  unsigned int value; // last measured range
20
} range[2];
20
  char done;
21
} volatile range[2];
21 22

  
22 23
static void on_edge(int which)
23 24
{
......
39 40
  }
40 41
  else
41 42
  {
43
    PORT_SONAR_TX &= ~ _BV(P_SONAR_TX);
42 44
    // if timer overflowed since start, this arithmetic should still work out
43 45
    range[which].value = time - range[which].start;
46
    range[which].done = 1;
44 47
  }
45 48
}
46 49

  
......
64 67
  // CS1 = 2, 1/8 prescaler
65 68
  // if this is changed, remember to change recv_edge in bom.cpp!
66 69
  TCCR5B = _BV(CS51);
67
  
68
  range[0].value = RANGE_ERR;
69
  range[1].value = RANGE_ERR;
70

  
71
  // set tx as output
72
  DDRG |= _BV(DDG1);
73
  PORT_SONAR_TX &= ~ _BV(P_SONAR_TX);
70 74
}
71 75

  
72
unsigned int range_get(int which)
76
void range_measure(unsigned int *values)
73 77
{
74
  unsigned int ret;
75
  if (0 <= which && which <= 1)
78
  int i;
79

  
80
  for (i = 0; i < 2; i++)
81
  {
82
    range[i].value = RANGE_ERR;
83
    range[i].done = 0;
84
  }
85

  
86
  // TODO ensure that one interrupt won't be delayed because of the other
87
  PORT_SONAR_TX |= _BV(P_SONAR_TX);
88

  
89
  for (i = 0; i < 2; i++)
76 90
  {
77
    cli();
78
    ret = range[which].value;
79
    sei();
80
    return ret;
91
    while (!range[i].done) {}
92
    values[i] = range[i].value;
81 93
  }
82
  else return RANGE_ERR;
83 94
}

Also available in: Unified diff