Arduino SimpleFOC Library - Introduction - 002

Motor driver settings

The library supports the processing of BLDC driver BLDCDriver3PWM and BLDCDriver6PWM classes, as well as the processing of stepping driver StepperDriver4PWM classes.

BLDCDriver3PWM   Class object:

  • Number of pins of phase A, B and C
  • enable pin number (optional)
#include <Arduino.h>

#include <SimpleFOC.h>

//  BLDCDriver3PWM( pin_pwmA, pin_pwmB, pin_pwmC, enable (optional))
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);

// instantiate sensor

void setup() {

	// init sensor

	// power supply voltage
	driver.voltage_power_supply = 12;
	// driver init
	driver.init();

}

void loop() {

}
 BLDCDriver3PWM(int phA,int phB,int phC, int en1 = NOT_SET, int en2 = NOT_SET, int en3 = NOT_SET);

 
BLDCDriver class constructor

  • @Valuepoint PHA phase pwm pin
  • @Valuepoint PHB phase pwm pin
  • @Valuepoint phC phase pwm pin
  • @Valuepoint en1 enable pin (optional input)
  • @Valuepoint en2 enable pin (optional input)
  • @Valuepoint en3 enable pin (optional input)
     
#include <SimpleFOC.h>
// BLDC driver instance
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);

void setup() {
  // pwm frequency to be used [Hz]
  driver.pwm_frequency = 50000;
  // power supply voltage [V]
  driver.voltage_power_supply = 12;
  // Max DC voltage allowed - default voltage_power_supply
  driver.voltage_limit = 12;

  // driver init
  driver.init();

  // enable driver
  driver.enable();

  _delay(1000);
}

void loop() {
    // setting pwm (A: 3V, B: 1V, C: 5V)
    driver.setPwm(3,1,5);
}

Simple test

Make sure that all phases output PWM signals. You can try to connect a small LED light between each phase and ground or measure it with a multimeter.

3.Current detection settings

The library supports online current detection InlineCurrentSense architecture

