1st simpleFOC speed control sketch issue

My config:
Windows 11
Arduino UNO
simpleFOC shield V2.03
AMT 103 2048 PPR
BGN\M4108-130T

The code below is my attempt to make a simple motor speed control. It initializes and syncs the motor and encoder, but then it just moves about a quarter turn and stops.What needs to change to keep it rotating at a selected speed?

#include <SimpleFOC.h>
int16_t loop_counter = 0;

BLDCMotor motor = BLDCMotor(12);
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);

Encoder encoder = Encoder(2, 3, 2048);

void doA() {
  encoder.handleA();
}

void doB() {
  encoder.handleB();
}

void setup()
{
  encoder.init();
  encoder.enableInterrupts(doA, doB);

  driver.voltage_power_supply = 16;
  driver.init();

  motor.torque_controller = TorqueControlType::voltage;
  motor.controller = MotionControlType::torque;
  motor.linkSensor(&encoder);
  motor.linkDriver(&driver);
  motor.voltage_limit = 3;
  motor.init();
  motor.initFOC();
}

void loop() 
{
  motor.loopFOC();
  if (loop_counter++ > 25)
  {
    // set the motor speed
    motor.move(5);
    loop_counter = 0;
  }
}

Make sure you have the BLDCMotor pole count correct. Most 4108 motors are 24N22P, so should be 11 pole pairs, not 12.

As a side comment, you can achieve what you’re doing with the counter in the main loop also by setting:

motor.motion_downsample = 25;

then you don’t need the check in the loop and can just call:

motor.loopFOC();
motor.move();

:slight_smile:

In terms of the actual problem, I think the pole pair number is a good bet for what may be wrong.

Another potential problem could be the high PPR count of the encoder - at 2048 PPR in quadrature mode that’s 8196 interrupts per revolution that the UNO has to handle. If it gets overloaded and starts to miss interrupts, the measured position will drift compared to the real one, and you will lose control of the motor (because the electrical zero will be wrong).

I am now using the downsample option rather than a separate loop counter… Thanks!

Regarding the pole count, it looks like 24 poles to me. Here is a picture.

I’ll see what I can do about the 2048 PPR. There are switches that allow for different PPR.

I turned off quadrature mode to reduce the interrupts to 2048 and still don’t get the expected movement.
Here is the current code. What else might be wrong?

#include <SimpleFOC.h>

BLDCMotor motor = BLDCMotor(12);
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);

Encoder encoder = Encoder(2, 3, 2048);

void doA() {
  encoder.handleA();
}

void doB() {
  encoder.handleB();
}

void setup()
{
  encoder.quadrature = Quadrature::OFF;
  encoder.init();
  encoder.enableInterrupts(doA, doB);

  driver.voltage_power_supply = 16;
  driver.init();

  motor.torque_controller = TorqueControlType::voltage;
  motor.controller = MotionControlType::torque;
  motor.linkSensor(&encoder);
  motor.linkDriver(&driver);
  motor.voltage_limit = 3;
  motor.init();
  motor.initFOC();
  motor.motion_downsample = 25;
}

void loop()
{
  motor.loopFOC();
  // set the motor speed
  motor.move(5);
}

Despite it looking to me like the motor has 12 poles, I set it to 11 and now it does the expected rotation.
It also requires the encoder quadrature mode to be OFF. With those two changes, it works as expected. Thanks for the help!

Hey,

Yeah, it looks to me like the motor has 24 electro-magnets on the stator, but the pole-pairs is the number of permanent magnets on the rotor, divided by two.

That’s something to look into… I think we actually have some code examples for encoders in the examples directory that use the AMT 103. Perhaps it’s a CPR/PPR confusion and the correct setting is 512 PPR in quadrature? Or maybe it’s just that the 8196 really was too much for the Arduino at the speed you were testing.

In any case, its good to hear it is working!

I just tried 512 with quadrature ON and it just vibrates uncontrollable. 2048 and quadrature off seems to be the right solution.

I have a sparkfun Teensy adapter that will allow me to use a Teensy 3.2 and the simpleFOC shield. When it arrives, I will try the 2048 and quadrature ON combination.