summaryrefslogtreecommitdiff
path: root/OLD/sketch_apr18a.ino
blob: c3f6730b28b7c75c95be12f6dee9a48cb9623982 (plain)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/**
MIT License

Copyright (c) 2022 Abel Tim

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
 *
 */

 // Code for rbc soccer robot
 //librarys
#include "Wire.h"                 //https://www.arduino.cc/reference/en/language/functions/communication/wire/
#include "Adafruit_TCS34725.h"    //https://github.com/adafruit/Adafruit_TCS34725

#define PCAADDR 0x70              //i2c addres for i2c multiplexer https://learn.adafruit.com/adafruit-tca9548a-1-to-8-i2c-multiplexer-breakout/arduino-wiring-and-test

Adafruit_TCS34725 tcs = Adafruit_TCS34725();

//IR sensors and multiplexer
int sens1 = 24;
int sens2 = 25;
int sens3 = 26;
int sens4 = 27;
int sens5 = 28;
int sens6 = 29;

//color sensors vars
int r1 = 1;
int g1 = 1;
int b1 = 1;
int r2 = 1;
int g2 = 1;
int b2 = 1;
int r3 = 1;
int g3 = 1;
int b3 = 1;
int r4 = 1;
int g4 = 1;
int b4 = 1;
int r5 = 1;
int g5 = 1;
int b5 = 1;
int r6 = 1;
int g6 = 1;
int b6 = 1;

//Motors
// motor one
int enA = 2;
int in1 = 3;
int in2 = 4;
// motor two
int enB = 7;
int in3 = 5;
int in4 = 6;
// motor three
int enC = 8;
int in5 = 9;
int in6 = 10;

int sp = 160; //speed
//variables used for motor controll
double dirA = 0.00;
double dirB = 0.00;
double dirC = 0.00;
double spdA = 0.00;
double spdB = 0.00;
double spdC = 0.00;
double dirrad = 0.00;

