Go Down

Topic: Motor won't run backwards (Read 468 times) previous topic - next topic

Anton_H

Feb 24, 2018, 04:12 pm Last Edit: Feb 24, 2018, 04:23 pm by Anton_H
Hi everyone,

I'm making a line-follower robot, and my right motor won't run backwards in my code (in a test code I made before assembling the robot, it works fine). I'm using a Pololu TB6612FNG motor driver, a Pololu QTR-8A line-sensor, and a Teensy3.6 micro controller.
The left motor works fine and runs backwards.

Here is the test code (that works):
Code: [Select]
void setup() {
  // put your setup code here, to run once:
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(2, LOW);    //this is one control for the left motor: LOW = backwards, HIGH = forwards
  digitalWrite(3, HIGH);   //this is one control for the left motor: LOW = forwards, HIGH = backwards
  digitalWrite(4, HIGH);   //Set low to set motor driver in standby mode
  digitalWrite(5, LOW);   //this is one control for the right motor: LOW = backwards, HIGH = forwards
  digitalWrite(6, HIGH);  //this is one control for the right motor: LOW = forwards, HIGH = backwards
  analogWrite(35, 65);  // set motor speed for left motor
  analogWrite(36, 65);  // set motor speed for right motor
  delay(1000);
}


And this is the code for my line-follower (This is the part of code where the problem should be. The whole code is in attachement (it's in different tabs)):

Code: [Select]
#define motorSTBY 4
#define lMotorControl1 2
#define lMotorControl2 3
#define rMotorControl1 5
#define rMotorControl2 6
#define lMotor 35
#define rMotor 36

void setup() {
pinMode(lMotorControl1, OUTPUT);
  pinMode(lMotorControl2, OUTPUT);
  pinMode(motorSTBY, OUTPUT);
  pinMode(rMotorControl1, OUTPUT);
  pinMode(lMotorControl2, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  Display();
  TCS34725_1();
  TCS34725_2();
  Calibration();
 
  baseSpeed = ((MaxMspeed - MinMspeed) / 2) + MinMspeed;
  for (i=0; i<=7; i++) {
    if (analogRead(lineSensor[i]) >= calibrationValue[i]) {
      sensorValue[i] = 1000;
    } else {
      sensorValue[i] = 0;
    }
  }
// calculating the position of the line (a number between 0 and 70, where 35 is the middle):
  pos = (0 * sensorValue[0] + 10 * sensorValue[1] + 20 * sensorValue[2] + 30 * sensorValue[3] + 40 * sensorValue[4] + 50 * sensorValue[5] + 60 * sensorValue[6] + 70 * sensorValue[7]) / (sensorValue[0] + sensorValue[1] + sensorValue[2] + sensorValue[3] + sensorValue[4] + sensorValue[5] + sensorValue[6] + sensorValue[7]);
  
// keep outputting 70 when the robot lose the line
  if (pos == 0 && error > 0) {
    pos = 70;
  }
  error = pos - 35;
  correction = Kp * error + Kd * (error - lastError);

  MspeedL = baseSpeed - correction;
  MspeedR = baseSpeed + correction;

  if (MspeedL > MaxMspeed) {
    MspeedL = MaxMspeed;
  }
  if (MspeedL < MinMspeed) {
    MspeedL -= 2 * MinMspeed;
    digitalWrite(lMotorControl1, LOW);
    digitalWrite(lMotorControl2, HIGH);
  } else {
    digitalWrite(lMotorControl1, HIGH);
    digitalWrite(lMotorControl2, LOW);
  }
  if (MspeedR > MaxMspeed) {
    MspeedR = MaxMspeed;
  }
  if (MspeedR < MinMspeed) {
    MspeedR -= 2 * MinMspeed;
    Serial.println(MspeedR);
    Serial.println(abs(MspeedR));
    digitalWrite(rMotorControl1, LOW);
    digitalWrite(rMotorControl2, HIGH);
  } else {
    digitalWrite(rMotorControl1, HIGH);
    digitalWrite(rMotorControl2, LOW);
  }

  analogWrite(lMotor, abs(MspeedL));
  analogWrite(rMotor, abs(MspeedR));
  
  if (b_start_stop == 1) {
    digitalWrite(motorSTBY, HIGH);
//    Serial.println("Motors running");
  } else {
    digitalWrite(motorSTBY, LOW);
//    Serial.println("Motors off");
  }
}


Any help would be appreciated.
Thanks in advance.

jremington

Double check the correctness of the wiring, and all connections for continuity. Swap the two motors.

Anton_H

#2
Feb 24, 2018, 04:41 pm Last Edit: Feb 24, 2018, 06:48 pm by Anton_H
swapped the motors and same result: the (now left) motor won't run backwards

jremington

In your original post, you stated
Quote
in a test code I made before assembling the robot, it works fine
This might lead an intelligent person to suspect that robot assembly could be a problem.

Good luck with your project!

Anton_H

#4
Feb 24, 2018, 06:50 pm Last Edit: Feb 24, 2018, 06:56 pm by Anton_H
I think you didn't understood me well.
The test code that I made before assembling my robot, works also now when my robot is assembled.
Maybe that an intelligent person (apparently you are) would read my post 2 times before saying something like that, because I used the present and not the past.

jremington

Quote
I think you didn't understood me well.
Evidently, English is not your native language. There are sections for other languages.

If you want informed help from this forum, it is important to communicate your problem clearly.

Anton_H

I'm sorry for you that only 5.52% from the world population (2007) a native English speaker is. But I don't think that this is the point of the conversation and this forum.
My excuses if it wasn't clear what I mean't.

Robin2

The test code that I made before assembling my robot, works also now when my robot is assembled.
Then post the test code so we can compare it with the problem code.

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

Anton_H

the test code is in my first post

slipstick

If the test code really runs correctly now then the obvious conclusion is that your real code is never getting to a point where it is trying to run the Right motor in reverse. Can you put some serial prints in to see if it ever gets to that point?

Steve

Anton_H

#10
Feb 25, 2018, 03:34 pm Last Edit: Feb 25, 2018, 04:24 pm by Anton_H
that's already in my code (see first post), and it gets there

Robin2

Having looked at the test code, I am now confused about all the other code. Is the code you included in your Original Post a complete program that illustrates the problem, or is it one piece that is intended to work with all the supplementary .ino files that you have attached?

If it is the latter then I'm afraid I am too lazy to try making sense of how all that stuff interacts to find a problem.

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

Anton_H

It's the part of the code where the problem should be, in other words everything  to do with the motors

Robin2

It's the part of the code where the problem should be, in other words everything  to do with the motors
That does not answer my question in Reply #11. And without an answer I don't know how to help you.

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

slipstick

But if your test code still works but your "real" code doesn't then it seems likely that it's never getting into the part that should run the right motor in reverse. Why not add a few serial prints to check if the code ever actually goes to where you think it should be going?

Steve

Go Up