SimpleFOClibrary  2.1
BLDCDriver3PWM.cpp
Go to the documentation of this file.
1 #include "BLDCDriver3PWM.h"
2 
3 BLDCDriver3PWM::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
17 
18 }
19 
20 // enable motor driver
22  // enable_pin the driver - if enable_pin pin available
23  if ( _isset(enableA_pin) ) digitalWrite(enableA_pin, HIGH);
24  if ( _isset(enableB_pin) ) digitalWrite(enableB_pin, HIGH);
25  if ( _isset(enableC_pin) ) digitalWrite(enableC_pin, HIGH);
26  // set zero to PWM
27  setPwm(0,0,0);
28 }
29 
30 // disable motor driver
32 {
33  // set zero to PWM
34  setPwm(0, 0, 0);
35  // disable the driver - if enable_pin pin available
36  if ( _isset(enableA_pin) ) digitalWrite(enableA_pin, LOW);
37  if ( _isset(enableB_pin) ) digitalWrite(enableB_pin, LOW);
38  if ( _isset(enableC_pin) ) digitalWrite(enableC_pin, LOW);
39 
40 }
41 
42 // init hardware pins
44  // a bit of separation
45  _delay(1000);
46 
47  // PWM pins
48  pinMode(pwmA, OUTPUT);
49  pinMode(pwmB, OUTPUT);
50  pinMode(pwmC, OUTPUT);
51  if( _isset(enableA_pin)) pinMode(enableA_pin, OUTPUT);
52  if( _isset(enableB_pin)) pinMode(enableB_pin, OUTPUT);
53  if( _isset(enableC_pin)) pinMode(enableC_pin, OUTPUT);
54 
55 
56  // sanity check for the voltage limit configuration
58 
59  // Set the pwm frequency to the pins
60  // hardware specific function - depending on driver and mcu
62  return 0;
63 }
64 
65 
66 
67 // Set voltage to the pwm pin
68 void BLDCDriver3PWM::setPhaseState(int sa, int sb, int sc) {
69  // disable if needed
71  digitalWrite(enableA_pin, sa == _HIGH_IMPEDANCE ? LOW : HIGH);
72  digitalWrite(enableB_pin, sb == _HIGH_IMPEDANCE ? LOW : HIGH);
73  digitalWrite(enableC_pin, sc == _HIGH_IMPEDANCE ? LOW : HIGH);
74  }
75 }
76 
77 // Set voltage to the pwm pin
78 void BLDCDriver3PWM::setPwm(float Ua, float Ub, float Uc) {
79 
80  // limit the voltage in driver
81  Ua = _constrain(Ua, 0.0, voltage_limit);
82  Ub = _constrain(Ub, 0.0, voltage_limit);
83  Uc = _constrain(Uc, 0.0, voltage_limit);
84  // calculate duty cycle
85  // limited in [0,1]
86  dc_a = _constrain(Ua / voltage_power_supply, 0.0 , 1.0 );
87  dc_b = _constrain(Ub / voltage_power_supply, 0.0 , 1.0 );
88  dc_c = _constrain(Uc / voltage_power_supply, 0.0 , 1.0 );
89 
90  // hardware specific writing
91  // hardware specific function - depending on driver and mcu
93 }
BLDCDriver3PWM::pwmB
int pwmB
phase B pwm pin number
Definition: BLDCDriver3PWM.h:36
_configure3PWM
void _configure3PWM(long pwm_frequency, const int pinA, const int pinB, const int pinC)
Definition: drivers/hardware_specific/generic_mcu.cpp:31
NOT_SET
#define NOT_SET
Definition: foc_utils.h:27
BLDCDriver3PWM::init
int init() override
Definition: BLDCDriver3PWM.cpp:43
_constrain
#define _constrain(amt, low, high)
Definition: foc_utils.h:9
BLDCDriver3PWM::pwmC
int pwmC
phase C pwm pin number
Definition: BLDCDriver3PWM.h:37
_isset
#define _isset(a)
Definition: foc_utils.h:11
BLDCDriver::voltage_limit
float voltage_limit
limiting voltage set to the motor
Definition: BLDCDriver.h:18
_delay
void _delay(unsigned long ms)
Definition: time_utils.cpp:5
_writeDutyCycle3PWM
void _writeDutyCycle3PWM(float dc_a, float dc_b, float dc_c, int pinA, int pinB, int pinC)
Definition: drivers/hardware_specific/generic_mcu.cpp:64
BLDCDriver::dc_c
float dc_c
currently set duty cycle on phaseC
Definition: BLDCDriver.h:23
BLDCDriver3PWM::disable
void disable() override
Definition: BLDCDriver3PWM.cpp:31
BLDCDriver3PWM::pwmA
int pwmA
phase A pwm pin number
Definition: BLDCDriver3PWM.h:35
BLDCDriver3PWM::BLDCDriver3PWM
BLDCDriver3PWM(int phA, int phB, int phC, int en1=NOT_SET, int en2=NOT_SET, int en3=NOT_SET)
Definition: BLDCDriver3PWM.cpp:3
BLDCDriver3PWM::setPwm
void setPwm(float Ua, float Ub, float Uc) override
Definition: BLDCDriver3PWM.cpp:78
BLDCDriver::voltage_power_supply
float voltage_power_supply
power supply voltage
Definition: BLDCDriver.h:17
_HIGH_IMPEDANCE
#define _HIGH_IMPEDANCE
Definition: foc_utils.h:28
DEF_POWER_SUPPLY
#define DEF_POWER_SUPPLY
default power supply voltage
Definition: defaults.h:4
BLDCDriver3PWM::enableB_pin
int enableB_pin
enable pin number
Definition: BLDCDriver3PWM.h:39
BLDCDriver::pwm_frequency
long pwm_frequency
pwm frequency value in hertz
Definition: BLDCDriver.h:16
BLDCDriver3PWM::enable
void enable() override
Definition: BLDCDriver3PWM.cpp:21
BLDCDriver3PWM.h
BLDCDriver3PWM::setPhaseState
virtual void setPhaseState(int sa, int sb, int sc) override
Definition: BLDCDriver3PWM.cpp:68
BLDCDriver::dc_a
float dc_a
currently set duty cycle on phaseA
Definition: BLDCDriver.h:21
BLDCDriver::dc_b
float dc_b
currently set duty cycle on phaseB
Definition: BLDCDriver.h:22
BLDCDriver3PWM::enableA_pin
int enableA_pin
enable pin number
Definition: BLDCDriver3PWM.h:38
BLDCDriver3PWM::enableC_pin
int enableC_pin
enable pin number
Definition: BLDCDriver3PWM.h:40