Ripley: The Evolution of a PiBot – Part 2b: Code Snippet: Drive Motor Control Function

As with the hardware, the function to control the drive motor is relatively simple. MotorPin_A controls the the direction of the motor and MotorPin_B controls the speed using PWM through the softPwmWrite() function (part of the wiringPi library).

/*
* File name   : MainDriveMotor.h
* Description : Control function for Ripley's main drive motor
* E-mail      : support@athenacomputers.co.uk
* Author      : Christine Anderson
* Date        : 02/02/2017
*/

#define MotorPin_A          4    // Motor Direction
#define MotorPin_B          5    // Motor Speed

int DriveMotor( int status, int dir, int speed);

int DriveMotor(int status, int dir, int speed)
{
    if(1 == status){                        // Turn motor on.
        if(1 == dir){                       // Motor direction == clockwise
            digitalWrite(MotorPin_A, HIGH);
            softPwmWrite(MotorPin_B, 100-speed);
        }else{                              // Motor direction == anti-clockwise
            digitalWrite(MotorPin_A, LOW);
            softPwmWrite(MotorPin_B, speed);
        }
    }else{                                  // Turn motor off
        digitalWrite(MotorPin_A, HIGH);
        digitalWrite(MotorPin_B, HIGH);
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s