Electrical – Weighted average

mathoperational-amplifierpid controller

I'm kinda new to this forum. I'm an electronics student from Argentina (sorry if my English is not the best, you know). I'm a robotics enthusiast. Now I'm building a racing robot.
The thing is: it's a line follower that uses PID with a weighted average of the sensors' values, to know "where the robot is" having the line as the reference. For the ones who don't know, weighted average is

(v1*w1)+(v2*w2)+…(vn*wn)/(w1+w2+…wn)

(v = value, w = weight)

I use a number from 1 to N (quantity of sensors) as the value and the voltage as the weight. In this way I can know where, within the sensor array, the darkness is concentrated.
A friend gave me the idea to calculate the weighted average with op-amps, because I can save time and processing and make it more precise (analog > digital). I've been doing some research and found that I could use resistors to give weight to the voltages. But I couldn't really understand because I've just started learning op-amps and I know almost nothing about them. Could you tell me how to achieve this? Or at least how to make operations such as sum, product and division…

Sorry for the long text and thanks for reading.

Best Answer

A basic summing circuit using an op-amp is shown below.

Image source Wikipedia

(Image credit to Wikipedia)

The transfer function is Vout=-Rf*Vn/Rn for each input. The value of input Vn can therefore be weighted through the selection of Rn. The sum of all inputs must not exceed the maximum Vout of the op-amp.

While it is possible to build a summing amplifier in a non-inverting format, the number of practical inputs is can be significantly limited. So generally the inverting form shown is applied and if necessary, a second unity gain inverting op-amp follows it to provide an overall positive summing function.

Strictly speaking an averaging function is not necessary in your application since the number of inputs is fixed so a simple scaling function is all that is required. Of course the scaling function can be selected to obtain an average. The scaling can be done through the selection of Rf and Rn or it can be performed through a simple resistive divider on the output of the op-amp. If the divider is placed between the two inverting op amps, the gain of the second op amp could then be used to scale the result to the input range of a A/D converter, for example.