This topic has 2 replies, 2 voices, and was last updated 4 years, 3 months ago by SuperDroid.

  • Author
    Posts
  • #3361
     jamal
    Participant

    I have this issue I don’t know what and where is it exactly , cause I have connected my two servos along with the two sonar to read and send command to the robot motors to move , but problem occurs when I connect them all in the same time the servo start to hang in some angle and when I disconnected the motors or slow down their speed form the code the servos stars to work perfectly , what the issue could be ? is it the battery cause I use 12V battery 4500mAhr, even thought I connect the servo to the voltage regulators and motors to the source directly , for the record I used this code , could it be from the code maybe ? please help

    thanks

    #include <Servo.h>
    
    #define pwmFR 4
    #define dirFR 5
    #define pwmFL 2
    #define dirFL 3
    #define pwmRR 6
    #define dirRR 7
    #define pwmRL 8
    #define dirRL 9
    
    typedef struct {int pulse;} MotorValues;
    
    MotorValues motorFL;	
    MotorValues motorFR;
    MotorValues motorRL;	
    MotorValues motorRR;
    
    int trigger=10;
    int echo=11;
    
    int leftscanval, centerscanval, rightscanval, ldiagonalscanval, rdiagonalscanval;
    char choice;
    
      
    
    const int distancelimit = 30; //Distance limit for obstacles in front           
    const int sidedistancelimit = 12; //Minimum distance in cm to obstacles at both sides (the robot will allow a shorter distance sideways)
    
    int distance;
    int numcycles = 0;
    char turndirection; //Gets 'l', 'r' or 'f' depending on which direction is obstacle free
    const int turntime = 2000; //Time the robot spends turning (miliseconds)
    int thereis;
    Servo head;
    
    void setup(){
      Serial.begin(9600);    // Initialize Serial
      head.attach(13);
      head.write(80);
      pinMode(trigger,OUTPUT);
      pinMode(echo,INPUT);
      pinMode(dirFL, OUTPUT);
      pinMode(dirFR, OUTPUT);
      pinMode(dirRL, OUTPUT);
      pinMode(dirRR, OUTPUT);
      pinMode(pwmFL, OUTPUT);
      pinMode(pwmFR, OUTPUT);
      pinMode(pwmRL, OUTPUT);
      pinMode(pwmRR, OUTPUT);
      digitalWrite(trigger,LOW);
      
      digitalWrite(dirFL, LOW);
      digitalWrite(dirFR, LOW);
      digitalWrite(dirRL, LOW);
      digitalWrite(dirRR, LOW);
      digitalWrite(pwmFL, LOW);
      digitalWrite(pwmFR, LOW);
      digitalWrite(pwmRL, LOW);
      digitalWrite(pwmRR, LOW);
      
      int i = 0 ; 
      
    }
    
    void loop(){
    //  power=1;
    ////  if(power==1){
    //     echo = analogRead(11);
    //     trigger =analogRead(10);
    //     
       long howfar;
      digitalWrite(trigger,LOW);
      delayMicroseconds(5);                                                                              
      digitalWrite(trigger,HIGH);
      delayMicroseconds(15);
      digitalWrite(trigger,LOW);
      howfar=pulseIn(echo,HIGH);
      howfar=howfar*0.01657; //how far away is the object in cm
      Serial.print(" How far is ");
      Serial.println(howfar);
    
      int power =1;
    
        if ( power ==1 ) {
        DriveForward();
    
       // if nothing is wrong go forward using go() function above.
    
        if (howfar > distancelimit){
        head.write(80); //I used 80 degrees because its the central angle of my 160 degrees span (use 90 degrees if you are moving your servo through the whole 180 degrees)
         }
          else {
            ++numcycles;
          }
          
        while(numcycles>130){ //Watch if something is around every 130 loops while moving forward 
        watchsurrounding();
        if(leftscanval<sidedistancelimit || ldiagonalscanval<distancelimit){
          SpinClockwise();
        }
        if(rightscanval<sidedistancelimit || rdiagonalscanval<distancelimit){
          SpinCounterClockwise();
        }
        numcycles=0; //Restart count of cycles
      }
      distance = watch(); // use the watch() function to see if anything is ahead (when the robot is just moving forward and not looking around it will test the distance in front)
      head.write(80);
      if (distance<distancelimit){ // The robot will just stop if it is completely sure there's an obstacle ahead (must test 25 times) (needed to ignore ultrasonic sensor's false signals)
          ++thereis;}
      if (distance>distancelimit){
      head.write(80);
          thereis=0;
        } //Count is restarted
      if (thereis > 25){
        turndirection = decide(); //Decide which direction to turn.
        switch (turndirection){
          case 'l':
            SlideLeft(turntime);
            break;
          case 'r':
            SlideRight(turntime);
            break;
          case 'f':
    //        ; //Do not turn if there was actually nothing ahead
            break;
        }
        thereis=0;
      }
    
     }
    }
    void SpinClockwise()//Spins Clockwise
    {
      digitalWrite(dirFR, LOW); digitalWrite(dirFL, LOW); digitalWrite(dirRR, LOW); digitalWrite(dirRL, LOW);//Reverses the direction of counterclockwise
      motorFR.pulse = 65 ; 
      motorFL.pulse = 65 ; 
      motorRR.pulse = 65 ;
      motorRL.pulse = 65 ;
      commandMotors();
    //  delay(t);
    }
    
      void commandMotors() { // This sends the speed and direction to the motors
      analogWrite(pwmFR, motorFR.pulse);
      analogWrite(pwmRR, motorRR.pulse);
      analogWrite(pwmFL, motorFL.pulse);
      analogWrite(pwmRL, motorRL.pulse);
    }
    
    void allStop()// This sets all motors to 0 to stop their movement
    {  
      motorFR.pulse = 0;
      motorFL.pulse = 0;
      motorRR.pulse = 0;
      motorRL.pulse = 0;
      commandMotors();
    }
    
    void SpinCounterClockwise()//spins in the counterclockwise direction 
    {
      digitalWrite(dirFR, HIGH); digitalWrite(dirFL, HIGH); digitalWrite(dirRR, HIGH); digitalWrite(dirRL, HIGH);//sets Right to forward and left to backward
      motorFR.pulse = 65 ; 
      motorFL.pulse = 65 ; 
      motorRR.pulse = 65 ;
      motorRL.pulse = 65 ;
        commandMotors();
    //delay (t);
    }
     void DriveBackward()
    {
      digitalWrite(dirFR, HIGH);
       motorFR.pulse = 65 ;  
      digitalWrite(dirFL, LOW);
       motorFL.pulse = 65 ;  
      digitalWrite(dirRR, HIGH);
       motorRR.pulse = 65 ;  
      digitalWrite(dirRL, LOW);
       motorRL.pulse = 65 ; 
       commandMotors();
    }
    
    void DriveForward()
    {
      digitalWrite(dirFR, LOW);
      motorFR.pulse = 20;
      digitalWrite(dirFL, HIGH);
     motorFL.pulse = 20 ; 
      digitalWrite(dirRR, LOW);
     motorRR.pulse = 20 ; 
      digitalWrite(dirRL, HIGH);
     motorRL.pulse = 20 ;
    commandMotors(); 
    
    }
    
    void SlideRight(int t )
    {
      digitalWrite(dirFR, LOW);
      motorFR.pulse = 65 ; 
      digitalWrite(dirFL, HIGH);
      motorFL.pulse = 65 ; 
      digitalWrite(dirRR, HIGH); 
      motorRR.pulse = 65 ;
      digitalWrite(dirRL, LOW);
      motorRL.pulse = 65 ;
       commandMotors();     
    delay ( t) ;  
    }
    
    void SlideLeft(int t )
    {
      digitalWrite(dirFR, HIGH); 
        motorFR.pulse = 65 ; 
      digitalWrite(dirFL, LOW); 
       motorFL.pulse = 65 ; 
      digitalWrite(dirRR, LOW);
      motorRR.pulse = 65 ;
      digitalWrite(dirRL, HIGH); 
        motorRL.pulse = 65 ;
    commandMotors();
    delay (t);
    }
    
    int watch(){
      long howfar;
      digitalWrite(trigger,LOW);
      delayMicroseconds(5);                                                                              
      digitalWrite(trigger,HIGH);
      delayMicroseconds(15);
      digitalWrite(trigger,LOW);
      howfar=pulseIn(echo,HIGH);
      howfar=howfar*0.01657; //how far away is the object in cm
      return round(howfar);
      Serial.print(" How far is ");
      Serial.println(howfar);
    
    }
    
    void watchsurrounding(){ //Meassures distances to the right, left, front, left diagonal, right diagonal and asign them in cm to the variables rightscanval, 
                             //leftscanval, centerscanval, ldiagonalscanval and rdiagonalscanval (there are 5 points for distance testing)
      centerscanval = watch();
      if(centerscanval<distancelimit){allStop();}
      head.write(120);
      delay(100);
      ldiagonalscanval = watch();
      if(ldiagonalscanval<distancelimit){allStop();}
      head.write(160); //Didn't use 180 degrees because my servo is not able to take this angle
      delay(300);
      leftscanval = watch();
      if(leftscanval<sidedistancelimit){allStop();}
      head.write(120);
      delay(100);
      ldiagonalscanval = watch();
      if(ldiagonalscanval<distancelimit){allStop();}
      head.write(80); //I used 80 degrees because its the central angle of my 160 degrees span (use 90 degrees if you are moving your servo through the whole 180 degrees)
      delay(100);
      centerscanval = watch();
      if(centerscanval<distancelimit){allStop();}
      head.write(40);
      delay(100);
      rdiagonalscanval = watch();
      if(rdiagonalscanval<distancelimit){allStop();}
      head.write(0);
      delay(100);
      rightscanval = watch();
      if(rightscanval<sidedistancelimit){allStop();}
    
      head.write(80); //Finish looking around (look forward again)
      delay(300);
    }
    
    char decide(){
      watchsurrounding();
      if (leftscanval>rightscanval && leftscanval>centerscanval){
        choice = 'l';
      }
      else if (rightscanval>leftscanval && rightscanval>centerscanval){
        choice = 'r';
      }
      else{
        choice = 'f';
      }
      return choice;
    }
    
    #3362
     SuperDroid
    Keymaster

    Servos are completely different than PWM motor controllers. For support with servos see this page http://www.superdroidrobots.com/shop/custom.aspx/remote-control-rc-support/40/ For support with motors controllers see this page http://www.superdroidrobots.com/shop/custom.aspx/motor-controllers/60/

    All you have done is copied the source code we provide on the GIT hub for our autonomous robot. We can not debug why its not working on your system. It was written for a very specific system. You will have to adjust the code to adapt to your specific configuration. Beyond that we can provide engineering support by filling out this form. http://www.superdroidrobots.com/shop/custom.aspx/engineering-request/89/ The only way we can properly program and debug the system is to have the exact same hardware setup as you. So either we will need the robot or we will need to reproduce the hardware with the same configuration as yours.

    #3363
     SuperDroid
    Keymaster

    Servos are completely different than PWM motor controllers. For support with servos see this page http://www.superdroidrobots.com/shop/custom.aspx/remote-control-rc-support/40/ For support with motors controllers see this page http://www.superdroidrobots.com/shop/custom.aspx/motor-controllers/60/

    All you have done is copied the source code we provide on the GIT hub for our autonomous robot. We can not debug why its not working on your system. It was written for a very specific system. You will have to adjust the code to adapt to your specific configuration. Beyond that we can provide engineering support by filling out this form. http://www.superdroidrobots.com/shop/custom.aspx/engineering-request/89/ The only way we can properly program and debug the system is to have the exact same hardware setup as you. So either we will need the robot or we will need to reproduce the hardware with the same configuration as yours.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

Login Register

©2020 SDRobots.com | All rights reserved.

Log in with your credentials

or    

Forgot your details?

Create Account