Go Down

Topic: Robor car (Read 964 times) previous topic - next topic

redkite

Hi.
I can't get the rc car to turn. I am using Motorshield from Ladyada.
The forward and reverse part works, but not left or right.
Can't seem to find an error in the code either. Suggestions??

Code: [Select]
/
#include <AFMotor.h>
#define FAILSAFE 2000
unsigned long last;
AF_DCMotor motor(1);
AF_DCMotor turn(2);

void setup() {
  Serial.begin(115200);           // set up Serial library at 115200 bps
  Serial.println("Let's Drive");
  last = millis();

  // turn on motor
  motor.setSpeed(255);
  turn.setSpeed(255);

  motor.run(RELEASE);
  turn.run(RELEASE);
}

void motor_fwd()
{
    motor.run(FORWARD);
    motor.setSpeed(255);
}

void motor_back()
{
    motor.run(BACKWARD);
    motor.setSpeed(255);
}

void turn_r()
{
    turn.run(FORWARD);
    turn.setSpeed(255);
}

void turn_l()
{
    turn.run(BACKWARD);
    turn.setSpeed(255);
}

void turn_s()
{
    turn.run(RELEASE);
    turn.setSpeed(0);
}

void motor_s()
{
    motor.run(RELEASE);
    motor.setSpeed(0);
}


void loop() {
  if (Serial.available()) {  //is there anything to read?
        char getData = Serial.read();  //if yes, read it
        last = millis();

      switch (getData) {
    case 'F':   //Forward
        turn_s();
        motor_fwd();
        Serial.println("Forward");
      break;
    case 'B':    //Reverse
        turn_s();
        motor_back();
        Serial.println("Reverse");
      break;
    case 'L':    //Left
        turn_l();
        motor_s();
        Serial.println("Left");
      break;
    case 'R':    //Right
        turn_r();
        motor_s();
        Serial.println("Right");
      break;
    case 'S':    //Stop
        motor_s();
        turn_s();
        Serial.println("Stop");
      break;
      }
  }
  else {
        if (millis() - last > FAILSAFE)
                motor_s();
                turn_s();
        }
}

PeterH

What is controlling your steering? It sounds as if you're using a proportional motor control for that, which seems like a pretty strange approach. What hardware is used to control the steering, and how is it connected to the Arduino?
I only provide help via the forum - please do not contact me for private consultancy.

redkite


What is controlling your steering? It sounds as if you're using a proportional motor control for that, which seems like a pretty strange approach. What hardware is used to control the steering, and how is it connected to the Arduino?



I am using an old rc car. It uses two dc motors, one for steering and one for throttle.
These are connected to the motorshield from adafruit. But i cant get it to work with two motors, only one. Even though it has 4 outputs.

PeterH

If you swap the connections from the motor shield to the motors does the other motor work?

If you swap the pin assignments of your two motors in the sketch does the other motor work?
I only provide help via the forum - please do not contact me for private consultancy.

holmes4

How old is the car? How good expensive was it new?

Do you have any small <500k photo's?.

The steering  may be a servo not a motor.

Mark

redkite

The car is using a dc motor for steering.


I have tried to switch pins, and pinnumber in the code, but i still can't get both motors to work at the same time. When i switched pins, the steering would work, but
not the other, and vica verce when i switched back.

Maybe its an fault in the code somewhere but i can't find any :(

PeterH



I have tried to switch pins, and pinnumber in the code, but i still can't get both motors to work at the same time. When i switched pins, the steering would work, but not the other, and vica verce when i switched back.


Getting both motors working at the same time was not the goal. The goal was to confirm that both motors worked, and both channels on the motor drive shield worked, and both software controllers worked. Unfortunately, since you haven't told us exactly which configurations you tested and what worked in each configuration, we have no idea what your test proves.
I only provide help via the forum - please do not contact me for private consultancy.

redkite




I have tried to switch pins, and pinnumber in the code, but i still can't get both motors to work at the same time. When i switched pins, the steering would work, but not the other, and vica verce when i switched back.


Getting both motors working at the same time was not the goal. The goal was to confirm that both motors worked, and both channels on the motor drive shield worked, and both software controllers worked. Unfortunately, since you haven't told us exactly which configurations you tested and what worked in each configuration, we have no idea what your test proves.


I have tried all of the 4 channels on the shield. And as long I am only using on motor on one channel at the time, it works.
When i changed the channel, i also change the code.
Code: [Select]
AF_DCMotor motor(1); to
Code: [Select]
AF_DCMotor motor(2); and so on.
That works, so I can confirm that all the channels works, but not on the same time.
I've also tried to connect more power to the sheild, 3cell lipo but still nothing

redkite


Foolios

Can you do something with your code to make sure that both parts that deal with each motor separately are working at the same time?
Like say, place an led and code for it in one block along with the motor1 instructions and then place an led and the code for it in the other motor's(motor2) code block and then see whether both leds react to your controls.

PeterH


When i changed the channel, i also change the code.


I had really intended that you changed one thing at a time, i.e. switch the motor wiring over and show that the 'motor' control now operated the steering, then switch the pins over and show that the 'steering' control now operated the steering.  However, I gather you have proved that both motors work and that both channels of the motor shield work and that the commands to steer and drive can both make motors work.

When you say they only work one at a time, do you mean that in a given configuration one of the controls works perfectly and the other doesn't work at all? I'm puzzled by the implementation of the F/B/L/R/S commands, because they seem to be mutually exclusive. Moving forwards centers the steering; turning the steering stops forwards movement. Is that usable in practice? I'd have thought separate independent control of each channel was more useful, and I'm just wondering whether I've just mis-understood your description and this is your real problem.
I only provide help via the forum - please do not contact me for private consultancy.

redkite

I get it to work. I've used a different setup. A Tamiya gearbox.
This is the code that i used.

Code: [Select]
#include <AFMotor.h>
AF_DCMotor Lmotor(3);
AF_DCMotor Rmotor(4);

void setup() {
  Serial.begin(115200);           // set up Serial library at 115200 bps

  // turn on motor
  Lmotor.setSpeed(200);
  Rmotor.setSpeed(200);
}

void move_fwd()
{
Lmotor.run(FORWARD);      // turn it on going forward
Rmotor.run(FORWARD);      // turn it on going forward
}

void move_back()
{
Lmotor.run(BACKWARD);      // turn it on going BACKWARD
Rmotor.run(BACKWARD);      // turn it on going BACKWARD
}

void move_left()
{
Lmotor.run(FORWARD);      // turn it on going forward
Rmotor.run(BACKWARD);      // turn it on going BACKWARD
}

void move_rigth()
{
Lmotor.run(BACKWARD);      // turn it on going BACKWARD
Rmotor.run(FORWARD);      // turn it on going forward
}

void loop() {
if (Serial.available()) {  //is there anything to read?
        char getData = Serial.read();  //if yes, read it
       
        switch (getData) {
    case 'F':   //Forward
        move_fwd();
      break;
    case 'B':    //Reverse
        move_back();
      break;
    case 'L':    //Left
        move_left();
      break;
    case 'R':    //Right
        move_rigth();
      break;
        default:
        Lmotor.run(RELEASE);
        Rmotor.run(RELEASE);
        }       
}
}


So my problem is solved

Go Up