PlayBack

Dependencies:   TPixy-Interface

Fork of ManualControlFinal by ECE4333 - 2018 - Ahmed & Brandon

Committer:
asobhy
Date:
Sat Mar 03 00:53:06 2018 +0000
Revision:
13:2a00b7a6f4bd
Parent:
9:fe56b888985c
Child:
24:e88753f090b8
two motors working after robots assembly and direction correction

Who changed what in which revision?

UserRevisionLine numberNew contents of line
asobhy 9:fe56b888985c 1 /******************************************************************************/
asobhy 9:fe56b888985c 2 // ECE4333
asobhy 9:fe56b888985c 3 // LAB Partner 1: Ahmed Sobhy - ID: 3594449
asobhy 9:fe56b888985c 4 // LAB Partner 2: Brandon Kingman - ID: 3470444
asobhy 9:fe56b888985c 5 // Project: Autonomous Robot Design
asobhy 9:fe56b888985c 6 // Instructor: Prof. Chris Diduch
asobhy 9:fe56b888985c 7 /******************************************************************************/
asobhy 9:fe56b888985c 8 // filename: motor_driver.cpp
asobhy 9:fe56b888985c 9 // file content description:
asobhy 9:fe56b888985c 10 // * function to initialize the hardware used to control a DC Motor
asobhy 9:fe56b888985c 11 // * functions to drive to motor in forward, reverse and stop.
asobhy 9:fe56b888985c 12 /******************************************************************************/
asobhy 9:fe56b888985c 13
asobhy 9:fe56b888985c 14 #include "mbed.h"
asobhy 9:fe56b888985c 15 #include "motor_driver_r.h"
asobhy 9:fe56b888985c 16 #include "motor_driver_l.h"
asobhy 9:fe56b888985c 17
asobhy 9:fe56b888985c 18
asobhy 9:fe56b888985c 19
asobhy 9:fe56b888985c 20 DigitalOut DirL(p30); // Right motor direction control pin
asobhy 9:fe56b888985c 21 PwmOut PwmL(p22); // Right motor PWMz
asobhy 9:fe56b888985c 22
asobhy 9:fe56b888985c 23 Mutex SpeedMutexL;
asobhy 9:fe56b888985c 24
asobhy 9:fe56b888985c 25 static motor_state_t motor_status = MOTOR_UNINIT;
asobhy 9:fe56b888985c 26
asobhy 9:fe56b888985c 27
asobhy 9:fe56b888985c 28 /*******************************************************************************
asobhy 9:fe56b888985c 29 * @brief This function initializes the PWM channel to be used to control a DC
asobhy 9:fe56b888985c 30 * motor
asobhy 9:fe56b888985c 31 * @param none
asobhy 9:fe56b888985c 32 * @return motor_state_t the state of the motor driver
asobhy 9:fe56b888985c 33 *******************************************************************************/
asobhy 9:fe56b888985c 34 motor_state_t motorDriver_L_init()
asobhy 9:fe56b888985c 35 {
asobhy 9:fe56b888985c 36 // motor is still uninitialized
asobhy 9:fe56b888985c 37 motor_status = MOTOR_UNINIT;
asobhy 9:fe56b888985c 38
asobhy 9:fe56b888985c 39 // DC Motor Control Pins
asobhy 9:fe56b888985c 40 DirL = 0;
asobhy 9:fe56b888985c 41 PwmL = 0;
asobhy 9:fe56b888985c 42
asobhy 9:fe56b888985c 43 PwmL.period_us(PERIOD_L); // This sets the PWM period in us
asobhy 9:fe56b888985c 44
asobhy 9:fe56b888985c 45
asobhy 9:fe56b888985c 46 motor_status = MOTOR_INIT;
asobhy 9:fe56b888985c 47
asobhy 9:fe56b888985c 48 return motor_status;
asobhy 9:fe56b888985c 49
asobhy 9:fe56b888985c 50 }
asobhy 9:fe56b888985c 51
asobhy 9:fe56b888985c 52 /*******************************************************************************
asobhy 9:fe56b888985c 53 * @brief This function sets the speed and the direction of the DC motor to move
asobhy 9:fe56b888985c 54 * forward
asobhy 9:fe56b888985c 55 * @param speed
asobhy 9:fe56b888985c 56 * @return motor_state_t the state of the motor driver
asobhy 9:fe56b888985c 57 *******************************************************************************/
asobhy 13:2a00b7a6f4bd 58 motor_state_t motorDriver_L_reverse(int speed)
asobhy 9:fe56b888985c 59 {
asobhy 9:fe56b888985c 60
asobhy 9:fe56b888985c 61 if( motor_status != MOTOR_UNINIT )
asobhy 9:fe56b888985c 62 {
asobhy 9:fe56b888985c 63 SpeedMutexL.lock();
asobhy 9:fe56b888985c 64 DirL = 0;
asobhy 9:fe56b888985c 65 PwmL.pulsewidth_us(abs(speed));
asobhy 9:fe56b888985c 66 SpeedMutexL.unlock();
asobhy 9:fe56b888985c 67 motor_status = MOTOR_FORWARD;
asobhy 9:fe56b888985c 68 }
asobhy 9:fe56b888985c 69
asobhy 9:fe56b888985c 70 return motor_status;
asobhy 9:fe56b888985c 71
asobhy 9:fe56b888985c 72 }
asobhy 9:fe56b888985c 73
asobhy 9:fe56b888985c 74 /*******************************************************************************
asobhy 9:fe56b888985c 75 * @brief This function sets the speed and the direction of the DC motor to move
asobhy 9:fe56b888985c 76 * backwards
asobhy 9:fe56b888985c 77 * @param speed
asobhy 9:fe56b888985c 78 * @return motor_state_t the state of the motor driver
asobhy 9:fe56b888985c 79 *******************************************************************************/
asobhy 13:2a00b7a6f4bd 80 motor_state_t motorDriver_L_forward(int speed)
asobhy 9:fe56b888985c 81 {
asobhy 9:fe56b888985c 82
asobhy 9:fe56b888985c 83 if( motor_status != MOTOR_UNINIT )
asobhy 9:fe56b888985c 84 {
asobhy 9:fe56b888985c 85 SpeedMutexL.lock();
asobhy 9:fe56b888985c 86 DirL = 1;
asobhy 9:fe56b888985c 87 PwmL.pulsewidth_us(abs(speed));
asobhy 9:fe56b888985c 88 SpeedMutexL.unlock();
asobhy 9:fe56b888985c 89 motor_status = MOTOR_REVERSE;
asobhy 9:fe56b888985c 90 }
asobhy 9:fe56b888985c 91
asobhy 9:fe56b888985c 92 return motor_status;
asobhy 9:fe56b888985c 93
asobhy 9:fe56b888985c 94 }
asobhy 9:fe56b888985c 95
asobhy 9:fe56b888985c 96 /*******************************************************************************
asobhy 9:fe56b888985c 97 * @brief This function stops the motor
asobhy 9:fe56b888985c 98 * @param none
asobhy 9:fe56b888985c 99 * @return motor_state_t the state of the motor driver
asobhy 9:fe56b888985c 100 *******************************************************************************/
asobhy 9:fe56b888985c 101 motor_state_t motorDriver_L_stop()
asobhy 9:fe56b888985c 102 {
asobhy 9:fe56b888985c 103
asobhy 9:fe56b888985c 104 if( motor_status != MOTOR_UNINIT )
asobhy 9:fe56b888985c 105 {
asobhy 9:fe56b888985c 106 PwmL.pulsewidth_us(0);
asobhy 9:fe56b888985c 107 motor_status = MOTOR_STOPPED;
asobhy 9:fe56b888985c 108 }
asobhy 9:fe56b888985c 109
asobhy 9:fe56b888985c 110 return motor_status;
asobhy 9:fe56b888985c 111
asobhy 9:fe56b888985c 112 }