Eurobot2012_Primary

Dependencies:   mbed Eurobot_2012_Primary

Committer:
narshu
Date:
Wed Oct 17 22:22:47 2012 +0000
Revision:
26:0995f61cb7b8
Parent:
23:1901cb6d0d95
Eurobot 2012 Primary;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
narshu 23:1901cb6d0d95 1 /* mbed Servo Library without using PWM pins
narshu 23:1901cb6d0d95 2 * Copyright (c) 2010 Jasper Denkers
narshu 23:1901cb6d0d95 3 *
narshu 23:1901cb6d0d95 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
narshu 23:1901cb6d0d95 5 * of this software and associated documentation files (the "Software"), to deal
narshu 23:1901cb6d0d95 6 * in the Software without restriction, including without limitation the rights
narshu 23:1901cb6d0d95 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
narshu 23:1901cb6d0d95 8 * copies of the Software, and to permit persons to whom the Software is
narshu 23:1901cb6d0d95 9 * furnished to do so, subject to the following conditions:
narshu 23:1901cb6d0d95 10 *
narshu 23:1901cb6d0d95 11 * The above copyright notice and this permission notice shall be included in
narshu 23:1901cb6d0d95 12 * all copies or substantial portions of the Software.
narshu 23:1901cb6d0d95 13 *
narshu 23:1901cb6d0d95 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
narshu 23:1901cb6d0d95 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
narshu 23:1901cb6d0d95 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
narshu 23:1901cb6d0d95 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
narshu 23:1901cb6d0d95 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
narshu 23:1901cb6d0d95 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
narshu 23:1901cb6d0d95 20 * THE SOFTWARE.
narshu 23:1901cb6d0d95 21 */
narshu 23:1901cb6d0d95 22
narshu 23:1901cb6d0d95 23 #ifndef MBED_SERVO_H
narshu 23:1901cb6d0d95 24 #define MBED_SERVO_H
narshu 23:1901cb6d0d95 25
narshu 23:1901cb6d0d95 26 #include "mbed.h"
narshu 23:1901cb6d0d95 27
narshu 23:1901cb6d0d95 28 /** Class to control a servo on any pin, without using pwm
narshu 23:1901cb6d0d95 29 *
narshu 23:1901cb6d0d95 30 * Example:
narshu 23:1901cb6d0d95 31 * @code
narshu 23:1901cb6d0d95 32 * // Keep sweeping servo from left to right
narshu 23:1901cb6d0d95 33 * #include "mbed.h"
narshu 23:1901cb6d0d95 34 * #include "Servo.h"
narshu 23:1901cb6d0d95 35 *
narshu 23:1901cb6d0d95 36 * Servo Servo1(p20);
narshu 23:1901cb6d0d95 37 *
narshu 23:1901cb6d0d95 38 * Servo1.Enable(1500,20000);
narshu 23:1901cb6d0d95 39 *
narshu 23:1901cb6d0d95 40 * while(1) {
narshu 23:1901cb6d0d95 41 * for (int pos = 1000; pos < 2000; pos += 25) {
narshu 23:1901cb6d0d95 42 * Servo1.SetPosition(pos);
narshu 23:1901cb6d0d95 43 * wait_ms(20);
narshu 23:1901cb6d0d95 44 * }
narshu 23:1901cb6d0d95 45 * for (int pos = 2000; pos > 1000; pos -= 25) {
narshu 23:1901cb6d0d95 46 * Servo1.SetPosition(pos);
narshu 23:1901cb6d0d95 47 * wait_ms(20);
narshu 23:1901cb6d0d95 48 * }
narshu 23:1901cb6d0d95 49 * }
narshu 23:1901cb6d0d95 50 * @endcode
narshu 23:1901cb6d0d95 51 */
narshu 23:1901cb6d0d95 52
narshu 23:1901cb6d0d95 53 class Servo {
narshu 23:1901cb6d0d95 54
narshu 23:1901cb6d0d95 55 public:
narshu 23:1901cb6d0d95 56 /** Create a new Servo object on any mbed pin
narshu 23:1901cb6d0d95 57 *
narshu 23:1901cb6d0d95 58 * @param Pin Pin on mbed to connect servo to
narshu 23:1901cb6d0d95 59 */
narshu 23:1901cb6d0d95 60 Servo(PinName Pin);
narshu 23:1901cb6d0d95 61
narshu 23:1901cb6d0d95 62 /** Change the position of the servo. Position in us
narshu 23:1901cb6d0d95 63 *
narshu 23:1901cb6d0d95 64 * @param NewPos The new value of the servos position (us)
narshu 23:1901cb6d0d95 65 */
narshu 23:1901cb6d0d95 66 void SetPosition(int NewPos);
narshu 23:1901cb6d0d95 67
narshu 23:1901cb6d0d95 68 /** Enable the servo. Without enabling the servo won't be running. Startposition and period both in us.
narshu 23:1901cb6d0d95 69 *
narshu 23:1901cb6d0d95 70 * @param StartPos The position of the servo to start (us)
narshu 23:1901cb6d0d95 71 * @param Period The time between every pulse. 20000 us = 50 Hz(standard) (us)
narshu 23:1901cb6d0d95 72 */
narshu 23:1901cb6d0d95 73 void Enable(int StartPos, int Period);
narshu 23:1901cb6d0d95 74
narshu 23:1901cb6d0d95 75 /** Disable the servo. After disabling the servo won't get any signal anymore
narshu 23:1901cb6d0d95 76 *
narshu 23:1901cb6d0d95 77 */
narshu 23:1901cb6d0d95 78 void Disable();
narshu 23:1901cb6d0d95 79
narshu 23:1901cb6d0d95 80 private:
narshu 23:1901cb6d0d95 81 void StartPulse();
narshu 23:1901cb6d0d95 82 void EndPulse();
narshu 23:1901cb6d0d95 83
narshu 23:1901cb6d0d95 84 int Position;
narshu 23:1901cb6d0d95 85 DigitalOut ServoPin;
narshu 23:1901cb6d0d95 86 Ticker Pulse;
narshu 23:1901cb6d0d95 87 Timeout PulseStop;
narshu 23:1901cb6d0d95 88 };
narshu 23:1901cb6d0d95 89
narshu 23:1901cb6d0d95 90 #endif