Open tuxcnc opened 3 months ago
To be honest I'm not sure if it's possible or not, but this microcontroller is already stretched pretty thin.
To schedule the main loop tasks, I'm incrementing counters for them in the 1kHz servo thread, which will be checked in the main loop to see if enough time has passed. I suppose you could do a similar thing in the base thread, eg. increment a value and when it gets to 50 then the main loop knows to do one 'servo' update.
I think that would run ok, but the real question is how much disruption the massive number of interrupts coming from the spindle encoder would cause. I'm not familiar enough with the use case of such encoders to know how much precision would be acceptable.
Another thing to consider is that if you're running a lathe, then you probably don't care about the PWM signal for the spindle, right? I don't know which timers are "capable for hardware quadrature", but if TIM1 is not acceptable maybe the other timers could be shuffled around to free up one that is.
Another thing to consider is the 'rotary encoder' feature which already exists, using interrupts. It was mainly intended for low speed inputs (eg. dial turned by human hands) but for all I know it might work ok at high speed too. I don't even know what speeds are required.
To summarize I really don't know anything about the use case or hardware requirements, and I don't personally have the need for this myself. So.... feel free to experiment yourself :)
The STM32F103C8T6 has 3 counters capable for hardware quadrature encoders. I found all are used.
extern TIM_HandleTypeDef htim1; // spindle PWM extern TIM_HandleTypeDef htim2; // timer compare used to reset step pins after step, all 4 channels extern TIM_HandleTypeDef htim3; // servo thread 1kHz extern TIM_HandleTypeDef htim4; // base thread 50kHz
But... The servo_thread is 50*base_thread, so it can be service by one timer, I suppose.
Is it possible to free one timer and use it for hardware quadrature encoder ?