-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge.cpp
140 lines (127 loc) · 3.01 KB
/
bridge.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* bridge.cpp
*
* Created: 14.03.2016 14:22:22
* Author: KZM
*/
#include "bridge.h"
// default constructor
bridge::bridge() {
this->PinPWMH=0;
} //bridge
void bridge::start() {
Timer1.initialize(Period);
Timer1.pwm(PinPWMH, 0);
PWMH = 0;
digitalWrite(PinReset, HIGH);
digitalWrite(PinPWML, HIGH);
}
void bridge::setDir(int8_t Dir) {
this->DIR = Dir;
switch (DIR) {
case -1:
digitalWrite(PWML, HIGH);
digitalWrite(PinDir, HIGH);
break;
case 0:
digitalWrite(PWML, LOW);
setPWM(0);
break;
case 1:
digitalWrite(PWML, HIGH);
digitalWrite(PinDir, LOW);
break;
}
}
void bridge::setImaxRWD(int16_t Imax) {
ImaxRWD = min(Imax, 0);
}
void bridge::setImaxFWD(int16_t Imax) {
ImaxFWD = max(Imax, 0);
}
void bridge::setPWMmax(uint16_t PWMmax) {
maxPWMH = PWMmax;
}
void bridge::calibrateCS() {
csOffset = 0;
for (int i = 0; i < means; i++)
readCurrent();
csOffset = getCurrent();
}
int32_t bridge::readCurrent() {
static uint8_t i = 0;
uint8_t j;
int32_t res = 0;
CURRENT[i % means] = DIR * ((analogRead(PinCS) - 512) * -71.6796875 - csOffset);
i++;
for (j = 0; j < means; j++) {
res += CURRENT[j];
}
res /= means;
return (res);
}
int32_t bridge::getCurrent() {
int32_t res = 0;
for (int i = 0; i < means; i++)
res += CURRENT[i];
return ((res / means));
}
void bridge::stop() {
digitalWrite(PinReset, LOW);
}
void bridge::setup(uint8_t PinPWMH, uint8_t PinPWML, uint8_t PinDir, uint8_t PinReset, uint8_t PinCS, uint16_t Period) {
this->PinPWMH = PinPWMH;
this->PinPWML = PinPWML;
this->PinDir = PinDir;
this->PinReset = PinReset;
this->PinCS = PinCS;
this->Period = Period;
pinMode(PinPWMH, OUTPUT);
pinMode(PinReset, OUTPUT);
digitalWrite(PinReset, LOW); //Bis Richtung festgelegt wird...
pinMode(PinPWML, OUTPUT);
digitalWrite(PinPWML, LOW);
pinMode(PinDir, OUTPUT);
digitalWrite(PinDir, LOW);
}
//bridge::bridge(uint8_t PinPWMH, uint8_t PinPWML, uint8_t PinDir, uint8_t PinReset, uint8_t PinCS, uint16_t Period) :
// PinPWMH(PinPWMH), PinPWML(PinPWML), PinDir(PinDir), PinReset(PinReset), PinCS(PinCS), Period(Period) {
//
// pinMode(PinPWMH, OUTPUT);
// pinMode(PinReset, OUTPUT);
// digitalWrite(PinReset, LOW); //Bis Richtung festgelegt wird...
// pinMode(PinPWML, OUTPUT);
// digitalWrite(PinPWML, LOW);
// pinMode(PinDir, OUTPUT);
// digitalWrite(PinDir, LOW);
//
//}
void bridge::setPWM(int16_t value) {
float stell;
float thproz;
int16_t Itemp = readCurrent();
value = min(maxPWMH, value);
thproz = (float) value / (float) maxPWMH;
stell = ((value - PWMH) / 10.0);
if (stell >= 0)
stell = min(stell, min(20, (ImaxFWD*thproz-Itemp)/1000.0));
else
stell = max(stell, max(-20, (ImaxRWD*(1-thproz)-Itemp)/1000.0));
this->PWMH += stell;
//PWM begrenzen
PWMH = min(PWMH, maxPWMH);
PWMH = max(PWMH, 0);
Serial.print(PinPWMH);
Serial.print(";");
Serial.print(value);
Serial.print(";");
Serial.print(PWMH);
Serial.print(";");
Serial.print(stell);
Serial.print(";");
Serial.println(Itemp);
Timer1.pwm(PinPWMH, (int) PWMH + 1);
}
// default destructor
bridge::~bridge() {
} //~bridge