Go Down

Topic: motor rotates backwards after intended movement (Read 219 times) previous topic - next topic

rowarcc

Nov 19, 2019, 01:41 am Last Edit: Nov 19, 2019, 01:44 am by rowarcc
I'm using arduino connected to a sabertooth 2x12 RC driver thats connected to 2 DC motors, each motor are connected to a lithium battery.
I control the motors using servo library and each time it completes a sweeping loop that I ran, the motor rotates to a opposite direction for some reason that I cannot figure out, also another problem that I have is when I try to only turn the left motor(channel 1), the right motor(channel 2) also moves at a very slow speed, sometimes it doesn't. I've played around with the DIP switch on the RC motor driver but it didn't solve the problem
any help is appreciated
heres the code part with everything I've tried

#include <Servo.h>

Servo ST1, ST2;
int pos = 0;

void setup(){
  ST1.attach(5, 1000, 2000);
  ST2.attach(6, 1000, 2000);
}

void loop(){
  //turnLeft();
  //delay(100);
  turnRight();
  //delay(100);
  //forward();
  //delay(100);
  //backward();
  //delay(100);
  //doNothing();
  //forward and backward
  /*
  for (pos = 0; pos < 180; pos += 1){
    ST1.write(pos);
    ST2.write(pos);
    delay(15);
  }
  delay(1000);
 
  for (pos = 180; pos > 1; pos -= 1){
    ST1.write(pos);
    ST2.write(pos);
    delay(15);
  }
  delay(1000);
  */
  /*
  //left and right
  for (pos = 0; pos < 180; pos += 1){
    ST1.write(pos);
    delay(15);
  }
  delay(1000);
  /*
  for (pos = 0; pos < 180; pos += 1){
    ST2.write(pos);
    delay(15);
  }
  delay(1000);
  */
}

void doNothing(){
  while(1){
    ST1.detach();
    ST2.detach();
  }
}

void turnLeft(){
  ST1.attach(5, 1000, 2000);
  for (pos = 0; pos < 180; pos += 1){
    ST1.write(pos);
    delay(20);
  }
  delay(1000);
  ST1.detach();
}

void turnRight(){
  ST2.attach(6, 1000, 2000);
  for (pos = 0; pos < 180; pos += 1){
    ST2.write(pos);
    delay(20);
  }
  delay(1000);
  ST2.detach();
}

void forward(){
  ST1.attach(5, 1000, 2000);
  ST2.attach(6, 1000, 2000);
  for (pos = 0; pos < 180; pos += 1){
    ST1.write(pos);
    ST2.write(pos);
    delay(20);
  }
  delay(1000);
  ST1.detach();
  ST2.detach();
}

void backward(){
  ST1.attach(5, 1000, 2000);
  ST2.attach(6, 1000, 2000);
  for (pos = 180; pos > 1; pos -= 1){
    ST1.write(pos);
    ST2.write(pos);
    delay(10);
  }
  delay(1000);
  ST1.detach();
  ST2.detach();
}

sayHovis


slipstick

Get rid of all those attach() and detach() commands in the functions and try it again. You need the attach() in setup() and you never need to do a detach() in normal use.

When you do a detach() it stops sending control signals to the driver so it no longer knows what it should be doing. Then when you then do another attach() again the signal is reset to a default before you send the signal you actually wanted.

Steve

Robin2

I don't see a stop() function in the program. Detaching the ESCs from the servo library is not the same as STOP.

There should be no need ever to detach the ESCs.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

rowarcc

I used detach and attach when I try to have the motor do turn left then right then forward backward etc.. that didn't rly work(the left and right wheels went simutineously even when I have delay in between). but when I only ran the loops like below, the motor performs the command and moves backward

 for (pos = 180; pos > 1; pos -= 1){
    ST1.write(pos);
    ST2.write(pos);
    delay(15);
  }
  delay(1000);

slipstick

So does that mean that everything's working now without all the attach()/detach() commands?

Steve

rowarcc

no, it means that if I ran just the for loops it would still rotate backwards a little bitafter executing the command with or without the detach, the attach and detach is just something else I tried

Robin2

no, it means that if I ran just the for loops it would still rotate backwards a little bitafter executing the command with or without the detach, the attach and detach is just something else I tried
Have you tried creating, and using, a proper stop() function?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up