An incomplete quadcopter control programme.

Dependencies:   mbed

Committer:
Gurvan
Date:
Wed Jul 17 15:58:25 2013 +0000
Revision:
0:9cb9445a11f0
Pour Zobson, fi(r)st commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Gurvan 0:9cb9445a11f0 1 /* mbed PwmIn Library
Gurvan 0:9cb9445a11f0 2 * Copyright (c) 2008-2010, sford
Gurvan 0:9cb9445a11f0 3 *
Gurvan 0:9cb9445a11f0 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
Gurvan 0:9cb9445a11f0 5 * of this software and associated documentation files (the "Software"), to deal
Gurvan 0:9cb9445a11f0 6 * in the Software without restriction, including without limitation the rights
Gurvan 0:9cb9445a11f0 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Gurvan 0:9cb9445a11f0 8 * copies of the Software, and to permit persons to whom the Software is
Gurvan 0:9cb9445a11f0 9 * furnished to do so, subject to the following conditions:
Gurvan 0:9cb9445a11f0 10 *
Gurvan 0:9cb9445a11f0 11 * The above copyright notice and this permission notice shall be included in
Gurvan 0:9cb9445a11f0 12 * all copies or substantial portions of the Software.
Gurvan 0:9cb9445a11f0 13 *
Gurvan 0:9cb9445a11f0 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Gurvan 0:9cb9445a11f0 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Gurvan 0:9cb9445a11f0 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Gurvan 0:9cb9445a11f0 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Gurvan 0:9cb9445a11f0 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Gurvan 0:9cb9445a11f0 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Gurvan 0:9cb9445a11f0 20 * THE SOFTWARE.
Gurvan 0:9cb9445a11f0 21 */
Gurvan 0:9cb9445a11f0 22
Gurvan 0:9cb9445a11f0 23 #include "PwmIn.h"
Gurvan 0:9cb9445a11f0 24
Gurvan 0:9cb9445a11f0 25 PwmIn::PwmIn(PinName p) : _p(p) {
Gurvan 0:9cb9445a11f0 26 _p.mode(PullNone);
Gurvan 0:9cb9445a11f0 27 _p.rise(this, &PwmIn::rise);
Gurvan 0:9cb9445a11f0 28 _p.fall(this, &PwmIn::fall);
Gurvan 0:9cb9445a11f0 29 _period = 0.0;
Gurvan 0:9cb9445a11f0 30 _pulsewidth = 0.0;
Gurvan 0:9cb9445a11f0 31 _temp_pulsewidth = 0.0;
Gurvan 0:9cb9445a11f0 32 _t.start();
Gurvan 0:9cb9445a11f0 33 }
Gurvan 0:9cb9445a11f0 34
Gurvan 0:9cb9445a11f0 35 float PwmIn::period() {
Gurvan 0:9cb9445a11f0 36 return _period;
Gurvan 0:9cb9445a11f0 37 }
Gurvan 0:9cb9445a11f0 38
Gurvan 0:9cb9445a11f0 39 float PwmIn::pulsewidth() {
Gurvan 0:9cb9445a11f0 40 return _pulsewidth;
Gurvan 0:9cb9445a11f0 41 }
Gurvan 0:9cb9445a11f0 42
Gurvan 0:9cb9445a11f0 43 float PwmIn::dutycycle() {
Gurvan 0:9cb9445a11f0 44 if(_t.read_ms()<100){
Gurvan 0:9cb9445a11f0 45 return (_pulsewidth%_period) /((float) _period);}
Gurvan 0:9cb9445a11f0 46 else return 0;
Gurvan 0:9cb9445a11f0 47 }
Gurvan 0:9cb9445a11f0 48
Gurvan 0:9cb9445a11f0 49 void PwmIn::rise() {
Gurvan 0:9cb9445a11f0 50 _period = _t.read_us();
Gurvan 0:9cb9445a11f0 51 _t.reset();
Gurvan 0:9cb9445a11f0 52 }
Gurvan 0:9cb9445a11f0 53
Gurvan 0:9cb9445a11f0 54 void PwmIn::fall() {
Gurvan 0:9cb9445a11f0 55 _temp_pulsewidth = _t.read_us();
Gurvan 0:9cb9445a11f0 56 if(_temp_pulsewidth != 0) _pulsewidth = _temp_pulsewidth;
Gurvan 0:9cb9445a11f0 57 }