InlineCurrentSense   By providing instantiated classes:

  • Shunt resistance value   shunt_resistance
  • Amplifier gain   gain
  • `Phase A, B (and optional C) pin numbers
#include <SimpleFOC.h>

// instantiate driver
// instantiate sensor

//  InlineCurrentSense(shunt_resistance, gain, adc_a, adc_b)
InlineCurrentSense current_sense = InlineCurrentSense(0.01, 50, A0, A2);


void setup() {  

  // init sensor

  // init driver

  // init current sense
  current_sense.init();

}

void loop() {

}
  • hardware configuration
    • ADC resolution and frequency
    • Automatic zero offset lookup
  • Drive synchronization
    • ADC sampling event trigger
    • Adaptive alignment with driver phase
  • Read phase current
    • Calculation of current vector amplitude
    • Calculation of FOC d and q currents

Each implemented class can be used as a separate class that can be used to read Arduino   Simple   For the current value of BLDC drive output outside the scope of FOC library, see the example code utils > current in_ sense_ test. In order to make the FOC algorithm work, the current detection class is linked to BLDCMotor, and the driver reads the class of FOC current.

/**
 * Testing example code for the Inline current sensing class
*/
#include <SimpleFOC.h>

// current sensor
// shunt resistor value
// gain value
// pins phase A,B, (C optional)
InlineCurrentSense current_sense = InlineCurrentSense(0.01, 50.0, A0, A2);


void setup() {
  // initialise the current sensing
  current_sense.init();

  // for SimpleFOCShield v2.01/v2.0.2
  current_sense.gain_b *= -1;
  
  Serial.begin(115200);
  Serial.println("Current sense ready.");
}

void loop() {

    PhaseCurrent_s currents = current_sense.getPhaseCurrents();
    float current_magnitude = current_sense.getDCCurrent();

    Serial.print(currents.a*1000); // milli Amps
    Serial.print("\t");
    Serial.print(currents.b*1000); // milli Amps
    Serial.print("\t");
    Serial.print(currents.c*1000); // milli Amps
    Serial.print("\t");
    Serial.println(current_magnitude*1000); // milli Amps
}

InlineCurrentSense(float shunt_resistor, float gain, int pinA, int pinB, int pinC = NOT_SET);

InlineCurrentSense class constructor

  • @Valuepoint shunt resistance shunt resistance value
  • @Parametric gain current detection operational amplifier gain
  • @Valuepoint PHA phase adc pin
  • @Valuepoint PHB phase adc pin
  • @Valuepoint phC phase adc pin (optional)

4.Motor setting

The library supports BLDC motors processed by BLDCMotor class and stepping motors processed by StepperMotor class. Both classes provide pole only by_ Pairs are instantiated by motor number

// StepperMotor(int pole_pairs)
StepperMotor motor = StepperMotor(50);
// BLDCMotor(int pole_pairs)
BLDCMotor motor = BLDCMotor(11);
#include <SimpleFOC.h>

//  BLDCMotor( int pole_pairs )
BLDCMotor motor = BLDCMotor( 11);
 
// instantiate driver
// instantiate sensor 
// instantiate current sensor   

void setup() {  
  // init sensor
  // link the motor to the sensor
  motor.linkSensor(&sensor);

  // init driver
  // link the motor to the driver
  motor.linkDriver(&driver);
  
  // init current sense
  // link to the motor
  motor.linkCurrentSense(&current_sese);

  // set control loop type to be used
  motor.controller = MotionControlType::velocity;
  // initialize motor
  motor.init();

}

void loop() {

}

After creating the motor object, we need to link the motor with the sensor motor.linkSensor() and link the motor class to the driver motor.linkDriver() to which it is connected.
The next step is the configuration step. For this example, we will only configure the motion control loop we will use:

// set control loop type to be used
motor.controller = MotionControlType::velocity;

To complete the motor setup, we run the motor.init() function.

Step 5 FOC routines and real-time motion control

Once we initialize the position sensor, driver and motor, we need to align the motor and sensor before we run the FOC algorithm. This is done by calling motor.initFOC()   After this step, we have a functional position sensor. We have configured the motor. The FOC algorithm knows how to set the appropriate voltage according to the measured value of the position sensor.

For the real-time routine of FOC algorithm, we need to add motor.loopFOC() and motor.move(target) to Arduino's loop() function

  • motor.loopFOC(): FOC algorithm execution - it should be executed as soon as possible  > 1kHz
  • motor.move(target): motion control program - depends on the motor.controller parameter

This is its code as follows:

#include <SimpleFOC.h>

// instantiate motor
// instantiate driver
// instantiate sensor 
// instantiate current sensor   

void setup() {  
  
  // init sensor
  // link motor and sensor

  // init driver
  // link motor and driver

  // init current sense
  // link motor and current sense

  // configure motor
  // init motor

  // align encoder and start FOC
  motor.initFOC();
}

void loop() {
  // FOC algorithm function
  motor.loopFOC();

  // velocity control loop function
  // setting the target velocity or 2rad/s
  motor.move(2);
}

6.monitor

The BLDCMotor and StepperMotor classes provide monitoring capabilities. To enable the monitoring function, confirm the Serial port object to be output by calling motor.useMonitoring(). It uses the Serial class to output the motor.init() function and the motor initialization state during the operation of the motor.initFOC() function.

If you are interested in outputting variables of the real-time state of the motor (even if it affects performance - writing to the serial port is slow!), add the motor.monitor() function call to the Arduinoloop() function.

#include <SimpleFOC.h>

// instantiate motor
// instantiate driver
// instantiate senor

void setup() {  
  
  // init sensor
  // link motor and sensor

  // init driver
  // link motor and driver

  // init current sense
  // link motor and current sense

  // use monitoring with the BLDCMotor
  Serial.begin(115200);
  // monitoring port
  motor.useMonitoring(Serial);
  
  // configure motor
  // init motor
  
  // align encoder and start FOC
}

void loop() {
  
  // FOC execution
  // motion control loop

  // monitoring function outputting motor variables to the serial terminal 
  motor.monitor();
}

Step 7 Control command interface

Finally, in order to configure the control algorithm, set the target value, and obtain the state variable (not just dump, use motor.monitor()) Arduino in a friendly way   Simple   The FOC library provides you with a g-code - like communication interface in the form of a Commander class

#include <SimpleFOC.h>

// instantiate motor
// instantiate senor

//instantiate commander
Commander commander = Commander(Serial);
void doMotor(char* cmd){commander.motor(&motor, cmd);}

void setup() {  
  
  // init sensor
  // link motor and sensor

  // init driver
  // link motor and driver

  // init current sense
  // link motor and current sense
  
  // enable monitoring
  
  // subscribe motor to the commands
  commander.add('M',doMotor,"motor");

  // init motor
  
  // align encoder and start FOC
}

void loop() {
  
  // FOC execution
  // motion control loop
  // monitor variables

  // read user commands
  commander.run();
}

Keywords: Single-Chip Microcomputer ARM MCU

Added by harrisonad on Sat, 02 Oct 2021 20:43:13 +0300