Timers, counters, and Cogs Oh My!
AshTillotson
Posts: 2
Im looking to build a small controller using a Prop that looks at 2 inputs to control an Up/Dn Timer, then outputs a frequency that correlates to the Timer Value.
For demonstration Purposes I am using a Dribble technique to open and close a linear valve that takes 5 full seconds to go from closed to open and vise versa. There is a Product Flow Meter that measures flow in Gallons. This meter is designed for 700 Gallons per minute, so if my Timer is at 2500 mSec then my Valve should be at 50% open which means the meter will output 350 gallons per minute with a frequency of 291.66 hz
MaxTime = 5000 mSec
MaxFlow = 700 Gallons per min
PPG = 50 pulses per gallon
For example,
If Pin1 and Pin2 are High then a Timer counts up to Max Time. (for testing this would be 5000 milliseconds)
If Pin1 and Pin2 are Low then a Timer counts down to Zero.
(This Timer only counts up or down while this condition exists, so If from zero, both pins are high for 1.487 seconds then the Timer is held at 1.487 seconds. If an hour later they are then low for .309 seconds then the new Timer value is held at 1.178)
If Pin1 <> Pin2 then do nothing added for clarity
FreqOut = (Timer/Maxtime)* (Maxflow * PPG)/60S
Pin 16 = FreqOut
In this example the Timer value represents valve position. (It takes 5 seconds to Fully open and 5 Secs to fully close a valve)
Using 2 inputs in this fashion replicates the function of Dribble control.
The output frequency represents the (Timer value/Maxtime*Maxflow*pulses per gallon /60 sec) to create a pulse train (50/50 duty cycle prefered) that correlates to Valve position.
(I am assuming perfect linearization between valve position and flow rate which is virtually impossible, however it will work well for my needs)
If this could fit in one Cog that would be best as I need 4 of these and probably a 4 line LCD in the main Cog. For a total of 5 cogs. The display is not really necessary its just an added feature to help the project look a little more professional. For now I would likely use the TV out or the Serial Debug screen.
The Real Reason I want to build this:
I am a Maintenance Technician and The company I work for is giving me some artistic freedom to help build a test lab and If I produce a piece of test equipment that can simulate loading conditions in order to test our own systems then I will be trusted to expand this concept to everything that I do. There will literally be a dozen different controllers in my office used for troubleshooting and testing of various things for years to come with the Propeller Chip.. The best part is, embedded programming is a hobby of mine and I will finally get paid to do what I love! Before I started playing with the Prop Chip, I was creating a 5 chip Microchip platform where 4 (Pic 12fs) chips were virtual slave Cogs to the main (Pic 18f) using i2c to poll. Half way through my design I discovered the Prop and was blown away, but I havent had much time until now to get back into Embedded Programming. But today, I Begin!
Q1. If Im using 1 Counter for Timer and 1 Counter for FreqOut I think that this little bit of code would fit in one cog as there are two counters per cog. No?
Q2. Our automation system that uses Triacs for its outputs so the inputs are actually 110 VAC (Some bleed Voltage present, even when off) to drive an AC Input Opto coupler to the Prop Chip. Does anyone recommend an optocoupler I should use that wouldnt be triggered by the leaked voltage from the Triac output? (Should I use a Pull down resister?)
Q3. The pulse train will need to be 12 DC to the automation system and I think I can use a 2n2222 transistor but I was thinking of using an optocoupler for that. Any recommendations?
All opinions are welcome and ANY help with code would be awesome. I would like to program in C however, if Spin is quicker for getting started then thats fine too. I can always convert later.
Thanks so much,
Ash Tillotson
For demonstration Purposes I am using a Dribble technique to open and close a linear valve that takes 5 full seconds to go from closed to open and vise versa. There is a Product Flow Meter that measures flow in Gallons. This meter is designed for 700 Gallons per minute, so if my Timer is at 2500 mSec then my Valve should be at 50% open which means the meter will output 350 gallons per minute with a frequency of 291.66 hz
MaxTime = 5000 mSec
MaxFlow = 700 Gallons per min
PPG = 50 pulses per gallon
For example,
If Pin1 and Pin2 are High then a Timer counts up to Max Time. (for testing this would be 5000 milliseconds)
If Pin1 and Pin2 are Low then a Timer counts down to Zero.
(This Timer only counts up or down while this condition exists, so If from zero, both pins are high for 1.487 seconds then the Timer is held at 1.487 seconds. If an hour later they are then low for .309 seconds then the new Timer value is held at 1.178)
If Pin1 <> Pin2 then do nothing added for clarity
FreqOut = (Timer/Maxtime)* (Maxflow * PPG)/60S
Pin 16 = FreqOut
In this example the Timer value represents valve position. (It takes 5 seconds to Fully open and 5 Secs to fully close a valve)
Using 2 inputs in this fashion replicates the function of Dribble control.
The output frequency represents the (Timer value/Maxtime*Maxflow*pulses per gallon /60 sec) to create a pulse train (50/50 duty cycle prefered) that correlates to Valve position.
(I am assuming perfect linearization between valve position and flow rate which is virtually impossible, however it will work well for my needs)
If this could fit in one Cog that would be best as I need 4 of these and probably a 4 line LCD in the main Cog. For a total of 5 cogs. The display is not really necessary its just an added feature to help the project look a little more professional. For now I would likely use the TV out or the Serial Debug screen.
The Real Reason I want to build this:
I am a Maintenance Technician and The company I work for is giving me some artistic freedom to help build a test lab and If I produce a piece of test equipment that can simulate loading conditions in order to test our own systems then I will be trusted to expand this concept to everything that I do. There will literally be a dozen different controllers in my office used for troubleshooting and testing of various things for years to come with the Propeller Chip.. The best part is, embedded programming is a hobby of mine and I will finally get paid to do what I love! Before I started playing with the Prop Chip, I was creating a 5 chip Microchip platform where 4 (Pic 12fs) chips were virtual slave Cogs to the main (Pic 18f) using i2c to poll. Half way through my design I discovered the Prop and was blown away, but I havent had much time until now to get back into Embedded Programming. But today, I Begin!
Q1. If Im using 1 Counter for Timer and 1 Counter for FreqOut I think that this little bit of code would fit in one cog as there are two counters per cog. No?
Q2. Our automation system that uses Triacs for its outputs so the inputs are actually 110 VAC (Some bleed Voltage present, even when off) to drive an AC Input Opto coupler to the Prop Chip. Does anyone recommend an optocoupler I should use that wouldnt be triggered by the leaked voltage from the Triac output? (Should I use a Pull down resister?)
Q3. The pulse train will need to be 12 DC to the automation system and I think I can use a 2n2222 transistor but I was thinking of using an optocoupler for that. Any recommendations?
All opinions are welcome and ANY help with code would be awesome. I would like to program in C however, if Spin is quicker for getting started then thats fine too. I can always convert later.
Thanks so much,
Ash Tillotson
Comments
1. there is no counter mode which supports 2 input pins as you described it (counting up and down)
2. for the counters there is no limit, they simply go round and round without COG intervention
So, this means that I would simply implement some code to count up and down and avoid to exceed the limits. But here you have to answer some more questions:
Is it fine to sample the input pins? Meaning that you simply look at the pins each 1ms and count up/down according to the result?
Having dedicated code for the timer on the other hand frees a counter to generate the frequency. This way you can have 1 COG to do the counting for 2 timers and output 2 frequencies. So in the end for your 4 units, you'd only need 2 COGs.
I'd go with SPIN/PASM.
This is what the main loop in the cog might look like:
I have played with Jonnymac's code and got some success.
I do have a few issues with "timer := ++timer <# MAX_TIME" For some reason this will not pass the variable "timer" to "pub Product1(counts)".
When I reverse "<# " to " #>" It does pass the value but of course it will not limit it to Max_Time.
The other big issue is my main time loop needs to increment "timer" accurately but I'm pretty sure its taking too long. When I hold down both inputs for 5 seconds i usually get about 1200 ms which ultimately affects the Frequency.
I and using 2 objects found in the demo files of the Spin compiler.
I can attach if necessary.
"Synth" - Creates a nice 50/50 duty cycle square wave.
"Parallax Serial Terminal" - puts my data on the serial terminal.
Here is the Code: - many thanks to JonnyMac
I have added many comments and questions to the code for clarity.
Either opto or transformer.
Hardware and software can be replaced.
Wetware can't...
My industry standards are a (few decades?) out of date, so there may be better solutions out there now.
But Opto 22 is still making and selling parts.
http://www.opto22.com/
Thanks, Jon.
A "timely" suggestion (if you'll pardon a bad pun).
I've been having the same problem.
FF