SimpleFOClibrary 2.4.0
Loading...
Searching...
No Matches
BLDCDriver3PWM.cpp
Go to the documentation of this file.
1#include "BLDCDriver3PWM.h"
2
3BLDCDriver3PWM::BLDCDriver3PWM(int phA, int phB, int phC, int en1, int en2, int en3){
4 // Pin initialization
5 pwmA = phA;
6 pwmB = phB;
7 pwmC = phC;
8
9 // enable_pin pin
10 enableA_pin = en1;
11 enableB_pin = en2;
12 enableC_pin = en3;
13
14 // default power-supply value
18
19}
20
21// enable motor driver
23 // enable_pin the driver - if enable_pin pin available
24 if ( _isset(enableA_pin) ) digitalWrite(enableA_pin, enable_active_high);
25 if ( _isset(enableB_pin) ) digitalWrite(enableB_pin, enable_active_high);
26 if ( _isset(enableC_pin) ) digitalWrite(enableC_pin, enable_active_high);
27 // set zero to PWM
28 setPwm(0,0,0);
29}
30
31// disable motor driver
33{
34 // set zero to PWM
35 setPwm(0, 0, 0);
36 // disable the driver - if enable_pin pin available
37 if ( _isset(enableA_pin) ) digitalWrite(enableA_pin, !enable_active_high);
38 if ( _isset(enableB_pin) ) digitalWrite(enableB_pin, !enable_active_high);
39 if ( _isset(enableC_pin) ) digitalWrite(enableC_pin, !enable_active_high);
40
41}
42
43// init hardware pins
45 // PWM pins
46 pinMode(pwmA, OUTPUT);
47 pinMode(pwmB, OUTPUT);
48 pinMode(pwmC, OUTPUT);
49 if( _isset(enableA_pin)) pinMode(enableA_pin, OUTPUT);
50 if( _isset(enableB_pin)) pinMode(enableB_pin, OUTPUT);
51 if( _isset(enableC_pin)) pinMode(enableC_pin, OUTPUT);
52
53
54 // sanity check for the voltage limit configuration
56
57 // Set the pwm frequency to the pins
58 // hardware specific function - depending on driver and mcu
62}
63
64
65
66// Set voltage to the pwm pin
75
76// Set voltage to the pwm pin
77void BLDCDriver3PWM::setPwm(float Ua, float Ub, float Uc) {
78
79 // limit the voltage in driver
80 Ua = _constrain(Ua, 0.0f, voltage_limit);
81 Ub = _constrain(Ub, 0.0f, voltage_limit);
82 Uc = _constrain(Uc, 0.0f, voltage_limit);
83 // calculate duty cycle
84 // limited in [0,1]
85 dc_a = _constrain(Ua / voltage_power_supply, 0.0f , 1.0f );
86 dc_b = _constrain(Ub / voltage_power_supply, 0.0f , 1.0f );
87 dc_c = _constrain(Uc / voltage_power_supply, 0.0f , 1.0f );
88
89 // hardware specific writing
90 // hardware specific function - depending on driver and mcu
92}
PhaseState
Definition FOCDriver.h:7
@ PHASE_ON
Definition FOCDriver.h:9
int enableC_pin
enable pin number
virtual void setPhaseState(PhaseState sa, PhaseState sb, PhaseState sc) override
void enable() override
void disable() override
int pwmA
phase A pwm pin number
BLDCDriver3PWM(int phA, int phB, int phC, int en1=NOT_SET, int en2=NOT_SET, int en3=NOT_SET)
int enableA_pin
enable pin number
int pwmC
phase C pwm pin number
int enableB_pin
enable pin number
int init() override
void setPwm(float Ua, float Ub, float Uc) override
int pwmB
phase B pwm pin number
float dc_a
currently set duty cycle on phaseA
Definition BLDCDriver.h:10
float dc_b
currently set duty cycle on phaseB
Definition BLDCDriver.h:11
float dc_c
currently set duty cycle on phaseC
Definition BLDCDriver.h:12
long pwm_frequency
pwm frequency value in hertz
Definition FOCDriver.h:35
float voltage_power_supply
power supply voltage
Definition FOCDriver.h:36
bool initialized
true if driver was successfully initialized
Definition FOCDriver.h:39
float voltage_limit
limiting voltage set to the motor
Definition FOCDriver.h:37
bool enable_active_high
enable pin should be set to high to enable the driver (default is HIGH)
Definition FOCDriver.h:42
void * params
pointer to hardware specific parameters of driver
Definition FOCDriver.h:40
#define DEF_POWER_SUPPLY
default power supply voltage
Definition defaults.h:4
#define SIMPLEFOC_DRIVER_INIT_FAILED
void * _configure3PWM(long pwm_frequency, const int pinA, const int pinB, const int pinC)
void _writeDutyCycle3PWM(float dc_a, float dc_b, float dc_c, void *params)
#define NOT_SET
Definition foc_utils.h:34
#define _isset(a)
Definition foc_utils.h:13
#define _constrain(amt, low, high)
Definition foc_utils.h:11