BLDC Control Example

Dependencies:   mbed

Committer:
justinkim
Date:
Fri Apr 08 00:39:32 2016 +0000
Revision:
0:2c3908cbb374
BLDC Motor control example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
justinkim 0:2c3908cbb374 1 /**
justinkim 0:2c3908cbb374 2 ******************************************************************************
justinkim 0:2c3908cbb374 3 * @project Servo Motor example
justinkim 0:2c3908cbb374 4 * @author Justin Kim
justinkim 0:2c3908cbb374 5 * @version V1.0.0
justinkim 0:2c3908cbb374 6 * @date 08-APR-2016
justinkim 0:2c3908cbb374 7 * @brief Main program body
justinkim 0:2c3908cbb374 8 *******************************************************************************
justinkim 0:2c3908cbb374 9 **/
justinkim 0:2c3908cbb374 10
justinkim 0:2c3908cbb374 11 /* Includes ------------------------------------------------------------------*/
justinkim 0:2c3908cbb374 12 #include "mbed.h"
justinkim 0:2c3908cbb374 13
justinkim 0:2c3908cbb374 14 /* Private typedef -----------------------------------------------------------*/
justinkim 0:2c3908cbb374 15 /* Private define ------------------------------------------------------------*/
justinkim 0:2c3908cbb374 16 /* Private variables ---------------------------------------------------------*/
justinkim 0:2c3908cbb374 17 Serial pc(USBTX, USBRX);
justinkim 0:2c3908cbb374 18 PwmOut motor(P9);
justinkim 0:2c3908cbb374 19
justinkim 0:2c3908cbb374 20 /* Private function prototypes -----------------------------------------------*/
justinkim 0:2c3908cbb374 21
justinkim 0:2c3908cbb374 22 /* Private functions ---------------------------------------------------------*/
justinkim 0:2c3908cbb374 23 /**
justinkim 0:2c3908cbb374 24 * @brief Main Function
justinkim 0:2c3908cbb374 25 * @param None
justinkim 0:2c3908cbb374 26 * @retval None
justinkim 0:2c3908cbb374 27 */
justinkim 0:2c3908cbb374 28 int main(void)
justinkim 0:2c3908cbb374 29 {
justinkim 0:2c3908cbb374 30 char ch;
justinkim 0:2c3908cbb374 31 float speed = 0.0;
justinkim 0:2c3908cbb374 32 pc.baud(115200);
justinkim 0:2c3908cbb374 33 motor.period_us(1000000/490);
justinkim 0:2c3908cbb374 34 pc.printf("Hello Motor World!\n\r");
justinkim 0:2c3908cbb374 35
justinkim 0:2c3908cbb374 36 while(1)
justinkim 0:2c3908cbb374 37 {
justinkim 0:2c3908cbb374 38 if( pc.readable())
justinkim 0:2c3908cbb374 39 {
justinkim 0:2c3908cbb374 40 ch=pc.getc();
justinkim 0:2c3908cbb374 41 pc.printf("%c",ch);
justinkim 0:2c3908cbb374 42
justinkim 0:2c3908cbb374 43 if( ch == '+' )
justinkim 0:2c3908cbb374 44 {
justinkim 0:2c3908cbb374 45 speed = speed + 0.01;
justinkim 0:2c3908cbb374 46 motor.write(speed);
justinkim 0:2c3908cbb374 47 pc.printf("forward\r\n");
justinkim 0:2c3908cbb374 48 }
justinkim 0:2c3908cbb374 49 else if( ch == '-' )
justinkim 0:2c3908cbb374 50 {
justinkim 0:2c3908cbb374 51 speed = speed - 0.01;
justinkim 0:2c3908cbb374 52 motor.write(speed);
justinkim 0:2c3908cbb374 53 pc.printf("back\r\n");
justinkim 0:2c3908cbb374 54 }
justinkim 0:2c3908cbb374 55 else if( ch == '*' )
justinkim 0:2c3908cbb374 56 {
justinkim 0:2c3908cbb374 57 speed = 0.55;
justinkim 0:2c3908cbb374 58 motor.write(speed);
justinkim 0:2c3908cbb374 59 pc.printf("stop\r\n");
justinkim 0:2c3908cbb374 60 }
justinkim 0:2c3908cbb374 61
justinkim 0:2c3908cbb374 62 if( speed < 0.55 )
justinkim 0:2c3908cbb374 63 {
justinkim 0:2c3908cbb374 64 speed = 0.55;
justinkim 0:2c3908cbb374 65 motor.write(speed);
justinkim 0:2c3908cbb374 66 }
justinkim 0:2c3908cbb374 67 else if ( speed > 1.0 )
justinkim 0:2c3908cbb374 68 {
justinkim 0:2c3908cbb374 69 speed = 1.0;
justinkim 0:2c3908cbb374 70 motor.write(speed);
justinkim 0:2c3908cbb374 71 }
justinkim 0:2c3908cbb374 72 pc.printf("speed %.3f\r\n", speed);
justinkim 0:2c3908cbb374 73 }
justinkim 0:2c3908cbb374 74 }
justinkim 0:2c3908cbb374 75 }