L298N + NEMA17 + Teensy + Quadrature Encoder

I am trying to implement FOC using my NEMA 17 with a cheap L298N driver. I have been able to successfully test my Encoder separately. I have been able to get my NEMA 17 to work with velocity_openloop control. Whenever I try to get a close loop to nothing works. Not sure what am I doing wrong. Below is my code:

#include <SimpleFOC.h>

// Stepper motor instance
StepperMotor motor = StepperMotor( 50 );
// Stepper driver instance
StepperDriver4PWM driver = StepperDriver4PWM (15, 16, 17, 18);
Encoder sensor = Encoder(13, 14, 600);
void doA(){sensor.handleA();}
void doB(){sensor.handleB();}
// commander interface
float target_angle = 0;
Commander command = Commander(Serial);
void onTarget(char* cmd){ command.scalar(&target_angle, cmd); }

void setup() { 
  // enable/disable quadrature mode
  sensor.quadrature = Quadrature::ON;
  // check if you need internal pullups
  sensor.pullup = Pullup::USE_INTERN;
  // initialize encoder hardware
  sensor.enableInterrupts(doA, doB);
  // link the motor to the sensor

  // driver config
  // power supply voltage [V]
  driver.voltage_power_supply = 12;
  // link driver

  // set motion control loop to be used
  motor.controller = MotionControlType::angle;
  motor.PID_velocity.P = 0.2;
  motor.PID_velocity.I = 20;
  motor.PID_velocity.D = 0.001;
  // jerk control using voltage voltage ramp
  // default value is 300 volts per sec  ~ 0.3V per millisecond
  motor.PID_velocity.output_ramp = 1000;

  // velocity low pass filtering
  // default 5ms - try different values to see what is the best. 
  // the lower the less filtered
  motor.LPF_velocity.Tf = 0.01;

  // angle P controller -  default P=20
  motor.P_angle.P = 20;

  //  maximal velocity of the position control
  // default 20
  motor.velocity_limit =20;
  // default voltage_power_supply
  motor.voltage_limit = 5;

  // initialize motor
  // align sensor and start FOC

void loop() {
  // iterative FOC function
  target_angle = 20;
  // function calculating the outer position loop and setting the target position 

Hi @Bir_Bikram_Dey,

Welcome to the forum :slight_smile:

Did you solve your problem already? I have not worked with your setup but maybe I can still help you a bit!

I do not see an error in your code by looking at it, but maybe I missed something. That open loop is working and that your sensor is working, is great already!

Does your motor stop working after calibration? Or does the calibration also not work?

Teensy recently got support for SimpleFOC, which version are you using? Is it possible to check it with another microcontroller to see that it is not Teensy related?

Hi @Wittecactus,
I haven’t been able to make it work the way I wanted it to. I didn’t see any calibration steps? How do I do calibration?
I have also tried to change teensy to Arduino Due. It still didn’t work.
I just got my setup to work by changing the motion control type to “torque”.
Really needed the “velocity” control type to work. :frowning:

Hi @Bir_Bikram_Dey,

I meant the motor and sensor initialisation that can be seen here in the video.

Did you also look at this example in the docs?

Oke, so it is not Teensy related because it also does not work on the Arduino Due so we can exclude that! Weird that it does work on torque but does not work in velocity mode. I do not think I can help you much further… I do not have this hardware and therefore I can not test it. Maybe there is someone else that can help you further on this forum. I hope you can get some info from the video or example and good luck with solving the issue!