![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Programming Milestone Group 7 BMT M9
Dependencies: FastPWM MODSERIAL mbed
Diff: main.cpp
- Revision:
- 1:67b19c59b8c9
- Parent:
- 0:1e216b50d323
- Child:
- 2:6d3f3d1bcef7
--- a/main.cpp Tue Sep 25 10:36:50 2018 +0000 +++ b/main.cpp Tue Sep 25 12:41:08 2018 +0000 @@ -1,16 +1,29 @@ #include "mbed.h" +#include "FastPWM.h" +#include "MODSERIAL.h" + +Ticker motor; // ticker function +FastPWM pwmpin(D5); // SPECIFIC PIN (hoeft niet aangesloten te worden) Tells you how fast the motor has to go (later: pwmpin.write will tell you the duty cycle, aka how much voltage the motor gets) +DigitalOut directionpin1(D4); // SPECIFIC PIN (hoeft niet aangesloten te worden) Direction value (0-1) that the mbed will give the motor: in which direction the motor must rotate +DigitalOut directionpin2(D7); // SPECIFIC PIN (hoeft niet aangesloten te worden) Direction value (0-1) that the mbed will give the motor: in which direction the motor must rotate +AnalogIn potmeter1(A0); // Analoge input van potmeter 1 +AnalogIn potmeter2(A1); // Analoge input van potmeter 2 -PwmOut pwmpin(); -DigitalOut directionpin(); -pwmpin.period_us(60); //60 microseconds PWM period, 16.7 kHz -AnalogIn potmeter(); // Analoge input van potmeter +void motorfunction() +{ float pot1 = potmeter1.read(); // Reads out value potmeter 1 between 0-1 + float pot1_scale = pot1*2 -1; // Scales value potmeter from 0-1 to -1 - 1. + float pot2 = potmeter2.read(); + float pot2_scale = pot2*2 -1; // Scales value potmeter from 0-1 to -1 - 1. + float u1 = pot1_scale; // motor control signal + float u2 = pot2_scale; // motor control signal + directionpin1 = u1 > 0.0f; //either true or false, determines direction (0 or 1) + directionpin2 = u2 > 0.0f; //either true or false + pwmpin = fabs(u1); //pwm duty cycle can only be positive, floating point absolute value (if value is >0, the there still will be a positive value). +} int main() { - while (true) { - float u = -0.3f; //determine useful value, -0.3f is just an example - directionpin = u > 0.0f; //either true or false - pwmpin = fabs(u); //pwm duty cycle can only be positive, floating point absolute value - float pot = potmeter.read(); - } + pwmpin.period_us(60.0); //60 microseconds PWM period, 16.7 kHz + motor.attach(motorfunction,0.3); + while(true){} //Lege while loop zodat functie niet afloopt } \ No newline at end of file