No.9 Robotics / Mbed 2 deprecated Robotics_Servo_control

Dependencies:   mbed

Fork of Robotics_Lab_Servo by LDSC_Robotics

Committer:
YCTung
Date:
Thu Mar 03 15:04:40 2016 +0000
Revision:
2:a3c64321e9c2
Parent:
1:3b6c9baa7d0c
Child:
3:71a807b38a3e
Robotics_Lab_Servo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
YCTung 0:564d77fcaa70 1 /*LAB_SERVO*/
YCTung 0:564d77fcaa70 2 #include "mbed.h"
YCTung 0:564d77fcaa70 3
YCTung 0:564d77fcaa70 4 //The number will be compiled as type "double" in default
YCTung 0:564d77fcaa70 5 //Add a "f" after the number can make it compiled as type "float"
YCTung 1:3b6c9baa7d0c 6 #define Ts 0.01f //period of timer1 (s)
YCTung 2:a3c64321e9c2 7 #define Kp 0.053f
YCTung 2:a3c64321e9c2 8 #define Ki 0.013f
YCTung 0:564d77fcaa70 9
YCTung 0:564d77fcaa70 10 PwmOut servo(A0);
YCTung 0:564d77fcaa70 11 PwmOut pwm1(D7);
YCTung 0:564d77fcaa70 12 PwmOut pwm1n(D11);
YCTung 0:564d77fcaa70 13
YCTung 0:564d77fcaa70 14 AnalogIn adc(A2);//Temporary usage
YCTung 0:564d77fcaa70 15
YCTung 0:564d77fcaa70 16 //LED1 = D13 = PA_5 (LED on Nucleo board)
YCTung 0:564d77fcaa70 17 DigitalOut led1(LED1);
YCTung 0:564d77fcaa70 18 DigitalOut led2(D12);
YCTung 0:564d77fcaa70 19
YCTung 0:564d77fcaa70 20 Ticker timer1;
YCTung 1:3b6c9baa7d0c 21 void timer1_interrupt(void);
YCTung 0:564d77fcaa70 22
YCTung 1:3b6c9baa7d0c 23 void init_TIMER(void);
YCTung 0:564d77fcaa70 24 void init_IO(void);
YCTung 0:564d77fcaa70 25 void init_PWM(void);
YCTung 1:3b6c9baa7d0c 26 void flash(void);
YCTung 0:564d77fcaa70 27
YCTung 1:3b6c9baa7d0c 28 //Variable(s) for PI controller
YCTung 2:a3c64321e9c2 29 float angle_ref = 0.0; //unit in degree(s), range +-90 degrees
YCTung 2:a3c64321e9c2 30 float angle_read= 0.0;
YCTung 2:a3c64321e9c2 31 float angle_check;
YCTung 0:564d77fcaa70 32 float err = 0.0;
YCTung 0:564d77fcaa70 33 float ierr = 0.0;
YCTung 0:564d77fcaa70 34 float PI_out = 0.0;
YCTung 0:564d77fcaa70 35 float pwm1_duty = 0.5;
YCTung 1:3b6c9baa7d0c 36
YCTung 1:3b6c9baa7d0c 37 //Variable(s) for internal control
YCTung 2:a3c64321e9c2 38 float servo_duty = 0.079;//0.079 +(0.084/180)*angle, -90<angle<90
YCTung 0:564d77fcaa70 39
YCTung 1:3b6c9baa7d0c 40 int main (void)
YCTung 1:3b6c9baa7d0c 41 {
YCTung 1:3b6c9baa7d0c 42 init_IO();
YCTung 1:3b6c9baa7d0c 43 init_PWM();
YCTung 1:3b6c9baa7d0c 44 init_TIMER();
YCTung 1:3b6c9baa7d0c 45 while(1)
YCTung 1:3b6c9baa7d0c 46 {
YCTung 1:3b6c9baa7d0c 47 ;
YCTung 1:3b6c9baa7d0c 48 }
YCTung 1:3b6c9baa7d0c 49 }
YCTung 1:3b6c9baa7d0c 50
YCTung 0:564d77fcaa70 51 void timer1_interrupt(void)
YCTung 0:564d77fcaa70 52 {
YCTung 2:a3c64321e9c2 53 angle_read = (adc.read() - 0.45f) / 0.48f * 180.0f; //0.21 ~ 0.69 respect to -90 ~ +90 degree
YCTung 2:a3c64321e9c2 54 angle_check = angle_read;
YCTung 0:564d77fcaa70 55
YCTung 1:3b6c9baa7d0c 56 //////code for PI control//////
YCTung 0:564d77fcaa70 57
YCTung 0:564d77fcaa70 58
YCTung 0:564d77fcaa70 59
YCTung 0:564d77fcaa70 60
YCTung 0:564d77fcaa70 61
YCTung 2:a3c64321e9c2 62
YCTung 0:564d77fcaa70 63 ////////////
YCTung 0:564d77fcaa70 64 if(PI_out >= 0.5f)PI_out = 0.5;
YCTung 0:564d77fcaa70 65 else if(PI_out <= -0.5f)PI_out = -0.5;
YCTung 0:564d77fcaa70 66 pwm1_duty = PI_out + 0.5f;
YCTung 2:a3c64321e9c2 67 if(angle_check > 100.0f || angle_check < -100.0f)pwm1_duty = 0.5;
YCTung 0:564d77fcaa70 68 pwm1.write(pwm1_duty);
YCTung 0:564d77fcaa70 69 TIM1->CCER |= 0x4;
YCTung 0:564d77fcaa70 70
YCTung 1:3b6c9baa7d0c 71 //////code for internal control//////
YCTung 0:564d77fcaa70 72
YCTung 0:564d77fcaa70 73
YCTung 0:564d77fcaa70 74
YCTung 0:564d77fcaa70 75
YCTung 0:564d77fcaa70 76
YCTung 0:564d77fcaa70 77 ////////////
YCTung 0:564d77fcaa70 78 if(servo_duty >= 0.121f)servo_duty = 0.121;
YCTung 0:564d77fcaa70 79 else if(servo_duty <= 0.037f)servo_duty = 0.037;
YCTung 0:564d77fcaa70 80 servo.write(servo_duty);
YCTung 0:564d77fcaa70 81 }
YCTung 0:564d77fcaa70 82
YCTung 0:564d77fcaa70 83 void init_TIMER(void)
YCTung 0:564d77fcaa70 84 {
YCTung 0:564d77fcaa70 85 timer1.attach_us(&timer1_interrupt, 10000);//10ms interrupt period (100 Hz)
YCTung 0:564d77fcaa70 86 }
YCTung 0:564d77fcaa70 87
YCTung 0:564d77fcaa70 88 void init_IO(void)
YCTung 0:564d77fcaa70 89 {
YCTung 0:564d77fcaa70 90 led1 = 0;
YCTung 0:564d77fcaa70 91 led2 = 1;
YCTung 0:564d77fcaa70 92 }
YCTung 0:564d77fcaa70 93
YCTung 0:564d77fcaa70 94 void init_PWM(void)
YCTung 0:564d77fcaa70 95 {
YCTung 0:564d77fcaa70 96 servo.period_ms(20);
YCTung 0:564d77fcaa70 97 servo.write(servo_duty);
YCTung 0:564d77fcaa70 98 pwm1.period_us(50);
YCTung 0:564d77fcaa70 99 pwm1.write(0.5);
YCTung 0:564d77fcaa70 100 TIM1->CCER |= 0x4;
YCTung 0:564d77fcaa70 101 }
YCTung 0:564d77fcaa70 102
YCTung 0:564d77fcaa70 103 void flash(void)
YCTung 0:564d77fcaa70 104 {
YCTung 0:564d77fcaa70 105 led1 = !led1;
YCTung 0:564d77fcaa70 106 }