Arduino Forum

Topics => Robotics => Topic started by: ed001987 on May 24, 2016, 08:15 pm

Title: Strange question relating to personal code (DC Motors)
Post by: ed001987 on May 24, 2016, 08:15 pm
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)
Title: Re: Strange question relating to personal code (DC Motors)
Post by: PaulS on May 25, 2016, 01:06 am
Quote
delay (2000); // Straighten
That is not what that function does!
Title: Re: Strange question relating to personal code (DC Motors)
Post by: ed001987 on May 25, 2016, 05:31 am
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.
Title: Re: Strange question relating to personal code (DC Motors)
Post by: ed001987 on May 25, 2016, 05:37 am
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?
Title: Re: Strange question relating to personal code (DC Motors)
Post by: PaulS on May 25, 2016, 11:32 am
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.
Title: Re: Strange question relating to personal code (DC Motors)
Post by: ed001987 on May 25, 2016, 05:27 pm
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)
Title: Re: Strange question relating to personal code (DC Motors)
Post by: TomGeorge on May 25, 2016, 11:54 pm
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.. :)

Title: Re: Strange question relating to personal code (DC Motors)
Post by: ed001987 on May 26, 2016, 06:17 am
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.
Title: Re: Strange question relating to personal code (DC Motors)
Post by: PaulS on May 26, 2016, 10:41 am
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.
Title: Re: Strange question relating to personal code (DC Motors)
Post by: ed001987 on May 26, 2016, 06:13 pm
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.
Title: Re: Strange question relating to personal code (DC Motors)
Post by: PaulS on May 26, 2016, 06:39 pm
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?
Title: Re: Strange question relating to personal code (DC Motors)
Post by: ed001987 on May 26, 2016, 08:15 pm
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)
Title: Re: Strange question relating to personal code (DC Motors)
Post by: ed001987 on May 26, 2016, 08:26 pm
Please forgive the quality.  I've only got an old smart phone as a camera.
Title: Re: Strange question relating to personal code (DC Motors)
Post by: PaulS on May 26, 2016, 11:15 pm
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.
Title: Re: Strange question relating to personal code (DC Motors)
Post by: TomGeorge on May 26, 2016, 11:22 pm
Hi;

 That looks like a pot in one of your pictures.
(http://forum.arduino.cc/index.php?action=dlattach;topic=402768.0;attach=168378)

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..... :)
Title: Re: Strange question relating to personal code (DC Motors)
Post by: ed001987 on May 27, 2016, 05:57 am
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.
I have another motor for moving forwards and backwards, just that current sketch doesn't use it.  I was testing the turning.  Rather than figure out lots of broken things, I'll make sure one thing is perfect, then move forward from there.  (No pun intended)

Tom,

Thanks for that!  I really appreciate it!  Now I have to figure out how to hook up the servo.  Off to Google I go!
Title: Re: Strange question relating to personal code (DC Motors)
Post by: PaulS on May 27, 2016, 01:24 pm
Quote
I have another motor for moving forwards and backwards, just that current sketch doesn't use it.  I was testing the turning.
But, your turning function uses 3 of the names, TURNBRAKE, DIRSPEED, and TURNDIR. I guess I would expect that you are using a motor shield, and that you would be using the TURN side to control the brake, speed, and direction, so I'd be less confused by names like TurnBrake, TurnSpeed, and TurnDir. For turning, I really can't see the need to brake the motor. It should never get to the speed where braking is necessary.
Title: Re: Strange question relating to personal code (DC Motors)
Post by: ed001987 on May 27, 2016, 06:26 pm
That's an old styling habit from my school programming days.  Thank you for commenting!