Motor programma met EMG

Dependencies:   HIDScope MODSERIAL QEI biquadFilter mbed

Fork of frdm_Motor_V2_3 by Margreeth de Breij

Committer:
Rvs94
Date:
Tue Sep 29 13:37:22 2015 +0000
Revision:
10:80fe931a71e4
Parent:
9:774fc3c6a39e
Child:
11:0793a78109a2
p controller werkend, op naar PI controller

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Margreeth95 0:284ed397e046 1 #include "mbed.h"
Margreeth95 0:284ed397e046 2 #include "MODSERIAL.h"
Margreeth95 0:284ed397e046 3 #include "HIDScope.h"
Margreeth95 0:284ed397e046 4 #include "QEI.h"
Margreeth95 0:284ed397e046 5
Margreeth95 0:284ed397e046 6 Serial pc(USBTX, USBRX); // tx, rx
Margreeth95 0:284ed397e046 7 DigitalOut motor2direction(D4); //D4 en D5 zijn motor 2 (op het motorshield)
Margreeth95 0:284ed397e046 8 PwmOut motor2speed(D5);
Margreeth95 0:284ed397e046 9 DigitalIn button1(SW3);
Rvs94 9:774fc3c6a39e 10 DigitalIn encoderA(D3);
Rvs94 9:774fc3c6a39e 11 DigitalIn encoderB(D2);
Rvs94 9:774fc3c6a39e 12 AnalogIn potmeter2(A5);
Rvs94 5:455773cf460b 13 QEI Encoder(D3, D2, NC, 128);
Rvs94 1:48aba8d5610a 14 HIDScope scope(3);
Margreeth95 0:284ed397e046 15 Ticker ScopeTime;
Rvs94 9:774fc3c6a39e 16 Ticker myControllerTicker;
Rvs94 7:67b50d4fb03c 17
Rvs94 7:67b50d4fb03c 18 double Aantal_Degs;
Rvs94 7:67b50d4fb03c 19 double Aantal_pulses;
Rvs94 10:80fe931a71e4 20 double reference;
Rvs94 10:80fe931a71e4 21 double position;
Rvs94 2:099da0fc31b6 22
Margreeth95 0:284ed397e046 23 void ScopeSend()//Functie die de gegevens voor de scope uitleest en doorstuurt
Margreeth95 0:284ed397e046 24 {
Margreeth95 0:284ed397e046 25 scope.set(0, motor2direction.read());
Margreeth95 0:284ed397e046 26 scope.set(1, motor2speed.read());
Rvs94 1:48aba8d5610a 27 scope.set(2, Aantal_Degs);
Rvs94 10:80fe931a71e4 28 Aantal_Degs = Encoder.getPulses()*360/(0.5*128*131);
Rvs94 4:0d4aff8b57b3 29
Margreeth95 0:284ed397e046 30 scope.send();
Rvs94 1:48aba8d5610a 31
Margreeth95 0:284ed397e046 32 }
Rvs94 9:774fc3c6a39e 33 // Controller gain
Rvs94 9:774fc3c6a39e 34 const double motor1_Kp = 0.05;
Rvs94 10:80fe931a71e4 35
Rvs94 10:80fe931a71e4 36
Rvs94 9:774fc3c6a39e 37 // Reusable P controller
Rvs94 9:774fc3c6a39e 38 double P( double error, const double Kp )
Rvs94 9:774fc3c6a39e 39 {
Rvs94 9:774fc3c6a39e 40 return Kp * error;
Rvs94 9:774fc3c6a39e 41 }
Margreeth95 0:284ed397e046 42
Rvs94 9:774fc3c6a39e 43 // Next task, measure the error and apply the output to the plant
Rvs94 9:774fc3c6a39e 44 void motor1_Controller()
Rvs94 9:774fc3c6a39e 45 {
Rvs94 10:80fe931a71e4 46 reference = potmeter2.read()*360;
Rvs94 10:80fe931a71e4 47 position = Encoder.getPulses()*360/(0.5*128*131); // Aantal Degs
Rvs94 9:774fc3c6a39e 48 double P2 = P( reference - position, motor1_Kp );
Rvs94 9:774fc3c6a39e 49 motor2speed = abs(P2);
Rvs94 9:774fc3c6a39e 50 if(P2 > 0)
Rvs94 9:774fc3c6a39e 51 {
Rvs94 9:774fc3c6a39e 52 motor2direction = 0;
Rvs94 9:774fc3c6a39e 53 }
Rvs94 9:774fc3c6a39e 54 else
Rvs94 9:774fc3c6a39e 55 {
Rvs94 9:774fc3c6a39e 56 motor2direction = 1;
Rvs94 9:774fc3c6a39e 57 }
Rvs94 9:774fc3c6a39e 58
Rvs94 9:774fc3c6a39e 59 }
Rvs94 3:687729d7996e 60
Margreeth95 0:284ed397e046 61 int main()
Rvs94 9:774fc3c6a39e 62 {
Margreeth95 0:284ed397e046 63 pc.baud(115200);
Rvs94 3:687729d7996e 64 pc.printf("Tot aan loop werkt\n");
Rvs94 9:774fc3c6a39e 65
Margreeth95 0:284ed397e046 66 ScopeTime.attach_us(&ScopeSend, 10e4);
Rvs94 9:774fc3c6a39e 67 myControllerTicker.attach( &motor1_Controller, 0.01f ); // 100 Hz
Rvs94 9:774fc3c6a39e 68 while(true)
Margreeth95 0:284ed397e046 69 {
Rvs94 10:80fe931a71e4 70 pc.printf("position = %f aantal degs = %f \n",reference,position);
Rvs94 10:80fe931a71e4 71 wait(0.2f);
Margreeth95 0:284ed397e046 72 }
Rvs94 9:774fc3c6a39e 73
Margreeth95 0:284ed397e046 74 }