Go Down

Topic: Dual Motor Car Wont Go Straight When Controlled Via Bluetooth  (Read 137 times) previous topic - next topic

supercar14

I have a dual dc motor car chassis that I'm trying to control using my iPhone through the ArduinoBlue app. When connected to a physical joystick the car drives (mostly) fine, but when controlled through the Bluetooth on my phone it will only go in circles.

Here's my code
Code: [Select]
#include <SoftwareSerial.h>
#include <ArduinoBlue.h>

//Motor variables
const int M1_1A = 11; //Motor 1 Control Input A through pin 11
const int M1_1B = 10; //Motor 1 Control Input B through pin 10
const int M2_1A = 6;  //Motor 2 Control Input A through pin 6
const int M2_1B = 5;  //Motor 2 Control Input B through pin 5

// HM 10 bluetooth module connections
const int BLUETOOTH_TX = 8;
const int BLUETOOTH_RX = 7;

int throttle = 49;
int steering = 49;
int PWM_1;
int PWM_2;

SoftwareSerial bluetooth(BLUETOOTH_TX, BLUETOOTH_RX);
ArduinoBlue phone(bluetooth); // pass reference of bluetooth object to ArduinoBlue constructor.

void setup()
{
  pinMode(M1_1A, OUTPUT);
  pinMode(M1_1B, OUTPUT);
  pinMode(M2_1A, OUTPUT);
  pinMode(M2_1B, OUTPUT);
  stop(M1_1A,M1_1B);
  stop(M2_1A,M2_1B);
  Serial.begin(9600);
  // Begin serial communication with HM 10 at 9600 bps
  bluetooth.begin(9600);
  // delay in case the HM 10 needs time to respond
  delay(100);
}

void loop()
{
  throttle = phone.getThrottle();
  steering = phone.getSteering();
 
  //Forward
  if(throttle>50)
  {
    PWM_1=map(throttle,50,99,0,255);
    PWM_2=map(throttle,50,99,0,255);
    forward(M1_1A,M1_1B,PWM_1);
    forward(M2_1B,M2_1A,PWM_2);
  }
  //Reverse
  else if(throttle<48)
  {
    PWM_1=map(throttle,48,0,0,255);
    PWM_2=map(throttle,48,0,0,255);
    reverse(M1_1A,M1_1B,PWM_1);
    reverse(M2_1B,M2_1A,PWM_2);
  }
  //Sharp Left
  else if(steering<10)
  {
    forward(M2_1A,M2_1B,255);
    stop(M1_1A,M1_1B);
  }
  //Sharp Right
  else if(steering>90)
  {
    stop(M1_1A,M1_1B);
    reverse(M2_1A,M2_1B,255);
  }
  else
  {
    stop(M1_1A,M1_1B);
    stop(M2_1A,M2_1B);
  }
 
  Serial.print("Throttle: ");Serial.println(throttle);
  Serial.print("Steering: ");Serial.println(steering);
  Serial.println("\n\n");
  delay(50);
}

//function that controls forward motion
void forward (int m1A, int m1B, int ispeed)
// ispeed between 0 and 255.  m1A and m1B indicate motor
{
  if ((ispeed >= 0) and (ispeed <= 255))
  {
    stop (m1A,m1B);  // ensure control pins at 0,0 before changing
    analogWrite (m1A, ispeed);
    digitalWrite(m1B , LOW);
  }
}

//function that controls reverse motion
void reverse (int m1A, int m1B, int ispeed)
//ispeed between 0 and 255.  m1A and m1B indicate motor
{
  if ((ispeed >= 0) and (ispeed <= 255))
  {
    stop (m1A,m1B);  // ensure control pins at 0,0 before changing
    digitalWrite(m1A, LOW);
    analogWrite (m1B, ispeed);
  }
}

//stops both motors
void stop(int m1A, int m1B) // m1A and m1B indicate motor
{
   digitalWrite(m1A, LOW);
   digitalWrite(m1B, LOW);
}


One thing I noticed is that if i swap the one of the motors in the functions for forward and reverse the car will now only go in a straight line but will not turn

From
Code: [Select]
forward(M1_1A,M1_1B,PWM_1);
forward(M2_1B,M2_1A,PWM_2);


To
Code: [Select]
forward(M1_1A,M1_1B,PWM_1);
forward(M2_1A,M2_1B,PWM_2);


Switching the motors in the functions to turn has no effect.

I'm using an UNO board with a sbx h bridge and a DSDTECH HM 10 Bluetooth module.

Any ideas?
Thanks.

Robin2

I know nothing about the ArduinoBlue library. However if you have a program that works fine with a joystick and you just substitute the joystick values with the values from ArduinoBlue I don't see why the behaviour would change.

This is a case where it would be a good idea to have a readJoystick() function that collects data and saves it to two variables for speed and steering. And another function that gets data from ArduinoBlue and saves it to the same two variables.

Then the rest of the program would be completely indifferent to which function had produced the speed and steering values.

If you are using ArduinoBlue you could comment-out the call the the readJoystick() function and vice versa.

My suggestion is to start with a copy of the working program, move the joystick code into a function (checking that it still works properly) and then add a function for the ArduinoBlue stuff.

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

TomGeorge

Hi,
It sounds like your BT app, has one of the motor directions opposite to what it should.

When it goes in circles;
Does one motor go forward and the other reverse?
OR
Does one motor run and the other stops?

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

MarkT

Step 1:  Write a sketch to test that the bluetooth is returning the throttle/steering values correctly.

Step 2: Write a sketch to test that the motors do the right thing when commanded by various throttle and steering values.

Only when both of these work independently to your satisfaction:

Step 3: combine them.

In other words debug each individual piece of functionality carefully and separately.  Trying to debug a complex system when you're not sure of each part of it is always going to be difficult and confusing - don't let yourself get to that position.

This approach also forces separation-of-concerns into the structure of the code from the start, always a good thing.  The individual pieces are easy to reuse too.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

supercar14

I got it to work. Turns out my map functions were wrong. Thanks for the suggestions!

Go Up