first release. It is in the process of creation. Library for Motorfader https://www.switch-science.com/catalog/1285/
Motorfader.cpp@0:6b9fc326f973, 2019-07-29 (annotated)
- Committer:
- abanum
- Date:
- Mon Jul 29 05:42:03 2019 +0000
- Revision:
- 0:6b9fc326f973
first release.; It is in the process of creation.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
abanum | 0:6b9fc326f973 | 1 | /* mbed Motorfader Library |
abanum | 0:6b9fc326f973 | 2 | * |
abanum | 0:6b9fc326f973 | 3 | * Copyright (c) 2007-2010 sford, cstyles |
abanum | 0:6b9fc326f973 | 4 | * |
abanum | 0:6b9fc326f973 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
abanum | 0:6b9fc326f973 | 6 | * of this software and associated documentation files (the "Software"), to deal |
abanum | 0:6b9fc326f973 | 7 | * in the Software without restriction, including without limitation the rights |
abanum | 0:6b9fc326f973 | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
abanum | 0:6b9fc326f973 | 9 | * copies of the Software, and to permit persons to whom the Software is |
abanum | 0:6b9fc326f973 | 10 | * furnished to do so, subject to the following conditions: |
abanum | 0:6b9fc326f973 | 11 | * |
abanum | 0:6b9fc326f973 | 12 | * The above copyright notice and this permission notice shall be included in |
abanum | 0:6b9fc326f973 | 13 | * all copies or substantial portions of the Software. |
abanum | 0:6b9fc326f973 | 14 | * |
abanum | 0:6b9fc326f973 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
abanum | 0:6b9fc326f973 | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
abanum | 0:6b9fc326f973 | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
abanum | 0:6b9fc326f973 | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
abanum | 0:6b9fc326f973 | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
abanum | 0:6b9fc326f973 | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
abanum | 0:6b9fc326f973 | 21 | * THE SOFTWARE. |
abanum | 0:6b9fc326f973 | 22 | */ |
abanum | 0:6b9fc326f973 | 23 | |
abanum | 0:6b9fc326f973 | 24 | #include "Motorfader.h" |
abanum | 0:6b9fc326f973 | 25 | #include "mbed.h" |
abanum | 0:6b9fc326f973 | 26 | |
abanum | 0:6b9fc326f973 | 27 | Motorfader::Motorfader(PinName anarogpin,PinName cwpin,PinName ccwpin) : _faderPos(anarogpin) ,_cwpwm(cwpin),_ccwpwm(ccwpin) { |
abanum | 0:6b9fc326f973 | 28 | _cwpwm.period(0.020); |
abanum | 0:6b9fc326f973 | 29 | _ccwpwm.period(0.020); |
abanum | 0:6b9fc326f973 | 30 | _cwpwm.write(1.0); |
abanum | 0:6b9fc326f973 | 31 | _ccwpwm.write(1.0); |
abanum | 0:6b9fc326f973 | 32 | _setPos = 0.0; |
abanum | 0:6b9fc326f973 | 33 | _setVel = 1.0; |
abanum | 0:6b9fc326f973 | 34 | wait_ms(10); |
abanum | 0:6b9fc326f973 | 35 | } |
abanum | 0:6b9fc326f973 | 36 | |
abanum | 0:6b9fc326f973 | 37 | void Motorfader::set(float pos,float vel ) { |
abanum | 0:6b9fc326f973 | 38 | _setPos = pos; |
abanum | 0:6b9fc326f973 | 39 | _setVel = vel; |
abanum | 0:6b9fc326f973 | 40 | wait_ms(10); |
abanum | 0:6b9fc326f973 | 41 | } |
abanum | 0:6b9fc326f973 | 42 | |
abanum | 0:6b9fc326f973 | 43 | float Motorfader::get() { |
abanum | 0:6b9fc326f973 | 44 | return _faderPos.read(); |
abanum | 0:6b9fc326f973 | 45 | } |
abanum | 0:6b9fc326f973 | 46 | |
abanum | 0:6b9fc326f973 | 47 | void Motorfader::update() { |
abanum | 0:6b9fc326f973 | 48 | float P,D = 0; |
abanum | 0:6b9fc326f973 | 49 | _nowPos = _faderPos.read(); |
abanum | 0:6b9fc326f973 | 50 | _cwpwm.write(0.0); |
abanum | 0:6b9fc326f973 | 51 | _ccwpwm.write(0.0); |
abanum | 0:6b9fc326f973 | 52 | |
abanum | 0:6b9fc326f973 | 53 | P = abs(_setPos - _nowPos)*4.0f; |
abanum | 0:6b9fc326f973 | 54 | if (P >1.0f ) |
abanum | 0:6b9fc326f973 | 55 | { |
abanum | 0:6b9fc326f973 | 56 | P =1.0f; |
abanum | 0:6b9fc326f973 | 57 | // _oldPos = _nowPos; |
abanum | 0:6b9fc326f973 | 58 | } |
abanum | 0:6b9fc326f973 | 59 | // D = -abs(_oldPos - _nowPos)*1.0f; |
abanum | 0:6b9fc326f973 | 60 | // _oldPos = _nowPos; |
abanum | 0:6b9fc326f973 | 61 | if((_nowPos+0.03f) < _setPos) |
abanum | 0:6b9fc326f973 | 62 | { |
abanum | 0:6b9fc326f973 | 63 | _cwpwm.write(_setVel*(P+D)); |
abanum | 0:6b9fc326f973 | 64 | _ccwpwm.write(0.0f); |
abanum | 0:6b9fc326f973 | 65 | // printf("inc "); |
abanum | 0:6b9fc326f973 | 66 | } |
abanum | 0:6b9fc326f973 | 67 | if((_nowPos-0.03f) > _setPos) |
abanum | 0:6b9fc326f973 | 68 | { |
abanum | 0:6b9fc326f973 | 69 | _cwpwm.write(0.0f); |
abanum | 0:6b9fc326f973 | 70 | _ccwpwm.write(_setVel*(P+D)); |
abanum | 0:6b9fc326f973 | 71 | // printf("dec "); |
abanum | 0:6b9fc326f973 | 72 | } |
abanum | 0:6b9fc326f973 | 73 | // printf("nowpos=%1.3f setpos==%1.3f\r\n",_nowPos,_setPos); |
abanum | 0:6b9fc326f973 | 74 | |
abanum | 0:6b9fc326f973 | 75 | } |