![](/media/cache/group/9e3cc099b3b04bca937a1cca1da81b19.jpg.50x50_q85.jpg)
Motor control
Dependencies: mbed QEI HIDScope biquadFilter MODSERIAL FastPWM
main.cpp
- Committer:
- freek100
- Date:
- 2019-10-18
- Revision:
- 11:94a4dd7ed05c
- Parent:
- 10:b871d1b05787
- Child:
- 12:23b94b5dcc60
File content as of revision 11:94a4dd7ed05c:
#include "mbed.h" #include "HIDScope.h" #include "QEI.h" #include "MODSERIAL.h" #include "BiQuad.h" #include "FastPWM.h" // Button and potmeter1 control InterruptIn button1(D11); InterruptIn button2(D10); InterruptIn buttonsw2(SW2); InterruptIn buttonsw3(SW3); AnalogIn potmeter1(A0); AnalogIn potmeter2(A1); AnalogIn potmeter3(A2); AnalogIn potmeter4(A3); // Encoder DigitalIn encA(D13); DigitalIn encB(D12); QEI encoder(D13,D12,NC,64,QEI::X4_ENCODING); float Ts = 0.01; float angle; float omega; // Motor DigitalOut motor2Direction(D4); FastPWM motor2Power(D5); DigitalOut motor1Direction(D7); FastPWM motor1Power(D6); volatile int motor1Toggle = 1; //Motorcontrol bool motordir; double motorpwm; double premotorpwm; float refangle= 0; double u2; double potValue; double pi2= 6.283185; float e; //e = error float Kp=0.49; float Ki; float u_k; float u_i; //Windup control float ux; float up; float ek; float ei= 0; float Ka= 1; //Hidscope HIDScope scope(3); //Going to send 3 channels of data. To access data go to 'http:/localhost:18082/' after starting HIDScope application. // PC connection MODSERIAL pc(USBTX, USBRX); // Intializing tickers Ticker motorTicker; Ticker controlTicker; Ticker directionTicker; Ticker encoderTicker; Ticker scopeTicker; const float PWM_period = 1e-6; volatile int counts; // Encoder counts volatile int countsPrev = 0; volatile int deltaCounts; float factorin = 6.23185/64; // Convert encoder counts to angle in rad float gearratio = 131.25; // Gear ratio of gearbox float PID_controller(float e){ static float error_integral=0; //static float e_prev=e; //Proportional part: Kp=potmeter1.read(); u_k=Kp*e; //Integral part Ki=potmeter2.read(); error_integral=error_integral+ei*Ts; u_i=Ki*error_integral; // Sum and limit u2= u_k+u_i; if (u2>1){ up=1;} else if (u2<-1){ up=-1;} else { up=u2;} // To prevent windup ux= u2-up; ek= Ka*ux; ei= e-ek; //Return return up; } void readEncoder() { counts = encoder.getPulses(); deltaCounts = counts - countsPrev; countsPrev = counts; } void togglehoek(){ refangle= 0.5*pi2+refangle; // static float t = 0; // refangle= pi2/3.0f*sin(5.0f*t)*motor1Toggle; //t+=0.01; } void motorControl() { //togglehoek(); button1.fall(&togglehoek); angle = counts * factorin / gearratio; // Angle of motor shaft in rad omega = deltaCounts / Ts * factorin / gearratio; // Angular velocity of motor shaft in rad/s potValue= potmeter1.read(); //refangle= (potValue*2*pi2)-pi2; e=refangle-angle; u2=PID_controller(e); premotorpwm= fabs(u2); if (premotorpwm>1.0){ motorpwm=1;} else { motorpwm=premotorpwm;} if (u2<0){ motordir= 0;} else { motordir= 1;} motor1Power.write(motorpwm); motor1Direction= motordir; } void Plotje(){ scope.set(0,refangle); //gewenste hoek scope.set(1,angle); //Gemeten hoek scope.set(2,e); //verschil in gewenste en gemeten hoek scope.send(); //send what's in scope memory to PC } void toggleMotor() { motor1Toggle = !motor1Toggle; } int main() { pc.baud(115200); pc.printf("\r\nStarting...\r\n\r\n"); motor1Power.period(PWM_period); motorTicker.attach(motorControl, 0.01); scopeTicker.attach(Plotje, 0.01); encoderTicker.attach(readEncoder, Ts); button2.fall(&toggleMotor); while (true) { //pc.printf("Potmeter: %d \r\n", potValue,); //pc.printf("Counts: %i DeltaCounts: %i\r\n", counts, deltaCounts); pc.printf("Angle: %f Omega: %f\r\n", angle, omega); pc.printf("refangle: %f Error: %f \r\n",refangle, e); pc.printf("Kp: %f Ki: %f \r\n", Kp, Ki); wait(0.5); } }