I think we're doing it wrong: normally open/closed

Post Reply
WayOutWest
Posts: 198
Joined: Thu Jul 16, 2015 12:18 am
Location: Washington State, USA

I think we're doing it wrong: normally open/closed

Post by WayOutWest »

The TinyG allows for switches to be "normally open" or "normally closed".

The conventional wisdom of CNC is to wire Single-Throw limit switches as "normally closed" so the chip's I/O pad is connected directly to a power supply rail (VDD) during normal operation and therefore as noise-immune as possible. A limit-hit is signaled by the switch opening, which leaves the chip I/O pad floating; presumably there is a weak pull-down resistor to drag it down to GND.

HOWEVER: we aren't using single-throw switches! Our switches are double-throw switches: normally they connect the chip I/O pad to VDD, and when the limit is hit they connect it to GND. With a double-throw switch neither position is inherently more noise-immune than the other: you've got the chip I/O pad directly connected to a power supply rail through the same amount of wiring either way.

HOWEVER-EVER: there is a twist. Atmel AVR chip I/O pads include an optional weak pull-up resistor, but not a weak pull-down. The weak pull is useful for detecting when a switch has become unwired for some reason.

HOWEVER-EVER-EVER: the TinyG ALWAYS ENABLES THE PULL UP, NO MATTER WHICH SWITCH TYPE YOU HAVE SET. I just verified this. This is kinda bonkers. This means that if a normally-closed-configured limit switch comes unwired the TinyG won't notice! Days or weeks later, the machine could smash itself to pieces if run unattended. Very very bad. It also means that single-throw switches simply won't work in normally-open mode, although I guess you shouldn't do that anyways. The TinyG ought to disable the AVR I/O pad pull-up when $st=0 (Normally Open).

Conclusion: we should wire our switches as Normally-Closed-to-GND (swap the "NO" and "NC" terminals on the switch, then tell TinyG they are "normally-open" even thought this is technically incorrect). This will have these consequences:
  • [li] Not any more or less noise-sensitive, because we are using double-throw switches
    [li] If a switch comes unwired for any reason (defective switch, broken wire, etc) it will register as "limit hit" and prevent further operation until repaired.
- Adam
JuKu
Site Admin
Posts: 1110
Joined: Thu Feb 14, 2013 3:06 pm
Location: Tampere, Finland
Contact:

Re: I think we're doing it wrong: normally open/closed

Post by JuKu »

TinyG has hardware pull-up resistors on the switch inputs. Theoretically, this means that single-throw normally open switches should work. When not engaged, the pad is pulled up by the resistor, when engaged, the switch takes the pad to ground. Also, normally closed switch works by having the pad connected to ground; when engaged, switch opens, the resistor takes the pad high and TinyG notices.

> With a double-throw switch neither position is inherently more noise-immune than the other: you've got the chip I/O pad directly connected to a power supply rail through the same amount of wiring either way.

In theory, this and what I wrote above is correct. But you know the difference of theory and practice? (In theory, none.) In practice, if the wiring is poor, we need all the noise immunity we can get. (I learned this the hard way... :oops: ) The noise margin of the AVR pins is bigger from high to low than from low to high; also, the noise immunity of the wire itself is better if it hard-wired to VDD than if left open. So, I think the way it is done now gives the best noise immunity. (If needed, it could be improved even further by putting a capacitor in the switch end of the cable; the capacitor and switch resistance would make a low-pass filter. So far, I haven't got anybody to report that this was necessary, though.)

> swap the "NO" and "NC" terminals on the switch, then tell TinyG they are "normally-open" even thought this is technically incorrect).

Taken the above, I believe this would not work right. It has been a long while since I had to troubleshoot wiring, so I might remember this incorrectly, but: I think TinyG looks only transitions, not a steady state. For reliability, this is not optimal, imo, nor is my software: After reset, any operation should start with homing and only with user override (if even that) should any other operation than homing be possible (This should be on my software, but is only my todo list). _When homing, TinyG should look at the switch state_, if in engaged position try to clear it, and alert the user if the switch does not change state in a few mm of travel. Only after that, start looking for transitions.

Looking only transitions means that an incorrectly wired switch might work if you get a switch bounce; you might test it and find it working, but Mr. Murphy says that you would get a clean switch action when the machine would have needed a properly working limit switch.

> This means that if a normally-closed-configured limit switch comes unwired the TinyG won't notice!

And this is a valid concern. Using a normally closed switch to GND and an unconnected switch should be caught. I'll notify the TinyG team and if this is improved, we should change the wiring and perhaps add some capacitors and resistors for noise immunity circuitry.
mawa
Posts: 139
Joined: Wed Jun 10, 2015 1:23 pm
Location: Near Hamburg, Germany

Re: I think we're doing it wrong: normally open/closed

Post by mawa »

Please guys take a look at

https://github.com/synthetos/TinyG/blob ... 0page1.pdf

There is a 2.7k pullup resistor to +3.3V and a .22µ capacitor to Gnd as a classic debounce circuit for ALL 8 XYZA switches.

That means that WayOutWest is right that using the current connected to Vdd solution a wire break will not be recognized due to the pullup.
best regards
Manfred
WayOutWest
Posts: 198
Joined: Thu Jul 16, 2015 12:18 am
Location: Washington State, USA

Re: I think we're doing it wrong: normally open/closed

Post by WayOutWest »

JuKu wrote: The noise margin of the AVR pins is bigger from high to low than from low to high;
Ah, I had not accounted for this, a very good point indeed.

I retract my claim that "with a double-throw switch neither position is inherently more noise-immune than the other". :)
mawa wrote:Please guys take a look at
https://github.com/synthetos/TinyG/blob ... 0page1.pdf
There is a 2.7k pullup resistor to +3.3V and a .22µ capacitor to Gnd as a classic debounce circuit for ALL 8 XYZA switches.
That means that WayOutWest is right that using the current connected to Vdd solution a wire break will not be recognized due to the pullup.
Thanks... this still worries me and I still think it's an issue, although Juha does have a good explanation of why normally-high has a larger noise margin. So there's a tradeoff either way: larger noise margin or broken-wire-detection, you can only have one.
- Adam
WayOutWest
Posts: 198
Joined: Thu Jul 16, 2015 12:18 am
Location: Washington State, USA

Re: I think we're doing it wrong: normally open/closed

Post by WayOutWest »

FWIW, I have confirmed that you can daisy-chain the limit switches, thereby freeing up an additional five GPIO pins (although they have a weak pull-up, so they have to be either outputs or else inputs that are always strongly driven).

Leave one limit switch (z-max in my case) wired as before. On each of the rest of the switches, replace the switch's VDD input with the output of the previous switch. The output of the last switch goes to the Tinyg limit-switch pin (y-min in my case). Disconnect the other five limit-switch pins; TinyG will pull them high.

Tested and everything still works, including z-probing. I recommend this modification!

Next thing I'll try is moving the limit-switch input to a pin that doesn't have a weak pull-up so I can put in my own weak pull-down. This will provide the best of all possibilities: keeping the larger falling-edge noise margin that Juha pointed out (and I neglected), but still having wiring failures register as limit-switch hits (so the problem gets fixed before the machine damages itself).
- Adam
Post Reply