SimpleFOClibrary 2.4.0
Loading...
Searching...
No Matches
BLDCDriver6PWM.cpp
Go to the documentation of this file.
1#include "BLDCDriver6PWM.h"
2
3BLDCDriver6PWM::BLDCDriver6PWM(int phA_h,int phA_l,int phB_h,int phB_l,int phC_h,int phC_l, int en){
4 // Pin initialization
5 pwmA_h = phA_h;
6 pwmB_h = phB_h;
7 pwmC_h = phC_h;
8 pwmA_l = phA_l;
9 pwmB_l = phB_l;
10 pwmC_l = phC_l;
11
12 // enable_pin pin
13 enable_pin = en;
14
15 // default power-supply value
19
20 // dead zone initial - 2%
21 dead_zone = 0.02f;
22
23}
24
25// enable motor driver
27 // enable_pin the driver - if enable_pin pin available
28 if ( _isset(enable_pin) ) digitalWrite(enable_pin, enable_active_high);
29 // set phase state enabled
31 // set zero to PWM
32 setPwm(0, 0, 0);
33}
34
35// disable motor driver
37{
38 // set phase state to disabled
40 // set zero to PWM
41 setPwm(0, 0, 0);
42 // disable the driver - if enable_pin pin available
43 if ( _isset(enable_pin) ) digitalWrite(enable_pin, !enable_active_high);
44
45}
46
47// init hardware pins
49
50 // PWM pins
51 pinMode(pwmA_h, OUTPUT);
52 pinMode(pwmB_h, OUTPUT);
53 pinMode(pwmC_h, OUTPUT);
54 pinMode(pwmA_l, OUTPUT);
55 pinMode(pwmB_l, OUTPUT);
56 pinMode(pwmC_l, OUTPUT);
57 if(_isset(enable_pin)) pinMode(enable_pin, OUTPUT);
58
59
60 // sanity check for the voltage limit configuration
62
63 // set phase state to disabled
67
68 // set zero to PWM
69 dc_a = dc_b = dc_c = 0;
70
71 // configure 6pwm
72 // hardware specific function - depending on driver and mcu
76}
77
78
79// Set voltage to the pwm pin
80void BLDCDriver6PWM::setPwm(float Ua, float Ub, float Uc) {
81 // limit the voltage in driver
82 Ua = _constrain(Ua, 0, voltage_limit);
83 Ub = _constrain(Ub, 0, voltage_limit);
84 Uc = _constrain(Uc, 0, voltage_limit);
85 // calculate duty cycle
86 // limited in [0,1]
87 dc_a = _constrain(Ua / voltage_power_supply, 0.0f , 1.0f );
88 dc_b = _constrain(Ub / voltage_power_supply, 0.0f , 1.0f );
89 dc_c = _constrain(Uc / voltage_power_supply, 0.0f , 1.0f );
90 // hardware specific writing
91 // hardware specific function - depending on driver and mcu
93}
94
95
96// Set the phase state
97// actually changing the state is only done on the next call to setPwm, and depends
98// on the hardware capabilities of the driver and MCU.
100 phase_state[0] = sa;
101 phase_state[1] = sb;
102 phase_state[2] = sc;
103}
PhaseState
Definition FOCDriver.h:7
@ PHASE_ON
Definition FOCDriver.h:9
@ PHASE_OFF
Definition FOCDriver.h:8
void disable() override
int init() override
BLDCDriver6PWM(int phA_h, int phA_l, int phB_h, int phB_l, int phC_h, int phC_l, int en=NOT_SET)
int pwmB_l
phase B pwm pin number
float dead_zone
a percentage of dead-time(zone) (both high and low side in low) for each pwm cycle [0,...
int pwmA_l
phase A pwm pin number
int enable_pin
enable pin number
virtual void setPhaseState(PhaseState sa, PhaseState sb, PhaseState sc) override
void enable() override
void setPwm(float Ua, float Ub, float Uc) override
PhaseState phase_state[3]
phase state (active / disabled)
int pwmC_l
phase C 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
void * _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, const int pinA_l, const int pinB_h, const int pinB_l, const int pinC_h, const int pinC_l)
#define SIMPLEFOC_DRIVER_INIT_FAILED
void _writeDutyCycle6PWM(float dc_a, float dc_b, float dc_c, PhaseState *phase_state, 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