No.9 Robotics / Mbed 2 deprecated Robotics_Servo_control

Dependencies:   mbed

Fork of Robotics_Lab_Servo by LDSC_Robotics

Committer:
dg0704
Date:
Thu Mar 10 06:32:13 2016 +0000
Revision:
4:facfa2ac9a59
Parent:
3:71a807b38a3e
Child:
5:d495dc55f709
add angle_ref

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);
dg0704 3:71a807b38a3e 11 PwmOut pwm1(PA_8);
dg0704 3:71a807b38a3e 12 PwmOut pwm1n(PB_13);
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
dg0704 4:facfa2ac9a59 20 // timer variables
YCTung 0:564d77fcaa70 21 Ticker timer1;
dg0704 4:facfa2ac9a59 22 int timer1_counter;
YCTung 1:3b6c9baa7d0c 23 void timer1_interrupt(void);
YCTung 0:564d77fcaa70 24
YCTung 1:3b6c9baa7d0c 25 void init_TIMER(void);
YCTung 0:564d77fcaa70 26 void init_IO(void);
YCTung 0:564d77fcaa70 27 void init_PWM(void);
YCTung 1:3b6c9baa7d0c 28 void flash(void);
YCTung 0:564d77fcaa70 29
YCTung 1:3b6c9baa7d0c 30 //Variable(s) for PI controller
YCTung 2:a3c64321e9c2 31 float angle_ref = 0.0; //unit in degree(s), range +-90 degrees
YCTung 2:a3c64321e9c2 32 float angle_read= 0.0;
YCTung 2:a3c64321e9c2 33 float angle_check;
YCTung 0:564d77fcaa70 34 float err = 0.0;
YCTung 0:564d77fcaa70 35 float ierr = 0.0;
YCTung 0:564d77fcaa70 36 float PI_out = 0.0;
YCTung 0:564d77fcaa70 37 float pwm1_duty = 0.5;
dg0704 3:71a807b38a3e 38 float kp = 0.025;
dg0704 3:71a807b38a3e 39 float ki = 0.025;
YCTung 0:564d77fcaa70 40
YCTung 1:3b6c9baa7d0c 41 int main (void)
YCTung 1:3b6c9baa7d0c 42 {
YCTung 1:3b6c9baa7d0c 43 init_IO();
YCTung 1:3b6c9baa7d0c 44 init_PWM();
YCTung 1:3b6c9baa7d0c 45 init_TIMER();
YCTung 1:3b6c9baa7d0c 46 while(1)
YCTung 1:3b6c9baa7d0c 47 {
dg0704 4:facfa2ac9a59 48 if(timer1_counter == 100)
dg0704 4:facfa2ac9a59 49 {
dg0704 4:facfa2ac9a59 50 timer1_counter = 0;
dg0704 4:facfa2ac9a59 51 angle_ref += 15;
dg0704 4:facfa2ac9a59 52 if (angle_ref == 45)
dg0704 4:facfa2ac9a59 53 break;
dg0704 4:facfa2ac9a59 54 }
YCTung 1:3b6c9baa7d0c 55 }
YCTung 1:3b6c9baa7d0c 56 }
YCTung 1:3b6c9baa7d0c 57
YCTung 0:564d77fcaa70 58 void timer1_interrupt(void)
YCTung 0:564d77fcaa70 59 {
dg0704 4:facfa2ac9a59 60 timer1_counter ++;
dg0704 4:facfa2ac9a59 61
YCTung 2:a3c64321e9c2 62 angle_read = (adc.read() - 0.45f) / 0.48f * 180.0f; //0.21 ~ 0.69 respect to -90 ~ +90 degree
YCTung 2:a3c64321e9c2 63 angle_check = angle_read;
YCTung 0:564d77fcaa70 64
YCTung 1:3b6c9baa7d0c 65 //////code for PI control//////
dg0704 3:71a807b38a3e 66 err = angle_ref - angle_read;
dg0704 3:71a807b38a3e 67 ierr = 0.01f/(err-1);
dg0704 3:71a807b38a3e 68 PI_out = kp*err+ki*ierr;
YCTung 2:a3c64321e9c2 69
YCTung 0:564d77fcaa70 70 ////////////
YCTung 0:564d77fcaa70 71 if(PI_out >= 0.5f)PI_out = 0.5;
YCTung 0:564d77fcaa70 72 else if(PI_out <= -0.5f)PI_out = -0.5;
YCTung 0:564d77fcaa70 73 pwm1_duty = PI_out + 0.5f;
YCTung 2:a3c64321e9c2 74 if(angle_check > 100.0f || angle_check < -100.0f)pwm1_duty = 0.5;
YCTung 0:564d77fcaa70 75 pwm1.write(pwm1_duty);
dg0704 3:71a807b38a3e 76 TIM1->CCER |= 0x4; //enable ch1 complementary output
dg0704 3:71a807b38a3e 77
YCTung 0:564d77fcaa70 78 }
YCTung 0:564d77fcaa70 79
YCTung 0:564d77fcaa70 80 void init_TIMER(void)
YCTung 0:564d77fcaa70 81 {
YCTung 0:564d77fcaa70 82 timer1.attach_us(&timer1_interrupt, 10000);//10ms interrupt period (100 Hz)
dg0704 4:facfa2ac9a59 83 timer1_counter = 0;
YCTung 0:564d77fcaa70 84 }
YCTung 0:564d77fcaa70 85
YCTung 0:564d77fcaa70 86 void init_IO(void)
YCTung 0:564d77fcaa70 87 {
dg0704 4:facfa2ac9a59 88 angle_ref = -45;
YCTung 0:564d77fcaa70 89 }
YCTung 0:564d77fcaa70 90
YCTung 0:564d77fcaa70 91 void init_PWM(void)
YCTung 0:564d77fcaa70 92 {
YCTung 0:564d77fcaa70 93 pwm1.period_us(50);
YCTung 0:564d77fcaa70 94 pwm1.write(0.5);
YCTung 0:564d77fcaa70 95 TIM1->CCER |= 0x4;
YCTung 0:564d77fcaa70 96 }
YCTung 0:564d77fcaa70 97
YCTung 0:564d77fcaa70 98 void flash(void)
YCTung 0:564d77fcaa70 99 {
YCTung 0:564d77fcaa70 100 led1 = !led1;
YCTung 0:564d77fcaa70 101 }