//code for i2c multiplexer
void pcaselect(uint8_t i) {
  if (i > 7) return;
  Wire1.beginTransmission(PCAADDR);
  Wire1.write(1 << i);
  Wire1.endTransmission();  
}
//code for the motors
//for motor A
void Amove(int sped){
  Serial.print("moving A ");
  if (sped < 0) {
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    Serial.print("back");
  }
  else{
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    Serial.print("forward");
  } 
  Serial.print("with speed: ");
  sp = abs(sped); 
  analogWrite(enA, sp);
  Serial.println(sp);
}
//for motor B
void Bmove(int sped) {
  Serial.print("moving B ");
  if (sped < 0) {
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    Serial.print("back");
  } else {
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    Serial.print("forward");
  } 
  Serial.print("with speed: ");
  int sp = abs(sped);
  analogWrite(enB, sp);
  Serial.println(sp);
}
//for motor C
void Cmove(int sped) {
  Serial.print("moving C ");
  if (sped < 0) {
    digitalWrite(in5, LOW);
    digitalWrite(in6, HIGH);
    Serial.print("back");
  } else {
    digitalWrite(in5, HIGH);
    digitalWrite(in6, LOW);
    Serial.print("forward");
  } 
  Serial.print("with speed: ");
  int sp = abs(sped);
  analogWrite(enC, sp);
  Serial.println(sp);
}
//for omhi drive capabilities
void moveohmi(int speed, int direction)
{
  turnoff();

  double dirrad = (direction*71) / 4068;  
  Serial.println(dirrad);
  //vector ontb in x en y
  double x = sin(dirrad)*speed;
  double y = cos(dirrad)*speed;
  Serial.println((String)"x:"+x+" y:"+y);
  spdA = -0.5*x - sqrt(3)/2*y;
  spdB = x;
  spdC = -0.5*x + sqrt(3)/2*y;
  Amove(spdA);
  Bmove(spdB);
  Cmove(spdC);

  Serial.println(spdA);
  Serial.println(spdB);
  Serial.println(spdC);
  Serial.println();
  Serial.println();
}
//for turing
void turn(int speed){
  Serial.println("turn");
  analogWrite(enA, speed);
  analogWrite(enB, speed);
  analogWrite(enC, speed);
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  digitalWrite(in5, HIGH);
  digitalWrite(in6, LOW);
}
//for moving forward
void forward(int speed){
  Serial.println("forward");
  analogWrite(enA, speed);
  analogWrite(enB, speed);
  analogWrite(enC, speed);
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  digitalWrite(in5, HIGH);
  digitalWrite(in6, LOW);
}
//for turning all engines off
void turnoff(){
  Serial.println("stop");
  analogWrite(enA, 0);
  analogWrite(enB, 0);
  analogWrite(enC, 0);
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  digitalWrite(in5, LOW);
  digitalWrite(in6, LOW); 
}
//for finding the ball
void findball(int irs1, int irs2, int irs3, int irs4, int irs5, int irs6, int spedo){
  if (irs1 == 0){
    //0deg
    Bmove(-spedo);
    Cmove(spedo);
  }else if (irs2 == 0){
    //60deg
    Amove(-spedo);
    Cmove(spedo);
  }else if (irs3 == 0){
    //120deg
    Amove(-spedo);
    Bmove(spedo);
  }else if (irs4 == 0){
    //180deg
    Cmove(-spedo);
    Bmove(spedo);
  }else if (irs5 == 0){
    //240deg
    Cmove(-spedo);
    Amove(spedo);
  }
  else if (irs6 == 0){
    //300deg
    Bmove(-spedo);
    Amove(spedo);
  }
  else{
    //turn to search ball
    turn(100);
  }
}


  /**
    
  starting setup

**/
void setup() {
  // Motor setup
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(enC, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(in5, OUTPUT);
  pinMode(in6, OUTPUT);

  //sensor setup
  delay(1000);
  Wire.begin();
  Wire1.begin();
    
  Serial.begin(115200);
  Serial.println("\nPCAScanner ready!");
  for (uint8_t t=0; t<8; t++) {
    pcaselect(t);
    Serial.print("PCA Port #"); Serial.println(t);

    for (uint8_t addr = 0; addr<=127; addr++) {
      if (addr == PCAADDR) continue;

      Wire.beginTransmission(addr);
      if (!Wire.endTransmission()) {
        Serial.print("Found I2C 0x");  Serial.println(addr,HEX);
      }
    }
  }
  Serial.println("\ndone");
  Serial.println("mark1");

  //ir sensor setup
  pinMode(sens1, INPUT);
  pinMode(sens2, INPUT);
  pinMode(sens3, INPUT);
  pinMode(sens4, INPUT);
  pinMode(sens5, INPUT);
  pinMode(sens6, INPUT);
  Serial.println("mark2");

}

  /**
    
  starting loop 
  first collecting sensor data

**/

void loop() {


  delay(100);
  Wire.begin();
  Wire1.begin();
  Serial.println("\nPCAScanner ready!");
  //port expander
  Serial.println("ir sensors");
  // read the input pin:
  int sens1state = digitalRead(sens1);
  int sens2state = digitalRead(sens2);
  int sens3state = digitalRead(sens3);
  int sens4state = digitalRead(sens4);
  int sens5state = digitalRead(sens5);
  int sens6state = digitalRead(sens6);

  // Define a string to store the collected data
  String irsensdata = "";

  // Read the digital state of each IR sensor pin and concatenate the data to the string
  irsensdata += String(sens1state);
  irsensdata += String(", ");
  irsensdata += String(sens2state);
  irsensdata += String(", ");
  irsensdata += String(sens3state);
  irsensdata += String(", ");
  irsensdata += String(sens4state);
  irsensdata += String(", ");
  irsensdata += String(sens5state);
  irsensdata += String(", ");
  irsensdata += String(sens6state);
  // Print the collected data
  Serial.print("IR sensor data: ");
  Serial.println(irsensdata);

  //collorsensor 1
  pcaselect(1);
  Serial.println("PCA Port #1   collor sensor 1");
  uint16_t r, g, b, c, colorTemp, lux;
  tcs.getRawData(&r, &g, &b, &c);
  colorTemp = tcs.calculateColorTemperature_dn40(r, g, b, c);
  lux = tcs.calculateLux(r, g, b);
  Serial.print("RGB: "); Serial.print(r, DEC); Serial.print(", "); Serial.print(g, DEC); Serial.print(", "); Serial.print(b, DEC); Serial.println(" ");
  r1 = (r, DEC);
  g1 = (g, DEC);
  b1 = (b, DEC);
  //collorsensor 2
  pcaselect(2);
  Serial.println("PCA Port #2   collor sensor 2");
  tcs.getRawData(&r, &g, &b, &c);
  colorTemp = tcs.calculateColorTemperature_dn40(r, g, b, c);
  lux = tcs.calculateLux(r, g, b);
  Serial.print("RGB: "); Serial.print(r, DEC); Serial.print(", "); Serial.print(g, DEC); Serial.print(", "); Serial.print(b, DEC); Serial.println(" ");
  r2 = (r, DEC);
  g2 = (g, DEC);
  b2 = (b, DEC);
  //collorsensor 3
  pcaselect(3);
  Serial.println("PCA Port #3   collor sensor 3");
  tcs.getRawData(&r, &g, &b, &c);
  colorTemp = tcs.calculateColorTemperature_dn40(r, g, b, c);
  lux = tcs.calculateLux(r, g, b);
  Serial.print("RGB: "); Serial.print(r, DEC); Serial.print(", "); Serial.print(g, DEC); Serial.print(", "); Serial.print(b, DEC); Serial.println(" ");
  r3 = r;
  g3 = g;
  b3 = b;
  //collorsensor 4
  pcaselect(4);
  Serial.println("PCA Port #4   collor sensor 4");
  tcs.getRawData(&r, &g, &b, &c);
  colorTemp = tcs.calculateColorTemperature_dn40(r, g, b, c);
  lux = tcs.calculateLux(r, g, b);
  Serial.print("RGB: "); Serial.print(r, DEC); Serial.print(", "); Serial.print(g, DEC); Serial.print(", "); Serial.print(b, DEC); Serial.println(" ");
  r4 = r;
  g4 = g;
  b4 = b;
  //collorsensor 5
  pcaselect(5);
  Serial.println("PCA Port #5   collor sensor 5");
  tcs.getRawData(&r, &g, &b, &c);
  colorTemp = tcs.calculateColorTemperature_dn40(r, g, b, c);
  lux = tcs.calculateLux(r, g, b);
  Serial.print("RGB: "); Serial.print(r, DEC); Serial.print(", "); Serial.print(g, DEC); Serial.print(", "); Serial.print(b, DEC); Serial.println(" ");
  r5 = r;
  g5 = g;
  b5 = b;
  //collorsensor 6
  pcaselect(6);
  Serial.println("PCA Port #6   collor sensor 6");
  tcs.getRawData(&r, &g, &b, &c);
  colorTemp = tcs.calculateColorTemperature_dn40(r, g, b, c);
  lux = tcs.calculateLux(r, g, b);
  Serial.print("RGB: "); Serial.print(r, DEC); Serial.print(", "); Serial.print(g, DEC); Serial.print(", "); Serial.print(b, DEC); Serial.println(" ");
  r6 = r;
  g6 = g;
  b6 = b;


/**
    Sensor data collected moving on to engines    

    IR sensor variables collected are sens1state, sens2state, sens3state, sens4state, sens5state, sens6state
    if 0 ball is that direction if 1 ball isnt detected
    ir1 = 0deg
    ir2 = 60deg
    ir3 = 120deg
    ir4 = 180deg
    ir5 = 240deg
    ir6 = 300deg

    Collor sensor variables are r1, g1, b1, r2, g2, b2  ... r6, g6, b6
    stil need to check what color it uses
**/

  findball(sens1state, sens2state, sens3state, sens4state, sens5state, sens6state, sp);



  Serial.println("\ndone");
  Serial.println("mark end loop");
}