![](/media/cache/group/Download.jpg.50x50_q85.jpg)
6.8 Motor mittels Tasten vor-, rückwärts, Stop laufen lassen und Poti benützen um die Geschwindigkeit einzustellen.
Dependencies: mbed
Revision 0:5d64a3e741ec, committed 2015-03-04
- Comitter:
- marcel1691
- Date:
- Wed Mar 04 16:30:53 2015 +0000
- Commit message:
- 6.8 Motor mittels Tasten vor-, r?ckw?rts, Stop laufen lassen und Poti ben?tzen um die Geschwindigkeit einzustellen.
Changed in this revision
diff -r 000000000000 -r 5d64a3e741ec Motor/Motor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Motor/Motor.cpp Wed Mar 04 16:30:53 2015 +0000 @@ -0,0 +1,46 @@ +/* mbed simple H-bridge motor controller + * Copyright (c) 2007-2010, sford, http://mbed.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "Motor.h" + +#include "mbed.h" + +Motor::Motor(PinName pwm, PinName fwd, PinName rev): + _pwm(pwm), _fwd(fwd), _rev(rev) { + + // Set initial condition of PWM + _pwm.period(0.001); + _pwm = 0; + + // Initial condition of output enables + _fwd = 0; + _rev = 0; +} + +void Motor::speed(float speed) { + _fwd = (speed > 0.0); + _rev = (speed < 0.0); + _pwm = abs(speed); +} + + +
diff -r 000000000000 -r 5d64a3e741ec Motor/Motor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Motor/Motor.h Wed Mar 04 16:30:53 2015 +0000 @@ -0,0 +1,56 @@ +/* mbed simple H-bridge motor controller + * Copyright (c) 2007-2010, sford, http://mbed.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MBED_MOTOR_H +#define MBED_MOTOR_H + +#include "mbed.h" + +/** Interface to control a standard DC motor + * + * with an H-bridge using a PwmOut and 2 DigitalOuts + */ +class Motor { +public: + + /** Create a motor control interface + * + * @param pwm A PwmOut pin, driving the H-bridge enable line to control the speed + * @param fwd A DigitalOut, set high when the motor should go forward + * @param rev A DigitalOut, set high when the motor should go backwards + */ + Motor(PinName pwm, PinName fwd, PinName rev); + + /** Set the speed of the motor + * + * @param speed The speed of the motor as a normalised value between -1.0 and 1.0 + */ + void speed(float speed); + +protected: + PwmOut _pwm; + DigitalOut _fwd; + DigitalOut _rev; + +}; + +#endif
diff -r 000000000000 -r 5d64a3e741ec main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Mar 04 16:30:53 2015 +0000 @@ -0,0 +1,29 @@ +/** 6.8 Motor mittels Tasten vor-, rückwärts, Stop laufen lassen und Poti benützen um die Geschwindigkeit einzustellen. +*/ + +#include "mbed.h" +#include "Motor.h" + +// vor-/rueckwaerts - beide = Notstop +DigitalIn button1( PTC9, PullUp ); +DigitalIn button2( PTC0, PullUp ); + +// Geschwindigkeit +AnalogIn poti ( A0 ); + +// Motor an M01 +Motor m1(D3, D2, D4); // PWM, Vorwaerts, Rueckwarts + +int main() +{ + while (true) + { + if ( button1 == 0 && button2 == 0 ) + m1.speed( 0.0f ); + else if ( button1 == 0 ) + m1.speed( poti ); + else if ( button2 == 0 ) + m1.speed( poti * -1.0f ); + wait(0.2f); + } +} \ No newline at end of file
diff -r 000000000000 -r 5d64a3e741ec mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Mar 04 16:30:53 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/7e07b6fb45cf \ No newline at end of file