GirlsGoIT Wiki (ro)
RU
  • Introducere
  • Unelte
    • Slack
    • Visual Studio Code
    • CodeSandbox
      • Cum rezolvi un task la proiect
      • Cum rezolvi un exercițiu
    • GitHub
  • FRONTEND STATIC
    • Internet
    • HTML
    • CSS
    • Exerciții
      • Erontend Static Exercitii
  • FRONTEND DINAMIC
    • JavaScript
      • Introducere
      • Consola Browser'ului
      • Variabile
      • Tipuri de date
      • Operatori
      • JavaScript & HTML
      • Funcții
      • DOM & Window API
      • Math API
      • Condiționale (if)
      • Cicluri (for & while)
    • Vue.js
      • Componente Vue
      • Componente & Forme
      • Lifecycle hooks
      • Comunicare între componente
      • Vue Router
      • Axios
      • Instalare locală
    • Exercitii
    • Rest API
      • Rest api GET
      • Rest api GET one
      • Rest api POST
      • Rest api PUT
      • Rest api DELETE
  • PYTHON
    • Introducere
    • Variabile
    • Tipuri de Date
    • Operatori
    • Control Flow
    • Liste
    • Cicluri for
    • Funcții
    • Dictionare
    • Seturi
    • Fișiere
    • Try-except-finally. Lambda
    • Librării, module
    • Exercitii
      • Variabile
      • TIpuri de Date
      • Operatori
      • If...else
      • Liste
      • Cicluri for
      • Funcții
      • Dicționare
      • Seturi
      • Fișiere
      • Try-except, Lambda
      • Librării , module
      • ✨Mai multe exerciții ✨
    • Python Archiva
  • BACKEND
    • Instalare Python
    • Python OOP
    • Baze de date
    • Django
    • REST API's
    • Django REST Framework
    • Exerciții
      • Python OOP
      • Django
      • Django Rest Framework
  • Data Science
    • Introducere în Data Science
    • Vizualizarea datelor
      • Matplotlib
    • Numpy
    • Pandas
    • DecisionTrees
  • 3D Printing
    • Intro
    • Fusion 360 Intro
    • Sketch
    • Solid body
    • Arduino
    • Final Projects
  • ARHIVA
    • Milligram
    • Drone
      • Intro
    • Old Backend
      • Bootstrap
      • Hello Flask
      • DB Modeling
      • Ubuntu Quest
      • Baze de date
      • Heroku Deploy
    • Robotica NAO
      • Finalizarea proiectelor
      • Flow Control
      • Cum creez un Dialog box
      • Convenția GirlsGoIT Robotics Choregraphe
      • Tracker
      • Dialog - QiChat
    • Robotica
      • 1.1 mBlock
      • 1.2 Descărcarea, instalarea și conectarea
      • 1.3 Execută un program de pe mBlock IDE sau încarcă pe Board
      • 1.4 Conectarea prin USB, Bluetooth sau 2.4GHz
      • 2.1 Programe Simple
      • 2.2 Senzori
      • 2.3 Exerciții avansate - Scratch
      • 3.0.1 Instalare Arduino IDE
      • 3.0.2 Instalare Arduino IDE WEB
      • 3.1 Configurare Arduino IDE
      • 3.2 Introducere în limbajul C
      • 3.3 Exemple Arduino - mBot
      • 3.4 Line follow - Arduino
      • 3.5 Ultimate robot
      • 4.1 Planificarea și managementul proiectelor
      • Custom robot
      • Noțiuni și explicații
      • Delay fara sa opreasca programul
      • Alte resurse și exemple
Powered by GitBook
On this page
  • Sensorul de line follow
  • 4 cazuri posibile
  • Schema logică a programului
  • O versiune a codului final (linia albă)
  • O versiune a codului final (linia neagră):
  1. ARHIVA
  2. Robotica

3.4 Line follow - Arduino

Previous3.3 Exemple Arduino - mBotNext3.5 Ultimate robot

Last updated 6 years ago

Sensorul de line follow

4 cazuri posibile

Schema logică a programului

O versiune a codului final (linia albă)

#include "MeMCore.h"

MeLineFollower lineFinder(PORT_2); /* Line Finder module can only be connected to PORT_3, PORT_4, PORT_5, PORT_6 of base shield. */

MeDCMotor motor1(M1);
MeDCMotor motor2(M2);

float MOTOR1_TUNE = -1.0;
float MOTOR2_TUNE = 1.0;

uint8_t motorSpeed = 100;

bool prev_move_left = false;
bool prev_move_fwd = true;

void setup()
{
  // Optional: robotul porneste de la un buton
  pinMode(7, INPUT); //Define button pin as input
  while (analogRead(7) > 100) {
    delay(50); //Wait till button pressed to start.
  }
 
  // setez comunicarea prin portul serial
  Serial.begin(9600);
}

