EdOliver
Published © MIT

The LEGO® Guardian

A wicked autonomous bulldozer capable of removing harmful pieces of LEGO® that are detected by computer vision.

AdvancedFull instructions providedOver 1 day1,115
The LEGO® Guardian

Things used in this project

Hardware components

Spresense boards (main & extension)
Sony Spresense boards (main & extension)
×1
DC motor (generic)
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×3
Servo Module (Generic)
×1
Sony Spresense camera board
×1
Raspberry Pi Zero Wireless
Raspberry Pi Zero Wireless
×1

Software apps and online services

Arduino IDE
Arduino IDE
Fusion
Autodesk Fusion
Python
Autodesk AutoCAD
Node-RED
Node-RED
Watson
IBM Watson

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Schematics of only Bluetooth control

Code

Spresense simple Bluetooth control

C/C++
Simple Spresense Bluetooth Control without the other characteristics. Run it with Arduino IDE and a HC-06 Bluetooth module.
//This is a simple control Program via Bluetooth. Made by Edoliver, Luis Eduardo Oliver for the Make it better contest by Hackster and Sony, this is intended only to be used for the Spresense board.

char blueToothVal;           //value sent over via bluetooth
char lastValue;              //stores last state of device (on/off)


void setup()
{
 Serial2.begin(9600);  // The Spresense board uses Serial for USB and Serial2 for TX/RX pins
 Serial.begin(38400); //Notice how we have to Serials at different bauds 
 pinMode(7,OUTPUT);         
 pinMode(2, OUTPUT);
 pinMode(3, OUTPUT);
 pinMode(4, OUTPUT);
 pinMode(5, OUTPUT);
}
 
 
void loop()
{
  if(Serial2.available())
  {//if there is data being recieved
    blueToothVal=Serial2.read(); //read it
  }
  
  if (blueToothVal=='a')
  {//if value from bluetooth serial is a
    digitalWrite(2,HIGH); 
    digitalWrite(3, LOW); 
    digitalWrite(4,HIGH); 
    digitalWrite(5, LOW);         
    if (lastValue!='a')
      Serial.println(F("Forward")); //print Forward
    lastValue=blueToothVal;
  }
  else if (blueToothVal=='b')
  {//if value from bluetooth serial is b
    digitalWrite(2,LOW); 
    digitalWrite(3, HIGH);  
    digitalWrite(4, LOW); 
    digitalWrite(5, HIGH); 
    if (lastValue!='b')
      Serial.println(F("Backwards")); //print Backwards
    lastValue=blueToothVal;
  }
    else if (blueToothVal=='c')
  {//if value from bluetooth serial is c
    digitalWrite(2,LOW); 
    digitalWrite(3, HIGH);  
    digitalWrite(4, HIGH); 
    digitalWrite(5, LOW); 
    if (lastValue!='c')
      Serial.println(F("Right")); //print Right
    lastValue=blueToothVal;
  }
    else if (blueToothVal=='d')
  {//if value from bluetooth serial is d
    digitalWrite(2,HIGH); 
    digitalWrite(3, LOW);  
    digitalWrite(4, LOW); 
    digitalWrite(5, HIGH); 
    if (lastValue!='d')
      Serial.println(F("Left")); //print Left
    lastValue=blueToothVal;
  }
  else if (blueToothVal=='g')
  {//if value from bluetooth serial is g
    digitalWrite(2,LOW); 
    digitalWrite(3, LOW);  
    digitalWrite(4, LOW); 
    digitalWrite(5, LOW); 
    if (lastValue!='d')
      Serial.println(F("Left")); //print STOP
    lastValue=blueToothVal;
  }
  delay(1000);
}

Spresense Obstacle Avoidance

C/C++
Obstacle avoidance for the bulldozer.
//By Edoliver inspired by: https://www.youtube.com/watch?v=1n_KjpMfVT0

// defines pins numbers
const int trigPin = 8;
const int echoPin = 9;
const int trigPin1 = 10;
const int echoPin1 = 11;
const int trigPin2 = 12;
const int echoPin2 = 13;
// defines variables

long duration;
long duration1;
long duration2;

int distance;
int distance1;
int distance2;

