# The Twisted Guts of It All

A project log for The SkeinTwister

Automating the skein-twisting process for indie dyers; eliminating repetitive stress injuries and helping their product look more consistent

Alpenglow Industries 05/26/2016 at 21:160 Comments

How does it all work, anyway? I've explained that generally, a knob sets the desired twist firmness. Press a foot pedal to turn the twister on, and it'll automatically turn off when it gets to the set firmness. But how do is firmness calculated and compared?

Yarn is essentially a big spring. As the skein gets more and more twisted, the amount of torque required to keep it twisted goes up. I'm using a brushed DC motor, which means that current is directly proportional to torque.

Torque is difficult to measure directly (from a pure electronics perspective), but current is easy. So instead of setting a torque limit, we can set a current limit. First, we'll need to measure the motor current. Here's the schematic, for reference (also in the "Files" section as a pdf).

Starting on the GND side of the motor, there's a FET. Ignore it for now. Next is a 0.005 ohm current sense resistor (it's really just a calibrated thick bar), and the voltage across the resistor goes into an INA199 current sense amplifier. The motor can pull about 5A before it stalls. Using trusty:

means that the voltage across the resistor at 5A is 0.025V. We're running the processor off 5V and would like to use that entire range to sense current, so we can really dial it in. 5V/0.025V = 200. It just so happens that the B3 model of the INA199 has a gain of 200V/V, so it's perfect. Add a little TVS clamp to prevent any accidental over-voltage from damaging the processor pin, and a little RC filter to smooth out the signal. That signal then goes into the AIN0, or the "positive" input to the analog comparator, and is a measurement of our motor current.

Now we need to be able to set the current limit. Really, we don't even need to know what the exact current or torque is, users don't know this piece of information and will just turn the knob until the skein feels right. So a simple trim pot that varies the comparison voltage from GND to 5V works great. Moving the pot towards GND will be less torque and a "softer" skein, moving it towards 5V will be more torque and a "harder" skein. This voltage is fed into the AIN1 input, or the "negative" input to the comparator. When AIN0 > AIN1 (or motor current > the setpoint), the comparator generates an interrupt.

Next, we need some method of telling the motor when to turn on and turn off. A momentary foot pedal is great for this. Feed this into a digital input that is internally pulled high. When the foot pedal is depressed, the pin is shorted to GND. When the foot pedal is in the normal position, the pin is pulled to 5V. This pin is also INT0, meaning it generates an interrupt. I've set this to falling edge detection, so when the pin goes from 5V to GND (the foot pedal is depressed), the interrupt fires.

Finally, we need to be able to actually turn the motor on and off. The dirtiest, simplest method is to just use an N-channel FET on the GND side. When the gate is high, current flows through the FET and the motor is turned on. When the gate is low, no current flows through the FET and the motor is off. Important (but not shown on the above sketch) is that there's an additional diode across the motor, to prevent voltage spikes when the current is switched on/off.

Putting it all together:

• The foot pedal is pressed, generating an interrupt.
• The interrupt routine toggles the FET from off to on. Motor turns on.
• If the foot pedal is pressed again while the motor is on, the interrupt routine again toggles the FET. This time the motor turns off.
• Otherwise, when the motor current reaches the setpoint, the comparator interrupt fires.
• The interrupt routine sets the gate to GND and turns the motor off.

That's it! I've included the C code in the files section. Nothing happens in the main loop, it's entirely interrupt-based. I may eventually add a soft start (instead of turning the FET fully on, send a PWM to the gate of the FET which slows ramps it up over ~ 0.5s). This will decrease the start-up current spike, and allow for lower knob settings (more softly twisted skeins).