Destroyer – Robô mini sumo por Daniel Barradas

Os robôs mini sumo estão cada vez mais popularizados nas comunidades robótica por todo o mundo. Hoje apresento-vos uma criação de Daniel Barradas mais conhecido por iCar.

O Destroyer é um robô mini sumo, o seu cérebro é um ATmega328 com o bootloader do Arduino. Utiliza sensores infra-vermelhos para detectar os oponentes e phototransistors para detectar as extremidades do ringue de combate.

“Mini-sumo é uma competição entre robôs baseado em wrestling japonês;
Sumo em japonês significa wrestling;
Dois oponentes (robôs) enfrentam-se num anel chamado uma dohyo;
O objectivo é empurrar o robô oponente para fora do ringue;
O robô que permanecer no anel ganha a ronda.”

Esta categoria de sumo é apenas para robôs com um máximo de 10cm de largura por 10 de comprimento e 500 gramas de massa!

Alguns combates de sumo robótico:

Fotografias do Destroyer:


O sketch do código para o Arduino foi disponibilizado pelo autor:

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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
// **************************************************************
// Destroyer - Mini Sumo Robot
//
// By Daniel Barradas (iCAR)
//
// 29/03/10 - Ver. 1.2
// **************************************************************
 
#include <EEPROM.h> // Libraria EEprom interna
 
// Digital Pins
#define LeftMotorDir1Pin 2 // Front
#define LeftMotorDir2Pin 3 // Back
#define CalibSwitchPin 4 
#define LeftMotorSpeedPin 5 // Pwm
#define RightMotorSpeedPin 6 // Pwm
#define RightMotorDir1Pin 7 // Front
#define RightMotorDir2Pin 8 // Back
// 9,10,11 estão livres
#define GrnLedPin 12
#define RedLedPin 13
 
// Analog Pins
#define IrLineFrontPin 0
#define IrLineRearPin 5
#define IrFrontPin 2
#define IrLeftPin 3
#define IrRightPin 4
#define IrRearPin 1
 
// Calibration Switch
int CalibSwitchVal = 0;
 
// Ir Sensor SHARP (Analog Input)
//int MinIrFrontValue = 500; // Muito perto (Calibragem sensores)
//int MedIrFrontValue = 250; // Perto (Calibragem sensores)
int MaxIrFrontValue = 100; // longe (Calibragem sensores)
int IrFrontVal = 0;
int IrFrontDetect = 0;
int IrFrontTolerance = 70;
int IrFrontTolMin = 0;
int IrFrontTolMax = 0;
 
// Ir Left Sensor (Analog Input)
int MaxIrLeftValue = 120; // longe (Calibragem sensores)
int IrLeftVal = 0;
int IrLeftDetect = 0;
int IrLeftTolerance = 20;
int IrLeftTolMin = 0;
//int IrLeftTolMax = 0;
 
// Ir Right Sensor (Analog Input)
int MaxIrRightValue = 1000; // longe (Calibragem sensores)
int IrRightVal = 0;
int IrRightDetect = 0;
int IrRightTolerance = 20;
int IrRightTolMin = 0;
//int IrRightTolMax = 0;
 
// Ir Rear Sensor (Analog Input)
int MaxIrRearValue = 500; // longe (Calibragem sensores)
int IrRearVal = 0;
int IrRearDetect = 0;
int IrRearTolerance = 500;
int IrRearTolMin = 0;
//int IrRearTolMax = 0;
 
// Ir Front Line Detect
int LineFrontVal = 0;
int LineFrontBlack = 800; // (Calibragem sensores)
int LineFTolerance = 5;
int LineFTolMin = 0;
int LineFTolMax = 0;
 
// Ir Rear Line Detect
int LineRearVal = 0;
int LineRearBlack = 800; // (Calibragem sensores)
int LineRTolerance = 5;
int LineRTolMin = 0;
int LineRTolMax = 0;
 
int BeginTime = 3500; // Tempo em Segundos iniciais conforme as regras (tenho que ajustar tem mais de 5 segundos)
int Debug = 0;
 
int Turn=0;
 
int MaxSpeed = 200;
int MinSpeed = 127;
 
//  Motors
int motor_left[] = {2, 3};
int motor_right[] = {7, 8};
int motor_PwrLeftPin = 5;
int motor_PwrRightPin = 6;
int MotorStatus;
int MotorStatusOld=0;
 
#define Motor_MaxSpeed 255
#define Motor_MedSpeed 150 // não estou a usar
#define Motor_MinSpeed 127 // não estou a usar
 
