PWM input & 360 position control with AS5048A PWM Encoder

Thank you Owen!

My powerful motor setup(36N42P) that I’ve used on my project, I think encoder is AS5048A


Martin,

You’ve done a lot of research and tests so let’s hope the Extra RAM will do the trick. I see the code is pretty much the same so that is good news. From here let’s wait for your controllers to arrive and get your brushless motors working properly in closed loop and afterwards we will do the implementation of the two systems. I should be free to skype one of these days if you need help or just to chat :slight_smile:

With those motors you’ve got the perfect setup to get SimpleFOC running smoothly.

David,

Thank you much!, expecting shipment in about two week.

I’ve also came across with Justine Haupt here & looks like she got almost everything including PWM input but no Magnetic encoder implementation (I left message about feasibility); https://youtu.be/OZvjfbpXpro

I think Justine, Pablo and Simple FOC will make great team in terms of affordable yet advanced motor controller product packaged development and substantial business growth perspective additionally going forward with 32bit processing power, initially toward Adurino & Pi community.

I’ve seen her board and I find it very cool, but a bit expensive.

An advantage of this platform being based on Arduino and completely open-source is that you can implement the communication protocol you want and have the same result. This is why I tell you to not worry a lot about the PWM or any communication protocol as this is will be able to be done whatever way we want.

True, in comparison with Simple FOC(Single channel, stackable design over Arduino and rather may be bulky for 2 or more channel application) & Pablo’s board(Dual Channel, require boot loader install from end user but somewhat require small foot print & slim) but all are still cheaper than Odrive or Roboteq’s although they were primarily aimed toward high power & higher RPM application and based on 32bit processor.

By the way, I think her design & code are all open source and used Mega2560 although I was hoping that to be 32bit.

She does already have pretty good motor design & appears already have supply chain sourcing for mass production stage for both controller and customized higher quality motor :slightly_smiling_face:

I think you’ve all established great motor control method & perhaps possible capability for positioning control like this kind motor; https://www.semanticscholar.org/paper/Improvements-of-Performance-of-Multi-DOF-Spherical-Lee-Park/760f7e91f300f3300a065dd042a0290e55e5cddc

1 Like

Have you seen my custom board? ESP32 Brushless controller - Dagor (work in progress)

Cool! I’m definitely in the right place!

David,

I’ve received board from Pablo (which I think it’s older version of BGC32) & flashed it for Arduino code. and also got Pi4 with 8GB, setup with Tensorflow face tracking with PID tested and also setup with OpenVINO face tracking with PID for comparison. turns out Tensor provide much better accuracy(face detection) and smoother tracking.

I’ve searched Simple FOC’s code for closed control loop and I could not find it, looking at Pablos code I saw “closed_loop_double_youtube_board.ino” which was written to run 2 brushless motors in closed loop mode as a Steer-by-Wire and this is link;

I think this code is good start since it already have 2 motor control loop but looking at code, I have no idea what to change or modify to use with 2 PWM input for independent motor position control.

Kindly help or input will be deeply appreciated!

Hey @Martin-Kim,

Glad to hear back form you. I wouldn’t use Pablo’s code because it’s not efficient and your motor will most likely get hotter because of the way he does position control. Here you can find the SimpleFOC closed loop example for using a magnetic sensor to close the loop:

Try running this code for one motor and tune your PID controller, be careful with the pins you’re using on Pablo’s board. I’ll help you set up then for the two motors and then for the PWM inputs :slight_smile:

David,

Thank you so much & I’ll try to figure out pins and work on PID

David,
The magnetic sensor that I have is AS5048A with PWM output and I’m wondering it doesn’t get support from this code?

I got it, it was under analog!

Can you use the SPI bus of the AS5048A? if yes the code should work by writing any pin on your microcontroller you’re not using and connecting it to the “CSn” of the AS5048

Replace 10 by the pin you want to use

csnPin = 10;
MagneticSensorSPI sensor = MagneticSensorSPI(csnPin, 14, 0x3FFF);

Analog should work too, give it a go and let me know if it works.

David,

SPI is too tight for me solder new wire connection & I tried with “MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);” and “BLDCMotor motor = BLDCMotor(9, 10, 11, 24, 8);” since I have 48 pole motor
but motor is not turning at all upon any target angle input ( I tried with different P of 1 and max voltage of 10 & result were same), my power supply is 12Volt 19 AMP unit with circuit breaker of 2 AMP connected.

Perhaps it’s because Pablo’s new board that I’m using is not L6234 based?

Here is my Serial output;

