Programming Milestone Group 7 BMT M9
Dependencies: FastPWM MODSERIAL mbed
main.cpp@1:67b19c59b8c9, 2018-09-25 (annotated)
- Committer:
- MAHCSnijders
- Date:
- Tue Sep 25 12:41:08 2018 +0000
- Revision:
- 1:67b19c59b8c9
- Parent:
- 0:1e216b50d323
- Child:
- 2:6d3f3d1bcef7
Milestone 1 gehaald: motor draait, podmeter werkt alleen nog niet (voltage aanpassen). Alleen met motor 1 uitgeprobeerd.; Aansluitingen motor shield:; - Stroom motor; - Motor 1; - Analog0 met POT1; - Analog1 met POT2
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 | 1:67b19c59b8c9 | 5 | Ticker motor; // ticker function |
MAHCSnijders | 1:67b19c59b8c9 | 6 | 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) |
MAHCSnijders | 1:67b19c59b8c9 | 7 | 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 | 8 | 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 | 9 | AnalogIn potmeter1(A0); // Analoge input van potmeter 1 |
MAHCSnijders | 1:67b19c59b8c9 | 10 | AnalogIn potmeter2(A1); // Analoge input van potmeter 2 |
MAHCSnijders | 0:1e216b50d323 | 11 | |
MAHCSnijders | 1:67b19c59b8c9 | 12 | void motorfunction() |
MAHCSnijders | 1:67b19c59b8c9 | 13 | { float pot1 = potmeter1.read(); // Reads out value potmeter 1 between 0-1 |
MAHCSnijders | 1:67b19c59b8c9 | 14 | float pot1_scale = pot1*2 -1; // Scales value potmeter from 0-1 to -1 - 1. |
MAHCSnijders | 1:67b19c59b8c9 | 15 | float pot2 = potmeter2.read(); |
MAHCSnijders | 1:67b19c59b8c9 | 16 | float pot2_scale = pot2*2 -1; // Scales value potmeter from 0-1 to -1 - 1. |
MAHCSnijders | 1:67b19c59b8c9 | 17 | float u1 = pot1_scale; // motor control signal |
MAHCSnijders | 1:67b19c59b8c9 | 18 | float u2 = pot2_scale; // motor control signal |
MAHCSnijders | 1:67b19c59b8c9 | 19 | directionpin1 = u1 > 0.0f; //either true or false, determines direction (0 or 1) |
MAHCSnijders | 1:67b19c59b8c9 | 20 | directionpin2 = u2 > 0.0f; //either true or false |
MAHCSnijders | 1:67b19c59b8c9 | 21 | 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). |
MAHCSnijders | 1:67b19c59b8c9 | 22 | } |
MAHCSnijders | 0:1e216b50d323 | 23 | |
MAHCSnijders | 0:1e216b50d323 | 24 | int main() |
MAHCSnijders | 0:1e216b50d323 | 25 | { |
MAHCSnijders | 1:67b19c59b8c9 | 26 | pwmpin.period_us(60.0); //60 microseconds PWM period, 16.7 kHz |
MAHCSnijders | 1:67b19c59b8c9 | 27 | motor.attach(motorfunction,0.3); |
MAHCSnijders | 1:67b19c59b8c9 | 28 | while(true){} //Lege while loop zodat functie niet afloopt |
MAHCSnijders | 0:1e216b50d323 | 29 | } |