Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

Flipping motor direction

I am creating an rc car using the romi 32u4 board and chassis kit. I am unable to flip the motor direction on either motor. I have tried the normal commands that flip the motor direction and changed the values of the speeds to negative. This had no effect on either motor.

I have also checked if I connected the motor to the board correctly using the encoder.
I have found no errors. How can I fix this?

Hello.

Can you load the MotorTest example program from the Romi 32U4 Library and see if the motors rotate in both directions? If that doesn’t work, can you post close-up pictures showing the soldering of both encoder boards?

- Amanda

The motor test program works. It seems to be a problem with my code. Could you check what is wrong with it I myself cant seem to find the problem with it.

#include <SPI.h>

#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>

#include <Servo.h>

#include <FastGPIO.h>
#include <Romi32U4.h>

Romi32U4Motors motors;
RF24 radio(7, 8);

const byte address[6] = "10101";
 
int mspeedl = 0, mspeedr = 0;

int hcount = 0, ccount = 0;

int button = 10;

Servo arm;
Servo claw;


void setup()
{
  arm.attach(A2); // reset pin nums
  claw.attach(A0);

  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.startListening();
}

void loop()
{

  if (radio.available()) {
    int buf[4];  // buf[0] - y motors, buf[1] - arm, buf[2] - button input, buf[3] - x motors
    radio.read(&buf, sizeof(buf));
    button = buf[2];
    if (button != 10) {
      buttonstate();
    }
    armud(buf[1]);
    forward(buf[0], buf[3]);
    turn(buf[0], buf[3]);
  }
}

void drive(int ml, int mr) {
  motors.setRightSpeed(mr);  
  motors.setLeftSpeed(ml);
}

void forward(int y, int x) {
  if (x <= 515 && x >= 512) {
    int speedy = (y - 515) / 1.7;
    //mspeedl = -speedy;
    //mspeedr = speedy;
    drive(speedy, -speedy);
  }
}

void turn(int y, int x) {
  if (x > 515) {
    int speedl = (y - 515) / 1.7;
    int speedr = speedl - ((x - 515) / 1.7);
    //mspeedl = -speedr; 
    drive(speedl, speedr);
  }
  if (x < 515) {
    int speedr = (y - 515) / 1.7;
    int speedl = speedr - ((515 - x) / 1.7) ;
    //mspeedl = -speedl;
    drive(speedl, speedr);
  }
}


void buttonstate() {
  if (button == 2) {
    clawopen();
  }
  if (button == 3) {
    clawclose();
  }
}

void armud(int val) {
  hcount = map(val, 512, 1024, 0, 90);
  arm.write(hcount);
}

void clawopen() {
  if (ccount < 180) {
    claw.write(ccount);
    ccount += 5;
  }
}

void clawclose() {
  if (ccount > 90) {
    claw.write(ccount);
    ccount -= 5;
  }
}

-Rohan

I suspect the libraries you have declared in your code are conflicting with the Romi32U4Motors library. In particular, it looks like you might be doing something with hardware SPI, which would interfere with the left and right motor direction control, so you might need to switch to using software SPI on pins that will not interfere with the Romi’s hardware. You can look at the table under the “Pin assignment” section in the Pololu Romi 32U4 Control Board User’s Guide to see which pins might be good candidates for this.

Also, did you modify the Servo library to use Timer 3 instead of Timer 1 as described in the “Controlling a servo” section of the Pololu Romi 32U4 Control Board User’s Guide? If not, that could cause a conflict with Romi32U4Motor library since they both use Timer1 by default.

- Amanda