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
Revision 3:71a807b38a3e, committed 2016-03-10
- Comitter:
- dg0704
- Date:
- Thu Mar 10 06:22:25 2016 +0000
- Parent:
- 2:a3c64321e9c2
- Child:
- 4:facfa2ac9a59
- Commit message:
- add Kp and Ki;
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Thu Mar 03 15:04:40 2016 +0000
+++ b/main.cpp Thu Mar 10 06:22:25 2016 +0000
@@ -8,8 +8,8 @@
#define Ki 0.013f
PwmOut servo(A0);
-PwmOut pwm1(D7);
-PwmOut pwm1n(D11);
+PwmOut pwm1(PA_8);
+PwmOut pwm1n(PB_13);
AnalogIn adc(A2);//Temporary usage
@@ -33,9 +33,8 @@
float ierr = 0.0;
float PI_out = 0.0;
float pwm1_duty = 0.5;
-
-//Variable(s) for internal control
-float servo_duty = 0.079;//0.079 +(0.084/180)*angle, -90<angle<90
+float kp = 0.025;
+float ki = 0.025;
int main (void)
{
@@ -54,11 +53,9 @@
angle_check = angle_read;
//////code for PI control//////
-
-
-
-
-
+ err = angle_ref - angle_read;
+ ierr = 0.01f/(err-1);
+ PI_out = kp*err+ki*ierr;
////////////
if(PI_out >= 0.5f)PI_out = 0.5;
@@ -66,18 +63,8 @@
pwm1_duty = PI_out + 0.5f;
if(angle_check > 100.0f || angle_check < -100.0f)pwm1_duty = 0.5;
pwm1.write(pwm1_duty);
- TIM1->CCER |= 0x4;
-
- //////code for internal control//////
-
-
-
-
-
- ////////////
- if(servo_duty >= 0.121f)servo_duty = 0.121;
- else if(servo_duty <= 0.037f)servo_duty = 0.037;
- servo.write(servo_duty);
+ TIM1->CCER |= 0x4; //enable ch1 complementary output
+
}
void init_TIMER(void)
@@ -93,8 +80,6 @@
void init_PWM(void)
{
- servo.period_ms(20);
- servo.write(servo_duty);
pwm1.period_us(50);
pwm1.write(0.5);
TIM1->CCER |= 0x4;
