1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-11-29 10:24:12 +01:00

Servo library to format 1.5 rev.2

This commit is contained in:
Cristian Maglie 2014-01-02 00:20:31 +01:00
parent 03a7cf3212
commit 4b7302692c
6 changed files with 626 additions and 620 deletions

View File

@ -1,10 +1,8 @@
name=Servo name=Servo
author= version=1.0
email= author=Michael Margolis, Arduino
maintainer=Arduino <info@arduino.cc>
sentence=Controls a lot of Servos. sentence=Controls a lot of Servos.
paragraph=This library can control a great number of servos.<br />It makes careful use of timers: the library can control 12 servos using only 1 timer.<br />On the Arduino Due you can control up to 60 servos.<br /> paragraph=This library can control a great number of servos.<br />It makes careful use of timers: the library can control 12 servos using only 1 timer.<br />On the Arduino Due you can control up to 60 servos.<br />
url=http://arduino.cc/en/Reference/Servo url=http://arduino.cc/en/Reference/Servo
architectures=avr,sam architectures=avr,sam
version=1.0
dependencies=
core-dependencies=arduino (>=1.5.0)

View File

@ -59,7 +59,13 @@
*/ */
// Architecture specific include // Architecture specific include
#include <ServoTimers.h> #if defined(ARDUINO_ARCH_AVR)
#include "avr/ServoTimers.h"
#elif defined(ARDUINO_ARCH_SAM)
#include "sam/ServoTimers.h"
#else
#error "This library only supports boards with an AVR or SAM processor."
#endif
#define Servo_VERSION 2 // software version of this library #define Servo_VERSION 2 // software version of this library

View File

@ -17,6 +17,8 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#if defined(ARDUINO_ARCH_AVR)
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <Arduino.h> #include <Arduino.h>
@ -280,7 +282,7 @@ void Servo::writeMicroseconds(int value)
else if( value > SERVO_MAX() ) else if( value > SERVO_MAX() )
value = SERVO_MAX(); value = SERVO_MAX();
value = value - TRIM_DURATION; value = value - TRIM_DURATION;
value = usToTicks(value); // convert to ticks after compensating for interrupt overhead - 12 Aug 2009 value = usToTicks(value); // convert to ticks after compensating for interrupt overhead - 12 Aug 2009
uint8_t oldSREG = SREG; uint8_t oldSREG = SREG;
@ -311,3 +313,5 @@ bool Servo::attached()
return servos[this->servoIndex].Pin.isActive ; return servos[this->servoIndex].Pin.isActive ;
} }
#endif // ARDUINO_ARCH_AVR

View File

@ -36,24 +36,24 @@
#define _useTimer1 #define _useTimer1
#define _useTimer3 #define _useTimer3
#define _useTimer4 #define _useTimer4
typedef enum { _timer5, _timer1, _timer3, _timer4, _Nbr_16timers } timer16_Sequence_t ; typedef enum { _timer5, _timer1, _timer3, _timer4, _Nbr_16timers } timer16_Sequence_t;
#elif defined(__AVR_ATmega32U4__) #elif defined(__AVR_ATmega32U4__)
#define _useTimer1 #define _useTimer1
typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t ; typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t;
#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
#define _useTimer3 #define _useTimer3
#define _useTimer1 #define _useTimer1
typedef enum { _timer3, _timer1, _Nbr_16timers } timer16_Sequence_t ; typedef enum { _timer3, _timer1, _Nbr_16timers } timer16_Sequence_t;
#elif defined(__AVR_ATmega128__) ||defined(__AVR_ATmega1281__)||defined(__AVR_ATmega2561__) #elif defined(__AVR_ATmega128__) ||defined(__AVR_ATmega1281__)||defined(__AVR_ATmega2561__)
#define _useTimer3 #define _useTimer3
#define _useTimer1 #define _useTimer1
typedef enum { _timer3, _timer1, _Nbr_16timers } timer16_Sequence_t ; typedef enum { _timer3, _timer1, _Nbr_16timers } timer16_Sequence_t;
#else // everything else #else // everything else
#define _useTimer1 #define _useTimer1
typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t ; typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t;
#endif #endif

View File

