Newsgroups: comp.robotics
Path: brunix!cat.cis.Brown.EDU!noc.near.net!paperboy.wellfleet.com!news-feed-1.peachnet.edu!news.duke.edu!MathWorks.Com!europa.eng.gtefsd.com!howland.reston.ans.net!EU.net!sunic!ericom!etlxd43k!etlrwy
From: etlrwy@etlxd30.ericsson.se (Rob Wheatley XD/HDN)
Subject: Re: HELP on a circuit design (simple)
Message-ID: <1994May2.095546.2860@ericsson.se>
Sender: news@ericsson.se
Nntp-Posting-Host: etlxd43k.ericsson.se
Reply-To: etlrwy@etlxd30.ericsson.se
Organization: Ericsson Telecom AB
Date: Mon, 2 May 1994 09:55:46 GMT
Lines: 302


  THIS IS A REPOSTING WITH A WORLD DISTRIBUTION ON BEHALF OF KEITH McCLARY

            (His news reader is busted!)




In a recent comp.robotics posting, you wrote:
>From: etlrwy@etlxd30.ericsson.se (Rob Wheatley XD/HDN at Ericsson Telecom AB)
>Subject: HELP on circuit design
>
> I was wondering if someone could help me.
> I need to design a simple circuit that will do the following
> Take 4 analogue DC voltages, compare them and give
> 4 digital outputs 3 of them at logic 0 and the [fourth]
> at logic 1 (this logic 1 output indicates which one of
> the four inputs had the highest value)
> Any offers???
> Please E-mail me if you can give me any pointers.
> MORE INFO:
> What I want to do is have 4 light dependent resistors,
> some kind of automatic gain so that the ambient light
> level doesn't [affect] the decision making and I want the
> circuit to decide which resistor has the most amount
> of light falling on it.
> Is this simple? if so I'd love some ideas.
>
> Ta,
> Rob.

First, I'd like to ask a couple of questions.

  1) What are the voltage ranges involved?  0-5v?
  2) Are you trying to create a 4cell light tracker (four cells in a "+"
     pattern to control the aiming of a light-seeking device)?  If so,
     you don't need to do it like you have spec'd here.  Just connect
     opposite pairs to a differential amp with a bi-polar supply, and
     use each of the two outputs to drive the motors directly.

  To answer your question as posed, I'll assume you are working with
logic voltages.

  This problem is equivalent to taking a pyramid shape (tetrahedron for
the purists :-), tumbling it, and trying to determine the highest vertex
in 3-space.  Each corner (vertex) represents a voltage, and each edge
represents a comparison that you will have to do.

  The general solution to the minimum number of comparisons C you will
have to do for N voltages equals:

(***)     C = N! / 2

  For 4 voltages, that = 4*3*2*1 / 2 = 6 comparisons.  for voltages V0-V3,
the tests would be:

          0>1, 0>2, 0>3, 1>2, 1>3, 2>3


  This can be done with 6 comparators and an EPROM or PAL.  I will show that
