Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 05:58 25 May 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Rotary encoder interrupt issues

     Page 2 of 2    
Author Message
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4923
Posted: 10:17am 05 Nov 2024
Copy link to clipboard 
Print this post

  PhenixRising said  
Just thinking about this though; I run a program, stop it, make a code change, re-run the program and the encoder-count is preserved from the previous version.


Hi Phenix,

Don't count on that to work. It is not good practise to start a PIO program with registers in a unknown state. Therefore I am sure these will be reset to a known state, either by the SDK or by PM (Peter Mather), else by me.
If you want that you will have to pre-load a value through FIFO.
PicomiteVGA PETSCII ROBOTS
 
Andy-g0poy
Regular Member

Joined: 07/03/2023
Location: United Kingdom
Posts: 73
Posted: 10:26am 05 Nov 2024
Copy link to clipboard 
Print this post

  Volhout said  @Andy,

I looked at your original code, and I think it could be optimized to work better.
In MMBasic you can set an interrupt on a pin. If you set that for interrupt going high on clock, you do not need to check the clock being high in your interrupt routine.

...


Regards,

Volhout


Yes, I have tried that same issue more or less.

That code is a hangover from yet another test where I was trying a polling system rather than interrupt. I even cranked up the clock speed. That made a slight difference but not enough. I think it's going to be PIO code that solves the problem. At least it's looking easier every time I run through the code.

I'll have a go at coding your examples in the explaining PIO series which should get me going.

Thanks again,


Andy
 
Andy-g0poy
Regular Member

Joined: 07/03/2023
Location: United Kingdom
Posts: 73
Posted: 10:38am 05 Nov 2024
Copy link to clipboard 
Print this post

  PhenixRising said  


It might be that your 1000 pulse encoder has complementary outputs (A, -A, B, -B) for better noise immunity. I use the MC3486 line receiver to interface to the MCU.


The 1000 pulse encoder has the same outputs as the control knob version in the image A , B and the complimentary outputs as well. The control knob version mentions a AM26LS31 which is an RS422 driver, I can't 100% confirm that as I did not take the encoder apart that far and of course the electronics were on the reverse side of the main PCB :-)

It would not surprise me if the 1000 pulse shaft encoder used the same. There is no index pulse on either of the devices, so one less thing to worry about :-)
The intended use for these things is tuning controls for radio use. SI5351 VCO etc.

Andy
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4923
Posted: 11:07am 05 Nov 2024
Copy link to clipboard 
Print this post

Andy,

Since the start of the PIO series training, there has been one change in MMBasic that is important to remember:

In this training
PIO(EXECCTRL 0,a,b)

is used. The 0 designated the GP0 pin, but that was inconsistent with the rest of MMBasic that use either pin numbers or GPx identification. To enforce the use of GP0 (or any other GP pin) with 5.08.00 and newer the "GP" is required.

The user manual states correctly that you should use
PIO(EXECCTRL GP0,a,b)


But the early examples in the training may still use the "0" without "GP".
This is only with PIO(EXECCTRL   )

Volhout
PicomiteVGA PETSCII ROBOTS
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 1193
Posted: 11:11am 05 Nov 2024
Copy link to clipboard 
Print this post

  Quote  AM26LS31
 

Some line receiver options:
AM26LS32
SN75175
MC3486
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 1193
Posted: 11:23am 05 Nov 2024
Copy link to clipboard 
Print this post

  Volhout said  

Hi Phenix,

Don't count on that to work. It is not good practise to start a PIO program with registers in a unknown state. Therefore I am sure these will be reset to a known state, either by the SDK or by PM (Peter Mather), else by me.
If you want that you will have to pre-load a value through FIFO.


Yeah, not a good idea to take risks with axis positions  

Just been testing though; I set an encoder position register (12345), load and run a completely different program (no PIO stuff), re-load the quadrature program and interrogate the position register and it still holds the same value.

I guess we could store the current value in a tempVar, load the other PIO code, read the position register and compare it with tempVar.
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 1193
Posted: 10:43am 20 Nov 2024
Copy link to clipboard 
Print this post

  PhenixRising said  @Volhout

Hi Harm. I don't know if this is feasible but; apart from the A & B signals, there can also be a third "Index" signal (can be "C" or "I" or "Z"). This pulse occurs once/revolution and is used for accurate referencing (homing).

Currently, I have this triggering an input-interrupt and just like most CNC machines, the homing routine has to turn the motor very slowly to register a consistent home position.

The reason that other controllers have to do this slowly is because they only read the encoder during a PID update which is typically a 1KHz loop. So as per Nyquist, it's typical to use something like 250 quad-counts/sec homing speed (painful when 360° = 10,000 counts)

