viliamk
Published © GPL3+

MyTurtle: self-learning robot (Part 2 - Movement unit)

This project is about creating a self-learning robot from the begining to a hopeful end. Part 2 deals about the mechanical base.

BeginnerWork in progress570
MyTurtle: self-learning robot (Part 2 - Movement unit)

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Stepper Motor, Mini Step
Stepper Motor, Mini Step
×2
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
HC-05 Bluetooth Module
HC-05 Bluetooth Module
×1

Story

Read more

Code

Movement unit

Arduino
Serves the movement unit
/* Basic robot car as a preparation for creating a self-learning robot
 * by Viliam Krunka
 */

#include <Stepper.h>
#include <Servo.h>

#define servoPin 3

//motors
const int stepsPerRevolution = 2048; 
const int rpm = 15;   //set speed of motors (max 16 for chosen step motors)
const int cycle = 500;

Servo Head;
Stepper LeftMotor = Stepper(stepsPerRevolution, 8, 10, 9, 11);
Stepper RightMotor = Stepper(stepsPerRevolution, 4, 6, 5, 7);

void setup() {
  InitializeHead();
  InitializeMotors();
}

void loop() {
  switch(random(10)) {
    case 0:
      delay(1000);
    case 1:
      MoveForward(cycle);
      break;
    case 2:
      MoveBack(cycle);
      break;
    case 3:
      MoveLeftForward(cycle);
      break;
    case 4:
      MoveRightForward(cycle);
      break;
    case 5:
      TurnLeft(cycle);
      break;
    case 6:
      TurnRight(cycle);
      break;
    case 7:
      TurnHeadFront();
      break;
    case 8:
      TurnHeadLeft();
      break;
    case 9:
      TurnHeadRight();
      break;
  }
}

//servo motor

void InitializeHead() {
  Head.attach(servoPin);  //set pin & 
}

void TurnHeadFront() {
  int k = Head.read();
  if (k < 90) {
    for (k; k <= 90; k++) {
      Head.write(k);   //set angle of servo motor  
      delay(10);
    }
  } else {
      for (k; k >= 90; k--) {
      Head.write(k);   //set angle of servo motor  
      delay(10);
    }
  }
}

void TurnHeadLeft() {
  for (int k = Head.read(); k <= 150; k++) {
    Head.write(k);   //set angle of servo motor  
    delay(10);
  }
}

void TurnHeadRight() {
  for (int k = Head.read(); k >= 30; k--) {
    Head.write(k);   //set angle of servo motor
    delay(10);
  }
}

//motors

void InitializeMotors() {
  LeftMotor.setSpeed(rpm);
  RightMotor.setSpeed(rpm);
}

void MoveForward(int cycle) {
 for (int k=0; k < cycle; k++) {
    LeftMotor.step(1);
    RightMotor.step(1);
 }
}

void MoveBack(int cycle) {
 for (int k=0; k < cycle; k++) {
    LeftMotor.step(-1);
    RightMotor.step(-1);
 }
}

void MoveLeftForward(int cycle) {  //45 degrees = cycle 500
 for (int k=0; k < cycle; k++) {
    LeftMotor.step(1);
    RightMotor.step(3);
 }
}

void MoveRightForward(int cycle) {  //45 degrees = cycle 500
 for (int k=0; k < cycle; k++) {
    LeftMotor.step(3);
    RightMotor.step(1);
 }
}

void TurnLeft(int cycle) {    
 for (int k=0; k < cycle; k++) {
    LeftMotor.step(-1);
    RightMotor.step(1);
 }
}

void TurnRight(int cycle) { 
 for (int k=0; k < cycle; k++) {
    LeftMotor.step(1);
    RightMotor.step(-1);
 }
}

Credits

viliamk
14 projects • 5 followers
Contact

Comments

Please log in or sign up to comment.