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: Encoder MODSERIAL mbed
Fork of P_controller_motor_Bouke by
main.cpp@10:1d8b21515ed1, 2015-09-17 (annotated)
- Committer:
- ThomasBNL
- Date:
- Thu Sep 17 12:53:08 2015 +0000
- Revision:
- 10:1d8b21515ed1
- Parent:
- 9:3278558361a5
- Child:
- 11:c5befe79aa53
hidscope + P controller motor - scope set werkt niet verder geen errors (niet getest nog);
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ThomasBNL | 0:843492f4fe62 | 1 | #include "mbed.h" |
| ThomasBNL | 0:843492f4fe62 | 2 | #include "HIDScope.h" |
| ThomasBNL | 3:eee8d5461256 | 3 | #include "encoder.h" |
| ThomasBNL | 0:843492f4fe62 | 4 | |
| ThomasBNL | 1:b0d3c64bd4d8 | 5 | // Define the HIDScope and Ticker object |
| ThomasBNL | 10:1d8b21515ed1 | 6 | HIDScope scope(2); |
| ThomasBNL | 10:1d8b21515ed1 | 7 | Ticker scopeTimer; |
| ThomasBNL | 10:1d8b21515ed1 | 8 | Ticker motorP; |
| ThomasBNL | 4:dfdfcb518e60 | 9 | |
| ThomasBNL | 4:dfdfcb518e60 | 10 | // Define Encoder |
| ThomasBNL | 4:dfdfcb518e60 | 11 | |
| ThomasBNL | 6:34b39eb3dcb6 | 12 | Encoder encoder1(PTD0,PTC9,true); |
| ThomasBNL | 4:dfdfcb518e60 | 13 | Serial pc(USBTX,USBRX); |
| ThomasBNL | 4:dfdfcb518e60 | 14 | |
| ThomasBNL | 4:dfdfcb518e60 | 15 | // Define Potmeter |
| ThomasBNL | 5:8ea7a765c1f7 | 16 | AnalogIn potmeter1(PTB0); |
| ThomasBNL | 4:dfdfcb518e60 | 17 | |
| ThomasBNL | 1:b0d3c64bd4d8 | 18 | ////// P Motor Controller |
| ThomasBNL | 0:843492f4fe62 | 19 | |
| ThomasBNL | 0:843492f4fe62 | 20 | // Controller gain |
| ThomasBNL | 5:8ea7a765c1f7 | 21 | const double motorP_Kp=2.500 ; |
| ThomasBNL | 5:8ea7a765c1f7 | 22 | const double Convert_volt_to_position=0.00300 ; |
| ThomasBNL | 0:843492f4fe62 | 23 | |
| ThomasBNL | 0:843492f4fe62 | 24 | // Reusable P controller (FUNCTIE) |
| ThomasBNL | 0:843492f4fe62 | 25 | double P (double error, const double Kp) |
| ThomasBNL | 0:843492f4fe62 | 26 | { |
| ThomasBNL | 3:eee8d5461256 | 27 | return Kp*error; |
| ThomasBNL | 0:843492f4fe62 | 28 | } |
| ThomasBNL | 3:eee8d5461256 | 29 | |
| ThomasBNL | 6:34b39eb3dcb6 | 30 | |
| ThomasBNL | 0:843492f4fe62 | 31 | // Error measurement and apply the output to the plant |
| ThomasBNL | 9:3278558361a5 | 32 | void motorP_Controller() |
| ThomasBNL | 0:843492f4fe62 | 33 | { |
| ThomasBNL | 0:843492f4fe62 | 34 | double reference_position = potmeter1.read(); |
| ThomasBNL | 6:34b39eb3dcb6 | 35 | double position = Convert_volt_to_position*encoder1.getPosition(); |
| ThomasBNL | 9:3278558361a5 | 36 | double motorP = P(reference_position-position, motorP_Kp); |
| ThomasBNL | 0:843492f4fe62 | 37 | } |
| ThomasBNL | 0:843492f4fe62 | 38 | |
| ThomasBNL | 0:843492f4fe62 | 39 | int main() |
| ThomasBNL | 0:843492f4fe62 | 40 | { |
| ThomasBNL | 9:3278558361a5 | 41 | motorP.attach(&motorP_Controller, 0.01f); //100Hz |
| ThomasBNL | 0:843492f4fe62 | 42 | while(1){} |
| ThomasBNL | 0:843492f4fe62 | 43 | } |
| ThomasBNL | 3:eee8d5461256 | 44 | |
| ThomasBNL | 3:eee8d5461256 | 45 | // Read the analog input |
| ThomasBNL | 10:1d8b21515ed1 | 46 | |
| ThomasBNL | 3:eee8d5461256 | 47 | |
| ThomasBNL | 3:eee8d5461256 | 48 | // The data read and send function |
| ThomasBNL | 3:eee8d5461256 | 49 | void scopeSend() |
| ThomasBNL | 3:eee8d5461256 | 50 | { |
| ThomasBNL | 10:1d8b21515ed1 | 51 | scope.set(0,Ticker motorP); |
| ThomasBNL | 3:eee8d5461256 | 52 | scope.send(); |
| ThomasBNL | 3:eee8d5461256 | 53 | } |
| ThomasBNL | 3:eee8d5461256 | 54 | |
| ThomasBNL | 6:34b39eb3dcb6 | 55 | int scopeattach() |
| ThomasBNL | 3:eee8d5461256 | 56 | { |
| ThomasBNL | 6:34b39eb3dcb6 | 57 | // Attach the data read and send function at 100 Hz |
| ThomasBNL | 10:1d8b21515ed1 | 58 | scopeTimer.attach_us(&scopeSend, 1e4); |
| ThomasBNL | 3:eee8d5461256 | 59 | |
| ThomasBNL | 3:eee8d5461256 | 60 | while(1) { } |
| ThomasBNL | 3:eee8d5461256 | 61 | } |
