Hi all,
It's been a while since I've posted on this forum, but I need some advice on a Teensy project I'm doing. I have a Brushless motor with 3 Hall sensors. The motor has 7 pole pairs that each are being read by the Hall sensors at 5000 rpm. Controlling the motor is done using a separate motor controller, but I need to convert the Hall Sensor input into an A/B quadrature Encoder signal for another device. My first idea was using the Encoder library for Teensy, but that only supports two inputs. I then figured that I'll do a simplistic approach using Interrupts and I came up with the code below.
It works quite well. I can get direction and position out, but even at moderate speeds, I'm loosing some "steps" so the position on the motor controller will differ from the one on the Teensy. I could of course throw a Teensy 4.0 at it and hope that the extra speed solves the issue, but I can't help but think that there are others that have done similar things and come up with a better solution? I'd love to hear how others have solved similar problems.
It's been a while since I've posted on this forum, but I need some advice on a Teensy project I'm doing. I have a Brushless motor with 3 Hall sensors. The motor has 7 pole pairs that each are being read by the Hall sensors at 5000 rpm. Controlling the motor is done using a separate motor controller, but I need to convert the Hall Sensor input into an A/B quadrature Encoder signal for another device. My first idea was using the Encoder library for Teensy, but that only supports two inputs. I then figured that I'll do a simplistic approach using Interrupts and I came up with the code below.
It works quite well. I can get direction and position out, but even at moderate speeds, I'm loosing some "steps" so the position on the motor controller will differ from the one on the Teensy. I could of course throw a Teensy 4.0 at it and hope that the extra speed solves the issue, but I can't help but think that there are others that have done similar things and come up with a better solution? I'd love to hear how others have solved similar problems.
Code:
#define HALLPIN1 10
#define HALLPIN2 11
#define HALLPIN3 12
volatile unsigned int lastPin = 0;
volatile signed int dir = 0;
signed int dirDisplay = 0;
volatile signed long pos = 0;
volatile signed long lastChange = 0;
signed long prevPos = 0;
void setup() {
pinMode(HALLPIN1, INPUT);
pinMode(HALLPIN2, INPUT);
pinMode(HALLPIN3, INPUT);
attachInterrupt(digitalPinToInterrupt( HALLPIN1 ), hall1, CHANGE);
attachInterrupt(digitalPinToInterrupt( HALLPIN2 ), hall2, CHANGE);
attachInterrupt(digitalPinToInterrupt( HALLPIN3 ), hall3, CHANGE);
}
void loop() {
// nada
}
void hall1() {
if( lastPin == HALLPIN3 )
{
dir = -1; // backward
}
else if( lastPin == HALLPIN2 )
{
dir = 1; // forward
}
pos += dir;
lastPin = HALLPIN1;
}
void hall2() {
if( lastPin == HALLPIN1 )
{
dir = -1;
}
else if( lastPin == HALLPIN3 )
{
dir = 1;
}
pos += dir;
lastPin = HALLPIN2;
}
void hall3() {
if( lastPin == HALLPIN2 )
{
dir = -1;
}
else if( lastPin == HALLPIN1 )
{
dir = 1;
}
pos += dir;
lastPin = HALLPIN3;
}
Last edited: