![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
working version with calibration done
Fork of Eurobot2013 by
IR_Turret/PwmIn/PwmIn.cpp@11:5ba926692210, 2013-04-09 (annotated)
- Committer:
- xiaxia686
- Date:
- Tue Apr 09 15:32:47 2013 +0000
- Revision:
- 11:5ba926692210
- Parent:
- IR/PwmIn/PwmIn.cpp@10:2bd9f4e02b74
woking version (calibrated)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xiaxia686 | 10:2bd9f4e02b74 | 1 | /* mbed PwmIn Library |
xiaxia686 | 10:2bd9f4e02b74 | 2 | * Copyright (c) 2008-2010, sford |
xiaxia686 | 10:2bd9f4e02b74 | 3 | * |
xiaxia686 | 10:2bd9f4e02b74 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
xiaxia686 | 10:2bd9f4e02b74 | 5 | * of this software and associated documentation files (the "Software"), to deal |
xiaxia686 | 10:2bd9f4e02b74 | 6 | * in the Software without restriction, including without limitation the rights |
xiaxia686 | 10:2bd9f4e02b74 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
xiaxia686 | 10:2bd9f4e02b74 | 8 | * copies of the Software, and to permit persons to whom the Software is |
xiaxia686 | 10:2bd9f4e02b74 | 9 | * furnished to do so, subject to the following conditions: |
xiaxia686 | 10:2bd9f4e02b74 | 10 | * |
xiaxia686 | 10:2bd9f4e02b74 | 11 | * The above copyright notice and this permission notice shall be included in |
xiaxia686 | 10:2bd9f4e02b74 | 12 | * all copies or substantial portions of the Software. |
xiaxia686 | 10:2bd9f4e02b74 | 13 | * |
xiaxia686 | 10:2bd9f4e02b74 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
xiaxia686 | 10:2bd9f4e02b74 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
xiaxia686 | 10:2bd9f4e02b74 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
xiaxia686 | 10:2bd9f4e02b74 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
xiaxia686 | 10:2bd9f4e02b74 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
xiaxia686 | 10:2bd9f4e02b74 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
xiaxia686 | 10:2bd9f4e02b74 | 20 | * THE SOFTWARE. |
xiaxia686 | 10:2bd9f4e02b74 | 21 | */ |
xiaxia686 | 10:2bd9f4e02b74 | 22 | |
xiaxia686 | 10:2bd9f4e02b74 | 23 | #include "PwmIn.h" |
xiaxia686 | 11:5ba926692210 | 24 | #include "IR_Turret.h" |
xiaxia686 | 10:2bd9f4e02b74 | 25 | #include "globals.h" |
xiaxia686 | 10:2bd9f4e02b74 | 26 | |
xiaxia686 | 11:5ba926692210 | 27 | |
xiaxia686 | 10:2bd9f4e02b74 | 28 | PwmIn::PwmIn(PinName p) : _p(p) { |
xiaxia686 | 10:2bd9f4e02b74 | 29 | #ifdef PWM_INVERT |
xiaxia686 | 10:2bd9f4e02b74 | 30 | _p.rise(this, &PwmIn::fall); |
xiaxia686 | 10:2bd9f4e02b74 | 31 | _p.fall(this, &PwmIn::rise); |
xiaxia686 | 10:2bd9f4e02b74 | 32 | #else |
xiaxia686 | 10:2bd9f4e02b74 | 33 | _p.rise(this, &PwmIn::rise); |
xiaxia686 | 10:2bd9f4e02b74 | 34 | _p.fall(this, &PwmIn::fall); |
xiaxia686 | 10:2bd9f4e02b74 | 35 | #endif |
xiaxia686 | 10:2bd9f4e02b74 | 36 | |
xiaxia686 | 10:2bd9f4e02b74 | 37 | _period = 0.0; |
xiaxia686 | 10:2bd9f4e02b74 | 38 | _pulsewidth = 0.0; |
xiaxia686 | 10:2bd9f4e02b74 | 39 | _t.start(); |
xiaxia686 | 10:2bd9f4e02b74 | 40 | |
xiaxia686 | 10:2bd9f4e02b74 | 41 | //init callabck function |
xiaxia686 | 10:2bd9f4e02b74 | 42 | callbackfunc = NULL; |
xiaxia686 | 10:2bd9f4e02b74 | 43 | callbackobj = NULL; |
xiaxia686 | 10:2bd9f4e02b74 | 44 | mcallbackfunc = NULL; |
xiaxia686 | 10:2bd9f4e02b74 | 45 | |
xiaxia686 | 10:2bd9f4e02b74 | 46 | } |
xiaxia686 | 10:2bd9f4e02b74 | 47 | |
xiaxia686 | 10:2bd9f4e02b74 | 48 | float PwmIn::period() { |
xiaxia686 | 10:2bd9f4e02b74 | 49 | return _period; |
xiaxia686 | 10:2bd9f4e02b74 | 50 | } |
xiaxia686 | 10:2bd9f4e02b74 | 51 | |
xiaxia686 | 10:2bd9f4e02b74 | 52 | float PwmIn::pulsewidth() { |
xiaxia686 | 10:2bd9f4e02b74 | 53 | return _pulsewidth; |
xiaxia686 | 10:2bd9f4e02b74 | 54 | } |
xiaxia686 | 10:2bd9f4e02b74 | 55 | |
xiaxia686 | 10:2bd9f4e02b74 | 56 | float PwmIn::dutycycle() { |
xiaxia686 | 10:2bd9f4e02b74 | 57 | return _pulsewidth / _period; |
xiaxia686 | 10:2bd9f4e02b74 | 58 | } |
xiaxia686 | 10:2bd9f4e02b74 | 59 | |
xiaxia686 | 10:2bd9f4e02b74 | 60 | void PwmIn::rise() { |
xiaxia686 | 10:2bd9f4e02b74 | 61 | _period = _t.read_us(); |
xiaxia686 | 10:2bd9f4e02b74 | 62 | _t.reset(); |
xiaxia686 | 10:2bd9f4e02b74 | 63 | |
xiaxia686 | 10:2bd9f4e02b74 | 64 | |
xiaxia686 | 10:2bd9f4e02b74 | 65 | |
xiaxia686 | 10:2bd9f4e02b74 | 66 | |
xiaxia686 | 10:2bd9f4e02b74 | 67 | } |
xiaxia686 | 10:2bd9f4e02b74 | 68 | |
xiaxia686 | 10:2bd9f4e02b74 | 69 | void PwmIn::fall() { |
xiaxia686 | 10:2bd9f4e02b74 | 70 | _pulsewidth = _t.read_us(); |
xiaxia686 | 10:2bd9f4e02b74 | 71 | |
xiaxia686 | 10:2bd9f4e02b74 | 72 | if (callbackfunc) |
xiaxia686 | 10:2bd9f4e02b74 | 73 | (*callbackfunc)(_pulsewidth); |
xiaxia686 | 10:2bd9f4e02b74 | 74 | |
xiaxia686 | 10:2bd9f4e02b74 | 75 | if (callbackobj && mcallbackfunc) |
xiaxia686 | 10:2bd9f4e02b74 | 76 | (callbackobj->*mcallbackfunc)(_pulsewidth); |
xiaxia686 | 10:2bd9f4e02b74 | 77 | |
xiaxia686 | 10:2bd9f4e02b74 | 78 | } |