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@8:3b077fc3d1ec, 2015-10-08 (annotated)
- Committer:
- Gerth
- Date:
- Thu Oct 08 13:14:29 2015 +0000
- Revision:
- 8:3b077fc3d1ec
- Parent:
- 5:5794dbc42c3d
- Child:
- 10:37bdb3e5f03a
added stop function
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 | 4:e732794d8e7f | 55 | *Constructs a controller | 
| Gerth | 4:e732794d8e7f | 56 | */ | 
| Gerth | 2:a1b6930947a9 | 57 | controlandadjust(void); | 
| Gerth | 4:e732794d8e7f | 58 | |
| Gerth | 3:8e6dacabe898 | 59 | /** P controller | 
| Gerth | 3:8e6dacabe898 | 60 | * @param error1 : float Error from motor 1 | 
| Gerth | 3:8e6dacabe898 | 61 | * @param error2 : float Error from motor 2 | 
| Gerth | 3:8e6dacabe898 | 62 | * @param Kp : float Desired value of Kp for your controller | 
| Gerth | 3:8e6dacabe898 | 63 | * @return void | 
| Gerth | 3:8e6dacabe898 | 64 | */ | 
| Gerth | 0:041a12a5b315 | 65 | void P(float error1, float error2 ,float Kp ); | 
| Gerth | 3:8e6dacabe898 | 66 | |
| Gerth | 3:8e6dacabe898 | 67 | /** PI controller | 
| Gerth | 3:8e6dacabe898 | 68 | * @param error1 : float Error from motor 1 | 
| Gerth | 3:8e6dacabe898 | 69 | * @param error2 : float Error from motor 2 | 
| Gerth | 3:8e6dacabe898 | 70 | * @param Kp : float Desired value of Kp for your controller | 
| Gerth | 3:8e6dacabe898 | 71 | * @param Ki : float Desired value of Ki for your controller | 
| Gerth | 3:8e6dacabe898 | 72 | * @param Ts : float Sampling time of your controller (1/Fs) | 
| Gerth | 3:8e6dacabe898 | 73 | * @param &error1_int : float Error for the integral from motor 1 | 
| Gerth | 3:8e6dacabe898 | 74 | * @param &error2_int : float Error for the integral from motor 2 | 
| Gerth | 3:8e6dacabe898 | 75 | * @return void | 
| Gerth | 3:8e6dacabe898 | 76 | */ | 
| Gerth | 0:041a12a5b315 | 77 | void PI(float error1, float error2, float Kp, float Ki,float Ts, float &error1_int, float &error2_int); | 
| Gerth | 3:8e6dacabe898 | 78 | |
| Gerth | 3:8e6dacabe898 | 79 | /** PID controller | 
| Gerth | 3:8e6dacabe898 | 80 | * @param error1 : float Error from motor 1 | 
| Gerth | 3:8e6dacabe898 | 81 | * @param error2 : float Error from motor 2 | 
| Gerth | 3:8e6dacabe898 | 82 | * @param Kp : float Desired value of Kp for your controller | 
| Gerth | 3:8e6dacabe898 | 83 | * @param Ki : float Desired value of Ki for your controller | 
| Gerth | 3:8e6dacabe898 | 84 | * @param Kd : float Desired value of Kd for your controller | 
| Gerth | 3:8e6dacabe898 | 85 | * @param Ts : float Sampling time of your controller (1/Fs) | 
| Gerth | 3:8e6dacabe898 | 86 | * @param &error1_int : float Error for the integral from motor 1 | 
| Gerth | 3:8e6dacabe898 | 87 | * @param &error2_int : float Error for the integral from motor 2 | 
| Gerth | 3:8e6dacabe898 | 88 | * @param &error1_prev : float Previous error from motor 1 for the derivative | 
| Gerth | 3:8e6dacabe898 | 89 | * @param &error2_prev : float Previous error from motor 2 for the derivative | 
| Gerth | 3:8e6dacabe898 | 90 | * @return void | 
| Gerth | 3:8e6dacabe898 | 91 | */ | 
| Gerth | 0:041a12a5b315 | 92 | void PID(float error1, float error2, float Kp, float Ki,float Kd,float Ts, | 
| Gerth | 1:ece12a295ce3 | 93 | float &error1_int, float &error2_int, float &error1_prev, float &error2_prev); | 
| Gerth | 3:8e6dacabe898 | 94 | |
| Gerth | 3:8e6dacabe898 | 95 | /** Read the PWM signal to motor 1 | 
| Gerth | 3:8e6dacabe898 | 96 | * @return PWM signal to motor 1 | 
| Gerth | 3:8e6dacabe898 | 97 | */ | 
| Gerth | 1:ece12a295ce3 | 98 | float motor1pwm(); | 
| Gerth | 3:8e6dacabe898 | 99 | |
| Gerth | 3:8e6dacabe898 | 100 | /** Read the PWM signal to motor 2 | 
| Gerth | 3:8e6dacabe898 | 101 | * @return PWM signal to motor 2 | 
| Gerth | 3:8e6dacabe898 | 102 | */ | 
| Gerth | 1:ece12a295ce3 | 103 | float motor2pwm(); | 
| Gerth | 8:3b077fc3d1ec | 104 | |
| Gerth | 8:3b077fc3d1ec | 105 | /** STOPS both motors | 
| Gerth | 8:3b077fc3d1ec | 106 | * @return void | 
| Gerth | 8:3b077fc3d1ec | 107 | */ | 
| Gerth | 8:3b077fc3d1ec | 108 | void STOP(); | 
| Gerth | 0:041a12a5b315 | 109 | |
| Gerth | 0:041a12a5b315 | 110 | private: | 
| Gerth | 4:e732794d8e7f | 111 | /** | 
| Gerth | 4:e732794d8e7f | 112 | *Checks the signal and updates the PWM and direction for motor 1 and 2 | 
| Gerth | 4:e732794d8e7f | 113 | */ | 
| Gerth | 1:ece12a295ce3 | 114 | void verwerksignaal(float , float ); | 
| Gerth | 1:ece12a295ce3 | 115 | |
| Gerth | 0:041a12a5b315 | 116 | }; | 
| Gerth | 0:041a12a5b315 | 117 | |
| Gerth | 4:e732794d8e7f | 118 | #endif /* CONTROLANDADJUST_H */ |