Go Down

Topic: Strange question relating to personal code (DC Motors) (Read 2051 times) previous topic - next topic

ed001987

May 24, 2016, 08:15 pm Last Edit: May 24, 2016, 09:10 pm by ed001987 Reason: More information
I have a project I'm working on.  I am taking an existing RC car, and going to drive it with an Arduino + Motor Shield.  I'm not particularly savvy wiring and such, however, I do know the code.

The following is a function I have created to attempt a turn depending on the boolean value sent into it.
Code: [Select]
void turn (boolean dir)
{
  digitalWrite(TURNBRAKE, LOW);  // allow movement
  analogWrite(DIRSPEED,96);
  digitalWrite(TURNDIR, dir);
  delay(1500);
  Serial.print("dir = ");             // for debugging purposes
  Serial.println(dir);                 // for debugging purposes
  digitalWrite(TURNBRAKE,HIGH);
  delay(1000);
  digitalWrite(TURNBRAKE,LOW);
  digitalWrite(TURNDIR, !dir); // correction
  Serial.print("!dir = ");           // for debugging purposes
  Serial.println(!dir);               // for debugging purposes
  delay (2000); // Straighten
  digitalWrite(TURNBRAKE, HIGH); // stop
}


I then call that function with two decisions: A left turn (which is passes true) and a right turn (which passes false).

My confusion is if I call left turn, when it is supposed to correct to the right side, it proceeds to the left.  When rightTurn() is called, it proceeds as desired.

When debugging, the values are good, (left: dir = 1, and !dir = 0, and vice versa)


EDIT:  when observing the Motor shield, I've noticed that the signal LED doesn't turn off when leftTurn is inverted.  (the !dir when True is passed)

PaulS

Quote
delay (2000); // Straighten
That is not what that function does!
The art of getting good answers lies in asking good questions.

ed001987

Right, I understand that... the delay is there so it holds (in this case for 2 seconds) so the car can have some time to straighten out.  That comment was there for my sake/sanity.

ed001987

Code: [Select]
void turn (boolean dir)
{
  ...
  digitalWrite(TURNDIR, !dir); // correction
  Serial.print("!dir = ");           // for debugging purposes
  Serial.println(!dir);               // for debugging purposes
  delay (2000); // Straighten
  digitalWrite(TURNBRAKE, HIGH); // stop
}


This might be better for context sake?

PaulS

Quote
That comment was there for my sake/sanity.
So, why not make it accurate? Place it BEFORE the code, to explain why the code is there.
The art of getting good answers lies in asking good questions.

ed001987

Thank you for taking the time to look at my code.  To answer the question, I wasn't expecting to have anyone look at my code.  However, I asked this question because I am legitimately at a loss why this is happening. 

Though I may have an idea.  Because the Motor is running, do you think there would be a bit of back charge, or would the Shield prevent that?  (I had this idea while trying to sleep)

TomGeorge

Hi,
Quote
A left turn (which is passes true) and a right turn (which passes false).
What do you send to go straight ahead?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a copy of your  COMPLETE sketch?

Thanks.. Tom.. :)

Everything runs on smoke, let the smoke out, it stops running....

ed001987

I don't have the circuit schematic.  I'm more of a software engineer than an electrical engineer. 

But I'm happy to share my code here.

Code: [Select]

/*******************************************
* Defining Constants
*
********************************************/
#define ACCELBRAKE 8
#define TURNBRAKE  9
#define ACCELDIR   13
#define TURNDIR    12
#define SPEED      11
#define DIRSPEED   3

#include <NewPing.h>

// NewPing sonar(1/*trigger*/,2/*echo*/,200);

void setup()
{
  Serial.begin(9600);
  unsigned int uS = 0;

/*******************************
*  Wait until object is removed
*
********************************/
/*  while(uS < 100)
  {
    Serial.print(uS);
    Serial.println( "cm" );
    delay(1000);
    uS = sonar.ping_cm();
  }
  Serial.println("Out!");*/
  digitalWrite(ACCELBRAKE, LOW);
}


/*****************************************************************
* if dir = true, turn left
* else dir = false, turn right
*
******************************************************************/

