Previous in Forum: Resistor Error   Next in Forum: Butterfly Valve Minimum DP
Close
Close
Close
18 comments
Rate Comments: Nested
Active Contributor

Join Date: May 2011
Posts: 19
Good Answers: 3

Programatically Differentiate Sawtooth From Square Wave

05/27/2011 2:40 PM

I am helping develop a small mobile robot for a competition. One of the objectives is to quickly make a movement decision based on whether a signal between 2 plates on the course is a sawtooth wave or a square wave.

Signal will be around 100 kHz but we do not know the exact frequency, or if the sawtooth and square will be of the same frequency. Maximum current that can be sourced is 10mA. The RMS voltage for both signals will be 5V. We know which plate will be signal and which will be ground, but we do not know the polarity of the signal.

We are trying to keep it simple cheap and light so big lab equipment is out. We will have at least one AVR atmega microcontroller on board controlling our hardware, probably Arduino. We haven't decided yet if we need or want more brainpower onboard.

Does simple hardware exist that can feed our microcontroller the result on a digital pin? Will an ADC channel on our AVR be sufficient? Since we cannot guarantee that the signal ground is the same level as our AVR ground can we safely tie them together when we make contact or do we need to measure signal and ground differential over 2 adc channels? I am not sure the arduino hardware brings out Aref.

I was thinking: fill an array with samples from the ADC at free running speed over at least a few cycles of the signal. Count the number of samples that lie between high and low thresholds. If that count is greater than maybe 5% of the total samples then it is sawtooth, else it is square. Do you see any problems with that? I just noticed that if the RMS signal is 5v then each sample may be higher than 5v so I need to reduce the signal with a voltage divider, but how much? What is the maximum voltage the signal could be? How do I handle the case where the signal goes negative? How about that 5% figure? I wanted a fudge factor in case of noise or catching a rising or falling edge on a square wave.

Next step, measure a capacitance above or below 500nF with the same probes used above which are large parallel rings that may move.

Dustin Maki

Register to Reply
Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.

Good Answers:

These comments received enough positive votes to make them "good answers".

"Almost" Good Answers:

Check out these comments that don't yet have enough votes to be "official" good answers and, if you agree with them, vote them!
Guru

Join Date: Sep 2009
Posts: 1056
Good Answers: 88
#1

Re: Programatically Differentiate Sawtooth From Square Wave

05/27/2011 4:04 PM

Pass the signal through a simple differentiator. No big accuracy required. The square will be transformed to short bursts, easy to distinguish programmatically by their duty cycle. S.M.

__________________
Life is complex. It has a real part and an imaginary part.
Register to Reply
Active Contributor

Join Date: May 2011
Posts: 19
Good Answers: 3
#5
In reply to #1

Re: Programatically Differentiate Sawtooth From Square Wave

05/27/2011 7:03 PM

So the signal I would get after the differentiator for sawtooth would be a constant voltage with periodic spikes of the opposite polarity. And square wave would be a constant 0 voltage with periodic spikes in each direction. Is that correct? How would I distinguish programmatically by their duty cycle? I think the max free running ADC speed is around 200kHz. I suppose I could just take 3 samples and if any 2 are non zero then I am on a sawtooth. Does that sound right? I am still a novice, so please bear with me. I would still have the problem of reversed polarity. I suppose I could sample everything through a bridge rectifier. That seems at first glance to be more involved, but I guess I could even use a digital pin and same the ADC for other purposes. Please correct me if I am wrong on any of my assumptions.

Dustin Maki

Register to Reply
Guru

Join Date: Sep 2009
Posts: 1056
Good Answers: 88
#7
In reply to #5

Re: Programatically Differentiate Sawtooth From Square Wave

05/27/2011 8:14 PM

The derivative of the square will be a flat with alternating spikes at the times the signal changes state and a duty cycle say <5% The derivative of a sawtooth signal will be a deformed 'trapezoid' (with shape of course depending on sawtooth specifics) but at least 25% duty cycle (worst case if signal is true sawtooth and if the differentiator is fit for your freq range and correctly biased). Not that difficult to time between two signal (up to down to up) changes and compare with say 10% of your signal's detected period. If smaller square. If bigger but less than period sawtooth. If bigger than period no signal. Do it a few times over to eliminate random effects and you're OK. Hope this makes sense. Hell sometimes I have to read my posts twice to understand what I meant. S.M.

