├── README.md └── DefensiveRadar └── DefensiveRadar.ino /README.md: -------------------------------------------------------------------------------- 1 | # DefensiveRadar Project 2 | This is a school project where the instruction was to build a robot based on a Lego structure and on Makeblock sensors (because of hardware availability limitations). 3 | The structure we built looks like this : 4 | **![](https://lh6.googleusercontent.com/NJl1IC1Vx3p7mFe5O0Dq3-76MP3sFJ_4x0A9BOu3MntaWPR27tzvu6RsBbOtDkTc-stzYtoPWvHoJ_E9JZF3JhK9MXr3GQznPXHrOtjwhCO2IA0wNKTFuV5bmQUJP55OuIcXMUWi)****![](https://lh4.googleusercontent.com/9A2gKTU6L_dMkr5FrBQqPE4BSeYRzS7MSwhyvcV9rH0BDI8oVbE_2dGikxwtRVX6BQ0Dk3TaXSyiDvuP5gXtBJZ76DA3RlM4fTkERO38LzA0yvU_Rixyw7EyIf3MKhhtiFZVlkpk)** 5 | # General algorithm 6 | **![](https://lh5.googleusercontent.com/o9ZgcIeiMyhHDjQjz-KOZsirs-LIP-s4n-kV7BzZpwni9pqtFqx7bu5yTZcrzkCJCbRKlP3PD2tyW92BD3bmlIt_vv4SxFw8R-GJyJ0bk8khNSyp6XiBCIMAqf5kg-Kf-4MwTr4k)** 7 | **![](https://lh4.googleusercontent.com/ry9TTh2XFTeu-Rz6niBi3rXwzQ-pcme_Wj1KbRq6mbf67MJVtszh7Ocuds8bFyBENcUyKkQ-9guPetXLPT2-M5bbhvTQMMU99dXv_tzXCKxd71KMxvYzrJVwA1f0URZegTpHQWTj)** 8 | 9 | The `fire()` method simply asks motor2 to run 1 turn. 10 | 11 | 12 | # Contributors 13 | - Stéphane DELEBASSE 14 | - [Nans DUMORTIER](http://github.com/nansd) 15 | -------------------------------------------------------------------------------- /DefensiveRadar/DefensiveRadar.ino: -------------------------------------------------------------------------------- 1 | /** 2 | * @file DefensiveRadar.ino 3 | * @author Stéphane DELEBASSE - Nans DUMORTIER 4 | * @version V1.0.0 5 | * @date 2018/05/23 6 | * @brief Description: this file is a program that allows a robot to scan its environment and fire at targets 7 | * 8 | * Function List: 9 | * 1. checkDistance : function that checks what's in front of the robot, if a target is in range, rescan the area and fire at it 10 | * 2. fire : function that fire balls contained in the tank 11 | * 12 | * motorX.moveTo, motorX.getCurrentPosition, motorX.runTurns are functions provided by Makeblock 13 | * See : https://github.com/Makeblock-official/Me_EncodeDriver 14 | * ultraSensor.distanceCm is another function provided by Makeblock 15 | * 16 | **/ 17 | 18 | #include "MeOrion.h" 19 | #include 20 | #include 21 | 22 | MeUltrasonicSensor ultraSensor(PORT_6); /* Ultrasonic module can ONLY be connected to port 3, 4, 6, 7, 8 of base shield. */ 23 | MeEncoderMotor motor1(0x09, SLOT1); // motor at slot2 24 | MeEncoderMotor motor2(0x09, SLOT2); // motor at slot2 25 | 26 | void setup() 27 | { 28 | Serial.begin(9600); 29 | // motor1 is responsible for the rotation that allows to scan an area 30 | motor1.begin(); 31 | // motor2 is responsible for firing 32 | motor2.begin(); 33 | } 34 | 35 | void loop() 36 | { 37 | Serial.println("Moving left"); 38 | float desiredAngle = 50; 39 | float desiredSpeed = 10; 40 | motor1.moveTo(desiredAngle, desiredSpeed); 41 | for(int i = 0; i < 20; i++){ 42 | checkDistance(); // function that takes at least 100ms to run 43 | } 44 | 45 | 46 | Serial.println("Moving right"); 47 | desiredAngle = -desiredAngle; 48 | motor1.moveTo(desiredAngle, desiredSpeed); 49 | for(int j = 0; j < 20; j++){ 50 | checkDistance(); // function that takes at least 100ms to run 51 | } 52 | } 53 | 54 | void checkDistance() 55 | // function that checks what's in front of the robot 56 | // if a target is in range, rescan the area and fire at it 57 | { 58 | float currentPos = motor1.getCurrentPosition(); 59 | float distanceCm; 60 | distanceCm = ultraSensor.distanceCm(); 61 | 62 | if(distanceCm > 5.0 && distanceCm <= 25.0){ 63 | // rescan the area 64 | // memorize the position where the object is 65 | // (= where the distance between the robot and the 66 | // closest obstacle is the smallest) 67 | // move to that position 68 | // fire 69 | Serial.println("Target detected"); 70 | motor1.moveTo(currentPos - 10, 15); 71 | Serial.println("Moving for scanning"); 72 | float desiredAngle = currentPos + 10; 73 | Serial.println("Beginning scan"); 74 | float desiredSpeed = 7; 75 | float distance2 = 30; 76 | float reading; 77 | float position2 = currentPos; 78 | motor1.moveTo(desiredAngle, desiredSpeed); 79 | for(int k = 0; k < 10; k++){ 80 | reading = ultraSensor.distanceCm(); 81 | if(distance2 > reading){ 82 | distance2 = reading; 83 | position2 = motor1.getCurrentPosition(); 84 | } 85 | delay(100); 86 | } 87 | Serial.println("Moving towards the target"); 88 | Serial.println(position2); 89 | motor1.moveTo(position2, 15); 90 | delay(1000); 91 | Serial.println("Target's position reached"); 92 | Serial.println("FIRE !"); 93 | fire(); 94 | } 95 | 96 | if (distanceCm <= 5.0){ 97 | motor1.moveTo(currentPos, 10); 98 | delay(100); 99 | // fire almost instantly if the target is too close 100 | fire(); 101 | } 102 | delay(100); 103 | } 104 | 105 | void fire() 106 | { 107 | motor2.runTurns(0.5,200); 108 | delay(1000); 109 | Serial.println("Projectile launched"); 110 | } 111 | 112 | 113 | 114 | --------------------------------------------------------------------------------