Go Down

Topic: Changing the motor speed of obstacle avoiding robot (Read 237 times) previous topic - next topic

jorpec

Hi, this is my first big project using Arduino, i plan to build a R2D2 obstacle avoiding robot, for now im using those car robot chassis from ebay just to test and when everything is ok i transfer the parts to the R2D2 chassis

I'm following a project that i saw on youtube, everything work but the speed of the motors is too high and the ultrasonic sensor doesn't have time to respond and the robot bumps in to walls and things

Here the components that i use

- Arduino board

- Sensor shield board

- L298N board

- 2 DC motors (the yellow ones from ebay)

- Servo (sc90), ultrasonic (hc-sr04)
 
- As power i use 2 packs of lithium batteries 7,4v each, one for the L298N board the other to Arduino board   

I have try to remove the jumpers from ENB and ENA from L299N board and connect to two pwm outputs and using the analogWrite to change the motor speed. but didnt work very well, it has some weird behaviour like only one motor moves of the speed dont change

I dont know if this is the best way to change the motors speed, or if i'm doing something wrong


Thanks in advance

Here the original code

Code: [Select]
#include <Servo.h>          //Servo motor library. This is standard library
#include <NewPing.h>        //Ultrasonic sensor function library. You must install this library

//our L298N control pins
const int LeftMotorForward = 7;
const int LeftMotorBackward = 6;
const int RightMotorForward = 4;
const int RightMotorBackward = 5;

//sensor pins
#define trig_pin A1 //analog input 1
#define echo_pin A2 //analog input 2

#define maximum_distance 200
boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name


void setup(){

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
 
  servo_motor.attach(10); //our servo pin

  servo_motor.write(115);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop(){

  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);

  if (distance <= 20){
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);

    if (distance >= distanceLeft){
      turnRight();
      moveStop();
    }
    else{
      turnLeft();
      moveStop();
    }
  }
  else{
    moveForward();
  }
    distance = readPing();
}

int lookRight(){ 
  servo_motor.write(50);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
}

int lookLeft(){
  servo_motor.write(170);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
  delay(100);
}

int readPing(){
  delay(70);
  int cm = sonar.ping_cm();
  if (cm==0){
    cm=250;
  }
  return cm;
}

void moveStop(){
 
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
}

void moveForward(){

  if(!goesForward){

    goesForward=true;
   
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
 
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW);
  }
}

void moveBackward(){

  goesForward=false;

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
 
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
 
}

void turnRight(){

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
 
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorForward, LOW);
 
  delay(500);
 
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
 
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
 
 
 
}

void turnLeft(){

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
 
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  delay(500);
 
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
 
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);


And here the changes that i did

Code: [Select]
#include <Servo.h>          //Servo motor library. This is standard library
#include <NewPing.h>        //Ultrasonic sensor function library. You must install this library

//our L298N control pins
const int LeftMotorForward = 2;
const int LeftMotorBackward = 3;
const int RightMotorForward = 5;
const int RightMotorBackward = 4;

const int enA = 1;
const int enB = 6;

//sensor pins
#define trig_pin A1 //analog input 1
#define echo_pin A2 //analog input 2

#define maximum_distance 200
boolean goesForward = false;
int distance = 100;



NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name


void setup(){

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);



  servo_motor.attach(10); //our servo pin

  servo_motor.write(115);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop(){
 


  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);

  if (distance <= 20){
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);

    if (distance >= distanceLeft){
      turnRight();
      moveStop();
    }
    else{
      turnLeft();
      moveStop();
    }
  }
  else{
    moveForward();
  }
    distance = readPing();
}

int lookRight(){ 
  servo_motor.write(50);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
}

int lookLeft(){
  servo_motor.write(170);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
  delay(100);
}

int readPing(){
  delay(70);
  int cm = sonar.ping_cm();
  if (cm==0){
    cm=250;
  }
  return cm;
}

void moveStop(){
 
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorBackward, LOW);

}

void moveForward(){

  if(!goesForward){

    goesForward=true;
   
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
 
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW);
   
    analogWrite(enA, 150);
    analogWrite(enB, 150);
  }
}

void moveBackward(){

  goesForward=false;

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
 
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);

  analogWrite(enA, 150);
  analogWrite(enB, 150);
 
}

void turnRight(){

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
 
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorForward, LOW);

  analogWrite(enA, 150);
  analogWrite(enB, 150);
 
  delay(500);
 
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
 
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
 
  analogWrite(enA, 150);
  analogWrite(enB, 150);
 
 
 
}

void turnLeft(){

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
 
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  analogWrite(enA, 150);
  analogWrite(enB, 150);
 
  delay(500);
 
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
 
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  analogWrite(enA, 150);
  analogWrite(enB, 150);
}

Cactusface

Hi,
     How are you powering this and what vatteries are you using, low power caused by using the wrong batteries can cause will kinds of weird things... are all commons tied together. Tell me your not using those useless 9v square batteries to power motors. We need to see your schematic, etc.

Regards

Mel.

PS. See my bots here www.melsaunders.co.uk under Electronics...
Open your mind! But not too far, your brains might fall out.
Also like Photography, model building and my 300+ Cacti and Succs.

