Draaiende motor op commando van knopje (WERKEND)

Dependencies:   Encoder MODSERIAL mbed

Fork of P_controller_motor by Bouke Scheltinga

Committer:
ThomasBNL
Date:
Thu Sep 17 11:36:05 2015 +0000
Revision:
6:34b39eb3dcb6
Parent:
5:8ea7a765c1f7
Child:
7:961dcef17e08
motorP werkt nog niet;

Who changed what in which revision?

UserRevisionLine numberNew 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 1:b0d3c64bd4d8 6 HIDScope scope(1);
ThomasBNL 1:b0d3c64bd4d8 7 Ticker scopeTimer;
ThomasBNL 4:dfdfcb518e60 8
ThomasBNL 4:dfdfcb518e60 9 // Define Encoder
ThomasBNL 4:dfdfcb518e60 10
ThomasBNL 6:34b39eb3dcb6 11 Encoder encoder1(PTD0,PTC9,true);
ThomasBNL 4:dfdfcb518e60 12 Serial pc(USBTX,USBRX);
ThomasBNL 4:dfdfcb518e60 13
ThomasBNL 4:dfdfcb518e60 14 // Define Potmeter
ThomasBNL 5:8ea7a765c1f7 15 AnalogIn potmeter1(PTB0);
ThomasBNL 4:dfdfcb518e60 16
ThomasBNL 1:b0d3c64bd4d8 17 ////// P Motor Controller
ThomasBNL 0:843492f4fe62 18
ThomasBNL 0:843492f4fe62 19 // Controller gain
ThomasBNL 5:8ea7a765c1f7 20 const double motorP_Kp=2.500 ;
ThomasBNL 5:8ea7a765c1f7 21 const double Convert_volt_to_position=0.00300 ;
ThomasBNL 0:843492f4fe62 22
ThomasBNL 0:843492f4fe62 23 // Reusable P controller (FUNCTIE)
ThomasBNL 0:843492f4fe62 24 double P (double error, const double Kp)
ThomasBNL 0:843492f4fe62 25 {
ThomasBNL 3:eee8d5461256 26 return Kp*error;
ThomasBNL 0:843492f4fe62 27 }
ThomasBNL 6:34b39eb3dcb6 28
ThomasBNL 6:34b39eb3dcb6 29 // Motor function
ThomasBNL 3:eee8d5461256 30
ThomasBNL 6:34b39eb3dcb6 31
ThomasBNL 0:843492f4fe62 32 // Error measurement and apply the output to the plant
ThomasBNL 0:843492f4fe62 33 void motorP_Controller()
ThomasBNL 0:843492f4fe62 34 {
ThomasBNL 0:843492f4fe62 35 double reference_position = potmeter1.read();
ThomasBNL 6:34b39eb3dcb6 36 double position = Convert_volt_to_position*encoder1.getPosition();
ThomasBNL 6:34b39eb3dcb6 37 return P(reference_position-position, motorP_Kp);
ThomasBNL 0:843492f4fe62 38 }
ThomasBNL 0:843492f4fe62 39
ThomasBNL 0:843492f4fe62 40 int main()
ThomasBNL 0:843492f4fe62 41 {
ThomasBNL 6:34b39eb3dcb6 42 motorP = motorP_Controller()
ThomasBNL 6:34b39eb3dcb6 43 motorP.attach(&motorP_Controller,0.01f); //100Hz
ThomasBNL 0:843492f4fe62 44 while(1){}
ThomasBNL 0:843492f4fe62 45 }
ThomasBNL 3:eee8d5461256 46
ThomasBNL 3:eee8d5461256 47 // Read the analog input
ThomasBNL 3:eee8d5461256 48 float triangle_signal = 2.05;
ThomasBNL 3:eee8d5461256 49
ThomasBNL 3:eee8d5461256 50 // The data read and send function
ThomasBNL 3:eee8d5461256 51 void scopeSend()
ThomasBNL 3:eee8d5461256 52 {
ThomasBNL 3:eee8d5461256 53 scope.set(0,motorP);
ThomasBNL 3:eee8d5461256 54 scope.send();
ThomasBNL 3:eee8d5461256 55 }
ThomasBNL 3:eee8d5461256 56
ThomasBNL 6:34b39eb3dcb6 57 int scopeattach()
ThomasBNL 3:eee8d5461256 58 {
ThomasBNL 6:34b39eb3dcb6 59 // Attach the data read and send function at 100 Hz
ThomasBNL 3:eee8d5461256 60 scopeTimer.attach_us(&scopeSend, 2e4);
ThomasBNL 3:eee8d5461256 61
ThomasBNL 3:eee8d5461256 62 while(1) { }
ThomasBNL 3:eee8d5461256 63 }