-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperate.cpp
More file actions
115 lines (106 loc) · 2.49 KB
/
operate.cpp
File metadata and controls
115 lines (106 loc) · 2.49 KB
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
#include "operate.h"
operate::operate()
: count(0), pinDir(0), pin(0), lsw(0), limitSWPin(0), currpos(0), timer(0),
timeouttimer(0), MotSpeed(0), MotDelay(0), homingAllowed(E_NOK), MotSpeedforHoming(0),
setPos(0), MinLimit(0), MaxLimit(0)
{
}
void operate::setPin(int p, int pD)
{
pin = p;
pinDir = pD;
}
void operate::setSpeed(int MotS)
{
MotDelay = MotS;
}
void operate::move(int m, int dir)
{
if (dir == 1) {
digitalWrite(pinDir, LOW);
for (count = 0; count < m; count++) {
digitalWrite(pin, HIGH);
delayMicroseconds(MotDelay);
digitalWrite(pin, LOW);
delayMicroseconds(MotDelay);
currpos++;
}
} else {
digitalWrite(pinDir, HIGH);
for (count = 0; count < m; count++) {
digitalWrite(pin, HIGH);
delayMicroseconds(MotDelay);
digitalWrite(pin, LOW);
delayMicroseconds(MotDelay);
currpos--;
}
}
}
int operate::home()
{
digitalWrite(pinDir, HIGH);
lsw = digitalRead(limitSWPin);
while (lsw == HIGH && homingAllowed == E_OK) {
lsw = digitalRead(limitSWPin);
digitalWrite(pin, HIGH);
delayMicroseconds(MotSpeedforHoming);
digitalWrite(pin, LOW);
delayMicroseconds(MotSpeedforHoming);
}
currpos = 0;
return E_OK;
}
int operate::getCurrentPos()
{
return currpos;
}
int operate::setHoming(int homing, int motspeed, int timeout, int lswPin)
{
MotSpeedforHoming = motspeed;
timeouttimer = timeout;
limitSWPin = lswPin;
if (homing == E_OK) {
homingAllowed = E_OK;
return E_OK;
} else {
homingAllowed = E_NOK;
return E_NOK;
}
}
int operate::setPosition(long set)
{
long diff;
if (set > currpos && set >= MinLimit && set <= MaxLimit) {
diff = set - currpos;
digitalWrite(pinDir, LOW);
for (count = 0; count < diff; count++) {
digitalWrite(pin, HIGH);
delayMicroseconds(MotDelay);
digitalWrite(pin, LOW);
delayMicroseconds(MotDelay);
currpos++;
}
return E_OK;
} else if (set < currpos && set >= MinLimit && set <= MaxLimit) {
diff = currpos - set;
digitalWrite(pinDir, HIGH);
for (count = 0; count < diff; count++) {
digitalWrite(pin, HIGH);
delayMicroseconds(MotDelay);
digitalWrite(pin, LOW);
delayMicroseconds(MotDelay);
currpos--;
}
return E_OK;
} else if (set == currpos && set >= MinLimit && set <= MaxLimit) {
return E_OK;
} else {
return E_NOK;
}
}
int operate::setPositionLimits(long max, long min)
{
MinLimit = min;
MaxLimit = max;
return E_OK;
}