__________________
Life is complex. It has a real part and an imaginary part.
Register to Reply Score 1 for Good Answer
Guru
Engineering Fields - Electrical Engineering - Been there, done that. Engineering Fields - Control Engineering - New Member

Join Date: Dec 2008
Location: Long Island NY
Posts: 15600
Good Answers: 981
#9
In reply to #5

Re: Programatically Differentiate Sawtooth From Square Wave

05/27/2011 8:58 PM

Sorry, but I didn't see this post before I made my other post. Welcome to the world of real engineering. Unlike an academic exam, there isn't only one good answer to a problem.

__________________
"Don't disturb my circles." translation of Archimedes last words
Register to Reply Off Topic (Score 4)
Guru
Engineering Fields - Electrical Engineering - Been there, done that. Engineering Fields - Control Engineering - New Member

Join Date: Dec 2008
Location: Long Island NY
Posts: 15600
Good Answers: 981
#2

Re: Programatically Differentiate Sawtooth From Square Wave

05/27/2011 4:32 PM

My first concern is how will the signal will be transferred to your robot? At 100 kHz you're not going to get much if any RF transmission that your robot can pick up, particularly with only 10mA of drive current. A quarter wavelength antenna at 100 kHz is nearly 750 meters long. Now if this 100 kHz signal was the modulation of a much higher carrier frequency, then I could see your robot acquiring such a signal much more easily. I guess you would be able to magnetically pick this up with a ferrite core rod antenna.

__________________
"Don't disturb my circles." translation of Archimedes last words
Register to Reply Score 1 for Good Answer
Active Contributor

Join Date: May 2011
Posts: 19
Good Answers: 3
#3
In reply to #2

Re: Programatically Differentiate Sawtooth From Square Wave

05/27/2011 5:26 PM

Sorry if I lead you down a wrong path. The signal is not RF. It is delivered through 2- 2" x 3" metal plates mounted on the course. Our robot needs to have some method to make physical contact with the plates. There will be 4 similar stations along the course each providing a different property to be measured. Voltage, Temperature, Capacitance, and the square vs sawtooth station I described. The values presented will be randomized. At each station, we take a measurement and if it fits one criteria we go left, if it fits the opposite criteria we go right. We get points for each correct choice multiplied by the number of correct choices we have made in a row. We keep navigating laps of the course until 4 minutes expires.

Dustin Maki

Register to Reply Score 1 for Good Answer
Guru
Engineering Fields - Electrical Engineering - Been there, done that. Engineering Fields - Control Engineering - New Member

Join Date: Dec 2008
Location: Long Island NY
Posts: 15600
Good Answers: 981
#4
In reply to #3

Re: Programatically Differentiate Sawtooth From Square Wave

05/27/2011 5:33 PM

The easiest method I can think of is to include a true RMS voltage measurement.

__________________
"Don't disturb my circles." translation of Archimedes last words
Register to Reply Score 1 for Good Answer
Active Contributor

Join Date: May 2011
Posts: 19
Good Answers: 3
#6
In reply to #4

Re: Programatically Differentiate Sawtooth From Square Wave

05/27/2011 7:31 PM

That was my first thought too. Then I reread the rules. Both signals will have RMS voltage of 5v.

Register to Reply Score 1 for Good Answer
2
Guru
Engineering Fields - Electrical Engineering - Been there, done that. Engineering Fields - Control Engineering - New Member

Join Date: Dec 2008
Location: Long Island NY
Posts: 15600
Good Answers: 981
#8
In reply to #6

Re: Programatically Differentiate Sawtooth From Square Wave

05/27/2011 8:51 PM

You're correct. So since the RMS values and the approximate frequency are known then sampling at 200 kHz or faster will produce sequential random numbers in a fixed range for the saw tooth while a square wave will only produce sequences of two similar numbers. Naturally the noise of the acquisition system and the bandwidth will induce minor variations. The critical factor will be how fast can the data acquisition acquire.

__________________
"Don't disturb my circles." translation of Archimedes last words
Register to Reply Good Answer (Score 2)
Commentator

Join Date: Aug 2009
Location: Carrollton, TX.
Posts: 78
Good Answers: 6
#10

