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_l.cpp Source File

motor_driver_l.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 #include "motor_driver_l.h"
00017 
00018 
00019 
00020 DigitalOut DirL(p30);    // Right motor direction control pin
00021 PwmOut PwmL(p22);       // Right motor PWMz
00022 
00023 Mutex SpeedMutexL;
00024 
00025 static motor_state_t motor_status = MOTOR_UNINIT;
00026 
00027 
00028 /*******************************************************************************
00029 * @brief  This function initializes the PWM channel to be used to control a DC
00030 *         motor
00031 * @param  none
00032 * @return motor_state_t the state of the motor driver
00033 *******************************************************************************/
00034 motor_state_t motorDriver_L_init()
00035 {
00036     // motor is still uninitialized
00037     motor_status = MOTOR_UNINIT;
00038 
00039     // DC Motor Control Pins
00040     DirL = 0;
00041     PwmL = 0;
00042 
00043     PwmL.period_us(PERIOD_L); // This sets the PWM period in us
00044 
00045 
00046     motor_status = MOTOR_INIT;
00047 
00048     return motor_status;
00049 
00050 }
00051 
00052 /*******************************************************************************
00053 * @brief  This function sets the speed and the direction of the DC motor to move
00054 *         forward
00055 * @param  speed
00056 * @return motor_state_t the state of the motor driver
00057 *******************************************************************************/
00058 motor_state_t motorDriver_L_reverse(int speed)
00059 {
00060 
00061     if( motor_status != MOTOR_UNINIT ) 
00062     {
00063         SpeedMutexL.lock();
00064         DirL = 0;
00065         PwmL.pulsewidth_us(abs(speed));
00066         SpeedMutexL.unlock();
00067         motor_status = MOTOR_FORWARD;
00068     }
00069 
00070     return motor_status;
00071 
00072 }
00073 
00074 /*******************************************************************************
00075 * @brief  This function sets the speed and the direction of the DC motor to move
00076 *         backwards
00077 * @param  speed
00078 * @return motor_state_t the state of the motor driver
00079 *******************************************************************************/
00080 motor_state_t motorDriver_L_forward(int speed)
00081 {
00082 
00083     if( motor_status != MOTOR_UNINIT ) 
00084     {
00085         SpeedMutexL.lock();
00086         DirL = 1;
00087         PwmL.pulsewidth_us(abs(speed));
00088         SpeedMutexL.unlock();
00089         motor_status = MOTOR_REVERSE;
00090     }
00091 
00092     return motor_status;
00093 
00094 }
00095 
00096 /*******************************************************************************
00097 * @brief  This function stops the motor
00098 * @param  none
00099 * @return motor_state_t the state of the motor driver
00100 *******************************************************************************/
00101 motor_state_t motorDriver_L_stop()
00102 {
00103     
00104     if( motor_status != MOTOR_UNINIT ) 
00105     {
00106         PwmL.pulsewidth_us(0);
00107         motor_status = MOTOR_STOPPED;
00108     }
00109 
00110     return motor_status;
00111 
00112 }