-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestmerge1.ino
192 lines (170 loc) · 3.71 KB
/
testmerge1.ino
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
volatile long count = 0;
boolean A,B;
unsigned long timep, time, etime;
byte state, statep;
#define m11 9
#define m12 10
#define D0 19
#define D1 18
#define D2 17
#define D3 16
int speed_actual = 0;
double Kp = 0.5;
double Kd = 1;
int val= 0;
int mode;
unsigned long lastTime = 0;
unsigned long lastTime_print = 0;
#define LOOPTIME 100
void setup() {
Serial.begin(115200);
pinMode(2, INPUT);
pinMode(3, INPUT);
attachInterrupt(0,Achange,CHANGE);
attachInterrupt(1,Bchange,CHANGE);
timep = millis();
A = digitalRead(2);
B = digitalRead(3);
if ((A==HIGH)&&(B==HIGH)) statep = 1;
if ((A==HIGH)&&(B==LOW)) statep = 2;
if ((A==LOW)&&(B==LOW)) statep = 3;
if((A==LOW)&&(B==HIGH)) statep = 4;
pinMode(m11, OUTPUT);
pinMode(m12, OUTPUT);
pinMode(D0, INPUT);
pinMode(D1, INPUT);
pinMode(D2, INPUT);
pinMode(D3, INPUT);
pinMode(m11, OUTPUT);
pinMode(m12, OUTPUT);
}
void loop() {
int temp1=digitalRead(D0);
int temp2=digitalRead(D1);
int temp3=digitalRead(D2);
int temp4=digitalRead(D3);
if(temp1==0 && temp2==0 && temp3==0 && temp4==1)
mode=0;//stop
else if(temp1==0 && temp2==0 && temp3==1 && temp4==0)
mode=1;//forward speed
else if(temp1==0 && temp2==1 && temp3==0 && temp4==1)
mode=2;//forward slow
else if(temp1==0 && temp2==0 && temp3==1 && temp4==1)
mode=3;//backward fast
else if(temp1==0 && temp2==1 && temp3==1 && temp4==0)
mode=4;//backward slow
if((millis() - lastTime) >= LOOPTIME) {
lastTime = millis();
if(mode==0){
analogWrite(m11, LOW);
analogWrite(m12, LOW);
}
else if(mode==1){
findMotorData(255);
analogWrite(m11, LOW);
analogWrite(m12, val);
}
else if(mode==2){
findMotorData(100);
analogWrite(m11, LOW);
analogWrite(m12, val);
}
else if(mode==3){
findMotorData(255);
analogWrite(m11, val);
analogWrite(m12, LOW);
}
else if(mode==4){
findMotorData(100);
analogWrite(m11, val);
analogWrite(m12, LOW);
}
else{
analogWrite(m11, LOW);
analogWrite(m12, LOW);}
}
}
void findMotorData(int speed_req) {
static long count_prev = 0;
speed_actual = ((count - count_prev)*(60*(1000/LOOPTIME)))/4000;
count_prev = count;
float pidTerm = 0;
int error = 0;
int last_error = 0;
error = abs(speed_req) - abs(speed_actual);
pidTerm = (Kp * error) + (Kd * (error - last_error));
last_error = error;
val = constrain(pidTerm, 0, 255);
}
void Achange()
{
A = digitalRead(2);
B = digitalRead(3);
if ((A==HIGH)&&(B==HIGH)) state = 1;
if ((A==HIGH)&&(B==LOW)) state = 2;
if ((A==LOW)&&(B==LOW)) state = 3;
if((A==LOW)&&(B==HIGH)) state = 4;
switch (state)
{
case 1:
{
if (statep == 2) count++;
if (statep == 4) count--;
break;
}
case 2:
{
if (statep == 1) count--;
if (statep == 3) count++;
break;
}
case 3:
{
if (statep == 2) count --;
if (statep == 4) count ++;
break;
}
default:
{
if (statep == 1) count++;
if (statep == 3) count--;
}
}
statep = state;
}
void Bchange()
{
A = digitalRead(2);
B = digitalRead(3);
if ((A==HIGH)&&(B==HIGH)) state = 1;
if ((A==HIGH)&&(B==LOW)) state = 2;
if ((A==LOW)&&(B==LOW)) state = 3;
if((A==LOW)&&(B==HIGH)) state = 4;
switch (state)
{
case 1:
{
if (statep == 2) count++;
if (statep == 4) count--;
break;
}
case 2:
{
if (statep == 1) count--;
if (statep == 3) count++;
break;
}
case 3:
{
if (statep == 2) count --;
if (statep == 4) count ++;
break;
}
default:
{
if (statep == 1) count++;
if (statep == 3) count--;
}
}
statep = state;
}