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@6:52f6c4d57d74, 2016-03-10 (annotated)
- Committer:
- dg0704
- Date:
- Thu Mar 10 07:26:03 2016 +0000
- Revision:
- 6:52f6c4d57d74
- Parent:
- 5:d495dc55f709
- Child:
- 7:db4ddfc0cf4e
PI
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 | |
| dg0704 | 5:d495dc55f709 | 10 | PwmOut pwm1(D7); |
| dg0704 | 5:d495dc55f709 | 11 | PwmOut pwm1n(D11); |
| YCTung | 0:564d77fcaa70 | 12 | |
| YCTung | 0:564d77fcaa70 | 13 | AnalogIn adc(A2);//Temporary usage |
| YCTung | 0:564d77fcaa70 | 14 | |
| YCTung | 0:564d77fcaa70 | 15 | //LED1 = D13 = PA_5 (LED on Nucleo board) |
| YCTung | 0:564d77fcaa70 | 16 | DigitalOut led1(LED1); |
| YCTung | 0:564d77fcaa70 | 17 | DigitalOut led2(D12); |
| YCTung | 0:564d77fcaa70 | 18 | |
| dg0704 | 4:facfa2ac9a59 | 19 | // timer variables |
| YCTung | 0:564d77fcaa70 | 20 | Ticker timer1; |
| dg0704 | 4:facfa2ac9a59 | 21 | int timer1_counter; |
| YCTung | 1:3b6c9baa7d0c | 22 | void timer1_interrupt(void); |
| YCTung | 0:564d77fcaa70 | 23 | |
| YCTung | 1:3b6c9baa7d0c | 24 | void init_TIMER(void); |
| YCTung | 0:564d77fcaa70 | 25 | void init_IO(void); |
| YCTung | 0:564d77fcaa70 | 26 | void init_PWM(void); |
| YCTung | 1:3b6c9baa7d0c | 27 | void flash(void); |
| YCTung | 0:564d77fcaa70 | 28 | |
| YCTung | 1:3b6c9baa7d0c | 29 | //Variable(s) for PI controller |
| YCTung | 2:a3c64321e9c2 | 30 | float angle_ref = 0.0; //unit in degree(s), range +-90 degrees |
| YCTung | 2:a3c64321e9c2 | 31 | float angle_read= 0.0; |
| YCTung | 2:a3c64321e9c2 | 32 | float angle_check; |
| YCTung | 0:564d77fcaa70 | 33 | float err = 0.0; |
| YCTung | 0:564d77fcaa70 | 34 | float ierr = 0.0; |
| YCTung | 0:564d77fcaa70 | 35 | float PI_out = 0.0; |
| YCTung | 0:564d77fcaa70 | 36 | float pwm1_duty = 0.5; |
| YCTung | 0:564d77fcaa70 | 37 | |
| YCTung | 1:3b6c9baa7d0c | 38 | int main (void) |
| YCTung | 1:3b6c9baa7d0c | 39 | { |
| YCTung | 1:3b6c9baa7d0c | 40 | init_IO(); |
| YCTung | 1:3b6c9baa7d0c | 41 | init_PWM(); |
| YCTung | 1:3b6c9baa7d0c | 42 | init_TIMER(); |
| YCTung | 1:3b6c9baa7d0c | 43 | while(1) |
| YCTung | 1:3b6c9baa7d0c | 44 | { |
| dg0704 | 4:facfa2ac9a59 | 45 | if(timer1_counter == 100) |
| dg0704 | 4:facfa2ac9a59 | 46 | { |
| dg0704 | 4:facfa2ac9a59 | 47 | timer1_counter = 0; |
| dg0704 | 4:facfa2ac9a59 | 48 | angle_ref += 15; |
| dg0704 | 4:facfa2ac9a59 | 49 | if (angle_ref == 45) |
| dg0704 | 4:facfa2ac9a59 | 50 | break; |
| dg0704 | 4:facfa2ac9a59 | 51 | } |
| YCTung | 1:3b6c9baa7d0c | 52 | } |
| YCTung | 1:3b6c9baa7d0c | 53 | } |
| YCTung | 1:3b6c9baa7d0c | 54 | |
| YCTung | 0:564d77fcaa70 | 55 | void timer1_interrupt(void) |
| YCTung | 0:564d77fcaa70 | 56 | { |
| dg0704 | 4:facfa2ac9a59 | 57 | timer1_counter ++; |
| dg0704 | 4:facfa2ac9a59 | 58 | |
| YCTung | 2:a3c64321e9c2 | 59 | angle_read = (adc.read() - 0.45f) / 0.48f * 180.0f; //0.21 ~ 0.69 respect to -90 ~ +90 degree |
| YCTung | 2:a3c64321e9c2 | 60 | angle_check = angle_read; |
| YCTung | 0:564d77fcaa70 | 61 | |
| YCTung | 1:3b6c9baa7d0c | 62 | //////code for PI control////// |
| dg0704 | 3:71a807b38a3e | 63 | err = angle_ref - angle_read; |
| dg0704 | 6:52f6c4d57d74 | 64 | ierr += err; |
| dg0704 | 6:52f6c4d57d74 | 65 | PI_out = Kp * err + Ki * 0.01f*(ierr - err); |
| YCTung | 2:a3c64321e9c2 | 66 | |
| YCTung | 0:564d77fcaa70 | 67 | //////////// |
| YCTung | 0:564d77fcaa70 | 68 | if(PI_out >= 0.5f)PI_out = 0.5; |
| YCTung | 0:564d77fcaa70 | 69 | else if(PI_out <= -0.5f)PI_out = -0.5; |
| YCTung | 0:564d77fcaa70 | 70 | pwm1_duty = PI_out + 0.5f; |
| YCTung | 2:a3c64321e9c2 | 71 | if(angle_check > 100.0f || angle_check < -100.0f)pwm1_duty = 0.5; |
| YCTung | 0:564d77fcaa70 | 72 | pwm1.write(pwm1_duty); |
| dg0704 | 3:71a807b38a3e | 73 | TIM1->CCER |= 0x4; //enable ch1 complementary output |
| dg0704 | 3:71a807b38a3e | 74 | |
| YCTung | 0:564d77fcaa70 | 75 | } |
| YCTung | 0:564d77fcaa70 | 76 | |
| YCTung | 0:564d77fcaa70 | 77 | void init_TIMER(void) |
| YCTung | 0:564d77fcaa70 | 78 | { |
| YCTung | 0:564d77fcaa70 | 79 | timer1.attach_us(&timer1_interrupt, 10000);//10ms interrupt period (100 Hz) |
| dg0704 | 4:facfa2ac9a59 | 80 | timer1_counter = 0; |
| YCTung | 0:564d77fcaa70 | 81 | } |
| YCTung | 0:564d77fcaa70 | 82 | |
| YCTung | 0:564d77fcaa70 | 83 | void init_IO(void) |
| YCTung | 0:564d77fcaa70 | 84 | { |
| dg0704 | 4:facfa2ac9a59 | 85 | angle_ref = -45; |
| YCTung | 0:564d77fcaa70 | 86 | } |
| YCTung | 0:564d77fcaa70 | 87 | |
| YCTung | 0:564d77fcaa70 | 88 | void init_PWM(void) |
| YCTung | 0:564d77fcaa70 | 89 | { |
| YCTung | 0:564d77fcaa70 | 90 | pwm1.period_us(50); |
| YCTung | 0:564d77fcaa70 | 91 | pwm1.write(0.5); |
| YCTung | 0:564d77fcaa70 | 92 | TIM1->CCER |= 0x4; |
| YCTung | 0:564d77fcaa70 | 93 | } |
| YCTung | 0:564d77fcaa70 | 94 | |
| YCTung | 0:564d77fcaa70 | 95 | void flash(void) |
| YCTung | 0:564d77fcaa70 | 96 | { |
| YCTung | 0:564d77fcaa70 | 97 | led1 = !led1; |
| YCTung | 0:564d77fcaa70 | 98 | } |
