ManualControl
Dependencies: TPixy-Interface
Fork of MbedOS_Robot_Team by
Drivers/motor_driver_r.cpp@9:fe56b888985c, 2018-02-23 (annotated)
- Committer:
- asobhy
- Date:
- Fri Feb 23 20:58:34 2018 +0000
- Revision:
- 9:fe56b888985c
- Child:
- 24:e88753f090b8
right after the two motors are running
Who changed what in which revision?
User | Revision | Line number | New 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 | |
asobhy | 9:fe56b888985c | 17 | |
asobhy | 9:fe56b888985c | 18 | |
asobhy | 9:fe56b888985c | 19 | DigitalOut DirR(p29); // Right motor direction control pin |
asobhy | 9:fe56b888985c | 20 | PwmOut PwmR(p21); // Right motor PWMz |
asobhy | 9:fe56b888985c | 21 | |
asobhy | 9:fe56b888985c | 22 | Mutex SpeedMutexR; |
asobhy | 9:fe56b888985c | 23 | |
asobhy | 9:fe56b888985c | 24 | static motor_state_t motor_status = MOTOR_UNINIT; |
asobhy | 9:fe56b888985c | 25 | |
asobhy | 9:fe56b888985c | 26 | |
asobhy | 9:fe56b888985c | 27 | /******************************************************************************* |
asobhy | 9:fe56b888985c | 28 | * @brief This function initializes the PWM channel to be used to control a DC |
asobhy | 9:fe56b888985c | 29 | * motor |
asobhy | 9:fe56b888985c | 30 | * @param none |
asobhy | 9:fe56b888985c | 31 | * @return motor_state_t the state of the motor driver |
asobhy | 9:fe56b888985c | 32 | *******************************************************************************/ |
asobhy | 9:fe56b888985c | 33 | motor_state_t motorDriver_R_init() |
asobhy | 9:fe56b888985c | 34 | { |
asobhy | 9:fe56b888985c | 35 | // motor is still uninitialized |
asobhy | 9:fe56b888985c | 36 | motor_status = MOTOR_UNINIT; |
asobhy | 9:fe56b888985c | 37 | |
asobhy | 9:fe56b888985c | 38 | // DC Motor Control Pins |
asobhy | 9:fe56b888985c | 39 | DirR = 0; |
asobhy | 9:fe56b888985c | 40 | PwmR = 0; |
asobhy | 9:fe56b888985c | 41 | |
asobhy | 9:fe56b888985c | 42 | PwmR.period_us(PERIOD_R); // This sets the PWM period in us |
asobhy | 9:fe56b888985c | 43 | |
asobhy | 9:fe56b888985c | 44 | |
asobhy | 9:fe56b888985c | 45 | motor_status = MOTOR_INIT; |
asobhy | 9:fe56b888985c | 46 | |
asobhy | 9:fe56b888985c | 47 | return motor_status; |
asobhy | 9:fe56b888985c | 48 | |
asobhy | 9:fe56b888985c | 49 | } |
asobhy | 9:fe56b888985c | 50 | |
asobhy | 9:fe56b888985c | 51 | /******************************************************************************* |
asobhy | 9:fe56b888985c | 52 | * @brief This function sets the speed and the direction of the DC motor to move |
asobhy | 9:fe56b888985c | 53 | * forward |
asobhy | 9:fe56b888985c | 54 | * @param speed |
asobhy | 9:fe56b888985c | 55 | * @return motor_state_t the state of the motor driver |
asobhy | 9:fe56b888985c | 56 | *******************************************************************************/ |
asobhy | 9:fe56b888985c | 57 | motor_state_t motorDriver_R_forward(int speed) |
asobhy | 9:fe56b888985c | 58 | { |
asobhy | 9:fe56b888985c | 59 | |
asobhy | 9:fe56b888985c | 60 | if( motor_status != MOTOR_UNINIT ) |
asobhy | 9:fe56b888985c | 61 | { |
asobhy | 9:fe56b888985c | 62 | SpeedMutexR.lock(); |
asobhy | 9:fe56b888985c | 63 | DirR = 0; |
asobhy | 9:fe56b888985c | 64 | PwmR.pulsewidth_us(abs(speed)); |
asobhy | 9:fe56b888985c | 65 | SpeedMutexR.unlock(); |
asobhy | 9:fe56b888985c | 66 | motor_status = MOTOR_FORWARD; |
asobhy | 9:fe56b888985c | 67 | } |
asobhy | 9:fe56b888985c | 68 | |
asobhy | 9:fe56b888985c | 69 | return motor_status; |
asobhy | 9:fe56b888985c | 70 | |
asobhy | 9:fe56b888985c | 71 | } |
asobhy | 9:fe56b888985c | 72 | |
asobhy | 9:fe56b888985c | 73 | /******************************************************************************* |
asobhy | 9:fe56b888985c | 74 | * @brief This function sets the speed and the direction of the DC motor to move |
asobhy | 9:fe56b888985c | 75 | * backwards |
asobhy | 9:fe56b888985c | 76 | * @param speed |
asobhy | 9:fe56b888985c | 77 | * @return motor_state_t the state of the motor driver |
asobhy | 9:fe56b888985c | 78 | *******************************************************************************/ |
asobhy | 9:fe56b888985c | 79 | motor_state_t motorDriver_R_reverse(int speed) |
asobhy | 9:fe56b888985c | 80 | { |
asobhy | 9:fe56b888985c | 81 | |
asobhy | 9:fe56b888985c | 82 | if( motor_status != MOTOR_UNINIT ) |
asobhy | 9:fe56b888985c | 83 | { |
asobhy | 9:fe56b888985c | 84 | SpeedMutexR.lock(); |
asobhy | 9:fe56b888985c | 85 | DirR = 1; |
asobhy | 9:fe56b888985c | 86 | PwmR.pulsewidth_us(abs(speed)); |
asobhy | 9:fe56b888985c | 87 | SpeedMutexR.unlock(); |
asobhy | 9:fe56b888985c | 88 | motor_status = MOTOR_REVERSE; |
asobhy | 9:fe56b888985c | 89 | } |
asobhy | 9:fe56b888985c | 90 | |
asobhy | 9:fe56b888985c | 91 | return motor_status; |
asobhy | 9:fe56b888985c | 92 | |
asobhy | 9:fe56b888985c | 93 | } |
asobhy | 9:fe56b888985c | 94 | |
asobhy | 9:fe56b888985c | 95 | /******************************************************************************* |
asobhy | 9:fe56b888985c | 96 | * @brief This function stops the motor |
asobhy | 9:fe56b888985c | 97 | * @param none |
asobhy | 9:fe56b888985c | 98 | * @return motor_state_t the state of the motor driver |
asobhy | 9:fe56b888985c | 99 | *******************************************************************************/ |
asobhy | 9:fe56b888985c | 100 | motor_state_t motorDriver_R_stop() |
asobhy | 9:fe56b888985c | 101 | { |
asobhy | 9:fe56b888985c | 102 | |
asobhy | 9:fe56b888985c | 103 | if( motor_status != MOTOR_UNINIT ) |
asobhy | 9:fe56b888985c | 104 | { |
asobhy | 9:fe56b888985c | 105 | PwmR.pulsewidth_us(0); |
asobhy | 9:fe56b888985c | 106 | motor_status = MOTOR_STOPPED; |
asobhy | 9:fe56b888985c | 107 | } |
asobhy | 9:fe56b888985c | 108 | |
asobhy | 9:fe56b888985c | 109 | return motor_status; |
asobhy | 9:fe56b888985c | 110 | |
asobhy | 9:fe56b888985c | 111 | } |