int Motor_leftSpeed = Motor_MaxSpeed;
int Motor_rightSpeed = Motor_MaxSpeed;
 
// ****************************************************************************************************
//
// Template: EEPROM_writeAnything - Escreve qualquer coisa na Emprom
// Retorno: int
//
// ****************************************************************************************************
template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
    const byte* p = (const byte*)(const void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
	  EEPROM.write(ee++, *p++);
    return i;
}
// ****************************************************************************************************
//
// Template: EEPROM_readAnything - Lê qualquer coisa da Emprom
// Retorno: int
//
// ****************************************************************************************************
template <class T> int EEPROM_readAnything(int ee, T& value)
{
    byte* p = (byte*)(void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
	  *p++ = EEPROM.read(ee++);
    return i;
}
 
// ****************************************************************************************************
//
// Estrutura: configuration - Estrutura da configuração gravada na Emprom
// Retorno: Varios
//
// ****************************************************************************************************
struct config_t
{
  int FrontLine;
  int RearLine;
  int MaxIrFront;
  int MaxIrLeft;
  int MaxIrRight;
  int MaxIrRear;
} configuration;
 
void setup() { 
  pinMode(LeftMotorDir1Pin, OUTPUT); // Front
  pinMode(LeftMotorDir2Pin, OUTPUT); // Back
  pinMode(CalibSwitchPin,  INPUT); 
  digitalWrite(CalibSwitchPin, HIGH); // Turn PullUp on
  pinMode(LeftMotorSpeedPin, OUTPUT); // Pwm
  pinMode(RightMotorSpeedPin, OUTPUT);// Pwm
  pinMode(RightMotorDir1Pin, OUTPUT); // Front
  pinMode(RightMotorDir2Pin, OUTPUT); // Back
  pinMode(GrnLedPin, OUTPUT);
  pinMode(RedLedPin, OUTPUT);
  delay(50);
  Serial.begin(9600);  
 
  LoadConfig();
 
  CalibSwitchVal = digitalRead(CalibSwitchPin); // Get Calibratrion Jumper/Switch 
 
  if (CalibSwitchVal==HIGH){ // Ponho o robot em cima da linha para calibrar
    TurnLed(0,GrnLedPin);
    if (Debug==1){Serial.println("Calibrando Sensores");}  
    BlinkLed(RedLedPin, 5, 1000);
 
    LineFrontBlack = analogRead(IrLineFrontPin); // Front Lint Sensor
    BlinkLed(RedLedPin, 1, 500);
 
    LineRearBlack = analogRead(IrLineRearPin); // Rear Line Sensor
    BlinkLed(RedLedPin, 1, 500);
 
    MaxIrLeftValue = analogRead(IrLeftPin);
    BlinkLed(RedLedPin, 1, 500);
 
    MaxIrRightValue = analogRead(IrRightPin);
    BlinkLed(RedLedPin, 1, 500);
 
    MaxIrRearValue = analogRead(IrRearPin); 
    BlinkLed(RedLedPin, 1, 500);
 
    MaxIrFrontValue = analogRead(IrFrontPin);
    BlinkLed(RedLedPin, 1, 500);
 
    SaveConfig();
    delay(500);
    TurnLed(0,RedLedPin);
    TurnLed(1,GrnLedPin);
  } else { // Inicio Normal
    TurnLed(0,RedLedPin);
    TurnLed(1,GrnLedPin); 
  }  
 
  motor_Speed(motor_PwrLeftPin,Motor_MaxSpeed);
  motor_Speed(motor_PwrRightPin,Motor_MaxSpeed);
 
  LineFTolMin = (LineFrontBlack - LineFTolerance);
  LineFTolMax = (LineFrontBlack + LineFTolerance);
 
  LineRTolMin = (LineRearBlack - LineRTolerance);
  LineRTolMax = (LineRearBlack + LineRTolerance);
 
  IrFrontTolMin = (MaxIrFrontValue - IrFrontTolerance);
  IrFrontTolMax = (MaxIrFrontValue + IrFrontTolerance);
 
  IrRearTolMin = (MaxIrRearValue - IrRearTolerance);
 
  IrLeftTolMin = (MaxIrLeftValue - IrLeftTolerance);
  //IrLeftTolMax = (MaxIrLeftValue + IrLeftTolerance);
 
  IrRightTolMin = (MaxIrRightValue - IrRightTolerance);
  //IrRightTolMax = (MaxIrRightValue + IrRightTolerance);    
 
  // Falta Sensor de trás
 
  GetSensores();
  delay(BeginTime); // Perfaz 5 Segundos iniciais conforme as regras
  TurnLed(0,GrnLedPin);
}
 
void loop() {
 
  GetSensores();
  TurnLed(0,RedLedPin);
  TurnLed(0,GrnLedPin);
 
  if (Debug==1){
    //Serial.println(IrRearVal);
    //Serial.print(MaxIrFrontValue);Serial.print(" "); Serial.print(IrFrontTolMax); Serial.print(" ");Serial.println(IrFrontVal);
    //Serial.print(MaxIrRightValue);Serial.print(" "); Serial.print(IrRightTolMax); Serial.print(" ");Serial.println(IrRightVal);
    //Serial.println("-------------------------------------------------------------");
    //Serial.print(MaxIrLeftValue);Serial.print(" "); Serial.print(IrLeftTolMax); Serial.print(" ");Serial.println(IrLeftVal);
    //Serial.println("-------------------------------------------------------------");
    //Serial.print(LineFrontBlack); Serial.print(" "); Serial.print(MaxIrLeftValue); Serial.print(" "); Serial.println(MaxIrRightValue);
    //Serial.print(LineFrontVal); Serial.print(" "); Serial.print(IrLeftVal); Serial.print(" "); Serial.println(IrRightVal);
    //delay(100);
  }
 
   if (Debug==0){drive_forward();}
   DetectLine(1);
   DetectLine(2);
 
   if (IrFrontVal > IrFrontTolMax){ // Detectou Objecto Frente
      TurnLed(1,GrnLedPin);
      IrFrontDetect = 1;
      if (Debug==0){
         if (DetectLine(1)==false){drive_forward();}
      } else {
        Serial.print("Objecto Frente: "); Serial.println(IrFrontVal); 
      }
   } else if (IrFrontVal <= IrFrontTolMax){  // Não Detectou Objecto
      IrFrontDetect = 0;
    }
 
    if (IrRearVal < IrRearTolMin){ // Detectou Objecto atras
      TurnLed(1,GrnLedPin);
      IrRearDetect = 1;
      if (Debug==0){
          if (DetectLine(2)==false){  drive_backward();}
      } else {
       Serial.print("Objecto atras: "); Serial.println(IrRearVal);
      }  
    } else { // Nao Detectou objecto atras
      IrRearDetect = 0;
    }  
 
    if (IrLeftVal < IrLeftTolMin){ //detectou Objectos
      if ((IrFrontDetect == 0) && (IrRearDetect==0)){
        TurnLed(1,GrnLedPin);
        if (Debug==0){
          DetectLine(1);
          DetectLine(2);
          turn_left();
          delay(1000);
        } else {
          Serial.print("Objecto Esquerda: ");Serial.println(IrLeftVal); 
        }
      }
    }
 
    if (IrRightVal < IrRightTolMin){ //  detectou Objectos
      if ((IrFrontDetect == 0) && (IrRearDetect==0)){
        TurnLed(1,GrnLedPin);
        if (Debug==0){
          DetectLine(1);
          DetectLine(2);
          turn_right();
          delay(1000);
        } else {
          Serial.print("Objecto Direita: ");Serial.println(IrRightVal);           
        }
      }  
    }
}
 
boolean DetectLine(int LineSensor){
  GetSensores();
  if (LineSensor==1){ // Front Sensor
    if ((LineFrontVal < LineFTolMin) || (LineFrontVal > LineFTolMax)){ // Não detectou linha
      return false;
    } else { // Detectou linha
      TurnLed(1,RedLedPin);
      if (Debug==0){
        drive_backward();
        //DetectLine(2); 
        delay(1500);
        turn_left();
        DetectLine(1);
        delay(1000);  
      } else {
        Serial.print("Linha Frente: ");Serial.println(LineFrontVal);  
      }
      return true;
    }
 
  } else if (LineSensor==2){ // Rear Sensor
    if ((LineRearVal < LineRTolMin) || (LineRearVal > LineRTolMax)){ // Não detectou linha
      return false;
    } else { // Detectou linha
      TurnLed(1,RedLedPin);
      if (Debug==0){
        drive_forward();
        //DetectLine(2); 
        delay(1500);
        turn_right();
        //DetectLine(1);
        DetectLine(2);
        delay(1000);  
      } else {
        Serial.print("Linha Tras: ");Serial.println(LineRearVal);
      }  
      return true;
    }
  }
}  
 
// ****************************************************************************************************
//
// Funcao: LoadConfig - Le da estrutura configuration
// Retorno: nada
//
// ****************************************************************************************************
static void LoadConfig(void){
  EEPROM_readAnything(1, configuration);
  delay(500); 
  LineFrontBlack = configuration.FrontLine;
  LineRearBlack = configuration.RearLine;
  MaxIrFrontValue = configuration.MaxIrFront;
  MaxIrLeftValue = configuration.MaxIrLeft;
  MaxIrRightValue = configuration.MaxIrRight;
  MaxIrRearValue = configuration.MaxIrRear;
}
 
// ****************************************************************************************************
//
// Funcao: SaveConfig - Grava na estrutura configuration
// Retorno: nada
//
// ****************************************************************************************************
static void SaveConfig(){
 configuration.FrontLine = LineFrontBlack;
 configuration.RearLine = LineRearBlack; 
 configuration.MaxIrFront = MaxIrFrontValue;
 configuration.MaxIrLeft = MaxIrLeftValue;
 configuration.MaxIrRight = MaxIrRightValue;
 configuration.MaxIrRear = MaxIrRearValue; 
 EEPROM_writeAnything(1, configuration);
}
 
// ****************************************************************************************************
//
// Funcao: GetSensores - Faz a leitura dos sensores
// Retorno: nada
//
// ****************************************************************************************************
void GetSensores(){
  CalibSwitchVal = digitalRead(CalibSwitchPin);
  LineFrontVal = analogRead(IrLineFrontPin); // Front Line Sensor
  LineRearVal = analogRead(IrLineRearPin); // Rear Line Sensor
  IrFrontVal = analogRead(IrFrontPin); // Front IR Sharp
  IrLeftVal = analogRead(IrLeftPin); // Left IR
  IrRightVal = analogRead(IrRightPin); // Right IR
  IrRearVal = analogRead(IrRearPin); // Rear IR  
  //delay(50);
}
 
static void TurnLed(int Turn, int LedPin){
  if (Turn==0){
    digitalWrite(LedPin, LOW);
//    ledVal = 0;
  } 
  else if (Turn==1){
    digitalWrite(LedPin, HIGH);
//    ledVal = 1;
  }
}
 
void motor_Speed(int motorPin, int motorSpeed){
  analogWrite(motorPin, motorSpeed);
}  
 
void motor_stop(){
  if ((MotorStatus != MotorStatusOld) || (MotorStatus != 1));
  {
    digitalWrite(motor_left[0], LOW);
    digitalWrite(motor_left[1], LOW);
    digitalWrite(motor_right[0], LOW);
    digitalWrite(motor_right[1], LOW);
    delay(25);
    MotorStatus = 1;
    MotorStatusOld = MotorStatus;
  }
}
 
void drive_forward(){
  if ((MotorStatus != MotorStatusOld) || (MotorStatus != 2)){
    digitalWrite(motor_left[0], HIGH);
    digitalWrite(motor_left[1], LOW);
    digitalWrite(motor_right[0], HIGH);
    digitalWrite(motor_right[1], LOW);
    MotorStatus = 2;
    MotorStatusOld = MotorStatus;
  }
}
 
void softLeft(){
  if ((MotorStatus != MotorStatusOld) || (MotorStatus != 2)){
    motor_Speed(motor_PwrLeftPin,Motor_MedSpeed-50);
    motor_Speed(motor_PwrRightPin,Motor_MaxSpeed);
    MotorStatus = 2;
    MotorStatusOld = MotorStatus;
  }  
}
 
void softRight(){
  if ((MotorStatus != MotorStatusOld) || (MotorStatus != 2)){
    motor_Speed(motor_PwrLeftPin,Motor_MaxSpeed);
    motor_Speed(motor_PwrRightPin,Motor_MedSpeed-50);
    MotorStatus = 2;
    MotorStatusOld = MotorStatus;
  }  
}
 
void drive_backward(){
  if ((MotorStatus != MotorStatusOld) || (MotorStatus != 3)){
    motor_Speed(motor_PwrLeftPin,Motor_MaxSpeed);
    motor_Speed(motor_PwrRightPin,Motor_MaxSpeed);
    digitalWrite(motor_left[0], LOW);
    digitalWrite(motor_left[1], HIGH);
    digitalWrite(motor_right[0], LOW);
    digitalWrite(motor_right[1], HIGH);
    MotorStatus = 3;
    MotorStatusOld = MotorStatus;
  }  
}
 
void turn_left(){
  if ((MotorStatus != MotorStatusOld) || (MotorStatus != 4)){  
    digitalWrite(motor_left[0], LOW);
    digitalWrite(motor_left[1], HIGH);
    digitalWrite(motor_right[0], HIGH);
    digitalWrite(motor_right[1], LOW);
    MotorStatus = 4;
    MotorStatusOld = MotorStatus;
  }  
}
 
void turn_right(){
  if ((MotorStatus != MotorStatusOld) || (MotorStatus != 5)){
    digitalWrite(motor_left[0], HIGH);
    digitalWrite(motor_left[1], LOW);
    digitalWrite(motor_right[0], LOW);
    digitalWrite(motor_right[1], HIGH);
    MotorStatus = 5;
    MotorStatusOld = MotorStatus;
  }  
}
 
 
void turn_lng_left(){
  if ((MotorStatus != MotorStatusOld) || (MotorStatus != 6)){
    digitalWrite(motor_left[0], LOW);
    digitalWrite(motor_left[1], LOW);
    digitalWrite(motor_right[0], HIGH);
    digitalWrite(motor_right[1], LOW);
    MotorStatus = 6;
    MotorStatusOld = MotorStatus;
  }  
}
 
void turn_lng_right(){
  if ((MotorStatus != MotorStatusOld) || (MotorStatus != 7)){
    digitalWrite(motor_left[0], HIGH);
    digitalWrite(motor_left[1], LOW);
    digitalWrite(motor_right[0], LOW);
    digitalWrite(motor_right[1], LOW);
    MotorStatus = 7;
    MotorStatusOld = MotorStatus;
  }  
}
 
static void TestMotors(){
  drive_forward();
  delay(2000);
  drive_backward();
  delay(2000);
  turn_left();
  delay(2000);
  turn_right();
  delay(2000);
  /*turn_lng_left();
  delay(2000);
  turn_lng_right();
  delay(2000);
  */
  motor_stop();
}
 
static void BlinkLed(byte targetPin, byte numBlinks, byte blinkRate) { // single pin
  for (byte i=0; i < numBlinks; i++) {
      digitalWrite(targetPin, HIGH);   // sets the LED on
      delay(blinkRate);                     // waits for blinkRate milliseconds
      digitalWrite(targetPin, LOW);    // sets the LED off
      delay(blinkRate);
  }
}
#

Obrigado ao Joni do blog LIXADO por ter sugerido um artigo sobre sumo Robótico.

Escrito por André Delgado

Fundador e administrador do Make Bits, desde cedo que se interessou por robótica e micro-controladores, principalmente pelo Arduino. Já escreveu 116 artigos no Make Bits sobre os mais variados temas da robótica.

Artigos Relacionados:

16 Comentários

  • Obrigado, já que fui eu que sugeri ;)

    Vou divulgar isto

    • Estava agora a editar para colocar o código, pode dar jeito :D

      • Boas

        ja aproveitando este topico eu gostava d saber se voces s importariam d me ajudar a programar o meu robô

        • Claro, sempre que tenhas dúvidas contactas-nos.

          • onde pode ser o sitio mais apropriado para te contactar?

          • É só adicionares no mensseger: msn@makebits.net

          • olha podias ajudar m com um sensor que estou a utilizar o qtr-8rc, nao me estou a dar mt bem com ele

          • Boas, apenas tens de utilizar como um sensor normal… Experimenta obter valores simples com um potenciomentro e uns leds

  • Quanto pode custar um bot destes?

    • 10€ para a gearbox
      10€ para as lagartas
      7€ para o breaduino
      5€ para a alimentação
      10€ para diversos

      10+10+7+5+10=42 no máximo 50€
      :D

      • Não te esqueças do preço dos sensores/electrónica. ;)

        • A electrónica (não é muita) tá incluida nos diversos – 10€. O que me esqueci foi do sensor IR frontal que custa +/- 14€ :)

          • Certo. :)

            Só mais um reparo. Pode sempre poupar algum "reciclando" alguns componentes coisas antigas. Um exemplo é utilizar rodas que se podem encontrar em velhas impressoras, em vez das lagartas :)

            Cumprimentos

          • também se pode poupar nos sensores ir, fazendo uns em casa :D basta um comandos de televisão e uns televisores velhos e temos uns sensores IR por 0€

  • gostava de saber como comprar um arduino UNO e o preço +/- pf :)
    Obrigado

    • Tens uma guia com varias lojas aqui no Make Bits

Comenta


Make Bits no teu email



* obrigatório


Publicidade:


Sistema Dinheiro Teste Drive