Script 15-10-2019
Dependencies: Servoaansturing mbed QEI HIDScope biquadFilter MODSERIAL FastPWM
Diff: main.cpp
- Revision:
- 3:6ee0b20c23b0
- Parent:
- 2:aee655d11b6d
- Child:
- 4:6742476fadcf
--- a/main.cpp Mon Oct 07 07:39:29 2019 +0000 +++ b/main.cpp Mon Oct 07 08:51:06 2019 +0000 @@ -1,56 +1,86 @@ #include "mbed.h" -//#include "HIDScope.h" -//#include "QEI.h" +#include "HIDScope.h" +#include "QEI.h" #include "MODSERIAL.h" -//#include "BiQuad.h" -//#include "FastPWM.h" +#include "BiQuad.h" +#include "FastPWM.h" #include <math.h> +//definieer objecten Serial pc(USBTX, USBRX); -Ticker myControllerTicker; -AnalogIn potMeter(A0); -PwmOut fan(D5); // kan ook D6 zijn -DigitalOut fan_dir(D4); + +Ticker myControllerTicker1; +Ticker myControllerTicker2; + +AnalogIn potMeter1(A0); +AnalogIn potMeter2(A1); + +PwmOut motor1(D5); +PwmOut motor2(D6); + +DigitalOut motor1_dir(D4); +DigitalOut motor2_dir(D7); + InterruptIn button1(D2); +InterruptIn button2(D1); -void direction(void) +//richting wisselen van motor 1 +void direction1(void) + { + motor1_dir=!motor1_dir; + } + +//richting wisselen van motor 2 +void direction2(void) + { + motor2_dir=!motor2_dir; + } + + +//snelheid motor 1 aansturen +void motor1Controller( ) { - fan_dir=!fan_dir; - - pc.printf("hello"); + // Determine reference (desired) fan speed + double y1_des = potMeter1.read(); + // Controller (calculate motor torque/pwm) + if( y1_des > 1 ) y1_des = 1; // y1_des must be <= 1 + if( y1_des < 0 ) y1_des = 0; // y1_des must be >= 0 + double power1 = pow(y1_des, 2.0); // Inverse relation between input and output + // Send to motor + motor1.write( power1 ); + pc.printf("power1: %.2f\t",power1); + } + +//snelheid motor 2 aansturen +void motor2Controller( ) + { + // Determine reference (desired) fan speed + double y2_des = potMeter2.read(); + // Controller (calculate motor torque/pwm) + if( y2_des > 1 ) y2_des = 1; // y2_des must be <= 1 + if( y2_des < 0 ) y2_des = 0; // y2_des must be >= 0 + double power2 = pow(y2_des, 2.0); // Inverse relation between input and output + // Send to motor + motor2.write( power2 ); + pc.printf("power2: %.2f\n\r",power2); + } + + +int main() + { + myControllerTicker1.attach(motor1Controller, 0.1 ); // Every 1/10 second + myControllerTicker2.attach(motor2Controller, 0.1 ); // Every 1/10 second + + button1.mode(PullUp); + button1.rise(direction1); + button2.mode(PullUp); + button2.rise(direction2); + + + while( true ) { /* do nothing */ } + return 0; } -// Feed forward fan speed controller -// Suppose an AnalogIn ’potMeter’ and PWMOut ’fan’ -void fanController( ) -{ -// Determine reference (desired) fan speed -double y_des = potMeter.read(); -// Controller (calculate motor torque/pwm) -if( y_des > 1 ) y_des = 1; // y_des must be <= 1 -if( y_des < 0 ) y_des = 0; // y_des must be >= 0 -double u = pow(y_des, 2.0); // Inverse relation between input and output -// Send to motor -fan.write( u ); -pc.printf("u: %.2f\n\r",u); -} - - - -int main() -{ -myControllerTicker.attach( fanController, 0.1 ); // Every second - -button1.mode(PullUp); -button1.rise(direction); - -while( true ) { /* do nothing */ } -return 0; -} - - - -