An Electronic Lead Screw controller using a Teensy 4.1

True, but coding a simulator is risky in the sense that you write code on your main platform that runs against the simulator. The risk is the simulator really isn't like reality. I've seen that a lot. That said maybe I can cobble together some cheapo arduino to generate a signal train.

Clearly some testing with the real encoder will be required, but a lot of software development and testing will be more convenient with a signal source. An encoder is hardly a difficult device to simulate. He showed it in a video and mentioned it, but anything you have laying around can do the job, you don't need his design for that.
 
Last edited:
I can't get the Clough EncoderSimulator to compile since it is for an AVR board. Really don't want to put in the effort to port the code right now. I can buy a 5V 328 board for $12.50 from Adafruit that is powered off USB, which is good enough. 5V will be the same as the actual encoder, so I will have to buy the buffer chips as well. Spending $12.50 to save untold hours to port to an M0 or M4, is well worth it to me.
 
Your choice of course.

Four resistors will convert 5V quadrature signal to 3.3V, should be more than adequate for testing at these low frequencies.

Generating quadrature signal is four lines of code inside a loop. Add delay calls to vary rate. Read ADC to control rate with pot if desired. I would not even bother looking at his code.
 
Found some Teensy timer libraries with callbacks, so I can set the parameters in a more sophisticated fashion. Have one phase running at about 1 MHz, as viewed on my oscilloscope.. Still trying to get a one shot setup that is offset by 90 degrees. Have a glimmer of an idea on how to do it now. Maybe I'll figure it out tomorrow. Eventually I will drive the period and phase offset via another function.

On the hardware front, the encoder arrived today, a second Teensy 4.1, and a small 24V 5A power supply. Ordered some bus drivers and miscellaneous electronic stuff.

Kind of hard getting back into an arduino coding mindset. Haven't done it for a year, and it's surprising all the stuff that I unlearned!
 
Found some Teensy timer libraries with callbacks, so I can set the parameters in a more sophisticated fashion. Have one phase running at about 1 MHz, as viewed on my oscilloscope.. Still trying to get a one shot setup that is offset by 90 degrees. Have a glimmer of an idea on how to do it now. Maybe I'll figure it out tomorrow. Eventually I will drive the period and phase offset via another function.

On the hardware front, the encoder arrived today, a second Teensy 4.1, and a small 24V 5A power supply. Ordered some bus drivers and miscellaneous electronic stuff.

Kind of hard getting back into an arduino coding mindset. Haven't done it for a year, and it's surprising all the stuff that I unlearned!
If your interrupt routine changes the condition so the next interrupt occurs on the opposite edge you've got it. Changes like that may not be acknowledged by the H/W until the next clock to that subsystem....unless forced (or maybe the clock rate is high enough to not be an issue). Another possible way to do it: use an XOR gate to swap the sense of the input. You'd use another digital output to the XOR gate for that. When you do that, the relevant digital input will become inverted so you'd need to make sure that doesn't result in an interrupt. That would surely cause a race condition you want to avoid.
 
I would make a small 4-state machine in the interrupt handler to generate A and B outputs in quadrature.
 
Made some sort of quadrature state machine this morning. Have it running at a period of 1us, So there is an interrupt every 250ns. I can change the direction ie, make A lead or lag B. Working on dynamically changing the period. A period of 1us, is much lower than I will ever see on the real lathe. Been using the TeensyTimerTool. As usual, with many libraries, not quite fully documented. However, the author is active on the PJRC forum, so has been very helpful to me. Very grateful for that.

Found the bug where my program was changing the period faster than the period was! I have to wait an integer number of periods before changing, else it gets difficult to unravel what is going on. Somehow, I'll get this going, but am making progress!
 
More stuff arrived. A baby NEMA 11 stepper, and a small stepper driver. I'd hook everything up, but alas, I don't have buffers.

Guess I could just use some NPN transistors like a 2N2222A to drive the optocouplers. Just a series resistor to the base, a pull up on the collector and set the emitter to ground. Logic High on the base will turn on the transistor. If the DIR+ is connected to +5V, then I connect DIR- to the collector. Logic HIGH should set DIR to whatever direction, and logic LOW sets DIR to the opposite direction. I'll actually think about this some more, to make sure I don't overdrive the opto-coupler, but that is the basic idea. Well, looked in the parts bin, I only have 2 2222A transistors left. Drat. Guess I need some more of them! Found an evil Bay listing for 100 for $6.29. That ought to keep me going for a bit!
 
Now have a sawtooth encoder waveform of sorts. There are four cycles of the encoder at a period, then the period is increased, four cycles at the new period and so forth until the maximum period is attained. Then the period is reset to the shortest period and the direction reversed. The minimum period of a complete cycle is 1 us. So each quadrant of the cycle is 250 ns. Max period is arbitrary. I have tested it to 100us then out to 5ms. Now to look at the decoding of this.

Pic with not reversing, but increasing waveform.
SDS01setPeriod.jpg
 
Last edited:
Nice progress. For software testing one might want a knob adjustable rate, but a sweep is useful.

I was looking at the Spindle Encoder. The one I have appears to have Open Collector outputs, so perhaps strong pullups to 3.3V could be used, avoiding a level shifter altogether. Might be worth testing. With proper cabling and grounding it might not be necessary to go to receivers or differential signaling.

Omron rotary encoder E6B2-CWZ6C

5-24V supply voltage, reverse polarity protected
100 khz max output frequency
6k rpm max
max shaft loading 30N radial, 20N thrust
1024 pulses per revolution, 4096 edges
a,b,z outputs, open collector output npn to 30v, 35mA
 
Last edited:
Back
Top