void activateLeftMotor() {
   motor1.run(MOTOR1_TUNE*motorSpeed); /* value: between -255 and 255. */
}

void activateRightMotor() {
   motor2.run(MOTOR2_TUNE*motorSpeed); /* value: between -255 and 255. */
}

void stopLeftMotor() {
   motor1.stop();
}

void stopRightMotor() {
   motor2.stop();
}

void turnLeft() {
  activateLeftMotor();
  stopRightMotor();
}

void turnRight() {
  activateRightMotor();
  stopLeftMotor();
}


void bothMotorsRun() {
  activateRightMotor();
  activateLeftMotor();
 }

void loop()
{
  int sensorState1 = lineFinder.readSensors();

  int sensorState = 1;
  
  if(sensorState1==S1_IN_S2_IN) {
    sensorState = S1_OUT_S2_OUT;
  }
  else if(sensorState1==S1_IN_S2_OUT) {
    sensorState = S1_OUT_S2_IN;
  }
  else if(sensorState1==S1_OUT_S2_IN) {
    sensorState = S1_IN_S2_OUT;
  }
  else if(sensorState1==S1_OUT_S2_OUT) {
    sensorState = S1_IN_S2_IN;
  }
  
  switch(sensorState)
  {
    case S1_IN_S2_IN: 
      Serial.println("Sensor 1 and 2 are inside of black line"); 
      bothMotorsRun();
      prev_move_left = false;
      prev_move_fwd = true;
      break;
    
    case S1_IN_S2_OUT: 
      Serial.println("Sensor 2 is outside of black line");
      turnRight();
      prev_move_left = false;
      prev_move_fwd = false;
      break;
      
    case S1_OUT_S2_IN: 
      Serial.println("Sensor 2 is outside of black line"); 
      prev_move_left = true;
      prev_move_fwd = false;
      turnLeft();
      break;
    
    case S1_OUT_S2_OUT: 
      if(prev_move_fwd) {
        bothMotorsRun();
      }
      else if(prev_move_left) {
        turnLeft();
      } else {
        turnRight();
      } 
      
      Serial.println("Sensor 1 and 2 are outside of black line"); 
      break;
    
    default: break;
  }
}

O versiune a codului final (linia neagră):

Codul tău final poate arăta altfel. Nu e nevoie să copiezi acest cod. Aici ai doar un exemplu din care să te inspiri

#include "MeMCore.h"

MeLineFollower lineFinder(PORT_2); /* Line Finder module can only be connected to PORT_3, PORT_4, PORT_5, PORT_6 of base shield. */

MeDCMotor motor1(M1);
MeDCMotor motor2(M2);

float MOTOR1_TUNE = -1.0;
float MOTOR2_TUNE = 1.0;

uint8_t motorSpeed = 100;

bool prev_move_left = false;
bool prev_move_fwd = true;

void setup()
{
  // Optional: robotul porneste de la un buton
  pinMode(7, INPUT); //Define button pin as input
  while (analogRead(7) > 100) {
    delay(50); //Wait till button pressed to start.
  }
  
  // setez comunicarea prin portul serial
  Serial.begin(9600);
}

void activateLeftMotor() {
   motor1.run(MOTOR1_TUNE*motorSpeed); /* value: between -255 and 255. */
}

void activateRightMotor() {
   motor2.run(MOTOR2_TUNE*motorSpeed); /* value: between -255 and 255. */
}

void stopLeftMotor() {
   motor1.stop();
}

void stopRightMotor() {
   motor2.stop();
}

void turnLeft() {
  activateLeftMotor();
  stopRightMotor();
}

void turnRight() {
  activateRightMotor();
  stopLeftMotor();
}


void bothMotorsRun() {
  activateRightMotor();
  activateLeftMotor();
 }

void loop()
{
  int sensorState = lineFinder.readSensors();
  
  switch(sensorState)
  {
    case S1_IN_S2_IN: 
      Serial.println("Sensor 1 and 2 are inside of black line"); 
      bothMotorsRun();
      prev_move_left = false;
      prev_move_fwd = true;
      break;
    
    case S1_IN_S2_OUT: 
      Serial.println("Sensor 2 is outside of black line");
      turnRight();
      prev_move_left = false;
      prev_move_fwd = false;
      break;
      
    case S1_OUT_S2_IN: 
      Serial.println("Sensor 2 is outside of black line"); 
      prev_move_left = true;
      prev_move_fwd = false;
      turnLeft();
      break;
    
    case S1_OUT_S2_OUT: 
      if(prev_move_fwd) {
        bothMotorsRun();
      }
      else if(prev_move_left) {
        turnLeft();
      } else {
        turnRight();
      } 
      
      Serial.println("Sensor 1 and 2 are outside of black line"); 
      break;
    
    default: break;
  }
  
}

cele 4 cazuri
Schema logică a programului