Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of Robotics_Lab_Servo by
main.cpp@3:71a807b38a3e, 2016-03-10 (annotated)
- Committer:
- dg0704
- Date:
- Thu Mar 10 06:22:25 2016 +0000
- Revision:
- 3:71a807b38a3e
- Parent:
- 2:a3c64321e9c2
- Child:
- 4:facfa2ac9a59
add Kp and Ki;
Who changed what in which revision?
| User | Revision | Line number | New 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 | |
| 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; |
| dg0704 | 3:71a807b38a3e | 36 | float kp = 0.025; |
| dg0704 | 3:71a807b38a3e | 37 | float ki = 0.025; |
| YCTung | 0:564d77fcaa70 | 38 | |
| YCTung | 1:3b6c9baa7d0c | 39 | int main (void) |
| YCTung | 1:3b6c9baa7d0c | 40 | { |
| YCTung | 1:3b6c9baa7d0c | 41 | init_IO(); |
| YCTung | 1:3b6c9baa7d0c | 42 | init_PWM(); |
| YCTung | 1:3b6c9baa7d0c | 43 | init_TIMER(); |
| YCTung | 1:3b6c9baa7d0c | 44 | while(1) |
| YCTung | 1:3b6c9baa7d0c | 45 | { |
| YCTung | 1:3b6c9baa7d0c | 46 | ; |
| YCTung | 1:3b6c9baa7d0c | 47 | } |
| YCTung | 1:3b6c9baa7d0c | 48 | } |
| YCTung | 1:3b6c9baa7d0c | 49 | |
| YCTung | 0:564d77fcaa70 | 50 | void timer1_interrupt(void) |
| YCTung | 0:564d77fcaa70 | 51 | { |
| YCTung | 2:a3c64321e9c2 | 52 | angle_read = (adc.read() - 0.45f) / 0.48f * 180.0f; //0.21 ~ 0.69 respect to -90 ~ +90 degree |
| YCTung | 2:a3c64321e9c2 | 53 | angle_check = angle_read; |
| YCTung | 0:564d77fcaa70 | 54 | |
| YCTung | 1:3b6c9baa7d0c | 55 | //////code for PI control////// |
| dg0704 | 3:71a807b38a3e | 56 | err = angle_ref - angle_read; |
| dg0704 | 3:71a807b38a3e | 57 | ierr = 0.01f/(err-1); |
| dg0704 | 3:71a807b38a3e | 58 | PI_out = kp*err+ki*ierr; |
| YCTung | 2:a3c64321e9c2 | 59 | |
| YCTung | 0:564d77fcaa70 | 60 | //////////// |
| YCTung | 0:564d77fcaa70 | 61 | if(PI_out >= 0.5f)PI_out = 0.5; |
| YCTung | 0:564d77fcaa70 | 62 | else if(PI_out <= -0.5f)PI_out = -0.5; |
| YCTung | 0:564d77fcaa70 | 63 | pwm1_duty = PI_out + 0.5f; |
| YCTung | 2:a3c64321e9c2 | 64 | if(angle_check > 100.0f || angle_check < -100.0f)pwm1_duty = 0.5; |
| YCTung | 0:564d77fcaa70 | 65 | pwm1.write(pwm1_duty); |
| dg0704 | 3:71a807b38a3e | 66 | TIM1->CCER |= 0x4; //enable ch1 complementary output |
| dg0704 | 3:71a807b38a3e | 67 | |
| YCTung | 0:564d77fcaa70 | 68 | } |
| YCTung | 0:564d77fcaa70 | 69 | |
| YCTung | 0:564d77fcaa70 | 70 | void init_TIMER(void) |
| YCTung | 0:564d77fcaa70 | 71 | { |
| YCTung | 0:564d77fcaa70 | 72 | timer1.attach_us(&timer1_interrupt, 10000);//10ms interrupt period (100 Hz) |
| YCTung | 0:564d77fcaa70 | 73 | } |
| YCTung | 0:564d77fcaa70 | 74 | |
| YCTung | 0:564d77fcaa70 | 75 | void init_IO(void) |
| YCTung | 0:564d77fcaa70 | 76 | { |
| YCTung | 0:564d77fcaa70 | 77 | led1 = 0; |
| YCTung | 0:564d77fcaa70 | 78 | led2 = 1; |
| YCTung | 0:564d77fcaa70 | 79 | } |
| YCTung | 0:564d77fcaa70 | 80 | |
| YCTung | 0:564d77fcaa70 | 81 | void init_PWM(void) |
| YCTung | 0:564d77fcaa70 | 82 | { |
| YCTung | 0:564d77fcaa70 | 83 | pwm1.period_us(50); |
| YCTung | 0:564d77fcaa70 | 84 | pwm1.write(0.5); |
| YCTung | 0:564d77fcaa70 | 85 | TIM1->CCER |= 0x4; |
| YCTung | 0:564d77fcaa70 | 86 | } |
| YCTung | 0:564d77fcaa70 | 87 | |
| YCTung | 0:564d77fcaa70 | 88 | void flash(void) |
| YCTung | 0:564d77fcaa70 | 89 | { |
| YCTung | 0:564d77fcaa70 | 90 | led1 = !led1; |
| YCTung | 0:564d77fcaa70 | 91 | } |
