0

Obstacle avoidance robot moving in pulses
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Obstacle avoidance robot moving in pulses

by Kidkill on Sun Apr 11, 2021 8:28 am

Hello, I've put together a robot with the motor shield v2 and an hc-sr04, it is operating as expected except while traveling forward, it's moving in pulses or lunging forward stopping then moving forward again over and over. I've tried commenting out delays all over the code but it still won't drive smoothly forward.

Any help would be appreciated.

Code: Select all | TOGGLE FULL SIZE
void loop() {
  distance = getDistance();
 
  uint8_t i;

  if(digitalRead(switchPin) == LOW){

     if(distance < 10){

      myMotor1->run(RELEASE);
      myMotor2->run(RELEASE);
      myMotor3->run(RELEASE);
      myMotor4->run(RELEASE);
      delay(100);

      myMotor1->run(BACKWARD);
      myMotor2->run(BACKWARD);
      myMotor3->run(BACKWARD);
      myMotor4->run(BACKWARD);
        for (i=0; i<255; i+=5) {
          myMotor1->setSpeed(i);
          myMotor2->setSpeed(i);
          myMotor3->setSpeed(i);
          myMotor4->setSpeed(i);
          delay(5);
        }
        for (i=255; i!=0; i-=5) {
          myMotor1->setSpeed(i);
          myMotor2->setSpeed(i);
          myMotor3->setSpeed(i);
          myMotor4->setSpeed(i);
          delay(5);
        }
      delay(backupTime);

      myMotor1->run(FORWARD);
      myMotor2->run(FORWARD);
      myMotor3->run(BACKWARD);
      myMotor4->run(BACKWARD);
        for (i=0; i<255; i+=5) {
          myMotor1->setSpeed(i);
          myMotor2->setSpeed(i);
          myMotor3->setSpeed(i);
          myMotor4->setSpeed(i);
          delay(5);
        }
        for (i=255; i!=0; i-=5) {
          myMotor1->setSpeed(i);
          myMotor2->setSpeed(i);
          myMotor3->setSpeed(i);
          myMotor4->setSpeed(i);
          delay(5);
        }
      delay(turnTime);

    }else{
          myMotor1->run(FORWARD);
          myMotor2->run(FORWARD);
          myMotor3->run(FORWARD);
          myMotor4->run(FORWARD);
        for (i=0; i<255; i+=5) {
          myMotor1->setSpeed(i);
          myMotor2->setSpeed(i);
          myMotor3->setSpeed(i);
          myMotor4->setSpeed(i);
          delay(5);
        }

       // for (i=255; i!=0; i-=5){
       //   myMotor1->setSpeed(i);
       //   myMotor2->setSpeed(i);
       //   myMotor3->setSpeed(i);
       //   myMotor4->setSpeed(i);
       //   delay(5);
       //   }
    }
    }else{

      myMotor1->run(RELEASE);
      myMotor2->run(RELEASE);
      myMotor3->run(RELEASE);
      myMotor4->run(RELEASE);
   
  }
  delay(50);
  }

Kidkill
 
Posts: 63
Joined: Mon Mar 18, 2019 7:21 pm

Re: Obstacle avoidance robot moving in pulses

by adafruit_support_bill on Sun Apr 11, 2021 10:15 am

What motors are you using and how are you powering them?

adafruit_support_bill
 
Posts: 81291
Joined: Sat Feb 07, 2009 10:11 am

Re: Obstacle avoidance robot moving in pulses

by Kidkill on Sun Apr 11, 2021 10:49 pm

I'm using 6v "tt" motors, powered with 4AA batteries. I've tried powering the motors with the battery pack, pulling the jumper and using the usb to power the arduino with the same result.

Kidkill
 
Posts: 63
Joined: Mon Mar 18, 2019 7:21 pm

Re: Obstacle avoidance robot moving in pulses

by adafruit_support_bill on Mon Apr 12, 2021 6:15 am

The issue is here in your code:

This is the block of code that gets executed whenever the distance is > 10:
Code: Select all | TOGGLE FULL SIZE
{
          myMotor1->run(FORWARD);
          myMotor2->run(FORWARD);
          myMotor3->run(FORWARD);
          myMotor4->run(FORWARD);
        for (i=0; i<255; i+=5) {
          myMotor1->setSpeed(i);
          myMotor2->setSpeed(i);
          myMotor3->setSpeed(i);
          myMotor4->setSpeed(i);
          delay(5);
        }


That will set all motors to go forward, then ramp the speed from 0 to 255. And it will repeat that on every pass through the loop.

What you probably want is something more like:

Code: Select all | TOGGLE FULL SIZE
   else
   {
      // Set motors to run forward
      myMotor1->run(FORWARD);
      myMotor2->run(FORWARD);
      myMotor3->run(FORWARD);
      myMotor4->run(FORWARD);
      
      //Accelerate to full speed
      for (i=0; i<255; i+=5)
      {
         myMotor1->setSpeed(i);
         myMotor2->setSpeed(i);
         myMotor3->setSpeed(i);
         myMotor4->setSpeed(i);
         delay(5);
      }
      // Maintain speed until next obstacle
      while (distance > 10)
      {
         distance = getDistance();
      }
   }

adafruit_support_bill
 
Posts: 81291
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.