Tutorial 5 Assignment --> Niet af
Dependencies: FastPWM MODSERIAL QEI mbed
main.cpp@0:fa6d20d54b4f, 2018-10-15 (annotated)
- Committer:
- MAHCSnijders
- Date:
- Mon Oct 15 08:21:03 2018 +0000
- Revision:
- 0:fa6d20d54b4f
- Child:
- 1:a1fe2023f69c
FINAL
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MAHCSnijders | 0:fa6d20d54b4f | 1 | #include "mbed.h" |
MAHCSnijders | 0:fa6d20d54b4f | 2 | #include "FastPWM.h" |
MAHCSnijders | 0:fa6d20d54b4f | 3 | #include "MODSERIAL.h" |
MAHCSnijders | 0:fa6d20d54b4f | 4 | |
MAHCSnijders | 0:fa6d20d54b4f | 5 | Ticker motor; // Ticker function |
MAHCSnijders | 0:fa6d20d54b4f | 6 | FastPWM pwmpin1(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) |
MAHCSnijders | 0:fa6d20d54b4f | 7 | FastPWM pwmpin2(D6); // 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) |
MAHCSnijders | 0:fa6d20d54b4f | 8 | 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 |
MAHCSnijders | 0:fa6d20d54b4f | 9 | 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 |
MAHCSnijders | 0:fa6d20d54b4f | 10 | AnalogIn potmeter1(A4); // Analoge input van potmeter 1 -> Motor 1 |
MAHCSnijders | 0:fa6d20d54b4f | 11 | AnalogIn potmeter2(A1); // Analoge input van potmeter 2 -> Motor 2 |
MAHCSnijders | 0:fa6d20d54b4f | 12 | |
MAHCSnijders | 0:fa6d20d54b4f | 13 | volatile float pot1; |
MAHCSnijders | 0:fa6d20d54b4f | 14 | volatile float pot2; |
MAHCSnijders | 0:fa6d20d54b4f | 15 | volatile float pot1_scale; |
MAHCSnijders | 0:fa6d20d54b4f | 16 | volatile float pot2_scale; |
MAHCSnijders | 0:fa6d20d54b4f | 17 | volatile float u1; |
MAHCSnijders | 0:fa6d20d54b4f | 18 | volatile float u2; |
MAHCSnijders | 0:fa6d20d54b4f | 19 | |
MAHCSnijders | 0:fa6d20d54b4f | 20 | void motorfunction() |
MAHCSnijders | 0:fa6d20d54b4f | 21 | { pot1 = potmeter1.read(); // reads out value potmeter 1 between 0-1 |
MAHCSnijders | 0:fa6d20d54b4f | 22 | pot1_scale = pot1*2 -1; // scales value potmeter from 0 - 1 to -1 - 1 |
MAHCSnijders | 0:fa6d20d54b4f | 23 | pot2 = potmeter2.read(); // reads out value potmeter 2 between 0-1 |
MAHCSnijders | 0:fa6d20d54b4f | 24 | pot2_scale = pot2*2 -1; // scales value potmeter from 0 - 1 to -1 - 1 |
MAHCSnijders | 0:fa6d20d54b4f | 25 | u1 = pot1_scale; // motor control signal |
MAHCSnijders | 0:fa6d20d54b4f | 26 | u2 = pot2_scale; // motor control signal |
MAHCSnijders | 0:fa6d20d54b4f | 27 | directionpin1 = u1 > 0.0f; // either true or false, determines direction (0 or 1) |
MAHCSnijders | 0:fa6d20d54b4f | 28 | directionpin2 = u2 > 0.0f; // either true or false, determines direction (0 or 1) |
MAHCSnijders | 0:fa6d20d54b4f | 29 | pwmpin1 = fabs(u1); // pwm duty cycle can only be positive, floating point absolute value (if value is >0, there still will be a positive value). |
MAHCSnijders | 0:fa6d20d54b4f | 30 | pwmpin2 = fabs(u2); // pwm duty cycle can only be positive, floating point absolute value (if value is >0, there still will be a positive value). |
MAHCSnijders | 0:fa6d20d54b4f | 31 | } |
MAHCSnijders | 0:fa6d20d54b4f | 32 | |
MAHCSnijders | 0:fa6d20d54b4f | 33 | int main() |
MAHCSnijders | 0:fa6d20d54b4f | 34 | { |
MAHCSnijders | 0:fa6d20d54b4f | 35 | pwmpin1.period_us(60.0); //60 microseconds PWM period, 16.7 kHz, defines all PWM pins (only needs to be done once) |
MAHCSnijders | 0:fa6d20d54b4f | 36 | motor.attach(motorfunction,0.5); |
MAHCSnijders | 0:fa6d20d54b4f | 37 | while(true){} //Lege while loop zodat functie niet afloopt |
MAHCSnijders | 0:fa6d20d54b4f | 38 | } |