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 10:45:54 2015 +0000
Revision:
3:eee8d5461256
Parent:
2:f5c9d981de51
Child:
4:dfdfcb518e60
Poging toevoegen encoder

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 1:b0d3c64bd4d8 8
ThomasBNL 1:b0d3c64bd4d8 9 ////// P Motor Controller
ThomasBNL 0:843492f4fe62 10
ThomasBNL 0:843492f4fe62 11 // Controller gain
ThomasBNL 0:843492f4fe62 12 const double motorP_Kp=2.5;
ThomasBNL 0:843492f4fe62 13 const double Convert_volt_to_position=0.00300;
ThomasBNL 0:843492f4fe62 14
ThomasBNL 0:843492f4fe62 15 // Reusable P controller (FUNCTIE)
ThomasBNL 0:843492f4fe62 16 double P (double error, const double Kp)
ThomasBNL 0:843492f4fe62 17 {
ThomasBNL 3:eee8d5461256 18 return Kp*error;
ThomasBNL 0:843492f4fe62 19 }
ThomasBNL 3:eee8d5461256 20
ThomasBNL 3:eee8d5461256 21 #include "Encoder.h"
ThomasBNL 3:eee8d5461256 22 *
ThomasBNL 3:eee8d5461256 23 * Encoder motor1(PTD0,PTC9,true);
ThomasBNL 3:eee8d5461256 24 * Serial pc(USBTX,USBRX);
ThomasBNL 3:eee8d5461256 25 * pc.baud(115200);
ThomasBNL 3:eee8d5461256 26 while(1) {
ThomasBNL 3:eee8d5461256 27 wait(0.2);
ThomasBNL 3:eee8d5461256 28 pc.printf("pos: %d, speed %f \r\n",motor1.getPosition(), motor1.getSpeed());
ThomasBNL 3:eee8d5461256 29 }
ThomasBNL 3:eee8d5461256 30
ThomasBNL 3:eee8d5461256 31 AnalogIn potmeter1(POT1);
ThomasBNL 3:eee8d5461256 32
ThomasBNL 3:eee8d5461256 33 // encoder1
ThomasBNL 3:eee8d5461256 34 // potmeter1
ThomasBNL 0:843492f4fe62 35
ThomasBNL 0:843492f4fe62 36 // Error measurement and apply the output to the plant
ThomasBNL 0:843492f4fe62 37 void motorP_Controller()
ThomasBNL 0:843492f4fe62 38 {
ThomasBNL 0:843492f4fe62 39 double reference_position = potmeter1.read();
ThomasBNL 0:843492f4fe62 40 double position = Convert_volt_to_position*encoder1.getPosition();
ThomasBNL 0:843492f4fe62 41 motorP=P(reference_position - position, motorP_Kp);
ThomasBNL 0:843492f4fe62 42 }
ThomasBNL 0:843492f4fe62 43
ThomasBNL 0:843492f4fe62 44 int main()
ThomasBNL 0:843492f4fe62 45 {
ThomasBNL 0:843492f4fe62 46 myControllerTicker.attach(&motorP_Controller,0.01f); //100Hz
ThomasBNL 0:843492f4fe62 47 while(1){}
ThomasBNL 0:843492f4fe62 48 }
ThomasBNL 3:eee8d5461256 49
ThomasBNL 3:eee8d5461256 50
ThomasBNL 3:eee8d5461256 51 // Read the analog input
ThomasBNL 3:eee8d5461256 52 float triangle_signal = 2.05;
ThomasBNL 3:eee8d5461256 53
ThomasBNL 3:eee8d5461256 54 // The data read and send function
ThomasBNL 3:eee8d5461256 55 void scopeSend()
ThomasBNL 3:eee8d5461256 56 {
ThomasBNL 3:eee8d5461256 57 scope.set(0,motorP);
ThomasBNL 3:eee8d5461256 58 scope.send();
ThomasBNL 3:eee8d5461256 59 }
ThomasBNL 3:eee8d5461256 60
ThomasBNL 3:eee8d5461256 61 int main()
ThomasBNL 3:eee8d5461256 62 {
ThomasBNL 3:eee8d5461256 63 // Attach the data read and send function at 200 Hz
ThomasBNL 3:eee8d5461256 64 scopeTimer.attach_us(&scopeSend, 2e4);
ThomasBNL 3:eee8d5461256 65
ThomasBNL 3:eee8d5461256 66 while(1) { }
ThomasBNL 3:eee8d5461256 67 }