PID motor controll for the biorobotics project.

Dependencies:   FastPWM QEI

Dependents:   PID_example Motor_calibration Demo_mode Demo_mode ... more

Revision:
12:2c65bcbebf8a
Parent:
11:34bd7f42c9db
Child:
13:9be1857401f8
diff -r 34bd7f42c9db -r 2c65bcbebf8a motor.cpp
--- a/motor.cpp	Wed Oct 31 10:45:40 2018 +0000
+++ b/motor.cpp	Wed Oct 31 16:19:09 2018 +0000
@@ -10,6 +10,8 @@
     target_angle = 0;
     extra_reduction_ratio = 1.0;
     
+    max_pwm_fraction = 1.0;
+    
     printcount = 0;
     pid_period = 0;
     serial_debugging = false;
@@ -28,6 +30,8 @@
     target_angle = 0;
     extra_reduction_ratio = 1.0;
     
+    max_pwm_fraction = 1.0;
+    
     printcount = 0;
     pid_period = 0;
     this->pc = pc;
@@ -117,7 +121,13 @@
     // either true or false, determines direction (0 or 1)
     dir_out = speed > 0;
     // pwm duty cycle can only be positive, floating point absolute value (if value is >0, the there still will be a positive value).
-    pwm_out = fabs(speed);
+    
+    // Limit the output speed.
+    double requested_pwm_fraction = fabs(speed);
+    if (requested_pwm_fraction > max_pwm_fraction) {
+        requested_pwm_fraction = max_pwm_fraction;
+    }
+    pwm_out = requested_pwm_fraction;
 }
 
 double Motor::encoder_pulses_to_radians(int pulses) {