Go Down

Topic: *VIDEO* Program react differently regards of POWER source - USB/BATTERY (Read 1 time) previous topic - next topic

TruePatrick

That's the seccond time i visit this forum. Now i am smarter then before. This is my first arduino project - selfriding robot that avoides obstacles.
SETUP:
-4WD platform with 4 DC motors.
-Romeo V2.2 board.
-Servo SG90
-distance sensor - ultrasonic HC-SR04
I downloaded the code from similiar project and modified this on my purpose, especialy for my sensor.
This is quite strange, when i connect my romeo board via USB and upload same code with 5V, everything starts quickly and another time when i connect board also via USB ---- robot starts moving slowly back (servo doesnt work) and stops. The White diode on the board with "L" name lights. And the same thing is when i connect 8 x 1,5V AA battery which are connected in series.
Here's the photo of my robot:
http://imgur.com/a/6Y6w4
and code below
https://github.com/TruePatrick/ROMEO-V2

:EDIT: - video here how's it looking - https://www.youtube.com/watch?v=SoNNOrAiGtc   -  check out my last answer, i have almost fixed this.

johnwasser

I don't see anything connected to "ServoPWR".  

The Romeo board uses a 32U4 processor like the Leonardo.  You should put in the:
Code: [Select]
     Serial.begin(9600);
     while (!Serial) {}

That will wait for the USB connection to complete.  It may be that your sketch can't use Serial when there is no USB connection.  In that case you may need to put  "if(Serial)" before any Serial calls like Serial.read() or Serial.print().  You might also need to use:
Code: [Select]
     Serial.begin(9600);
     while (!Serial && millis() < 5000) {}

 That way the waiting for a USB connection will time out after 5 seconds.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

TruePatrick

Let's see:
Code: [Select]
Serial.begin(9600);
     while (!Serial) {}


Code: [Select]

Arduino:1.8.3 (Windows 10), Płytka:"Arduino Leonardo"

RoverCommander:24: error: no match for call to '(HardwareSerial) ()'

  while(!Serial1()){

                 ^

exit status 1
no match for call to '(HardwareSerial) ()'

Ten raport powinien zawierać więcej informacji jeśli w
File -> Preferencje zostanie włączona opcja "Pokaż
szczegółowe informacje podczas kompilacji"


TruePatrick

Here's the thing. When i upload code on the board everything looks fine. Servo moving, sensor sees obstacles and robot try to avoid it. BUT right after i plug it out and THEN plug it in again via USB robot behave differently. Starts moving slowly back and servo and sensor don't move. Until i plug it again via usb and upload code from the beggining

TruePatrick

Is it becamuse once for like 0.2 sec i switched on the switch for external batery when usb cable was connected by mistake. I've heard that you mustn't do it!

johnwasser

Let's see:
Code: [Select]
RoverCommander:24: error: no match for call to '(HardwareSerial) ()'

  while(!Serial1()){ 

Where did that line come from?!?

Whoever wrote that did not understand why the Leonardo needs: 
Code: [Select]
     Serial.begin(9600);
     while (!Serial) {}
(Even though I explained why when I recommended it.)

Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

TruePatrick

This doesn't work, even when i delete ALL this lines which are responsible for serial kontrol with pc. When i upload the code for the first time motorst starts move forward, then i normally plug it out from the pc and switch it on on external batery. Robot starts slowly move backward - seems like this line is responsible for that reaction
Code: [Select]

if(distance<=10){
      back_off (100,100);
    }


Yep it is this line about, when i change "back_off (200,200)", and repeat all the stuff that i do - motors starts going backward faster.
It is all about the sensor i think.
Here's the code that i'm working on currently.
https://github.com/TruePatrick/Here-it-is - let me know if this site is working.

johnwasser

Code: [Select]
  distance = (duration/2) / 29.1;


   // if distance outside 5 - 20 cm range
   if(distance>15){
     obstacle = false;
     advance (200,200);
   }
   else {
     if(distance<=10){
       back_off (200,200);
     }
     else{


A: If the sensor isn't working or no echo is detected pulseIn() returns 0 which would cause the repeated back_off().  You might get better results if you treat 0 the same as >15.

B: Your comment about 5-20 cm range doesn't match the code
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

TruePatrick

Don't look at the commends, they are from the other code.
now i have this code and the same problem - soon i'll upload the video on youtube and show you.
Code: [Select]

void Explore(){

  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

 
  distance = (duration/2) / 29.1;

 
 if(distance>12 || distance<0){
    obstacle = false;
    advance (120,120);
  }
  else{
   
    // too close, distance < 15 cm
    if(distance<=1){
      back_off (150,150);
    }
    else{
     
      obstacle = true;
     
      if(GetPosition() > GetMiddlePosition()){
        turn_R (200,200);
       
       
        SetServoDirection(0);
      }
      else{
        turn_L (200,200);
     
       
        SetServoDirection(1);
      }
  }
}
}


I'm working on this like whole day and now night. I refresh this site every 10 minutes with hope that you adwise me.
VIDEO - https://www.youtube.com/watch?v=SoNNOrAiGtc
THE code works only when i will upload it via usb. After i plug out and then again plug in, robot behave like on the beginning of the video

sterretje

@johnwasser

You should never use while(!Serial) in a 32U4 based Arduino if it needs to run from external power; it will hang forever.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

TruePatrick

Even if i delete this whole lines. The problem still appears. Here's the robot of this guy form who i have this code.
He have different sensor so i modify my code that it can fit to my sensor - https://www.youtube.com/watch?v=87TKCV07cjM&index=15&list=PL4Hx9Al-UrglylyfTldES9apzLg4fAZO7

TruePatrick

Seems like I repaired the code for exploration. Now it works everytime i plug it into usb but not on external baterry!
Code: [Select]

/*
  # This code has been inspired by an article from Dominique Meurisse : http://arduino103.blogspot.fr/2011/06/detecteur-de-proximite-infrarouge-sharp.html
  #
  # this script is distributed under Creative Commons Attribution-ShareAlike 3.0 License
*/


void Explore(){

  #define trigPin 13
  #define echoPin 12
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

 
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

 
  distance = (duration/2) / 29.1;

   Serial.print(distance);
   Serial.println(" cm");

   if(distance>15){
     obstacle = false;
     advance (200,200);
   }
   else {
     if(distance<10 && distance>0){
       back_off (200,200);
     }
    else{
      if(distance>0){
      obstacle = true;
     
      if(GetPosition() > GetMiddlePosition()){
        turn_R (200,200);
        SetServoDirection(0);
      }
      else{
        turn_L (200,200);
        SetServoDirection(1);
      }
      }
  }
}
}


I add my previous project to find out if sensor works. It measures the distance in cm!

TruePatrick


johnwasser

Maybe you should add some LEDs to light up based on the distance reading.  That way you can see if the problem is your sensor or your motor driver.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

TruePatrick

I will tomorrow. I need a break from all this. The thing is, i've discover that when i will plug board via usb IT ALSO gather the power from the output baterry even when switch is off. I measured this by multimetr. That's strange. I tried adding more baterry, i had 18,5V but still was the same. It's not about voltage. I tried to charge this vai USB from POWERBANK 5V. Worked fine ... but i had to have connected external battery to work, even if it was on OFF switch.

Go Up