jorpec

Hi, thanks for the fast reply

I'm not using those 9v square bateries, i use two pair of two lithium batteries (18650 used in laptops) connected in series


here the youtube project that i use

https://www.youtube.com/watch?v=tXsP9STxdBc

Cactusface

Hi,
    Yes lith batteries are good, use them myself. The L298 and 283 are poor motor drivers, you lose 2v+ in the output stage, so I now tend to use the Pololu FET DRV8835/33 based modules (https://www.pololu.com/product/2130) .  You don't have to use the EN inputs for PWM, just tie them high for ENABLE and use PWM on the driver pins. Ie left motor pin 7=HIGH, pin 6=PWM value may be forward, and reverse the pins for reverse, or visa-versa!!

I can never see why you want to use a LIB for ping the code is so simple! Please put links in the right tabs so we can just click it, and not have to cut and paste.

I can't quite follow your code, so many directions and distance reading..

Regards

Mel.

Open your mind! But not too far, your brains might fall out.
Also like Photography, model building and my 300+ Cacti and Succs.

jorpec

Thanks again, sorry about the link (some other forums do it automatically), i fixed


I will try your suggestion, then i will report if i was able to change the speed

jorpec

I did the modifications but it had the same result, by testing i got some strange behaviour on the motor 

while the left motor the speed varies, on the right motor looks like the motor is or on or off

i guess i'm doing something wrong

here the changes that i did, i only changed the code on the move forward to test

Code: [Select]


test 1

    analogWrite(LeftMotorForward, 120);  <- the motor moves more slowly
    analogWrite(RightMotorForward, 120); <- the motor is stopped
 
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW);


test 2


    analogWrite(LeftMotorForward, 150);  <- the motor moves fast almost like when is on high
    analogWrite(RightMotorForward, 150); <- the motor moves fast almost like when is on high
 
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW);


jorpec

Hi

Finally i was able to solve it, how ever i dont know what was wrong, i guess my arduino board is kinda faulty cos not all digital pwm work with analogWrite, i had to test each one. Now i can move to the next problem :D

Edit: only now i realize some arduino boards the analogwrite on works on certain pins

Thanks for your help


So here the code, in case anyone need it

Code: [Select]
#include <Servo.h>          //Servo motor library. This is standard library
#include <NewPing.h>        //Ultrasonic sensor function library. You must install this library

//our L298N control pins
const int LeftMotorForward = 3;
const int LeftMotorBackward = 5;
const int RightMotorForward = 6;
const int RightMotorBackward = 11;


//sensor pins
#define trig_pin A0 //analog input 1
#define echo_pin A1 //analog input 2

#define maximum_distance 200
boolean goesForward = false;
int distance = 100;



NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name


void setup(){

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);




  servo_motor.attach(10); //our servo pin

  servo_motor.write(115);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop(){
  


  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);

  if (distance <= 20){
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);

    if (distance >= distanceLeft){
      turnRight();
      moveStop();
    }
    else{
      turnLeft();
      moveStop();
    }
  }
  else{
    moveForward();
  }
    distance = readPing();
}

int lookRight(){  
  servo_motor.write(50);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
}

int lookLeft(){
  servo_motor.write(170);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
  delay(100);
}

int readPing(){
  delay(70);
  int cm = sonar.ping_cm();
  if (cm==0){
    cm=250;
  }
  return cm;
}

void moveStop(){
  
  analogWrite(RightMotorForward, 0);
  analogWrite(LeftMotorForward, 0);
  analogWrite(RightMotorBackward, 0);
  analogWrite(LeftMotorBackward, 0);

}

void moveForward(){

  if(!goesForward){

    goesForward=true;
    
    analogWrite(LeftMotorForward, 90);
    analogWrite(RightMotorForward, 95);
  
    analogWrite(LeftMotorBackward, 0);
    analogWrite(RightMotorBackward, 0);
    

  }
}

void moveBackward(){

  
    goesForward=false;

  analogWrite(LeftMotorBackward, 90);
  analogWrite(RightMotorBackward, 95);
  
  analogWrite(LeftMotorForward, 0);
  analogWrite(RightMotorForward, 0);


}

void turnRight(){

  analogWrite(LeftMotorForward, 90);
  analogWrite(RightMotorBackward, 95);
  
  analogWrite(LeftMotorBackward, 0);
  analogWrite(RightMotorForward, 0);

  
  delay(450);
  
  analogWrite(LeftMotorForward, 90);
  analogWrite(RightMotorForward, 95);
  
  analogWrite(LeftMotorBackward, 0);
  analogWrite(RightMotorBackward, 0);
  

 
  
  
}

void turnLeft(){

  analogWrite(LeftMotorBackward, 90);
  analogWrite(RightMotorForward, 95);
  
  analogWrite(LeftMotorForward, 0);
  analogWrite(RightMotorBackward, 0);


  
  delay(450);
  
  analogWrite(LeftMotorForward, 90);
  analogWrite(RightMotorForward, 95);
  
  analogWrite(LeftMotorBackward, 0);
  analogWrite(RightMotorBackward, 0);


}

Go Up