My PicoMite uses an interrupt which responds fast enough that I don't need to be so slow BUT...

It would be super-cool if this could be handled by the PIO where the main counter is set to zero (or a preset value) due to an index pulse transition.

Enable_Index_Reset = True


Hey Harm. I have this figured. If I configure this input as CIN, I don't need to worry about interrupt latency. When CIN > 0, I just slowly reverse until the CIN value doubles  
 
Andy-g0poy
Regular Member

Joined: 07/03/2023
Location: United Kingdom
Posts: 73
Posted: 09:56pm 26 Nov 2024
Copy link to clipboard 
Print this post

  Volhout said  Hi Andy_g0poy,

If you want to use this quadrature decoder as a way to learn PIO then please look at below code. This shows the assembly instructions for the PIO, rather than hex codes. There is also a thread on thebackshed forum where interactively parts of PIO are explained. It is quite a read, so reserve one or more evenings to go through it.

...

Volhout



Many, many, thanks for this, I've now tried it on the large encoder I have and it works very nicely. The only "problem" is that it's giving 400 counts per rev rather than 100

That should be fixable. The jump table seems to be giving 4 increments per cycle so maybe a tweak there will do the job, but that's for later.

What I have found  which is probably working as designed, is that the PIO assembler does not accept variables or pin numbers, it seems to only use the GPxx format.

I found this out when I changed the pin assignments from GP0 and GP1 to GP14 and GP15 which the breadboard was already wired to.

I ended up having to find and replace the GP0's and GP1's with 14 and 15, which makes the code  slightly awkward to move to different pins.

Is this the expected behavior?  

I'm using:
PicoMiteRP2040V6.00.00RC1.uf2


Andy
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4923
Posted: 10:11pm 26 Nov 2024
Copy link to clipboard 
Print this post

Pio only knows gp numbers, not pin numbers.
The actual gp numbers are in the pio Pinctrl configuration. The pio state machines use offsets from the gp number in the config.
PicomiteVGA PETSCII ROBOTS
 
Andy-g0poy
Regular Member

Joined: 07/03/2023
Location: United Kingdom
Posts: 73
Posted: 10:25pm 26 Nov 2024
Copy link to clipboard 
Print this post

Thanks,

I'll stick to GP numbers then.

Andy
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 1193
Posted: 10:33pm 26 Nov 2024
Copy link to clipboard 
Print this post

  PhenixRising said  If that's a 100 line encoder, you will get 400 quadrature counts/revolution


That's what quadrature is.
 
Andy-g0poy
Regular Member

Joined: 07/03/2023
Location: United Kingdom
Posts: 73
Posted: 12:41am 27 Nov 2024
Copy link to clipboard 
Print this post

  PhenixRising said  
  PhenixRising said  If that's a 100 line encoder, you will get 400 quadrature counts/revolution


That's what quadrature is.


It's sold as a 100 pulse per rev, but I suspect that you are correct and what they actually mean is 100 "clicks" per rev.

Multiple choices really, either divide the output by 4 or knock out three of the increments etc. Another option is to not treat it as a quad. encoder and just look at the state of the two lines when A is positive then B gives you the direction. As there is no bounce with these things that should be more than good enough for a simple tuning control.

Whatever it will be good PIO practice to have a play with.

Andy
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 1193
Posted: 06:47am 27 Nov 2024
Copy link to clipboard 
Print this post

Yes, there are physically 100 pulses per channel and the 2 channels are 90 deg out of phase. Quad-decode counts the rising and falling edges to provide direction and X4 multiplication.

This is a wonderful development for the PicoMite as I now have a product that has no equal.

High performance, closed-loop servo motion control with user-programmable sequencing.

Modern motion control has, IMO, taken a retrograde step in that; axis feedback is as far away from "the action" as it can get....on the back of the motor. I don't care about the rotor angle, I care about the true position of the axis and so I always install two encoders/axis. I have the motor encoder for primary feedback and a load encoder for true position. It used to be that the motor was fitted with a tach-generator for velocity control and the position feedback came directly from the load.

On a typical modern machine, the motor can be decoupled and left to spin freely and the control system would still believe that the machine is actually moving  

With the PicoMite, we can handle 4 encoders which not only facilitates dual-loop feedback but one could also tap-off the encoder feedback from other axes (other PicoMites) for perfectly synchronized electronic gearing (which I use all the time)

Exciting times  
Edited 2024-11-27 16:56 by PhenixRising
 
     Page 2 of 2    
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025