Re: Programatically Differentiate Sawtooth From Square Wave

05/31/2011 9:31 AM

Since you have a microcontroller, use one of the adc inputs to aquire a number of samples and perform a kurtosis calculation. The kurtosis value is different for square, sine and triangle/ramp waveforms. The method would require some processor time, but would be low cost without need for extra circuitry. It should also be 'bulletproof' with a decent sample size.

__________________
MRH - "The search for truth is more precious than its possession." Albert Einstein
Register to Reply Score 1 for Good Answer
Anonymous Poster #1
#11

Re: Programatically Differentiate Sawtooth From Square Wave

05/31/2011 12:09 PM

The edges of the square wave have a rise time of 0 sec, in theory.

Sawtooth, a bit slower...

Pick two levels, say at 1/3 and 2/3 of nominal voltage.

Either sample with ADC, or run those references through comparators into two digital inputs.

When one changes, the other will also change (within microseconds probably) for the square wave.

If the other one hasn't changed, it's sawtooth.

This takes less than 1/2 cycle of the waveform.

Register to Reply Score 1 for Good Answer
Active Contributor

Join Date: May 2011
Posts: 19
Good Answers: 3
#12
In reply to #11

Re: Programatically Differentiate Sawtooth From Square Wave

05/31/2011 3:09 PM

Unfortunately I do not have both signals together at any one time. On each lap I encounter this station it may be either square wave OR sawtooth. Each lap it may(or may not be) be a different signal. Could be different in shape and/or frequency. The shape is the only thing I really need to determine. I've learned something from each of your answers and I thank you. I will be going with the concept I laid out at the start but modifying it a bit from each of your suggestions if I dont get the performance I need. Reading on the falling edge gives a known place in the waveform which is helpful. The 2 voltage levels may as well be the processor's default high and low thresholds. The differentiator and kurtosis approaches both appear to be the 'proper' way to do this but it looks like similar results can be achieved in fewer cycles with less hardware and less calculation with a hack approach. Building on RufusVS approach, if I split the signal to both a digital and ADC pin I can use the digital pin to sense the falling edges to determine the period and trigger a pin change interrupt which in turn triggers 2 ADC samples at 1/3 and 2/3 of the period(assuming I can sample fast enough). If the samples have more than a trivial positive slope it is a sawtooth, else it is square.

Dustin Maki

Register to Reply
Guru

Join Date: Dec 2010
Posts: 1686
Good Answers: 116
#13
In reply to #12

Re: Programatically Differentiate Sawtooth From Square Wave

06/03/2011 7:53 AM

The rules say the signal is 5V rms, so you do not have to worry about amplitude difference. I also take it sawtooth and square are to be taken literally and at simplest, no quibbles about "saw teeth" with flat at root, or "square" with 30% duty cycle!

Feed signal through capacitor to get an AC symmetrical signal.

Ignoring sign, For sawtooth mean/peak = 0.5. For square, mean/peak = 1.

So use a mean value rectifier with RC filter and peak value rectifier with RC reservoir. Compare half the peak value with the mean value and you should have a distinct change between sawtooth and square [which will not be much affected by frequency or magnitude].

Register to Reply
Active Contributor

Join Date: May 2011
Posts: 19
Good Answers: 3
#16
In reply to #13

Re: Programatically Differentiate Sawtooth From Square Wave

06/03/2011 3:23 PM

67model, I really appreciate your ideas and schematics. I think you are on the right track using analog methods to simplify the problem. Unfortunately no further information about the shape of the waves are available. I am not even sure how much I can rely on the 5vRMS spec. When the course was announced they said the stations would be built as simply and cheaply as possible and everything was subject to change. I have a gut feeling they may use 5v peak signal instead of RMS. I will write the host school and ask for confirmation that both waves are positive, the square wave duty cycle is 50% and that the sawtooth ramp immediately follows the falling edge. They may answer, or like other details leave it unspecified. I've been warned that in past years the provided specs lead to an obvious interpretation but the implemented course used something non obvious. Example, in an IEEE sponsored event bots had to read a serial signal. Almost everyone assumed the serial format would conform to an IEEE standard. It did not, and only a few teams that allowed for that possibility got past that point.

