Problem to write an openloop 6PWM motor control code with encoder?

Hello. I used the SparkFun RedBoard Artemis ATP microcontroller board (with Apollo3 processor) to control two gimbal SparkFun 12V 3-Phase BLDC motors via two SparkFun 2-11V 3-Phase Brushless motor-driver boards. I first want to build a code to control one motor with the SimplFOC openloop control by using the encoder AS5048A. The code without the encoder for one motor was presented here: Problem to execute SimpleFOC code on SparkFun RedBoard Artemis ATP? - #19 by B - Robotics - SparkFun Community .

I know that while() and for() loops should not be used inside loop() in the SimpleFOC control. I need to make the motor move intermittently. The motor should accelerate for the first 180 degrees and it should move uniformly for the second 180 degrees. In this way, the motor makes one full circle (cycle). Could someone please advise how to include an encoder in the code presented above?

P.S. I have a problem to update the encoder readings because while() and for() loops cannot be used inside loop() in the SimpleFOC control code. Thank you.

3 Answers

3

If you init the encoder to the motor, the position will be updated automatically with calling motor.loopFOC().
No need for while or for loops in that regard.
Nevertheless, I’m using while loop in the main loop and haven’t noticed big drama.

Hello. I wrote the following code, please see below. The motor #1 rotor got fixed in one position after uploading the code. Moreover, the blue LED of the SparkFun motor-driver board Hardware Overview - Hookup Guide - TMC6300 BLDC Motor Driver was off. The red LED of the motor-driver board was on, though. I do not understand why the blue LED was off. Could you please provide any clue? Here is the code:

/*

The following code is based on the Apollo3 driver and codes mentioned in this discussion:

*/

#include <SimpleFOC.h>
#include <Arduino.h>

//Setting all constants

//const float tolerance = 0.05; // defined in radians is (2.5 \* 1.145916) degrees

//setting target velocity at the end of the acceleration zone (180 degrees or PI)
//target velocity of 250 Rotations Per Minute (RPM) is about 4.1666 Rotations Per Second (RPS)
// 4.1666 RPS is abput 26.18 radiins per second
//To reach 26.18 RPS during the distance of PI radians, motor #1 takes PI / 26.18RPS = 0.1299997 second.
//const float target_acceleration_zone_velocity1 = 6.18; // radians per second
//const unsigned long ramp_time = 122; // ramp time in ms to reach target velocity
//const float target_angle1 = PI;

// Setting Motor #1 having 11 pole pairs
BLDCMotor motor1 = BLDCMotor(11);

// Initialize 6PWM motor-driver
const int VIO_PIN1 = D17;
BLDCDriver6PWM driver1 = BLDCDriver6PWM(D26, D19, D22, D4, D27, D23, VIO_PIN1);

//Initialize variables for the PWM pins of the PWM wires of the encoders

const int PWM_pin_encoder1 = A12; // Connect to the AS5048A encoder #1 PWM pin

//Initialize variable for encoder #1 by MagenticSensorPWM
//Pulse widths are 14 us and 936 us by oscilloscope measurements

MagneticSensorPWM encoder1 = MagneticSensorPWM(PWM_pin_encoder1, 14, 936);

// channel as callback

void doA1(){encoder1.handlePWM();}

//Commander command = Commander(Serial);
//void onTarget(char\* cmd){ command.scalar(&target_angle, cmd); }

// Build setup() block
void setup() {
Serial.begin(115200);
while (!Serial)
delay(1);
Serial.println("SimpleFOC Example");

SimpleFOCDebug::enable(&Serial);

//Initialize encoder #1 and Interrupts
encoder1.init();
encoder1.enableInterrupt(doA1);

// Link encoder #1 to motor #1 (vital for future closed-loop transitions)

motor1.linkSensor(&encoder1);

driver1.voltage_power_supply = 7.4;
driver1.voltage_limit = 7.4;

if (driver1.init())
Serial.println("Driver1 initialized");

motor1.linkDriver(&driver1);

motor1.controller = MotionControlType::angle;
motor1.torque_controller = TorqueControlType::voltage;

// controller configuration based on the control type
// velocity PI controller parameters
// default P=0.5 I = 10
motor1.PID_velocity.P = 0.5;
motor1.PID_velocity.I = 10;
// jerk control using voltage voltage ramp
// default value is 300 volts per sec  \~ 0.3V per millisecond
motor1.PID_velocity.output_ramp = 0.04;

motor1.voltage_limit = 4.0; // up to 7.4 Volts but not higher than voltage_power_supply

//motor1.velocity_limit = PI; // radians per second

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

// angle P controller
// default P=20
motor1.P_angle.P = 20;
//  maximal velocity of the position control
// default 20
motor1.velocity_limit = PI; // radians per second

if (motor1.init())
Serial.println("Motor initialized");

// motor1.useMonitoring(Serial);

motor1.initFOC();

// Get motor #1 to its initial position before rotating
// Gentle settling sequence to start position based on GoogleAI:
motor1.target = 0;
unsigned long settle_timer = millis();
while (millis() - settle_timer < 2000) {
motor1.loopFOC();
motor1.move();
}

//\_delay(20000);
}

void loop() {

motor1.loopFOC();

float angle1 = motor1.shaft_angle;

// Setting velocity limits based on position
if (angle1 >= 0 && angle1 < PI) {
motor1.velocity_limit =  26.18;   // acceleration zone speed in radians per second
}
if (angle1 >= PI && angle1 <= (2\*PI)) {
motor1.velocity_limit = 3;  // inertial zone speed in radians per second
}

// Optional: add feed-forward velocity
//motor1.feed_forward_velocity = (angle < 0.5) ? 2.0 : 0.0;

// Target angke stays the same
motor1.move(2\*PI);

}
unsigned long settle_timer = millis(); 
while (millis() - settle_timer < 2000) { 
motor1.loopFOC(); 
motor1.move(); } 

I would use motor1.move(target); here.

if (angle1 >= 0 && angle1 < PI) {
motor1.velocity_limit =  26.18;

Why is the speed so high during the 1st half turn and the second half is only 3 rad/s?