void setup() {

  pinMode(7,OUTPUT);         
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  pinMode(trigPin1, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin1, INPUT); // Sets the echoPin as an Input
  pinMode(trigPin2, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin2, INPUT); // Sets the echoPin as an Input

  Serial.begin(9600); // Starts the serial communication
}
void loop() {
//FOR #0
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
delay(100);
//FOR #1
// Clears the trigPin
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration1 = pulseIn(echoPin1, HIGH);
// Calculating the distance
distance1= duration1*0.034/2;
// Prints the distance on the Serial Monitor
delay(100);

//FOR #2
// Clears the trigPin
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
// Sets the trigPin2 on HIGH state for 10 micro seconds
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration2 = pulseIn(echoPin2, HIGH);
// Calculating the distance
distance2= duration2*0.034/2;
// Prints the distance on the Serial Monitor
delay(100);

moveForward();

if (distance<=16){ ///you'll have to alter these values accordingly, whether it be the delays or others so the robot makes a 45 degree turn
  moveStop();
  delay(100);
  moveBackward();
  delay(300);
  moveStop();
  delay(200);
  if(distance1<=distance2)
  {
    moveRight();
    delay(300);
    moveStop();
    delay(300);
  }else
  {
    moveLeft();
    delay(300);
    moveStop();
    delay(300);
  }
 }else
 {
  moveForward();
 }
}


void moveForward(){
    digitalWrite(2,HIGH); 
    digitalWrite(3, LOW); 
    digitalWrite(4,HIGH); 
    digitalWrite(5, LOW);
}

void moveBackward(){
  digitalWrite(2,LOW); 
  digitalWrite(3, HIGH);  
  digitalWrite(4, LOW); 
  digitalWrite(5, HIGH); 
}

void moveLeft(){
  digitalWrite(2,HIGH); 
  digitalWrite(3, LOW);  
  digitalWrite(4, LOW); 
  digitalWrite(5, HIGH); 
}

void moveRight(){
  digitalWrite(2,LOW); 
  digitalWrite(3, HIGH);  
  digitalWrite(4, HIGH); 
  digitalWrite(5, LOW); 
}

void moveStop(){
  digitalWrite(2,LOW); 
  digitalWrite(3, LOW);  
  digitalWrite(4, LOW); 
  digitalWrite(5, LOW);  
}

Spresense three ultrasonic sensors

C/C++
Functional code for the Spresense to use three simultaneous Ultrasonic sensors. NewPing does not work with the Spresense. Based on the code by Dejan Nedelkovski from Howtomechatronics.com
/*
* Ultrasonic Sensor HC-SR04 and Arduino Tutorial
*
* by Dejan Nedelkovski,
* www.HowToMechatronics.com
* Altered for three sensors by Edoliver
*
*/
// defines pins numbers
const int trigPin = 8;
const int echoPin = 9;
const int trigPin1 = 10;
const int echoPin1 = 11;
const int trigPin2 = 6;
const int echoPin2 = 7;
// defines variables

long duration;
long duration1;
long duration2;

int distance;
int distance1;
int distance2;

void setup() {
  
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  pinMode(trigPin1, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin1, INPUT); // Sets the echoPin as an Input
  pinMode(trigPin2, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin2, INPUT); // Sets the echoPin as an Input

  Serial.begin(9600); // Starts the serial communication
}
void loop() {
//FOR #0
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor

//FOR #1
// Clears the trigPin
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration1 = pulseIn(echoPin1, HIGH);
// Calculating the distance
distance1= duration1*0.034/2;
// Prints the distance on the Serial Monitor
delay(1000);

//FOR #2
// Clears the trigPin
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
// Sets the trigPin2 on HIGH state for 10 micro seconds
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration2 = pulseIn(echoPin2, HIGH);
// Calculating the distance
distance2= duration2*0.034/2;
// Prints the distance on the Serial Monitor


Serial.print("Distance: ");
Serial.println(distance);
Serial.print("Distance1: ");
Serial.println(distance1);
Serial.print("Distance2: ");
Serial.println(distance2);

delay(100);
}

Node-RED flow for image recognition

JavaScript
Node Red flow for image recognition, just set your credentials as the instructional says. All cloud services may ban you if you share credentials online.
[{"id":"d7ae1db2.4d201","type":"switch","z":"cdc0a8b4.e65748","name":"","property":"result.images[0].sharpener[0]","propertyType":"msg","rules":[{"t":"nnull"},{"t":"null"}],"checkall":"true","repair":false,"outputs":2,"x":617.1000366210938,"y":262.20001220703125,"wires":[["c7adca37.e93f18"],["43ec29f2.ac8a88"]]}]

Credits

EdOliver

EdOliver

38 projects • 74 followers
Engineer, Scientist, Maker. Entrepreneur and Futurist.

Comments