-
Notifications
You must be signed in to change notification settings - Fork 21
/
encoder.ino
90 lines (80 loc) · 2.22 KB
/
encoder.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
#include "Emakefun_MotorDriver.h"
#define ONE_CIRCLE_PULSE 1080 //90*12=1080
Emakefun_MotorDriver mMotorDriver = Emakefun_MotorDriver();
Emakefun_EncoderMotor *EncodeMotor_1 = mMotorDriver.getEncoderMotor(E1);
Emakefun_EncoderMotor *EncodeMotor_2 = mMotorDriver.getEncoderMotor(E2);
Emakefun_EncoderMotor *EncodeMotor_3 = mMotorDriver.getEncoderMotor(E3);
Emakefun_EncoderMotor *EncodeMotor_4 = mMotorDriver.getEncoderMotor(E4);
void encoder1(void)
{
if (digitalRead(EncodeMotor_1->ENCODER2pin) == LOW) {
EncodeMotor_1->EncoderPulse++;
} else {
EncodeMotor_1->EncoderPulse--;
}
}
void encoder2(void)
{
if (digitalRead(EncodeMotor_2->ENCODER2pin) == LOW) {
EncodeMotor_2->EncoderPulse++;
} else {
EncodeMotor_2->EncoderPulse--;
}
}
void encoder3(void)
{
if (digitalRead(EncodeMotor_3->ENCODER2pin) == LOW) {
EncodeMotor_3->EncoderPulse++;
} else {
EncodeMotor_3->EncoderPulse--;
}
}
void encoder4(void)
{
if (digitalRead(EncodeMotor_4->ENCODER2pin) == LOW) {
EncodeMotor_4->EncoderPulse++;
} else {
EncodeMotor_4->EncoderPulse--;
}
}
void setup()
{
Serial.begin(9600);
mMotorDriver.begin();
delay(100);
Serial.println("start");
EncodeMotor_1->init(encoder1);
EncodeMotor_2->init(encoder2);
EncodeMotor_3->init(encoder3);
EncodeMotor_4->init(encoder4);
EncodeMotor_1->setSpeed(200);
EncodeMotor_2->setSpeed(200);
EncodeMotor_3->setSpeed(200);
EncodeMotor_4->setSpeed(200);
EncodeMotor_1->run(FORWARD);
EncodeMotor_2->run(FORWARD);
EncodeMotor_3->run(BACKWARD);
EncodeMotor_4->run(BACKWARD);
EncodeMotor_1->run(BACKWARD, 200, ONE_CIRCLE_PULSE);
while(1);
}
void loop()
{
Serial.print("Encoder1Pulse:");
Serial.println(EncodeMotor_1->EncoderPulse);
Serial.print("Encoder2Pulse:");
Serial.println(EncodeMotor_2->EncoderPulse);
Serial.print("Encoder3Pulse:");
Serial.println(EncodeMotor_3->EncoderPulse);
Serial.print("Encoder4Pulse:");
Serial.println(EncodeMotor_4->EncoderPulse);
while(1) {
if (EncodeMotor_1->EncoderPulse > 180*12) {
// Serial.println("STOP");
EncodeMotor_1->run(BRAKE);
} else {
Serial.println(EncodeMotor_1->EncoderPulse);
}
//delay(10);
}
}