0

I have written a small C application to do PWM for some motors connected to a Raspberry Pi. I am using the PiGPIO library. The code as follows:

int[] FWD = {MOTOR1_FWD, MOTOR2_FWD};
int[] BCK = {MOTOR1_BCK, MOTOR2_BCK};

void setMotorSpeed(int motorIndex, int speed)
{
    if (speed > 0) //forward
    {
        gpioPWM(FWD[motorIndex], speed);
        gpioPWM(BCK[motorIndex], 0);
    }
    else if (speed < 0) //back
    {
         gpioPWM(FWD[motorIndex], 0);
         gpioPWM(BCK[motorIndex], -speed);
    }
    else /* stop */
    {
        gpioPWM(FWD[motorIndex], 0);
        gpioPWM(BCK[motorIndex], 0);
    }
}

int main(int argc, char *argv[])
{
   double start;

   if (gpioInitialise() < 0)
   {
      fprintf(stderr, "pigpio initialisation failed\n");
      return 1;
   }

   char *p;
   int speed = strtol(argv[2],&p, 10);

   /* Set GPIO modes */
   int i = 0;
   for (i = 0; i < 4; i++) {
      gpioSetMode(FWD[i], PI_OUTPUT);
      gpioSetMode(BCK[i], PI_OUTPUT);
      gpioSetMode(EN[i], PI_OUTPUT);

      setMotorSpeed(i, 0);
      gpioWrite(EN[i], ON);
   }

   if (argc <= 1)
   {
      printf("STOP\n");
   } else {
      if (strcmp(argv[1], "l") == 0 || strcmp(argv[1], "lr") == 0)
      {
         printf("LEFT ON: %d\n", speed);
         setMotorSpeed(0, speed);
      }
      if (strcmp(argv[1], "r") == 0 || strcmp(argv[1], "lr") == 0)
      {
         printf("RIGHT ON: %d\n", speed);
         setMotorSpeed(1, speed);
      }
   }
   gpioTerminate();
   return 0;
}

Works fine, but if I change MAX to 255 and use gpioPWM(motorX, MAX) it only works sometimes, and when it does work only the left will run. What should I do to make them both be able to work, and work consistently?

This does not work consistently when I compile and run as follows:

gcc -o drive drive.c -lpigpio -lrt -lpthread
sudo ./drive lr 150

Currently I just have the pins connected to LED's and sometimes the LED's get power, other times they do not.

  • The motor driver boards I use have two GPIO per motor. You have to set both GPIO to a known value to get the proper control (stop, forward, back, brake). You are only setting one GPIO. I presume the other is left at whatever it previously was. – joan Aug 9 '17 at 7:15
  • I based my code off of (this tutorial)[instructables.com/id/… - could you give me an example of what you mean in the answer section? – Quintin Balsdon Aug 9 '17 at 7:19
1

A DC motor is normally controlled with a motor driver using two GPIO. A typical motor driver allows control of two DC motors using pins 1A/1B for one motor and pins 2A/2B for the other.

I assume that MOTOR_1_A is a constant whose value is the GPIO connected to pin 1A etc.

void setMotorSpeed(int gpioA, int gpioB, int speed)
{
   if (speed > 0) /* clockwise */
   {
      gpioPWM(gpioA, speed);
      gpioPWM(gpioB, 0);
   }
   else if (speed < 0) /* counter clockwise */
   {
      gpioPWM(gpioA, 0);
      gpioPWM(gpioB, -speed);
   }
   else /* stop */
   {
      gpioPWM(gpioA, 0);
      gpioPWM(gpioB, 0);
   }
}

To make a two wheel buggy go forward

setMotorSpeed(MOTOR_1_A, MOTOR_1_B, speed);
setMotorSpeed(MOTOR_2_A, MOTOR_2_B, -speed);

To make a two wheel buggy go backward

setMotorSpeed(MOTOR_1_A, MOTOR_1_B, -speed);
setMotorSpeed(MOTOR_2_A, MOTOR_2_B, speed);

To make a two wheel buggy spin clockwise

setMotorSpeed(MOTOR_1_A, MOTOR_1_B, speed);
setMotorSpeed(MOTOR_2_A, MOTOR_2_B, speed);

To make a two wheel buggy spin counter clockwise

setMotorSpeed(MOTOR_1_A, MOTOR_1_B, -speed);
setMotorSpeed(MOTOR_2_A, MOTOR_2_B, -speed);

The actual movements made by your buggy will depend on the way the motors are wired (you might need to change the signs).

  • Hi there, I tried your solution and the results are the same. I have updated my question so it follows your pattern – Quintin Balsdon Aug 13 '17 at 9:28
0

I have since moved on from this technique. But the problem with this code is that the c program sets the PWM and then exits, meaning that any memory and control is released from the application. Therefore the GPIO pins just retain their state when the application terminates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.