And this is what I upload into Pablos’s board or Arduino within(code per your link) & Simple FOC board is still out of stock for me to try;

/**
*

  • Position/angle motion control example
  • Steps:
    1. Configure the motor and magnetic sensor
    1. Run the code
    1. Set the target angle (in radians) from serial terminal

*/
#include <SimpleFOC.h>

// magnetic sensor instance - SPI
// MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);
// magnetic sensor instance - MagneticSensorI2C
// MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
// magnetic sensor instance - analog output
MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);

// BLDC motor instance
BLDCMotor motor = BLDCMotor(9, 10, 11, 24, 8);
// Stepper motor instance
//StepperMotor motor = StepperMotor(9, 5, 10, 6, 50, 8);

void setup() {

// initialise magnetic sensor hardware
sensor.init();
// link the motor to the sensor
motor.linkSensor(&sensor);

// power supply voltage
// default 12V
motor.voltage_power_supply = 12;

// choose FOC modulation (optional)
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;

// set motion control loop to be used
motor.controller = ControlType::angle;

// contoller configuration
// default parameters in defaults.h

// velocity PI controller parameters
motor.PID_velocity.P = 0.2;
motor.PID_velocity.I = 20;
motor.PID_velocity.D = 0;
// maximal voltage to be set to the motor
motor.voltage_limit = 6;

// velocity low pass filtering time constant
// the lower the less filtered
motor.LPF_velocity.Tf = 0.01;

// angle P controller
motor.P_angle.P = 20;
// maximal velocity of the position control
motor.velocity_limit = 20;

// use monitoring with serial
Serial.begin(115200);
// comment out if not needed
motor.useMonitoring(Serial);

// initialize motor
motor.init();
// align sensor and start FOC
motor.initFOC();

Serial.println(“Motor ready.”);
Serial.println(“Set the target angle using serial terminal:”);
_delay(1000);
}

// angle set point variable
float target_angle = 0;

void loop() {

// main FOC algorithm function
// the faster you run this function the better
// Arduino UNO loop ~1kHz
// Bluepill loop ~10kHz
motor.loopFOC();

// Motion control function
// velocity, position or voltage (defined in motor.controller)
// this function can be run at much lower frequency than loopFOC() function
// You can also use motor.move() and set the motor.target in the code
motor.move(target_angle);

// function intended to be used with serial plotter to monitor motor variables
// significantly slowing the execution down!!!
// motor.monitor();

// user communication
serialReceiveUserCommand();
}

// utility function enabling serial communication with the user to set the target values
// this function can be implemented in serialEvent function as well
void serialReceiveUserCommand() {

// a string to hold incoming data
static String received_chars;

while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the string buffer:
received_chars += inChar;
// end of user input
if (inChar == ‘\n’) {

  // change the motor target
  target_angle = received_chars.toFloat();
  Serial.print("Target angle: ");
  Serial.println(target_angle);
  
  // reset the command buffer 
  received_chars = "";
}

}
}

@Martin-Kim,

I think it is a smart idea to try to move the motor open loop first with your new board. Try this example:

If you tell it to move 6.28 rads it should spin exactly one revolution, if it doesn’t it means your pole pair number is wrong.

David,

I’ve uploaded this code;


to make sure my board is working & find my pin connections are correct.

Although it tremble a lot, it worked to the extent that I at least know two motors and magnetic PWM input pins are connected correct.

I ran FOC open loop test by changing “BLDCMotor motor = BLDCMotor(9, 10, 11, 21, 8);”—you were correct my motor was 42pole but this didn’t run.

I see “Motor ready!” on serial terminal, I entered 6.28 and nothing happens and one motor at 9,10,11 is energized & not rotating

Do you feel like one coil of the motor is energized when you try to make it rotate? This feels like it really wants to stay in a position and spinning the rotor with your hand feels like the motor has “steps”.
Try changing the motor.voltage_limit = 3; to something higher. If you could attach a video of the behavior you’re experiencing I should be able to help more :slight_smile:

David,

Changed voltage to 6, once power is on that motor on 9,10,11 pins are energized (It won’t turn by hand easily but turn in small step upon good force)

Okay, that means it’s trying to hold it’s position. Does it try to do something when you change the position?

Try changing motor.velocity_limit = 20; to something like 1 rad/s.

Same thing with 1, entered “6.28” & it won’t move. I tried copy & paste mov file here it won’t work, so here is jpg.

This is very weird. Maybe try with the 3, 5 and 6 pins instead of 9, 10, 11. Do you know if Juan Pablo has published the schematic of the board?