1

Reading PWM signals

Posted by Chris on Sunday, November 08, 2009
While waiting for the screens and samples for developing ScoreSure v2.0 I've been looking at a little project for Tom from RobotBrighton. He builds "battle robots" and has developed a relay-driven controller board for one of his 'bots (he has a habit of letting smoke out of his components and says he's burnt through quite a few MOSFETs already!).

The idea is to take RC (remote control) signals and convert them to on/off switching signals. RC signals work in a similar fashion to driving servo motors. A signal with a pulse width of x milliseconds is decoded at the other end to determine how far a joystick has been pushed on a remote control handset.

For this project, there are a few rules that have to be put into place as well as basic pwm-decoding: firstly, the signal width MUST be between 0.5ms-2ms. Any signal outside of this range should result in the controller sending an "error" signal, or putting the controlled device into a 5 second delay/standby mode.
Also, pulse widths for servo controls are often repeated at 2.5m/s intervals, although this may or may not be the case for the RC controller. So we need to measure the width of a signal coming in and act on it accordingly, but not necessarily worry about the frequency of the signals as these may be subject to fluctuation - i.e. we cannot assume that all signals start exactly 2.5ms apart.

To achieve this, we're going to use a humble 16F628A PIC chip.
It's cheap and available (but to be honest, for something relatively simple like this, it's a bit of overkill: a smaller/cheaper PIC could be used, but we've got quite a few of these hanging about, so we'll use one of these instead).
In order to accurately detect when a signal starts and ends, we're going to use the PICs interrupt routines. A PIC chip can generate interrupts in a number of scenarios. We're going to generate an interrupt on every change of input on pin RB4.



We also need to make use of a 16-bit timer and some pre-scaling, to calculate the width of each incoming signal pulse. Whenever pin RB4 goes high, an interrupt occurs and at this point the timer is reset to zero. We will also make use of the CCP module to generate an interrupt whenever the timer has counted the equivalent of 2 milliseconds. This will form the basis of our fail-safe.

So now, whenever an interrupt occurs we need to look at the state of the RB4 pin and the interrupt source to decide from which of the following three sources the interrupt came:

a) RB4 just went from low to high (we need to reset the timer)
b) RB4 just went from high to low (we need to read the timer to calculate the pulse width)
c) The CCP module is telling us that more than 2ms have elapsed since the start of the pulse (i.e. pwm value is outside the valid range)

By using these two interrupt sources, we do not need to worry about the frequency of the pulses, or about matching the timing of them. Whenever an invalid pulse width is read, the PIC goes into "standby" or error mode for a period of time, but when it comes out of that state, it can simply pick up on the next pulse that comes in - we don't have to worry about the PIC starting reading pulses from the wrong place in time (an effect referred to a phase-shift).

By following this approach, we should be able to read the value of the timer, whenever pin RB4 goes from high to low, and calculate the width of the pulse that generated the interrupt. If the pulse width is too short, we should put the PIC into a standby/error state. If the pulse width is too long, it will generate it's own error interrupt and will be handled accordingly. Any other value for the timer means we can see if it has passed a specific threshold value and toggle the on/off pin as necessary.

An alternative approach would be to poll the input pin(s) continuously, setting and resetting the internal timer as the pin state changed from high-to-low and back again. We may also explore this approach and compare the two.

1 Comments


http://www.tuug.fi/~isaarine/electronics/rc-switch/

Already been done using simple components. I keep telling Tom this, plus you can pick them up for £5 all in.

http://www.active-robots.co.uk/pololu-rc-switch-with-digital-output-p-1840.html

Robot Steve.

Post a Comment

whos.amung.us

Copyright © 2009 .Nerd Club All rights reserved. Theme by Laptop Geek. | Bloggerized by FalconHive Supported by Blogger Templates.