PD Control for KL05Z

Dependencies:   QEI mbed

Files at this revision

API Documentation at this revision

Comitter:
abuchan
Date:
Thu Nov 19 17:39:20 2015 +0000
Parent:
4:d52e31c73e50
Commit message:
Downloading bin file seems to be working, PD control working

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Nov 19 00:02:49 2015 +0000
+++ b/main.cpp	Thu Nov 19 17:39:20 2015 +0000
@@ -15,16 +15,15 @@
 float p_error = 0;
 float old_error = 0;
 float goal_position = 0;
-float p_gain = 1;
+float p_gain = 1.0;
 float d_gain = 0;
-float sample_period = .01;
+float sample_period = 0.01;
+float pwm_period = 0.0002;
 float command = 0;
 
-
 PwmOut Motor = PTB13;       //h-bridge pwm pins
 DigitalOut dir1 = PTA0;    //h-bridge direction 1 pins
 DigitalOut dir2 = PTA8; 
-//
 
 float pulsesToRadians(float pulses) //Encoder steps to revolutions of output shaft in radians
 {
@@ -48,6 +47,7 @@
         dir2.write(0);
     }
 }
+
 float updateCommand(void){
     
     position = pulsesToRadians(encoder.getPulses());        //use the getPulses functin to read encoder position, convert value to radians
@@ -57,28 +57,40 @@
     
 }
 
-
-
 DigitalOut led1(LED1);
 DigitalOut led2(LED2);
 
 int main() {
+    led1 = 1;
+    led2 = 1;
+    //led3 = 1;
+    
+    wait(0.5);
     led1 = 0;
     led2 = 1;
+    
     Timer timer;
     timer.start();
-    Motor.period(.00005);
-    //SampleFrequency.attach(&updateCommand, sample_period);        //Encoder will be sampled at 2.5 kHz
+    
+    Motor.period(pwm_period);
+    
+    wait(0.5);
+    led1 = 1;
+    led2 = 0;
+    
+    bool state = false;
+    long count = 0;
     
     while(1) {
-    
-           if (timer.read() > .0004){
-                command = updateCommand();
-                timer.reset();
-
-           }
-           signal_to_hbridge(command);
-     
-        }
+         if (timer.read() > 0.0004) {
+              command = updateCommand();
+              timer.reset();
+              if(++count == 250) {
+                  count = 0;
+                  state = !state;
+                  led2 = state;
+              }
+         }
+         signal_to_hbridge(command);
     }
-
+}
\ No newline at end of file