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.
Dependents: buttoncontrol includeair includeair Oudverslag
controlandadjust.h@15:a1a29db96f4f, 2015-10-20 (annotated)
- Committer:
- Gerth
- Date:
- Tue Oct 20 15:14:14 2015 +0000
- Revision:
- 15:a1a29db96f4f
- Parent:
- 13:a128fd574295
- Child:
- 17:666505754e3f
added an errorband, if the error is within this band, the signal to the motors = 0
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Gerth | 4:e732794d8e7f | 1 | /** |
| Gerth | 4:e732794d8e7f | 2 | * @author Jasper Gerth |
| Gerth | 4:e732794d8e7f | 3 | * |
| Gerth | 4:e732794d8e7f | 4 | * |
| Gerth | 4:e732794d8e7f | 5 | * @section DESCRIPTION |
| Gerth | 4:e732794d8e7f | 6 | * |
| Gerth | 4:e732794d8e7f | 7 | * This library contains a P-, PI-, and a PID-controller. |
| Gerth | 4:e732794d8e7f | 8 | * The controller takes controller consta |
| Gerth | 4:e732794d8e7f | 9 | */ |
| Gerth | 4:e732794d8e7f | 10 | |
| Gerth | 5:5794dbc42c3d | 11 | #ifndef _CONTROLANDADJUST_H_ |
| Gerth | 5:5794dbc42c3d | 12 | #define _CONTROLANDADJUST_H_ |
| Gerth | 3:8e6dacabe898 | 13 | /** |
| Gerth | 3:8e6dacabe898 | 14 | * Includes |
| Gerth | 3:8e6dacabe898 | 15 | */ |
| Gerth | 0:041a12a5b315 | 16 | #include "mbed.h" |
| Gerth | 0:041a12a5b315 | 17 | |
| Gerth | 1:ece12a295ce3 | 18 | /** A library with a P-, PI- and PID-controller, useful for the module biorobotics |
| Gerth | 3:8e6dacabe898 | 19 | * Example |
| Gerth | 3:8e6dacabe898 | 20 | * @code |
| Gerth | 3:8e6dacabe898 | 21 | * #include "mbed.h" |
| Gerth | 3:8e6dacabe898 | 22 | * #include "controlandadjust.h" |
| Gerth | 3:8e6dacabe898 | 23 | * #include "QEI.h" //This is found at https://developer.mbed.org/users/aberk/code/QEI/ |
| Gerth | 3:8e6dacabe898 | 24 | * |
| Gerth | 3:8e6dacabe898 | 25 | * QEI encoder1 (D13, D12, NC, 32); //Encoder with X2 encoding and 32 counts per revolution |
| Gerth | 3:8e6dacabe898 | 26 | * QEI encoder2 (D111, D10, NC, 32); //Encoder with X2 encoding and 32 counts per revolution |
| Gerth | 3:8e6dacabe898 | 27 | * |
| Gerth | 3:8e6dacabe898 | 28 | * controlandadjust mycontroller; //NO () AT THE END!! |
| Gerth | 3:8e6dacabe898 | 29 | * const float kp=0.5; |
| Gerth | 3:8e6dacabe898 | 30 | * AnalogIn pot(A0); |
| Gerth | 3:8e6dacabe898 | 31 | * |
| Gerth | 3:8e6dacabe898 | 32 | * int main(){ |
| Gerth | 3:8e6dacabe898 | 33 | * while(1){ |
| Gerth | 3:8e6dacabe898 | 34 | * float error1=(2*PI*pot.read()-counttorad*encoder1.getPulses()); //calculate error motor 1 |
| Gerth | 3:8e6dacabe898 | 35 | * float error2=(2*PI*pot.read()-counttorad*encoder2.getPulses()); //calculate error motor 2 |
| Gerth | 3:8e6dacabe898 | 36 | * |
| Gerth | 3:8e6dacabe898 | 37 | * controller.P(error1,error2,kp); //controll with p controller |
| Gerth | 3:8e6dacabe898 | 38 | * }; |
| Gerth | 3:8e6dacabe898 | 39 | * return 0; |
| Gerth | 3:8e6dacabe898 | 40 | * } |
| Gerth | 3:8e6dacabe898 | 41 | * @endcode |
| Gerth | 1:ece12a295ce3 | 42 | */ |
| Gerth | 3:8e6dacabe898 | 43 | |
| Gerth | 3:8e6dacabe898 | 44 | /** |
| Gerth | 3:8e6dacabe898 | 45 | * Controller for biorobotics |
| Gerth | 3:8e6dacabe898 | 46 | */ |
| Gerth | 0:041a12a5b315 | 47 | class controlandadjust |
| Gerth | 0:041a12a5b315 | 48 | { |
| Gerth | 4:e732794d8e7f | 49 | |
| Gerth | 0:041a12a5b315 | 50 | public: |
| Gerth | 4:e732794d8e7f | 51 | |
| Gerth | 4:e732794d8e7f | 52 | /** |
| Gerth | 4:e732794d8e7f | 53 | * Constructor. |
| Gerth | 4:e732794d8e7f | 54 | * |
| Gerth | 13:a128fd574295 | 55 | *Constructs a controller no don't put () after your controller |
| Gerth | 15:a1a29db96f4f | 56 | *@param errorband : float Width of the band at of allowable error |
| Gerth | 4:e732794d8e7f | 57 | */ |
| Gerth | 15:a1a29db96f4f | 58 | controlandadjust(float errorband); |
| Gerth | 4:e732794d8e7f | 59 | |
| Gerth | 3:8e6dacabe898 | 60 | /** P controller |
| Gerth | 3:8e6dacabe898 | 61 | * @param error1 : float Error from motor 1 |
| Gerth | 3:8e6dacabe898 | 62 | * @param error2 : float Error from motor 2 |
| Gerth | 3:8e6dacabe898 | 63 | * @param Kp : float Desired value of Kp for your controller |
| Gerth | 3:8e6dacabe898 | 64 | * @return void |
| Gerth | 3:8e6dacabe898 | 65 | */ |
| Gerth | 0:041a12a5b315 | 66 | void P(float error1, float error2 ,float Kp ); |
| Gerth | 3:8e6dacabe898 | 67 | |
| Gerth | 3:8e6dacabe898 | 68 | /** PI controller |
| Gerth | 3:8e6dacabe898 | 69 | * @param error1 : float Error from motor 1 |
| Gerth | 3:8e6dacabe898 | 70 | * @param error2 : float Error from motor 2 |
| Gerth | 3:8e6dacabe898 | 71 | * @param Kp : float Desired value of Kp for your controller |
| Gerth | 3:8e6dacabe898 | 72 | * @param Ki : float Desired value of Ki for your controller |
| Gerth | 3:8e6dacabe898 | 73 | * @param Ts : float Sampling time of your controller (1/Fs) |
| Gerth | 3:8e6dacabe898 | 74 | * @param &error1_int : float Error for the integral from motor 1 |
| Gerth | 3:8e6dacabe898 | 75 | * @param &error2_int : float Error for the integral from motor 2 |
| Gerth | 3:8e6dacabe898 | 76 | * @return void |
| Gerth | 3:8e6dacabe898 | 77 | */ |
| Gerth | 0:041a12a5b315 | 78 | void PI(float error1, float error2, float Kp, float Ki,float Ts, float &error1_int, float &error2_int); |
| Gerth | 3:8e6dacabe898 | 79 | |
| Gerth | 3:8e6dacabe898 | 80 | /** PID controller |
| Gerth | 3:8e6dacabe898 | 81 | * @param error1 : float Error from motor 1 |
| Gerth | 3:8e6dacabe898 | 82 | * @param error2 : float Error from motor 2 |
| Gerth | 3:8e6dacabe898 | 83 | * @param Kp : float Desired value of Kp for your controller |
| Gerth | 3:8e6dacabe898 | 84 | * @param Ki : float Desired value of Ki for your controller |
| Gerth | 3:8e6dacabe898 | 85 | * @param Kd : float Desired value of Kd for your controller |
| Gerth | 3:8e6dacabe898 | 86 | * @param Ts : float Sampling time of your controller (1/Fs) |
| Gerth | 3:8e6dacabe898 | 87 | * @param &error1_int : float Error for the integral from motor 1 |
| Gerth | 3:8e6dacabe898 | 88 | * @param &error2_int : float Error for the integral from motor 2 |
| Gerth | 3:8e6dacabe898 | 89 | * @param &error1_prev : float Previous error from motor 1 for the derivative |
| Gerth | 3:8e6dacabe898 | 90 | * @param &error2_prev : float Previous error from motor 2 for the derivative |
| Gerth | 3:8e6dacabe898 | 91 | * @return void |
| Gerth | 3:8e6dacabe898 | 92 | */ |
| Gerth | 0:041a12a5b315 | 93 | void PID(float error1, float error2, float Kp, float Ki,float Kd,float Ts, |
| Gerth | 1:ece12a295ce3 | 94 | float &error1_int, float &error2_int, float &error1_prev, float &error2_prev); |
| Gerth | 3:8e6dacabe898 | 95 | |
| Gerth | 3:8e6dacabe898 | 96 | /** Read the PWM signal to motor 1 |
| Gerth | 3:8e6dacabe898 | 97 | * @return PWM signal to motor 1 |
| Gerth | 3:8e6dacabe898 | 98 | */ |
| Gerth | 1:ece12a295ce3 | 99 | float motor1pwm(); |
| Gerth | 3:8e6dacabe898 | 100 | |
| Gerth | 3:8e6dacabe898 | 101 | /** Read the PWM signal to motor 2 |
| Gerth | 3:8e6dacabe898 | 102 | * @return PWM signal to motor 2 |
| Gerth | 3:8e6dacabe898 | 103 | */ |
| Gerth | 1:ece12a295ce3 | 104 | float motor2pwm(); |
| Gerth | 8:3b077fc3d1ec | 105 | |
| Gerth | 8:3b077fc3d1ec | 106 | /** STOPS both motors |
| Gerth | 8:3b077fc3d1ec | 107 | * @return void |
| Gerth | 8:3b077fc3d1ec | 108 | */ |
| Gerth | 8:3b077fc3d1ec | 109 | void STOP(); |
| Gerth | 11:ef8d28e010a2 | 110 | |
| Gerth | 11:ef8d28e010a2 | 111 | /** set safety cutoff value for the pwm signal to the motors |
| Gerth | 11:ef8d28e010a2 | 112 | * @return void |
| Gerth | 11:ef8d28e010a2 | 113 | */ |
| Gerth | 11:ef8d28e010a2 | 114 | void cutoff(float maxvalue); |
| Gerth | 0:041a12a5b315 | 115 | |
| Gerth | 0:041a12a5b315 | 116 | private: |
| Gerth | 4:e732794d8e7f | 117 | /** |
| Gerth | 4:e732794d8e7f | 118 | *Checks the signal and updates the PWM and direction for motor 1 and 2 |
| Gerth | 4:e732794d8e7f | 119 | */ |
| Gerth | 1:ece12a295ce3 | 120 | void verwerksignaal(float , float ); |
| Gerth | 1:ece12a295ce3 | 121 | |
| Gerth | 0:041a12a5b315 | 122 | }; |
| Gerth | 0:041a12a5b315 | 123 | |
| Gerth | 4:e732794d8e7f | 124 | #endif /* CONTROLANDADJUST_H */ |