Patrick Gräf
Created November 10, 2016

Mark2

Bot, research the outer world!

156
Mark2

Things used in this project

Hardware components

Arduino 101
Arduino 101
×1
Adafruit Motor Shield
×1
DC motor (generic)
×2
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Story

Read more

Schematics

HC-SR04 SuperSonic Sensor Connection

This shows simply how to connect the Sonic Sensor to the Arduino

Motorshield Connection

This simply shows how to connect a DC Motor to the Motorshield. Motorshield can be used to drive up to 4 DC Motors, Interfaces for this are M1-M4 (like seen on the picture). The shield itself will be set up on top of the Arduino Board with it's pin connections.

Code

Drive around and avoid contact to environment

C/C++
Indroducing Mark1. It's one of my first iterations to reach the Project's goal. He drives around, recognizes near objects/walls and avoids to collide with them.
I tried to solve the 360° turn problem with DC Motors, it's nearly close but not enough. When i assemble other motors or battery status changes, calculations has to be different. It needs to be done with additional sensoric.
#include <AFMotor.h>
#include <NewPing.h>

#define TRIGGER_PIN  2  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     13  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

AF_DCMotor motor1(1, MOTOR12_8KHZ);
AF_DCMotor motor2(4, MOTOR34_8KHZ);

bool halt           = false;
float multiplicator = 0.68;
int motorSpeed      = 120;
int degree360       = (int)(920 / 148 * motorSpeed);
int degree180       = (int)(degree360 / 2);
int degree90        = (int)(degree360 / 4);

void setup() {
  Serial.begin(115200);
  
  motor1.setSpeed((int)(motorSpeed * multiplicator));
  motor2.setSpeed(motorSpeed);
}

void turnLeft(int del) {
  motor1.run(BACKWARD);
  motor2.run(FORWARD); 
  delay(del);
}

void turnRight(int del) {
  motor1.run(FORWARD);
  motor2.run(BACKWARD); 
  delay(del);
}

void stop(int del) {
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  delay(del);
}

bool isClose(float distance) {
  return distance > 0 && distance < 25;
}

float getDistance() {
  return (float)sonar.ping_median(7) / US_ROUNDTRIP_CM;
}

bool turnAround() {
  turnRight(random(degree90, degree180));

  if(isClose(getDistance())) {
    turnRight(random(degree90, degree180));
  }

  if(isClose(getDistance())) {
    turnLeft(random(degree90, degree180));
  }
  
  return isClose(getDistance()) ? true : false;
}
 
void loop() {
  delay(30);

  if(halt && !isClose(getDistance())) halt = false;

  if(halt) {
    stop(0);
  } else if(isClose(getDistance())) {
    halt = turnAround();
  } else {
    motor1.run(FORWARD);
    motor2.run(FORWARD);
  }
}

Credits

Patrick Gräf

Patrick Gräf

2 projects • 0 followers

Comments