Robotics Lab Servo

Dependencies:   mbed

Committer:
cpul5338
Date:
Mon Mar 19 09:12:35 2018 +0000
Revision:
0:f21fa73a3db3
Robotics_Lab_Servo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cpul5338 0:f21fa73a3db3 1 #include "mbed.h"
cpul5338 0:f21fa73a3db3 2
cpul5338 0:f21fa73a3db3 3 //****************************************************************************** Define
cpul5338 0:f21fa73a3db3 4 //The number will be compiled as type "double" in default
cpul5338 0:f21fa73a3db3 5 //Add a "f" after the number can make it compiled as type "float"
cpul5338 0:f21fa73a3db3 6 #define Ts 0.01f //period of timer1 (s)
cpul5338 0:f21fa73a3db3 7 #define Servo_Period 20
cpul5338 0:f21fa73a3db3 8 //****************************************************************************** End of Define
cpul5338 0:f21fa73a3db3 9
cpul5338 0:f21fa73a3db3 10 //****************************************************************************** I/O
cpul5338 0:f21fa73a3db3 11 //PWM
cpul5338 0:f21fa73a3db3 12 PwmOut servo(A0);
cpul5338 0:f21fa73a3db3 13 //Timer Setting
cpul5338 0:f21fa73a3db3 14 Ticker timer;
cpul5338 0:f21fa73a3db3 15 //****************************************************************************** End of I/O
cpul5338 0:f21fa73a3db3 16
cpul5338 0:f21fa73a3db3 17 //****************************************************************************** Functions
cpul5338 0:f21fa73a3db3 18 void init_timer(void);
cpul5338 0:f21fa73a3db3 19 void init_PWM(void);
cpul5338 0:f21fa73a3db3 20 void timer_interrupt(void);
cpul5338 0:f21fa73a3db3 21 //****************************************************************************** End of Functions
cpul5338 0:f21fa73a3db3 22
cpul5338 0:f21fa73a3db3 23 //****************************************************************************** Variables
cpul5338 0:f21fa73a3db3 24 // Servo
cpul5338 0:f21fa73a3db3 25 float servo_duty = 0.066; // 0.025~0.115(-90~+90)(not linear, so the experiment is needed)
cpul5338 0:f21fa73a3db3 26 //****************************************************************************** End of Variables
cpul5338 0:f21fa73a3db3 27
cpul5338 0:f21fa73a3db3 28 //****************************************************************************** Main
cpul5338 0:f21fa73a3db3 29 int main()
cpul5338 0:f21fa73a3db3 30 {
cpul5338 0:f21fa73a3db3 31 init_timer();
cpul5338 0:f21fa73a3db3 32 init_PWM();
cpul5338 0:f21fa73a3db3 33 while(1)
cpul5338 0:f21fa73a3db3 34 {
cpul5338 0:f21fa73a3db3 35 }
cpul5338 0:f21fa73a3db3 36 }
cpul5338 0:f21fa73a3db3 37 //****************************************************************************** End of Main
cpul5338 0:f21fa73a3db3 38
cpul5338 0:f21fa73a3db3 39 //****************************************************************************** timer_interrupt
cpul5338 0:f21fa73a3db3 40 void timer_interrupt()
cpul5338 0:f21fa73a3db3 41 {
cpul5338 0:f21fa73a3db3 42 // Code for servo motor
cpul5338 0:f21fa73a3db3 43 }
cpul5338 0:f21fa73a3db3 44 //****************************************************************************** End of timer_interrupt
cpul5338 0:f21fa73a3db3 45
cpul5338 0:f21fa73a3db3 46 //****************************************************************************** init_timer
cpul5338 0:f21fa73a3db3 47 void init_timer()
cpul5338 0:f21fa73a3db3 48 {
cpul5338 0:f21fa73a3db3 49 timer.attach_us(&timer_interrupt, 10000);//10ms interrupt period (100 Hz)
cpul5338 0:f21fa73a3db3 50 }
cpul5338 0:f21fa73a3db3 51 //****************************************************************************** End of init_timer
cpul5338 0:f21fa73a3db3 52
cpul5338 0:f21fa73a3db3 53 //****************************************************************************** init_PWM
cpul5338 0:f21fa73a3db3 54 void init_PWM()
cpul5338 0:f21fa73a3db3 55 {
cpul5338 0:f21fa73a3db3 56 servo.period_ms(Servo_Period);
cpul5338 0:f21fa73a3db3 57 servo.write(servo_duty);
cpul5338 0:f21fa73a3db3 58 }
cpul5338 0:f21fa73a3db3 59 //****************************************************************************** End of init_PWM