I like using the dual or single polarity of spikes out of the differentiator to determine the wave. A simple pulse in routine should capture that easilly with little proc time or code and be frequency, duty cycle, and flat at root insensitive .

Dustin Maki

Register to Reply
Guru
Engineering Fields - Electrical Engineering - Been there, done that. Engineering Fields - Control Engineering - New Member

Join Date: Dec 2008
Location: Long Island NY
Posts: 15600
Good Answers: 981
#17
In reply to #16

Re: Programatically Differentiate Sawtooth From Square Wave

06/03/2011 4:44 PM

I like the simple differentiate mode, too and have a suggestion. AC couple (aka high pass) the signal pick up but run the signal to two comparators with hysteresis. Without knowing what the actual slew rate of the sharp edge of either signal is I cannot give you a precise high pass filter corner frequency but I would start at at least 400 kHz. To claim that you have a square or saw tooth wave at 100 kHz, there has to be 5th order harmonics of a sufficient magnitude. One comparator goes low (briefly) when a spike goes below 0V plus some noise margin. The other comparator goes low when a spike goes above 0V plus some noise margin. Take these two plus and minus spikes to a negative going SR latch. One spike for Set and the other for Reset. If the latch toggles then you have a square wave. If the latch stays in one state you have a triangular wave. Now the problem you will have is how do you know when you are connected? Well the comparator signals can be NAND gated together to make a clock pulse signal that can be counted or detected in a variety of different methods. In using this approach you won't care if the sawtooth has a sharp positive or negative edge.

__________________
"Don't disturb my circles." translation of Archimedes last words
Register to Reply
Guru

Join Date: Dec 2010
Posts: 1686
Good Answers: 116
#18
In reply to #16

Re: Programatically Differentiate Sawtooth From Square Wave

06/03/2011 6:31 PM

If you are going to ask the Maze Master a question, the transition time of the sharp edges of the saw and square are necessary to help with differentiation time constant. It does not matter much what the polarities are, as I wrote, any DC bias is removed by a capacitor coupling. I like Redfred's idea with the S-R latch. If the latch could be preset, one could detect either polarity of pulse - detecting "signal" with square is easy, because the S-R always toggles. Double use of a digital output(s) could be possible to set S-R in either state, maybe capacitor coupling.

Register to Reply
Guru

Join Date: Dec 2010
Posts: 1686
Good Answers: 116
#14
In reply to #12

Re: Programatically Differentiate Sawtooth From Square Wave

06/03/2011 11:48 AM

I meant something like this - not digitizing everything and doing lots of software calculations. It is doubtful your MCU can work fast enough - which is probably why the Maze Master made the sawtooth a high frequency.

Hammers are still out there, even though steam rollers are big and expensive and draw the crowds! Oooh! Look, it can crack a nut!

Do not presume I have got all the component values right or have tested this. You have to do some work.......

Register to Reply
Guru

Join Date: Dec 2010
Posts: 1686
Good Answers: 116
#15
In reply to #12

Re: Programatically Differentiate Sawtooth From Square Wave

06/03/2011 1:09 PM

Another way from the Analog Dinosaur. The "charge pump".

Differentiated, the square wave has positive and negative spikes in each cycle, the saw has only negative going (or maybe only positive).

This circuit dumps a quantity of charge into the reservoir during each spike. With square the + and - amounts cancel if the input capacitors are same value. Saw has only one fast polarity, so output V is pumped in one direction. Practically, a bleed resistor is needed across the reservoir, because spike capacitors are not equal and the output needs to be zero with no input.

Add to diagram (I forgot ) - Resistors needed shunting each diode on left - to stop slow slope of sawtooth giving enough current to forward bias diode.

Another way would be to feed differentiators to two digital inputs, but return the resistor of one to logic +. One input will turn on only for positive spikes, the other will be ON, except during negative spikes. Or could be one input with resistor switched to + or minus.

Register to Reply
Register to Reply 18 comments

Good Answers:

These comments received enough positive votes to make them "good answers".

"Almost" Good Answers:

Check out these comments that don't yet have enough votes to be "official" good answers and, if you agree with them, vote them!
Copy to Clipboard

Users who posted comments:

67model (4); MRH620 (1); redfred (5); SimpleMind (2); slomobile (5)

Previous in Forum: Resistor Error   Next in Forum: Butterfly Valve Minimum DP

Advertisement