-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMEGAlisten.ino
More file actions
198 lines (148 loc) · 3.84 KB
/
Copy pathMEGAlisten.ino
File metadata and controls
198 lines (148 loc) · 3.84 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#define MotorLeftDir 4
#define MotorRightDir 2
#define MotorLeftSpeed A1
#define MotorRightSpeed A2
int incomingByte = 0; // for incoming serial data
char c;
// char buff[30] = "";
String buff = "";
float lin;
int lin_i;
float ang;
int ang_i;
float seconds;
int leftSpeed, leftVal;
int rightSpeed, rightVal;
bool incomingCommand = false;
int recvd = 0;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
Serial.println("Started listenin'...");
pinMode(MotorLeftDir, OUTPUT);
pinMode(MotorRightDir, OUTPUT);
pinMode(MotorLeftSpeed, OUTPUT);
pinMode(MotorRightSpeed, OUTPUT);
Serial.println("Giving a 255 kick for 100ms");
analogWrite(MotorLeftSpeed, 255);
analogWrite(MotorRightSpeed, 255);
delay(100);
analogWrite(MotorLeftSpeed, 0);
analogWrite(MotorRightSpeed, 0);
Serial.println("Starting loop...");
}
void move() {
/*
CONVENTION:
The values of 'lin' and 'ang' should be between -2.0 and 2.0. Time maximum allowed should be 30 seconds.
*/
// Process data
lin = max(-2.0, min(2.0, lin));
ang = max(-2.0, min(2.0, ang));
// Integer values should range from -2000 to 2000;
lin_i = lin * 1000;
ang_i = ang * 1000;
// Serial.println("HERE:");
// Serial.println(lin_i);
// Serial.println(ang_i);
// Serial.println(seconds);
// BIG BRAIN 🧠
leftSpeed = lin_i - ang_i;
rightSpeed = lin_i + ang_i;
// BIG BRAIN END 🐮
Serial.println("Calculated Speed Numbers:");
Serial.println(leftSpeed);
Serial.println(rightSpeed);
leftVal = abs(map(abs(leftSpeed), 0, 4000, 0, 1023));
rightVal = abs(map(abs(rightSpeed), 0, 4000, 0, 1023));
Serial.println("Final Values:");
Serial.println(leftVal);
Serial.println(rightVal);
if (leftSpeed > rightSpeed) {
// Clockwise
Serial.println("CLOCKWISE");
digitalWrite(MotorLeftDir, HIGH);
digitalWrite(MotorRightDir, LOW);
if (leftVal < 130){
leftVal = 130;
}
} else if (leftSpeed < rightSpeed) {
// Anti Clockwise
Serial.println("ANTI-CLOCKWISE");
digitalWrite(MotorLeftDir, LOW);
digitalWrite(MotorRightDir, HIGH);
if (rightVal < 130){
rightVal = 130;
}
} else if (leftSpeed < 0) { // LINEAR only!
// Backward
digitalWrite(MotorLeftDir, LOW);
digitalWrite(MotorRightDir, LOW);
if (leftVal < 130){
leftVal = 130;
}
if (rightVal < 130){
rightVal = 130;
}
} else {
// Forward
digitalWrite(MotorLeftDir, HIGH);
digitalWrite(MotorRightDir, HIGH);
if (leftVal < 130){
leftVal = 130;
}
if (rightVal < 130){
rightVal = 130;
}
}
// Motor wont run before 130 PWM value;
analogWrite(MotorLeftSpeed, leftVal);
analogWrite(MotorRightSpeed, rightVal);
delay(seconds * 1000);
analogWrite(MotorLeftSpeed, 0);
analogWrite(MotorRightSpeed, 0);
Serial.print("MOVED @ ");
Serial.print(leftVal);
Serial.println(leftVal);
}
void loop() {
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read(); // THIS IS ASCII VALUE of incoming string...
c = incomingByte;
if (c == '\n') {
return;
}
// Serial.println(incomingByte, DEC);
// Serial.println(c);
if (incomingCommand) {
if (c == ',') {
// Trace and clear buff
if (recvd == 0) {
// Store in lin
lin = buff.toFloat();
recvd++;
} else if (recvd == 1) {
// Store in ang
ang = buff.toFloat();
recvd++;
}
buff = "";
} else if (c == ']') {
incomingCommand = false;
// Final frisk
// Store in seconds
seconds = buff.toFloat();
recvd = 0;
buff = "";
// ACT
move();
} else {
// Numbers and dots
buff += c;
}
}
if (c == '[') {
incomingCommand = true;
}
}
}