void turn (boolean dir)
{
  digitalWrite(TURNBRAKE, LOW);  // allow movement
  analogWrite(DIRSPEED,255);
  digitalWrite(TURNDIR, dir); // actual turn (TODO: TEST MORE)
  delay(1500);
  Serial.print("dir = ");
  Serial.println(dir);
  digitalWrite(TURNBRAKE,HIGH);
  delay(1000);
  digitalWrite(TURNBRAKE,LOW);
  digitalWrite(TURNDIR, !dir); // correction
  Serial.print("!dir = ");
  Serial.println(!dir);
  delay (2000); // Straighten
  digitalWrite(TURNBRAKE, HIGH); // stop
}

void leftTurn()
{
  turn(true);
}

void rightTurn()
{
  turn(false);
}


/******************************************************************
*  corrections - adjusting course to avoid obsticals
*  if dir = true, correct to the left
*  else dir = false, correct to the right
*
* Example Left correction
* Process:  Turn Left
*       go little
*       Turn Right
*       Straight
*       Turn Right
*             go little
*       Turn Left

*******************************************************************/

void correction (boolean dir)
{
  digitalWrite(TURNBRAKE, LOW);
  digitalWrite(TURNDIR, dir); // adjust for correction
  analogWrite(DIRSPEED,200);
  delay(150);
  digitalWrite(TURNDIR, !dir); // straighten
  delay(150);
 
  analogWrite(DIRSPEED,0);

  digitalWrite(TURNBRAKE,HIGH);
}

void leftCorrection()
{
  correction(true);
}

void rightCorrection()
{
  correction(false);
}

/*****************************************************
* if dir = true; go forward
* else dir = false; go backward
*
*  variable speed?
*
******************************************************/
void go(boolean dir, int power = 255)
{
  digitalWrite(ACCELBRAKE, LOW); // disengage brake
  digitalWrite(ACCELDIR, dir);

  analogWrite(SPEED, power);
  delay(2000);
  digitalWrite(ACCELBRAKE, HIGH); // engage brake

}


void loop()
{
  Serial.println("Left");
  leftTurn();
  delay(5000);
  Serial.println("Right");
  rightTurn();
  delay (5000);
}


Right now, there is no sensor working.  I haven't fully implemented straight, but it seems a simple fix as turning off the brake and sending the proper signal.

I like to test one thing out and figure out the why a small thing is failing rather than find out in the finished product why it's failing.

PaulS

Quote
My confusion is if I call left turn, when it is supposed to correct to the right side, it proceeds to the left.  When rightTurn() is called, it proceeds as desired.
I don't entirely understand this statement.

For a right turn, explain exactly what happens. Do NOT use terms like "correct". That does not mean anything to anyone but you who can see what is happening.

For a left turn, explain exactly what happens. Do NOT use terms like "correct". That does not mean anything to anyone but you who can see what is happening.
The art of getting good answers lies in asking good questions.

ed001987

Ok,

The idea that I was intending was when I turn a corner, I have to turn to the left.  I then will have to cause the car to straighten out by causing the motor to turn to the right. Kind of like how you drive a car.  When you make a turn, you have to turn in the opposite direction to straighten the vehicle out.

When I call my left turn function (seen above) it veers to the left, but then after the delay, it continues turning left (even though the digitalWrite signal is the opposite of what it should be).

When I call my right turn function (seen above), it veers to the right, and after the delay, it turns left (because the digitalWrite signal has changed) and it corrects the direction as intended. 

I hope that makes more sense.

PaulS

I think that it is now time to talk about the hardware. You said "an existing RC car". The 4 of them I have steer using a servo. Servos are not driven the way you are driving whatever is steering your car.

So, what IS steering your car?
The art of getting good answers lies in asking good questions.

ed001987

This is where my inexperience will shine!

Currently, just a motor and a gear box is what is steering this thing.  There were some extra components that I didn't know what they were, but I will have pictures.

(I bought 2 but disassembled one and couldn't put it back together properly.)

The existing RC car I am using is an XMOD from Radio Shack.  This is the steering portion.

The motor is a simple DC motor. 

There is an electrical doodad (which I don't know what it is)

ed001987

Please forgive the quality.  I've only got an old smart phone as a camera.

PaulS

What do you have the motor connected to? From the names, it appears that you have some kind of motor shield.

But, I can't figure out why you have 6 names for controlling the speed and direction of one motor.
The art of getting good answers lies in asking good questions.

TomGeorge

Hi;

 That looks like a pot in one of your pictures.


I would say you have, not a simple motor, it is a servo unit, that is how you know where the steering wheels are pointing.
You are controlling a servo not a simple motor.

Taking it  apart, as you have done in one of the pictures, you may have got the servo motor and pot out of sync.

Tom..... :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up