ECE4333 - 2018 - Ahmed & Brandon / Mbed OS ObjectFollower

Dependencies:   TPixy-Interface

Fork of PlayBack by ECE4333 - 2018 - Ahmed & Brandon

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers motor_driver_r.cpp Source File

motor_driver_r.cpp

00001 /******************************************************************************/
00002 // ECE4333
00003 // LAB Partner 1:   Ahmed Sobhy - ID: 3594449
00004 // LAB Partner 2:   Brandon Kingman - ID: 3470444
00005 // Project:         Autonomous Robot Design
00006 // Instructor:      Prof. Chris Diduch
00007 /******************************************************************************/
00008 // filename: motor_driver.cpp
00009 // file content description:
00010 //      * function to initialize the hardware used to control a DC Motor
00011 //      * functions to drive to motor in forward, reverse and stop.
00012 /******************************************************************************/
00013 
00014 #include "mbed.h"
00015 #include "motor_driver_r.h"
00016 
00017 
00018 
00019 DigitalOut DirR(p29);    // Right motor direction control pin
00020 PwmOut PwmR(p21);       // Right motor PWMz
00021 
00022 Mutex SpeedMutexR;
00023 
00024 static motor_state_t motor_status = MOTOR_UNINIT;
00025 
00026 
00027 /*******************************************************************************
00028 * @brief  This function initializes the PWM channel to be used to control a DC
00029 *         motor
00030 * @param  none
00031 * @return motor_state_t the state of the motor driver
00032 *******************************************************************************/
00033 motor_state_t motorDriver_R_init()
00034 {
00035     // motor is still uninitialized
00036     motor_status = MOTOR_UNINIT;
00037 
00038     // DC Motor Control Pins
00039     DirR = 0;
00040     PwmR = 0;
00041 
00042     PwmR.period_us(PERIOD_R); // This sets the PWM period in us
00043 
00044 
00045     motor_status = MOTOR_INIT;
00046 
00047     return motor_status;
00048 
00049 }
00050 
00051 /*******************************************************************************
00052 * @brief  This function sets the speed and the direction of the DC motor to move
00053 *         forward
00054 * @param  speed
00055 * @return motor_state_t the state of the motor driver
00056 *******************************************************************************/
00057 motor_state_t motorDriver_R_forward(int speed)
00058 {
00059 
00060     if( motor_status != MOTOR_UNINIT ) 
00061     {
00062         SpeedMutexR.lock();
00063         DirR = 0;
00064         PwmR.pulsewidth_us(abs(speed));
00065         SpeedMutexR.unlock();
00066         motor_status = MOTOR_FORWARD;
00067     }
00068 
00069     return motor_status;
00070 
00071 }
00072 
00073 /*******************************************************************************
00074 * @brief  This function sets the speed and the direction of the DC motor to move
00075 *         backwards
00076 * @param  speed
00077 * @return motor_state_t the state of the motor driver
00078 *******************************************************************************/
00079 motor_state_t motorDriver_R_reverse(int speed)
00080 {
00081 
00082     if( motor_status != MOTOR_UNINIT ) 
00083     {
00084         SpeedMutexR.lock();
00085         DirR = 1;
00086         PwmR.pulsewidth_us(abs(speed));
00087         SpeedMutexR.unlock();
00088         motor_status = MOTOR_REVERSE;
00089     }
00090 
00091     return motor_status;
00092 
00093 }
00094 
00095 /*******************************************************************************
00096 * @brief  This function stops the motor
00097 * @param  none
00098 * @return motor_state_t the state of the motor driver
00099 *******************************************************************************/
00100 motor_state_t motorDriver_R_stop()
00101 {
00102     
00103     if( motor_status != MOTOR_UNINIT ) 
00104     {
00105         PwmR.pulsewidth_us(0);
00106         motor_status = MOTOR_STOPPED;
00107     }
00108 
00109     return motor_status;
00110 
00111 }