0

Arduino tečaj: praćenje linije

2. lesson

Ovo je dio cjelovitog tečaja Arduino-a.
  1. Štampajte dijelove koristeći ovaj Sketchup model RescueLine.zip.
  2. Sastavite robota koristeći upute za sastavljanje.
  3. Spojite kablove koristeći plan spajanja.
  4. Programirajte robota.
    1. Osnove.
    2. Praćenje crte (ova stranica).
    3. Naredbe.
    4. LIDARi.
    5. Prekidač, IMU, 8x8 LED.
    6. Praćenje zida.
    7. Srebro.
    8. 8x8 LED programiranje.
    9. Robotska ruka.
    10. Raspberry Pi.
    11. Prepoznavanje kamerom, OpenCV.
    12. Arduino i RPI surađuju.

Reflectance sensors

Enter the orange changes:

#include <Motors.h> //Library Motors will be used. It contain motor-control functions.
#include <ReflectanceSensors.h>

Motors motors; //motors is an C++ object of class Motors. If You do not understand this, never mind. Just alwasys use motors with this line.
ReflectanceSensors reflectanceSensors;

void setup() {
  Serial.begin(115200);    // Serial (USB) communication to our computer is established. You call function begin of the object Serial with 1 argument: 115200.
  delay(1000);             // Do nothing for 1000 ms (1 sec.) - wait for the USB connection.
  Serial.println("Start"); // Display "Start" on PC's screen. Function println() prints the argument ("Start") and jumps to a new line.

  //In the following 4 lines we will add 4 motors. First 2 parameters, numbers (like 3, 5), are pins that are connected
  //to motor controller. Third parameter (true or false) is true when it is a left motor, otherwise false. There is an optional fourth parameter in the
  //first 2 lines and there can be even more of them. Optional (default) parameters do not have to be mentioned.
  motors.add(3, 5, true, true);
  motors.add(23, 4, false);
  
  //10 reflectance sensors will be added. ML-R libraries always include new elements (like sensors and motors) using the function add().
  //First parameter is analog pin's name, second millimeters from the central longitudinal axis (negative numbers for left sensors).
  //The last parameter is always true and enables that sensor for the libary's line following algorithm.
  reflectanceSensors.add(A11, -49.5, true); 
  reflectanceSensors.add(A10, -38.5, true);
  reflectanceSensors.add(A13, -27.5, true);
  reflectanceSensors.add(A12, -16.5, true);
  reflectanceSensors.add(A1, -5.5, true);
  reflectanceSensors.add(A14, 5.5, true);
  reflectanceSensors.add(A0, 16.5, true);
  reflectanceSensors.add(A2, 27.5, true);
  reflectanceSensors.add(A17, 38.5, true);
  reflectanceSensors.add(A15, 49.5, true);
  reflectanceSensors.eepromRead();//Reading EEPROM calibration data into RAM.
  
  reflectanceSensors.test(); // Test in a continuous loop. Every ML-R library has this ("test()") function, when testing makes sense.
  
  //The following 3 lines will move the robot forwards and stop after 1 sec.
  motors.go(30, 30); //Turn the left and right motors using the same speed: 30 (100 is maximum).
  delay(1000);       //Wait 1 sec. (while the motors are running, so the robot goes forwards).
  motors.go(0, 0);   //Stop the motors.
}

void loop() {
}

/**All the ML-R libraries invoke this function when something goes wrong. Here we stop the motors, display the error
message which enters the function as the only argument ("message") and then enter an endless loop. while(1) never ends as the condition
(1) is always true. The only command that is in the loop is ";" (empty command - does nothing).
@param message*/
void error(String message){
  motors.go(0, 0);         //stop the motors to avoid harm
  Serial.println(message); //display error message
  while(1)                 //never stop
    ;                      //do nothing
}

We will cover reflectanceSensors.eepromRead() a little later. The code prepares 10 reflectance sensors for reading and then enters library's test function. Try moving the sensors over a black line on a white background. Values of sensors must change much. If everything works fine, go on the next program change.

