![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Programming Milestone Group 7 BMT M9
Dependencies: FastPWM MODSERIAL mbed
main.cpp@5:92360cb2e0e4, 2018-09-26 (annotated)
- Committer:
- MAHCSnijders
- Date:
- Wed Sep 26 16:41:28 2018 +0000
- Revision:
- 5:92360cb2e0e4
- Parent:
- 4:a4cd6f9c851d
- Child:
- 6:b5fc5007f228
Werkt voor M1 en 1 direction (kan harder/zachter).
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MAHCSnijders | 0:1e216b50d323 | 1 | #include "mbed.h" |
MAHCSnijders | 1:67b19c59b8c9 | 2 | #include "FastPWM.h" |
MAHCSnijders | 1:67b19c59b8c9 | 3 | #include "MODSERIAL.h" |
MAHCSnijders | 1:67b19c59b8c9 | 4 | |
MAHCSnijders | 4:a4cd6f9c851d | 5 | Ticker motor; // Ticker function |
MAHCSnijders | 2:6d3f3d1bcef7 | 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 | 2:6d3f3d1bcef7 | 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 | 1:67b19c59b8c9 | 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 | 1:67b19c59b8c9 | 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 | 1:67b19c59b8c9 | 10 | AnalogIn potmeter1(A0); // Analoge input van potmeter 1 |
MAHCSnijders | 1:67b19c59b8c9 | 11 | AnalogIn potmeter2(A1); // Analoge input van potmeter 2 |
MAHCSnijders | 0:1e216b50d323 | 12 | |
MAHCSnijders | 2:6d3f3d1bcef7 | 13 | volatile float pot1; |
MAHCSnijders | 2:6d3f3d1bcef7 | 14 | volatile float pot2; |
MAHCSnijders | 2:6d3f3d1bcef7 | 15 | volatile float pot1_scale; |
MAHCSnijders | 2:6d3f3d1bcef7 | 16 | volatile float pot2_scale; |
MAHCSnijders | 2:6d3f3d1bcef7 | 17 | volatile float u1; |
MAHCSnijders | 2:6d3f3d1bcef7 | 18 | volatile float u2; |
MAHCSnijders | 2:6d3f3d1bcef7 | 19 | |
MAHCSnijders | 1:67b19c59b8c9 | 20 | void motorfunction() |
MAHCSnijders | 4:a4cd6f9c851d | 21 | { pot1 = potmeter1.read(); // reads out value potmeter 1 between 0-1 |
MAHCSnijders | 4:a4cd6f9c851d | 22 | pot1_scale = pot1*2 -1; // scales value potmeter from 0-1 to -1 - 1. |
MAHCSnijders | 4:a4cd6f9c851d | 23 | pot2 = potmeter2.read(); // reads out value potmeter 2 between 0-1 |
MAHCSnijders | 4:a4cd6f9c851d | 24 | pot2_scale = pot2*2 -1; // scales value potmeter from 0-1 to -1 - 1. |
MAHCSnijders | 2:6d3f3d1bcef7 | 25 | u1 = pot1_scale; // motor control signal |
MAHCSnijders | 2:6d3f3d1bcef7 | 26 | u2 = pot2_scale; // motor control signal |
MAHCSnijders | 4:a4cd6f9c851d | 27 | directionpin1 = u1 > 0.0f; // either true or false, determines direction (0 or 1) |
MAHCSnijders | 4:a4cd6f9c851d | 28 | directionpin2 = u2 > 0.0f; // either true or false |
MAHCSnijders | 4:a4cd6f9c851d | 29 | pwmpin1 = 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). |
MAHCSnijders | 4:a4cd6f9c851d | 30 | pwmpin2 = fabs(u2); // pwm duty cycle can only be positive, floating point absolute value (if value is >0, the there still will be a positive value). |
MAHCSnijders | 1:67b19c59b8c9 | 31 | } |
MAHCSnijders | 0:1e216b50d323 | 32 | |
MAHCSnijders | 0:1e216b50d323 | 33 | int main() |
MAHCSnijders | 0:1e216b50d323 | 34 | { |
MAHCSnijders | 2:6d3f3d1bcef7 | 35 | pwmpin1.period_us(60.0); //60 microseconds PWM period, 16.7 kHz, defines all PWM pins (only needs to be done once) |
MAHCSnijders | 2:6d3f3d1bcef7 | 36 | motor.attach(motorfunction,0.5); |
MAHCSnijders | 4:a4cd6f9c851d | 37 | while(true){} //Lege while loop zodat functie niet afloopt |
MAHCSnijders | 0:1e216b50d323 | 38 | } |