Hello,
I’ve been trying to get this library working on a teensy 4.1 with a SimpleFOC Mini and an AS5147U magnetic sensor, but i’ve been experiencing a few issues:
At first the motor was making a high pitch squealing sound; the only thing that fixed this was manually setting the PWM frequency of each pin with analogWriteFrequency
to 25000 like in src/drivers/hardware_specific/teensy/teensy_mcu.h.
When I tried to use torque control, the motor would do nothing at all no matter what value I passed to move()
. If I manually moved the motor, it would keep moving in that direction (the torque can be at 0). While the motor was moving, I could change its direction by setting the torque, but if I stopped it, I had to manually restart it. video
This is my code:
#include <SimpleFOC.h>
// init BLDC motor
BLDCMotor motor = BLDCMotor(7, 5.6, 160);
// init driver
BLDCDriver3PWM driver = BLDCDriver3PWM(3, 4, 5, 2);
// init encoder
MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);
// target variable
float target = 0;
// commander interface
Commander command = Commander(Serial);
void onTarget(char* cmd){ command.scalar(& target, cmd); }
void setup() {
delay(2000);
Serial.begin(115200);
SimpleFOCDebug::enable(&Serial);
analogWriteFrequency(3, 25000);
analogWriteFrequency(4, 25000);
analogWriteFrequency(5, 25000);
sensor.clock_speed = 10000000;
sensor.init();
// link the motor to the sensor
motor.linkSensor(&sensor);
// power supply voltage
// default 12V
driver.voltage_power_supply = 12;
driver.pwm_frequency = 25000;
driver.init();
// link the motor to the driver
motor.linkDriver(&driver);
// set control loop to be used
motor.controller = MotionControlType::torque;
//default voltage_power_supply
motor.voltage_limit = 2;
// initialize motor
motor.init();
// align encoder and start FOC
motor.initFOC();
// add target command T
command.add('T', onTarget, "target");
motor.useMonitoring(Serial);
motor.monitor_variables = _MON_TARGET | _MON_VEL | _MON_ANGLE;
Serial.println("Motor ready.");
_delay(5000);
}
void loop() {
motor.monitor();
// iterative FOC function
motor.loopFOC();
// function calculating the outer position loop and setting the target position
motor.move(target);
// user communication
command.run();
}
I also tried velocity_openloop
, which worked well, and velocity
, which had similar issues as torque
.