A Servo control library that works on any output pin, not just the PWM pins

Fork of UniServ by Matt Parsons

Committer:
mario_meh
Date:
Wed Feb 08 08:35:32 2017 +0000
Revision:
1:8343f1715462
Parent:
0:dde1a0011645
Zadnje izmjene pred predaju

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bloodline 0:dde1a0011645 1 /* UniServ Servo Library
mario_meh 1:8343f1715462 2 * Copyright (c) 2010 Matt Parsons
mario_meh 1:8343f1715462 3 *
mario_meh 1:8343f1715462 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
mario_meh 1:8343f1715462 5 * of this software and associated documentation files (the "Software"), to deal
mario_meh 1:8343f1715462 6 * in the Software without restriction, including without limitation the rights
mario_meh 1:8343f1715462 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
mario_meh 1:8343f1715462 8 * copies of the Software, and to permit persons to whom the Software is
mario_meh 1:8343f1715462 9 * furnished to do so, subject to the following conditions:
mario_meh 1:8343f1715462 10 *
mario_meh 1:8343f1715462 11 * The above copyright notice and this permission notice shall be included in
mario_meh 1:8343f1715462 12 * all copies or substantial portions of the Software.
mario_meh 1:8343f1715462 13 *
mario_meh 1:8343f1715462 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
mario_meh 1:8343f1715462 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
mario_meh 1:8343f1715462 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
mario_meh 1:8343f1715462 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
mario_meh 1:8343f1715462 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mario_meh 1:8343f1715462 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
mario_meh 1:8343f1715462 20 * THE SOFTWARE.
mario_meh 1:8343f1715462 21 */
bloodline 0:dde1a0011645 22
bloodline 0:dde1a0011645 23
mario_meh 1:8343f1715462 24 /** Default metoda za servo motor C90
mario_meh 1:8343f1715462 25 * @code
mario_meh 1:8343f1715462 26 * #include "mbed.h"
mario_meh 1:8343f1715462 27 * #include "UniServ.h"
mario_meh 1:8343f1715462 28 *
mario_meh 1:8343f1715462 29 * t.start();
mario_meh 1:8343f1715462 30 * sMotor.write_us(2000);
mario_meh 1:8343f1715462 31 * t.stop();
mario_meh 1:8343f1715462 32 * pc.printf("%f\n\r", t.read());
mario_meh 1:8343f1715462 33 * wait_ms(1000);
mario_meh 1:8343f1715462 34 * t.reset();
mario_meh 1:8343f1715462 35 * t.start();
mario_meh 1:8343f1715462 36 * sMotor.write_us(0);
mario_meh 1:8343f1715462 37 * t.stop();
mario_meh 1:8343f1715462 38 * pc.printf("%f\n\r", t.read());
mario_meh 1:8343f1715462 39 * wait_ms(1000);
mario_meh 1:8343f1715462 40 * t.reset();
mario_meh 1:8343f1715462 41 * t.start();
mario_meh 1:8343f1715462 42 * sMotor.write_us(3000);
mario_meh 1:8343f1715462 43 * t.stop();
mario_meh 1:8343f1715462 44 * pc.printf("%f\n\r", t.read());
mario_meh 1:8343f1715462 45 * wait_ms(1000);
mario_meh 1:8343f1715462 46 * t.reset();
mario_meh 1:8343f1715462 47 * t.start();
mario_meh 1:8343f1715462 48 * sMotor.write_us(1000);
mario_meh 1:8343f1715462 49 * t.stop();
mario_meh 1:8343f1715462 50 * pc.printf("%f\n\r", t.read());
mario_meh 1:8343f1715462 51 * wait_ms(1000);
mario_meh 1:8343f1715462 52 * sMotor.Disable();
mario_meh 1:8343f1715462 53 *
mario_meh 1:8343f1715462 54 * Dodao metodu Disable() zbog toga da se oslobodi
mario_meh 1:8343f1715462 55 * cpu dok se ne izvrši slijedeća operacija
mario_meh 1:8343f1715462 56 * @endcode
mario_meh 1:8343f1715462 57 */
bloodline 0:dde1a0011645 58
mario_meh 1:8343f1715462 59 #include "mbed.h"
mario_meh 1:8343f1715462 60 #include "UniServ.h"
mario_meh 1:8343f1715462 61
mario_meh 1:8343f1715462 62 UniServ::UniServ(PinName pin) : ServPin(pin){
mario_meh 1:8343f1715462 63 Period=20000;
mario_meh 1:8343f1715462 64 Position=1500;
mario_meh 1:8343f1715462 65 ServMin=400;
mario_meh 1:8343f1715462 66 ServMax=2600;
mario_meh 1:8343f1715462 67
mario_meh 1:8343f1715462 68 Pulse.attach_us(this,&UniServ::SigStart,Period);
mario_meh 1:8343f1715462 69 }
mario_meh 1:8343f1715462 70
mario_meh 1:8343f1715462 71 void UniServ::SigStart(){
mario_meh 1:8343f1715462 72 ServPin=1;
mario_meh 1:8343f1715462 73 PulseEnd.attach_us(this,&UniServ::SigStop,Position);
mario_meh 1:8343f1715462 74 }
mario_meh 1:8343f1715462 75
mario_meh 1:8343f1715462 76 void UniServ::SigStop(){
mario_meh 1:8343f1715462 77 ServPin=0;
mario_meh 1:8343f1715462 78 }
mario_meh 1:8343f1715462 79
mario_meh 1:8343f1715462 80 void UniServ::write_us(int PosIn){
mario_meh 1:8343f1715462 81 Position=PosIn;
mario_meh 1:8343f1715462 82 if(PosIn<ServMin){Position=ServMin;}
mario_meh 1:8343f1715462 83 if(PosIn>ServMax){Position=ServMax;}
mario_meh 1:8343f1715462 84 }
bloodline 0:dde1a0011645 85
mario_meh 1:8343f1715462 86 int UniServ::read_us(){
mario_meh 1:8343f1715462 87 return Position;
mario_meh 1:8343f1715462 88 }
mario_meh 1:8343f1715462 89 void UniServ::Disable() {
mario_meh 1:8343f1715462 90 Pulse.detach();
mario_meh 1:8343f1715462 91 }
mario_meh 1:8343f1715462 92
mario_meh 1:8343f1715462 93
bloodline 0:dde1a0011645 94