Feedforward controller
Dependencies: MODSERIAL QEI mbed
Fork of Tut5_motor_controller by
Diff: main.cpp
- Revision:
- 1:659489c32e75
- Parent:
- 0:1816743ba8ba
- Child:
- 2:abd40da4a5e2
--- a/main.cpp Wed Oct 04 12:46:37 2017 +0000 +++ b/main.cpp Wed Oct 04 14:15:39 2017 +0000 @@ -7,8 +7,8 @@ DigitalOut gpo(D0); DigitalOut led(LED_BLUE); -DigitalOut motor1DC(D6); -DigitalOut motor1PWM(D7); +DigitalOut motor1DC(D7); +DigitalOut motor1PWM(D6); DigitalOut motor2DC(D4); DigitalOut motor2PWM(D5); @@ -16,6 +16,9 @@ DigitalIn button1(D11); MODSERIAL pc(USBTX,USBRX); +QEI Encoder(D12,D13,NC,32); // Input for the Encoder + +Ticker controller; float GetReferenceVelocity() { @@ -52,30 +55,34 @@ // bits for motor 1. Positive value makes motor rotating // clockwise. motorValues outside range are truncated to // within range - if (motorValue >=0) motor1DC=1; + if (motorValue >=0) motor1DC= 1; else motor1DC=0; if (fabs(motorValue)>1) motor1PWM = 1; else motor1PWM = fabs(motorValue); } +void MeasureAndControl(void) +{ + float referenceVelocity = GetReferenceVelocity(); + float motorValue = FeedForwardControl(referenceVelocity); + setMotor1(motorValue); +} + int main() { pc.baud(115200); - - QEI Encoder(D12,D13,NC,32); // Input for the Encoder + + //float v = GetReferenceVelocity(); + //float controlValue = FeedForwardControl(v); + //SetMotor1(controlValue); - while (true) { - - float v = GetReferenceVelocity(); - float controlValue = FeedForwardControl(v); - SetMotor1(controlValue); + controller.attach(&MeasureAndControl,0.01); - int counts; - counts = Encoder.getPulses(); - pc.printf("\r number of counts: %i\n",counts); - - pc.printf("\t\r reference velocity is: %f. Motor Value is: %f \t\r number of counts: %i\n",v,controlValue,counts); - } + int counts = Encoder.getPulses(); + pc.printf("\r `control value: %f reference velocity: %f. Motor Value is: %f number of counts: %i\n",referenceVelocity ,motorValue,counts); + + while(1) + {} }