@ -1,21 +1,22 @@
/* /*
Servo.cpp - Interrupt driven Servo library for Arduino using 16 bit timers - Version 2 Copyright (c) 2013 Arduino LLC. All right reserved.
Copyright (c) 2009 Michael Margolis. All right reserved.
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#if defined(ARDUINO_ARCH_SAM)
#include <Arduino.h> #include <Arduino.h>
#include <Servo.h> #include <Servo.h>
@ -23,7 +24,6 @@
#define usToTicks(_us) (( clockCyclesPerMicrosecond() * _us) / 32) // converts microseconds to tick #define usToTicks(_us) (( clockCyclesPerMicrosecond() * _us) / 32) // converts microseconds to tick
#define ticksToUs(_ticks) (( (unsigned)_ticks * 32)/ clockCyclesPerMicrosecond() ) // converts from ticks back to microseconds #define ticksToUs(_ticks) (( (unsigned)_ticks * 32)/ clockCyclesPerMicrosecond() ) // converts from ticks back to microseconds
#define TRIM_DURATION 2 // compensation ticks to trim adjust for digitalWrite delays #define TRIM_DURATION 2 // compensation ticks to trim adjust for digitalWrite delays
static servo_t servos[MAX_SERVOS]; // static array of servo structures static servo_t servos[MAX_SERVOS]; // static array of servo structures
@ -43,9 +43,6 @@ static volatile int8_t Channel[_Nbr_16timers ]; // counter for the s
/************ static functions common to all instances ***********************/ /************ static functions common to all instances ***********************/
//timer16_Sequence_t timer;
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
/// Interrupt handler for the TC0 channel 1. /// Interrupt handler for the TC0 channel 1.
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -111,7 +108,7 @@ static void _initISR(Tc *tc, uint32_t channel, uint32_t id, IRQn_Type irqn)
{ {
pmc_enable_periph_clk(id); pmc_enable_periph_clk(id);
TC_Configure(tc, channel, TC_Configure(tc, channel,
TC_CMR_TCCLKS_TIMER_CLOCK3 | // MCK/32 TC_CMR_TCCLKS_TIMER_CLOCK3 | // MCK/32
TC_CMR_WAVE | // Waveform mode TC_CMR_WAVE | // Waveform mode
TC_CMR_WAVSEL_UP_RC ); // Counter running up and reset when equals to RC TC_CMR_WAVSEL_UP_RC ); // Counter running up and reset when equals to RC
@ -131,23 +128,23 @@ static void initISR(timer16_Sequence_t timer)
{ {
#if defined (_useTimer1) #if defined (_useTimer1)
if (timer == _timer1) if (timer == _timer1)
_initISR(TC_FOR_TIMER1, CHANNEL_FOR_TIMER1, ID_TC_FOR_TIMER1, IRQn_FOR_TIMER1); _initISR(TC_FOR_TIMER1, CHANNEL_FOR_TIMER1, ID_TC_FOR_TIMER1, IRQn_FOR_TIMER1);
#endif #endif
#if defined (_useTimer2) #if defined (_useTimer2)
if (timer == _timer2) if (timer == _timer2)
_initISR(TC_FOR_TIMER2, CHANNEL_FOR_TIMER2, ID_TC_FOR_TIMER2, IRQn_FOR_TIMER2); _initISR(TC_FOR_TIMER2, CHANNEL_FOR_TIMER2, ID_TC_FOR_TIMER2, IRQn_FOR_TIMER2);
#endif #endif
#if defined (_useTimer3) #if defined (_useTimer3)
if (timer == _timer3) if (timer == _timer3)
_initISR(TC_FOR_TIMER3, CHANNEL_FOR_TIMER3, ID_TC_FOR_TIMER3, IRQn_FOR_TIMER3); _initISR(TC_FOR_TIMER3, CHANNEL_FOR_TIMER3, ID_TC_FOR_TIMER3, IRQn_FOR_TIMER3);
#endif #endif
#if defined (_useTimer4) #if defined (_useTimer4)
if (timer == _timer4) if (timer == _timer4)
_initISR(TC_FOR_TIMER4, CHANNEL_FOR_TIMER4, ID_TC_FOR_TIMER4, IRQn_FOR_TIMER4); _initISR(TC_FOR_TIMER4, CHANNEL_FOR_TIMER4, ID_TC_FOR_TIMER4, IRQn_FOR_TIMER4);
#endif #endif
#if defined (_useTimer5) #if defined (_useTimer5)
if (timer == _timer5) if (timer == _timer5)
_initISR(TC_FOR_TIMER5, CHANNEL_FOR_TIMER5, ID_TC_FOR_TIMER5, IRQn_FOR_TIMER5); _initISR(TC_FOR_TIMER5, CHANNEL_FOR_TIMER5, ID_TC_FOR_TIMER5, IRQn_FOR_TIMER5);
#endif #endif
} }
@ -187,7 +184,7 @@ Servo::Servo()
{ {
if (ServoCount < MAX_SERVOS) { if (ServoCount < MAX_SERVOS) {
this->servoIndex = ServoCount++; // assign a servo index to this instance this->servoIndex = ServoCount++; // assign a servo index to this instance
servos[this->servoIndex].ticks = usToTicks(DEFAULT_PULSE_WIDTH); // store default values servos[this->servoIndex].ticks = usToTicks(DEFAULT_PULSE_WIDTH); // store default values
} else { } else {
this->servoIndex = INVALID_SERVO; // too many servos this->servoIndex = INVALID_SERVO; // too many servos
} }
@ -282,3 +279,5 @@ bool Servo::attached()
return servos[this->servoIndex].Pin.isActive; return servos[this->servoIndex].Pin.isActive;
} }
#endif // ARDUINO_ARCH_SAM

View File

@ -1,6 +1,5 @@
/* /*
Servo.h - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2 Copyright (c) 2013 Arduino LLC. All right reserved.
Copyright (c) 2009 Michael Margolis. All right reserved.
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
@ -9,12 +8,12 @@
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
/* /*