-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswitch_module.h
94 lines (74 loc) · 1.79 KB
/
switch_module.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
int switch_pwm_A = 0;
int switch_pwm_B = 0;
void switchPinInit(){
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
pinMode(PWMB, OUTPUT);
// ledcSetup(channel_A, freq, ANALOG_WRITE_BITS);
// ledcAttachPin(PWMA, channel_A);
ledcAttach(PWMA, freq, ANALOG_WRITE_BITS);
// ledcSetup(channel_B, freq, ANALOG_WRITE_BITS);
// ledcAttachPin(PWMB, channel_B);
ledcAttach(PWMB, freq, ANALOG_WRITE_BITS);
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, LOW);
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, LOW);
}
void switchEmergencyStop(){
switch_pwm_A = 0;
switch_pwm_B = 0;
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, LOW);
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, LOW);
}
void switchPortCtrlA(float pwmInputA){
int pwmIntA = round(pwmInputA);
if(abs(pwmIntA) < 1e-6){
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, LOW);
return;
}
if(pwmIntA > 0){
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, HIGH);
ledcWrite(PWMA, pwmIntA);
}
else{
digitalWrite(AIN1, HIGH);
digitalWrite(AIN2, LOW);
ledcWrite(PWMA,-pwmIntA);
}
}
void switchPortCtrlB(float pwmInputB){
int pwmIntB = round(pwmInputB);
if(abs(pwmIntB) < 1e-6){
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, LOW);
return;
}
if(pwmIntB > 0){
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, HIGH);
ledcWrite(PWMB, pwmIntB);
}
else{
digitalWrite(BIN1, HIGH);
digitalWrite(BIN2, LOW);
ledcWrite(PWMB,-pwmIntB);
}
}
void switchCtrl(int pwmIntA, int pwmIntB) {
switch_pwm_A = pwmIntA;
switch_pwm_B = pwmIntB;
switchPortCtrlA(switch_pwm_A);
switchPortCtrlB(switch_pwm_B);
}
void lightCtrl(int pwmIn) {
switch_pwm_A = pwmIn;
switchPortCtrlA(-abs(switch_pwm_A));
}