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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
//motor: https://www.pololu.com/product/4861
//motor driver: https://www.pololu.com/product/2997
//Create the timers
IntervalTimer pwm_timer;
IntervalTimer enc_timer;
//define the pins
//If the motors are spinning incorectly, change pins not the drive function.
int led = 13;
int m1enA = 6;
int m1enB = 5;
int m1pwm1 = 4;
int m1pwm2 = 3;
int m1encoA = 32;
int m1encoB = 31;
int m2enA = 12;
int m2enB = 11;
int m2pwm1 = 9;
int m2pwm2 = 10;
int m2encoA = 30;
int m2encoB = 29;
int m3enA = 27;
int m3enB = 26;
int m3pwm1 = 24;
int m3pwm2 = 25;
int m3encoA = 28;
int m3encoB = 23;
//PWM variables
int pwmacc = 100; //how accurate the PWM is, means you can enter value from 0 to the value of pwmacc
int PWMval1 = 0; //value of the PWM1, means you can enter value from 0 to the value of pwmacc
int PWMval2 = 0; //same but for the second one
int PWMval3 = 0; //same but for the thirth one
//encoder variables
const int refreshrate = 50; //refresh rate of the encoder
volatile long oneENCcount = 0; //the amount of pulses from motor one encoder
volatile long twoENCcount = 0; //idem, but motor two
volatile long threeENCcount = 0; //kɛlɔ
double ONErpm = 0.00; //the amount of revolutions motor one makes per minute
double TWOrpm = 0.00; //mismo
double THREErpm = 0.00; //адилхан
void setup() {
pinMode(m1enA, OUTPUT);
pinMode(m1enB, OUTPUT);
pinMode(m1pwm1, OUTPUT);
pinMode(m1pwm2, OUTPUT);
pinMode(m2enA, OUTPUT);
pinMode(m2enB, OUTPUT);
pinMode(m2pwm1, OUTPUT);
pinMode(m2pwm2, OUTPUT);
pinMode(m3enA, OUTPUT);
pinMode(m3enB, OUTPUT);
pinMode(m3pwm1, OUTPUT);
pinMode(m3pwm2, OUTPUT);
pwm_timer.begin(updatePWM, refreshrate); //speed at wich PWM is updated PWM
enc_timer.begin(enc_refr, 50); //refresh rate of the encoder
attachInterrupt(digitalPinToInterrupt(m1encoA), AoneIRS, CHANGE); //calls void AoneIRS if there is a change of the stare of m1encoA
attachInterrupt(digitalPinToInterrupt(m1encoB), BoneIRS, CHANGE); //ditto
attachInterrupt(digitalPinToInterrupt(m2encoA), AtwoIRS, CHANGE); //hali
attachInterrupt(digitalPinToInterrupt(m2encoB), BtwoIRS, CHANGE); //একই
attachInterrupt(digitalPinToInterrupt(m3encoA), AthreeIRS, CHANGE); //ߊ߬ ߓߍ߮ ߓߋ߫ ߞߟߋ߫
attachInterrupt(digitalPinToInterrupt(m3encoB), AthreeIRS, CHANGE); //To samo
Serial.begin(9600);
Serial.print("start");
}
void updatePWM() {
//PWM one
//if the entered valvue is less the zero set it to zero, because lower that zero is not posible
if (PWMval1 < 0) {
PWMval1 = 0;
}
//if the enterd value is higher than the maximum posible value, set it to max.
if (PWMval1 > pwmacc) {
PWMval1 = pwmacc;
}
//check if the counter has passed the PWM value, and turn of the motor based on that.
if (counter1 < PWMval1) {
digitalWrite(m1enA, HIGH);
} else {
digitalWrite(m1enA, LOW);
}
counter1++;
if (counter1 >= pwmacc) {
counter1 = 0;
}
//PWM two
static int counter2 = 0;
if (PWMval2 < 0) {
PWMval2 = 0;
}
if (PWMval2 > pwmacc) {
PWMval2 = pwmacc;
}
if (counter2 < PWMval2) {
digitalWrite(m2enA, HIGH);
} else {
digitalWrite(m2enA, LOW);
}
counter2++;
if (counter2 >= pwmacc) {
counter2 = 0;
}
//PWM three
int static counter3 = 0;
if (PWMval3 < 0) {
PWMval3 = 0;
}
if (PWMval3 > pwmacc) {
PWMval3 = pwmacc;
}
if (counter3 < PWMval3) {
digitalWrite(m3enA, HIGH);
} else {
digitalWrite(m3enA, LOW);
}
counter3++;
if (counter3 >= pwmacc) {
counter3 = 0;
}
}
void AoneIRS(){
oneENCcount++ //adds one to the count
}
void BoneIRS(){
oneENCcount++ //dieselbe
}
void AtwoIRS(){
twoENCcount++ //même
}
void BtwoIRS(){
twoENCcount++ //samme
}
void AthreeIRS(){
threeENCcount++ //aynı
}
void BthreeIRS(){
threeENCcount++ //такой же
}
void enc_refr(){
//calculate RPM. It gets updated 48 times per rotation.
ONErpm = (oneENCcount/48.00) * (60000000.00 / refreshrate)
TWOrpm = (twoENCcount/48.00) * (60000000.00 / refreshrate)
THREErpm = (threeENCcount/48.00) * (60000000.00 / refreshrate)
//reset the counting to zero for next cycle.
oneENCcount = 0;
twoENCcount = 0;
threeENCcount = 0;
}
void drive(int speed, int direction){
//
//!!!
// If the motors are spinning incorectly, change pins not the drive function.
//!!!
//
// 0 = motor off
// + = motor spinning forwards
// - = motor spinning backwards
//
// Motor 1 is the left one.
// Motor 2 is the one in the back.
// Motor 3 is the right one.
//
// Direction............|.Motor 1.|.Motor 2.|.Motor 3.
// 0deg(forward)........|....-....|....0....|....+....
// 60deg................|....0....|....-....|....+....
// 120deg...............|....+....|....-....|....0....
// 180 deg (backward)...|....+....|....0....|....-....
// 240 deg..............|....0....|....+....|....-....
// 300 deg..............|....-....|....+....|....0....
//
PWMval1 = speed;
PWMval2 = speed;
PWMval3 = speed;
digitalWrite(m1enB, LOW);
digitalWrite(m2enB, LOW);
digitalWrite(m3enB, LOW);
Serial.println("debug2");
Serial.println(direction);
if(direction>330 || direction<=30){
//0 deg
Serial.println("0 deg");
digitalWrite(m1pwm1, LOW);
digitalWrite(m1pwm2, HIGH);
digitalWrite(m2pwm1, LOW);
digitalWrite(m2pwm2, LOW);
digitalWrite(m3pwm1, HIGH);
digitalWrite(m3pwm2, LOW);
}else if (direction>30 && direction <= 90){
//60 deg
Serial.println("60deg");
digitalWrite(m1pwm1, LOW);
digitalWrite(m1pwm2, LOW);
digitalWrite(m2pwm1, LOW);
digitalWrite(m2pwm2, HIGH);
digitalWrite(m3pwm1, HIGH);
digitalWrite(m3pwm2, LOW);
}else if (direction>90 && direction <= 150){
//120 deg
Serial.println("120 deg");
digitalWrite(m1pwm1, HIGH);
digitalWrite(m1pwm2, LOW);
digitalWrite(m2pwm1, LOW);
digitalWrite(m2pwm2, HIGH);
digitalWrite(m3pwm1, LOW);
digitalWrite(m3pwm2, LOW);
}else if (direction > 150 && direction <= 210) {
//180 degrees
Serial.println("on 180");
digitalWrite(m1pwm1, HIGH);
digitalWrite(m1pwm2, LOW);
digitalWrite(m2pwm1, LOW);
digitalWrite(m2pwm2, LOW);
digitalWrite(m3pwm1, LOW);
digitalWrite(m3pwm2, HIGH);
}else if (direction > 210 && direction <= 270) {
//240 degrees
Serial.println("240deg");
digitalWrite(m1pwm1, LOW);
digitalWrite(m1pwm2, LOW);
digitalWrite(m2pwm1, HIGH);
digitalWrite(m2pwm2, LOW);
digitalWrite(m3pwm1, LOW);
digitalWrite(m3pwm2, HIGH);
}else if (direction > 270 && direction <= 330) {
//300 degrees
Serial.println("300deg");
digitalWrite(m1pwm1, LOW);
digitalWrite(m1pwm2, HIGH);
digitalWrite(m2pwm1, HIGH);
digitalWrite(m2pwm2, LOW);
digitalWrite(m3pwm1, LOW);
digitalWrite(m3pwm2, LOW);
}else{
Serial.println("how?");
}
}
//the loop routine runs over and over again forever:
void loop() {
drive(50, 0); //speed value between 0-100
Serial.println("Motor one is at: "+ ONErpm + " RPM.");
Serial.println("Motor two is at: "+ TWOrpm + " RPM.");
Serial.println("Motor three is at: "+ THREErpm + " RPM.");
delay(1500);
}
|