later.  A simpler brute force method that comes to mind immediately is to
use 12 comparators (check both 0>1 and 1>0, etc) to simplify the decoding
logic and just create 4 copies of this circuit:

                |\
  V0--+---------| \ 1/4 of an LM339
      |         |  \_______________________
      |         |  /                       |
      |  V1 ----| /                        |
      |         |/                         |
      |                                    |
      |         |\                         |    ____
      +---------| \ 1/4 of an LM339        |___|    \
      |         |  \___________________________| AND \_______ V0 highest
      |         |  /                        ___|     /
      |  V2 ----| /                        |   |____/
      |         |/                         |
      |                                    |      (or use a NOR as an
      |         |\                         |       inverted input AND and
      `---------| \ 1/4 of an LM339        |       flip signs on 339s)
                |  \_______________________|
                |  /
         V3 ----| /
                |/

where each voltage is compared against the other three to indicate that
it is higher than all of them.  LM339 comparators are VERY low cost, and
come 4 comparators to a pack.  Depending on the logic family used, I
think you may need pullups on the outputs of the 339s (running from memory
here... please look up the comparator spec sheet).

  Another way may be to use op amps in differential mode in sets, combine
the outputs to create the 2 bit output truth table:

    00  - V0 highest
    01  - V1 highest
    10  - V2 highest
    11  - V3 highest

then run the output into a 2 bit decoder chip to yield your 4 bits of output,
but this will probably use more circuitry.

  The more optimal solution discussed earlier is as follows:


                V0   |\
  V0--------+--------| \ (1/4) LM339  0>1
            |        |  \______________________
            |   V1   |  /                      |
  V1------+----------| /                       |
          | |        |/                        |
          | |                                  |
          | |   V0   |\                        |
          | +--------| \ (1/4) LM339   0>2     |
          | |        |  \____________________  |          
          | |   V2   |  /                    | |
  V2----+------------| /                     | |
        | | |        |/                      | |
        | | |                                | |   _________
        | | |   V0   |\                      | |  |         |
        | | `--------| \ (1/4) LM339   0>3   | |__|D0       |
        | |          |  \__________________  |    |         |
        | |     V3   |  /                  | |____|D1     Q0|_____ V0 highest
  V3 -+--------------| /                   |      |         |
      | | |          |/                    |______|D2     Q1|_____ V1 highest
      | | |                                       |         |
      | | |                                 ______|D3     Q2|_____ V2 highest
      | | |     V1   |\                    |      |         |
      | | +----------| \ (1/4) LM339   1>2 |  ____|D4     Q3|_____ V3 highest
      | | |          |  \__________________| |    |         |
      | | |     V2   |  /                    |  __|D5       |
      | +------------| /                     | |  |         |
      | | |          |/                      | |  |_________|
      | | |                                  | |
      | | |     V1   |\                      | |
      | | `----------| \ (1/4) LM339   1>3   | |    PROM, EPROM or PAL
      | |            |  \____________________| |
      | |       V3   |  /                      |
      +--------------| /                       |
      | |            |/                        |
      | |                                      |
      | |       V2   |\                        |
      | `------------| \ (1/4) LM339   2>3     |
      |              |  \______________________|
      |         V3   |  /
      `--------------| /
                     |/

 The truth table is as follows:

-----------------------------------------------------------------------
 PROM  (L) : 0 1 3 7 : 8 A B F : 8 A E F : 0 1 5 7 : 0 4 5 7 : 8 C E F :
 ADDR  (H) : 0 0 0 0 : 0 0 0 0 : 1 1 1 1 : 2 2 2 2 : 3 3 3 3 : 3 3 3 3 :
-----------+---------+---------+---------+---------+---------+---------:
  D0   0>1 : 0 1 1 1 : 0 0 1 1 : 0 0 0 1 : 0 1 1 1 : 0 0 1 1 : 0 0 0 1 :
  D1   0>2 : 0 0 1 1 : 0 1 1 1 : 0 1 1 1 : 0 0 0 1 : 0 0 0 1 : 0 0 1 1 :
  D2   0>3 : 0 0 0 1 : 0 0 0 1 : 0 0 1 1 : 0 0 1 1 : 0 1 1 1 : 0 1 1 1 :
  D3   1>2 : 0 0 0 0 : 1 1 1 1 : 1 1 1 1 : 0 0 0 0 : 0 0 0 0 : 1 1 1 1 :
           :---------+---------+---------+---------+---------+---------:
  D4   1>3 : 0 0 0 0 : 0 0 0 0 : 1 1 1 1 : 0 0 0 0 : 1 1 1 1 : 1 1 1 1 :
  D5   2>3 : 0 0 0 0 : 0 0 0 0 : 0 0 0 0 : 1 1 1 1 : 1 1 1 1 : 1 1 1 1 :
-----------:-----------------------------------------------------------:
 SOLUTION->: 3 3 3 0 : 3 3 3 0 : 1 1 1 0 : 2 2 2 0 : 2 2 2 0 : 1 1 1 0 :
  Vhighest : 2 2 0 3 : 1 1 0 3 : 3 3 0 1 : 3 3 0 2 : 1 1 0 2 : 2 2 0 1 :
     to    : 1 0 2 2 : 2 0 1 1 : 2 0 3 3 : 1 0 3 3 : 3 0 1 1 : 3 0 2 2 :
  Vlowest  : 0 1 1 1 : 0 2 2 2 : 0 2 2 2 : 0 1 1 1 : 0 3 3 3 : 0 3 3 3 :
-----------------------------------------------------------------------
 PROM DATA : 8 8 8 1 : 8 8 8 1 : 2 2 2 1 : 4 4 4 1 : 4 4 4 1 : 2 2 2 1 :
-----------------------------------------------------------------------
            NOTE: *** All other data locations MUST = 0 ***
            -----------------------------------------------

  I leave it as an exercise to the reader to create the Mot S-records or
Intel Hex (sorry, I'm not THAT bored or crazy :-).

  The reason that all other locations must equal 0 is that they are illegal
states (ie 1>2, 2>3, 3>1).  Due to race conditions, they will be hit
as you walk through the states, so you need to disable the output (most
common EPROMS have an erased value of FF, which would say thay are ALL
the highest).

  This design lends itself to a lot of flexibility in PC board layout.
Comparator inputs, comparator to Data inputs, and output pins can be
assigned for ease in layout, and all you have to do is recompute the
PROM/PAL data.

  Also, you already have the exact order of the voltages figured out,
so you could generate "Voltage order" data with 8 bits of output if you
wish.

  A word of warning... This system has no hysteresis built in.  You did
not specify what you wanted to do if the voltages were exactly equal or
close to respond to 60Hz noise on the wiring.  The 60Hz noise may cause
rapid ringing on the output as you near a switch boundry.  This circuit
 will jitter if there is no debouncing in the output.
You WILL get noise as you switch from one state to the other.  If the
output is driving motors, it probably won't be a problem, but if driving
digital logic, you may need to debounce the input.  You will have cleaner
output if you use a PAL vs a PROM/EPROM, but normally more people have
access to EPROM programmers than PAL/PROM.  You need to consider adding
hysteresis or deadband (no output if really close) to your spec.

  The bottom line is to use comparators or op amps in saturation mode to select
the highest, and run decoding logic of some kind.  I haven't seen the range
of parts available these days, but it used to be that you could handle
wider input voltage ranges by using op amps as comparators.



 Talking at lunch to John Steele (a co-worker in Electrical/Electronic Systems 
Engineering Department), he came up with these solutions:

1) Make a block of a comparator and an analog mux as follows:

        .-----------R2---.
        |                |
        |       |\       |
  Va--+-+---R1--| \      |
      |         |  \     |
      |         |   >----+----+----------------- Control output
      |         |  /          |
  Vb----+-------| /           |
      | |       |/     .-------------.
      | |______________|Vin1         |
      |________________|     Amux  Vo|---------- Analog forward
                       |Vin2         |
                       `-------------'

(R1 and R2 give the hysteresis talked about earlier, but if used, the
analog inputs MUST be buffered.  If not used, short R1 and remove R2).
You can easily make 4 blocks out of a quad comparator and a quad Amux chip.
Now, take three of them like so:

              ______
    V0_______|      |____________ Control output
    V1_______|  A   |_______
             |______|       |      ______
                      Vout  |_____|      |___________ Highest voltage out
                             _____|  C   |_____
              ______  Vout  |     |______|     |_____ MSB of data out
    V2_______|      |_______|
    V3_______|  B   |____________ Control output
             |______|

  This give you the actual analog voltage as an output, and only takes
two chips (so far).  Use the fourth block to "decide" which of the two 
control outputs (A or B) will be the LSB, and you have two bits of digital 
data representing a binary code of the highest voltage input line.  You 
will need a third chip as a 2 bit decoder to break out the information 
to 4 lines, if necessary.  They may be inverted, though (logic low = 
highest voltage using common decoder chips).

  Then, in a flash of "ooh, ooh, wait a minute" insight, John came up with
this one:
                              Vcc
                               |
                .---------+----+----+------ ... ---.
                |         |         |              |
                R1        R2        R3             Rn
                |         |         |              |
                +-- Q0/   +-- Q1/   +--Q2/         +--Qn/
                |         |         |              |
              |/        |/        |/             |/
          V0--|     V1--|     V2--|          Vn--|
              |\        |\        |\             |\
                V         V         V              V
                |         |         |              |
                `---------+----+----+------- ... --'
                               |
                               O | Current source
                               | V
                              ----
                             / / /

  Whichever voltage is highest steals all the current, yielding a low output.
You can extend this one to check more input lines.  You need to use matched
transistors.  You can get them all in 1 IC pack (2803 I think) which
reduces it to a current source (1 transistor) and 1 IC.  Buffer the output
with a schmidt triggered hex inverter (ie 74HC14) and you're done, but
you may wish to use the outputs as is.  Again, watch out for near equal
voltages.  This one will go analog on you when two voltages are close,
but schmidt trigger buffering will help.  There is a small chance of
getting two outputs on at one time out of the schmidt triggers with a
slowly varying, near equal highest voltage on two inputs.  Email me back 
if you need more explanation.

  There are probably more optimal solutions, but John's current mirror wins
the elegant solution in my book. Good luck, and let me know what happens.

- Keith

--
     __
    /  \    Keith McClary PLEASE EMAIL ALL REPLIES TO ME (I'll post summaries)
   /    \   mcclary@alp061.alp.ford.com (UNIX, preferred) or: KMCCLARY (PROFS)
  /  /\  \  "I do not speak for Ford. Now singing, that's a different story.."
 /  /__\  \ Ford Intelligent CAD/CAM Research Project, Vehicle Engineer'g Dept
/_____/ \__\LPHA Simultaneous Engineering, Ford Motor Company, Dearborn MI,USA

P.S.- Please post this reply for me to comp.robotics for others to critique
as our poster is still down. Thanks!

