Hi Hank,
Why not just use count statement, store count in a variable ?
Looks like you want to find the end of each revolution and then increment the feed over so the wire does not overlap, yes?
Hi Hank,
Why not just use count statement, store count in a variable ?
Looks like you want to find the end of each revolution and then increment the feed over so the wire does not overlap, yes?
If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
.
Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
.
There simply is no "Happy Spam" If you do it you will disappear from this forum.
just another thought on my last post. If you check the state of "magnetic_switch_closed" and jump into you're code where you increment your counter. Before you jump out and look for it being closed again, you should look for it to be open.
If not then you could count the same swith close multiple times all for the same instance.
something like:
ShaneCode:if RB7pin10 = 1 then main ' make sure the switch opens before looking for another close Else stay in loop...
Firstly ....Shane/Joe, thank you for taking the trouble to respond (esp to a basket case of a kludger like me!)
Ok, to your points...
Shane you are right about the temp variablle needing to be set to zero...I didn't post up that bit (my program has a lot of variables going on, but I stripped it back to basics so as not to cloud the issue...but I can see it's caused probs!)
Next....I think you've nailed the issue (my head was fogged last night after realising my program wasn't counting the revs right!)...essentially, my program traps the switch being closed (sort of!), but not the corresponding open. Therefore in reality, my program could see the switch being closed, head off to do some stuff elsewhere in the program, come back to check the switch & by bad luck/timing the next magnet could be passing so it thinks the same (original) magnet is closing the switch. therefore I need to wait for the switch to close, then open, then have my program act.
Joe... re the counter/variable melarkey - remember, I'm very new to this. I've almost certainly done a tutorial somewhwere & simply applied what I learnt there to my program! (I haven't a lot of time to dedicate to being a slick programmer, so I just throw into the program the little bit that I know!). Could you be so kind as to outline the tighter code for that little increment you've honed in on as being a bit 'first lesson at PICBASIC evening class'!). BTW ...I use this magnet/switch 'count' to derive when to send a pulse to a stepper. This stepper I'm sending the pulse to, moves in synchronicity with the main motor, back & forward (the idea being to neatly feed coppoer wire on to the main turning motor). It's really cool on the odd (seemingly fluke) times I've had it working!
Shane... the magnets are actually glued around the circumference of a wooden ring (& this ring itself is glued to the fan blades!). The diameter of this ring is about 10cm (4" if you're still in imperial) which makes for a circumference of 31cm ...the magnets themselves are about 1cm wide & there are 10 of them. So the magnets take up a third of the circumference - meaning a duty cycle of 33%?
Therefore, at a maximum of 350RPM, the magnets will pass by the switch at about 58 times per second. This works out at every 17ms....because of the magnet width, this yields a duty cycle of 33% - therefore the switch will actually be closed for about 5.6ms, every 17ms
Now my head still spins at the thought of building in the debounce aspect, so
with these 'knowns' I have, which would be the best approach to ensure an accurate switch 'closing' count?
(BTW, I'm thinking now that maybe an optical pickup would be a much better solution, as this removes the debounce aspect & I can have more resolution -I will look into this, but for now I'd really like to get the cheap 'n dirty solution working!)
Building a debounce isn't too hard, if it's even needed. It just depends on the magnetic switch you are using, if it's solid state you won't need to do much more than check if it's closed, being the way I am, I'd double check it again before actually stepping the moter etc. If it's a mechanical relay, that will need a "real" debounce and maybe some additional external hardware, like a cap.
To debounce you need a loop that checks if the switch is closed. If it is then increase a debounce var. and check it again, if it's not closed set your debounce var = 0 and keep checking. Determine the amount of time / number if times you want to increase your debounce var. you can put a pauseus 100 and recheck if it's still closed, and when your debounce var = 10 then you know the switch has been closed for at least 1 ms ( 100 us * 10 = 1ms)
Joe S. had the idea of using count, I've used it and it's simple.
The above code counts the pulses on porta.1 over 150 ms and stores the value in the Var "freqval".Code:count porta.1, 150 , freqval
In your case you would count for 10 ms? something like this:
Joe S. may have a better idea, and better code for you, above is where I would start, I'm no pro at this myself. I'd use the above code as an example, I just typed it out, so it still needs more work.Code:Counting_Mags: count portx.x, 10, mag_count ' count port x pin x for 100 ms Num_Mags = Num_Mags + mag_count if Num_Mags > 9 then One_Rev ' counted at least ten magnets so one revolution has occured, so step motor. goto Counting_Mags One_Rev: Num_Mags =0 ' reset count 'do the rest of you stuff here Goto Counting_Mags ' go back and count another revolution
<b>HA HA HA, ME EITHER !</b> I wrote a Tachometer program, using count, look it over as an exemplar of using count. The code is in post #9. http://www.picbasic.co.uk/forum/showthread.php?t=9037
Optical can be cheap and reliable, magnetic is good too, thinking hall effect switch. Do you really need 36 degree resolution (10 magnets)? Using an interrupt might be your solution to all this. I do not think I would use a magneto mechanical (reed) switch as a pickup, but using a hall effect would free you of all that debounce nonsense. Sam used my code with 1 magnet for his Tach. and says it works well. As for optical, tear open an old mouse and you will find a PIC and 2 optical pickups using shutter wheels. People use printed encoders for tabletop robots all the time. A coil of wire, magnets, and a comparator input will work too, that is what works most auto ignitions, in fact I used an old car distributor while working out my tach. code. A 555 timer can be used as a wave shaper if you are not ready to try comparators yet.
Last edited by Archangel; - 28th April 2009 at 18:20.
If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
.
Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
.
There simply is no "Happy Spam" If you do it you will disappear from this forum.
Joes' idea for the hall effect sensor would be the way to go. Then you could simplify the
whole thing with a timer configured to count external pulses. Preload the timer to over
flow after 500 ticks, and have an interrupt flip the port pin to kill the motor.
The timer counts in the background, so you can have all sorts of other things happening,
code wise in the meantime. And with zero code for counting pulses.
If you prefer to sit & spin in a loop until killing the motor, you could just monitor the timer
overflow bit, and not use any interrupts.
Alas, my existing switch setup is a simple reed switch (so I have to debounce).
I've done some googling today & think that optical is the way to go. I'll probably use this guys implementation - a CD with some black stripes drawn on it!
http://www.vk2zay.net/article/29
http://www.vk2zay.net/article/28
(but perhaps having it feed into a monostable to clean those pulses up a bit)
Firstly, it appeals to the cheapskate in me, but more importantly, I can get even finer resolution - surprisingly 36 degree resolution isn't really sufficient...I'd much rather have 10 degrees or less
Totally off topic, but here's the maths.
Let's I'm feeding 0.15mm wire onto the main motor (actually the main motor holds a steel rod in jaws, upon which the copper wire feeds onto forming a coil)
So, for every turn of the main motor, I need the stepper motor (feeding the wire onto it), to traverse by 0.15mm.
My stepper is a 7.5 deg motor (meaning it takes 48 pulses to get it to turn one full revolution). I've placed some M8 threaded rod over the stepper shaft (this has a pitch of 1.5mm)....it is via this thread that I wrap the copper wire around feeding onto the main motor, if you can't visualize this...have a look here for a similar setup about 23 seconds in...he's using *much* chunkier wire, mine is more akin to thick hair).
Therefore for every full turn of the stepper (48 pulses) the wire moves 1.5mm (this being the thread pitch). But I *need* it to move just 0.15mm for every turn of the main motor. So to get the stepper to move the wire just 0.15mm, I need to send 4.8 pulses to the stepper for every turn of the main motor - obviously, I can't send 0.8 of a pulse to the stepper! Therefore if I had more tach resolution, I'd avoid such 'rounding' errors (or as I'm doing now, having to add in extra pulses every so often to make up for the shortfall)
Last edited by HankMcSpank; - 28th April 2009 at 20:17.
Hi Hank,
48 steps per rev, wow most steppers are 200 steps per rev. Saw the video. 1 Q & D way to get resolution in accurate form, Order from your local speed shop a degree wheel for setting cam timing, has 360 degrees embossed on an 8 inch aluminum disc. you can drill holes at exact spacing or use it as a protractor to make other discs.
If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
.
Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
.
There simply is no "Happy Spam" If you do it you will disappear from this forum.
Bookmarks