Line following

Enter again the orange changes:

#include <Motors.h> //Library Motors will be used. It contain motor-control functions.
#include <PID.h> //Library that implements a very simple proportional–integral–derivative controller. Check Wikipedia.
#include <ReflectanceSensors.h>

Motors motors; //motors is an C++ object of class Motors. If You do not understand this, never mind. Just always use motors with this line.
PID pidLine(20, 15, 0.01); //PID object: pidLine. After studying Wikipedia, alter the 3 parameters.
ReflectanceSensors reflectanceSensors;

void setup() {
  Serial.begin(115200);    // Serial (USB) communication to our computer is established. You call function begin of the object Serial with 1 argument: 115200.
  delay(1000);             // Do nothing for 1000 ms (1 sec.) - wait for the USB connection.
  Serial.println("Start"); // Display "Start" on PC's screen. Function println() prints the argument ("Start") and jumps to a new line.

  //In the following 4 lines we will add 4 motors. First 2 parameters, numbers (like 3, 5), are pins that are connected
  //to motor controller. Third parameter (true or false) is true when it is a left motor, otherwise false. There is an optional fourth parameter in the
  //first 2 lines and there can be even more of them. Optional (default) parameters do not have to be mentioned.
  motors.add(3, 5, true, true);
  motors.add(23, 4, false);
  
  //10 reflectance sensors will be added. ML-R libraries always include new elements (like sensors and motors) using the function add().
  //First parameter is analog pin's name, second millimeters from the central longitudinal axis (negative numbers for left sensors).
  //The last parameter is always true and enables that sensor for the library's line following algorithm.
  reflectanceSensors.add(A11, -49.5, true); 
  reflectanceSensors.add(A10, -38.5, true);
  reflectanceSensors.add(A13, -27.5, true);
  reflectanceSensors.add(A12, -16.5, true);
  reflectanceSensors.add(A1, -5.5, true);
  reflectanceSensors.add(A14, 5.5, true);
  reflectanceSensors.add(A0, 16.5, true);
  reflectanceSensors.add(A2, 27.5, true);
  reflectanceSensors.add(A17, 38.5, true);
  reflectanceSensors.add(A15, 49.5, true);
  reflectanceSensors.eepromRead();//Reading EEPROM calibration data into RAM.
  
  reflectanceSensors.test(); // Test in a continuous loop. Every ML-R library has this ("test()") function, when testing makes sense.
}

void loop() {
  bool found, nonLineFound;
  //The following function returns position of the black line against the longitudinal centre of the robot. found and notFound are output
  //parameters. found is true if a line is found (at least one sensor black). nonLineFound not found (that at least one sensor doesn't detect black).
  float centerMm = reflectanceSensors.findLine(found, nonLineFound);
  //centerMm is in fact error in line following. It should be 0. PID algorithm uses it as an input and calculates error correction:
  float correction = pidLine.calculate(centerMm);

  const int speedAhead = 60; //Average speed of the left and right motor. You can change this number.
  if (found)
    motors.go(speedAhead + correction, speedAhead - correction);//Here we control the motors.
  else
    motors.go(50, 50); //If no line, go straight ahead.
}

/**All the ML-R libraries invoke this function when something goes wrong. Here we stop the motors, display the error
message which enters the function as the only argument ("message") and then enter an endless loop. while(1) never ends as the condition
(1) is always true. The only command that is in the loop is ";" (empty command - does nothing).
@param message*/
void error(String message){
  motors.go(0, 0);         //stop the motors to avoid harm
  Serial.println(message); //display error message
  while(1)                 //never stop
    ;                      //do nothing
}

This program can force Your robot to follow a line quite well but one thing is still missing: calibrated sensors. Different reflectance sensor produce different values. The values also depend on types of black and white surface below. Therefore, a calibration is needed. If in a hurry, You can change this program to invoke reflectance library's calibration function. A proper way would be to go to the next lesson, learn more about commands, and use one to do the actual calibration.

Previous lesson
Next lesson