-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTesting Code
More file actions
180 lines (144 loc) · 2.68 KB
/
Testing Code
File metadata and controls
180 lines (144 loc) · 2.68 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
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
#include <Wire.h>
#include <SPI.h>
#include <math.h>
#include <Adafruit_LSM9DS1.h>
#include <Adafruit_Sensor.h> // not used in this demo but required!
#include <digitalWriteFast.h>
int D1=6; //H Bridge Outputs
int D2=7;
int D3=8;
int D4=12;
//Motor
int LEnable=9; //Digital pin 9
int REnable=10; //Digital pin 10
//Encoders
int LEA=2; //Left Encoder A
int LEB=4; //Left Encoder B
int REA=3; //Right Encode A
int REB=5; //Right Encode B
int VPL= 0; //Virtual Position Left
int VPR= 0; //Virtual Postion Right
int Master= 180;
int Slave= 180;
int error=0;
int kp=1;
void setup() {
// put your setup code here, to run once:
attachInterrupt(digitalPinToInterrupt(LEA), isl, RISING);
attachInterrupt(digitalPinToInterrupt(REA), isr, RISING);
Serial.begin(9600);
FW();
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print("Left");
Serial.print("\n");
Serial.print(VPL);
Serial.print("\n");
Serial.print("Right");
Serial.print("\n");
Serial.print(VPR);
Serial.print("\n");
// Serial.print("Master");
// Serial.print("\n");
// Serial.print(Master);
// Serial.print("\n");
//
//Serial.print("Slave");
//Serial.print("\n");
//Serial.print(Slave);
//Serial.print("\n");
//
//Serial.print("error");
//Serial.print("\n");
//Serial.print(error);
//Serial.print("\n");
//
//Serial.print("\n");
//
error= VPL-VPR;
Slave += error/kp;
if (Slave>255)
{
Slave=255;
}
if (Slave<25)
{
Slave=25;
}
// VPL=0;
// VPR=0;
analogWrite(LEnable, Master);
analogWrite(REnable, Slave);
delay(10);
}
void isl() //Left Encoder
{
if (digitalReadFast(LEB)== HIGH)
{
VPL++;
}
else
{
VPL--;
}
}
void isr() //Left Encoder
{
if (digitalReadFast(REB)== LOW)
{
VPR++;
}
else
{
VPR--;
}
}
void FW() //Foward
{
LCW();
RCW();
}
void BW() //Backwards
{
LCCW();
RCCW();
}
void STP() //Stop
{
LSTP();
RSTP();
}
//Wheel Functions
//Left Functions
void LCW() //Left Clock Wise
{
digitalWrite(D1, LOW);
digitalWrite(D2, HIGH);
}
void LCCW() //Left Counter Clock Wise
{
digitalWrite(D1, HIGH);
digitalWrite(D2, LOW);
}
void LSTP() //Left Stop
{
digitalWrite(D1, LOW);
digitalWrite(D2, LOW);
}
//Right Functions
void RCW() //Right Clock Wise
{
digitalWrite(D3, HIGH);
digitalWrite(D4, LOW);
}
void RCCW() //Right Counter Clock Wise
{
digitalWrite(D3, LOW);
digitalWrite(D4, HIGH);
}
void RSTP() //Right Stop
{
digitalWrite(D3, LOW);
digitalWrite(D4, LOW);
}