aboutsummaryrefslogtreecommitdiff
path: root/libraries/Servo
diff options
context:
space:
mode:
authorDavid A. Mellis <d.mellis@arduino.cc>2009-06-01 08:32:11 +0000
committerDavid A. Mellis <d.mellis@arduino.cc>2009-06-01 08:32:11 +0000
commitdb605dd18b11ecfb5cd9f92c721c52cb70543384 (patch)
tree8a9029ffc560970ce1204ba86785ad44ef044906 /libraries/Servo
First integration of the Arduino code in Processing 5503: PreProcessor and Compiler have been integrated with changes to the Sketch.
Compilation still has problems (Thread error on success, and can't handle non-pde files in a sketch). Modified the Mac OS X make.sh to copy the hardware, avr tools, and example over. Removing some of the antlr stuff. Disabling the Commander (command-line execution) for now. Added Library, LibraryManager, and Target. Added support for prefixed preferences (e.g. for boards and programmers).
Diffstat (limited to 'libraries/Servo')
-rwxr-xr-xlibraries/Servo/Servo.cpp133
-rwxr-xr-xlibraries/Servo/Servo.h52
-rw-r--r--libraries/Servo/examples/Knob/Knob.pde22
-rw-r--r--libraries/Servo/examples/Sweep/Sweep.pde29
-rwxr-xr-xlibraries/Servo/keywords.txt22
5 files changed, 258 insertions, 0 deletions
diff --git a/libraries/Servo/Servo.cpp b/libraries/Servo/Servo.cpp
new file mode 100755
index 0000000..8578fef
--- /dev/null
+++ b/libraries/Servo/Servo.cpp
@@ -0,0 +1,133 @@
+#include <avr/interrupt.h>
+#include <wiring.h>
+#include <Servo.h>
+
+/*
+ Servo.h - Hardware Servo Timer Library
+ Author: Jim Studt, jim@federated.com
+ Copyright (c) 2007 David A. Mellis. All right reserved.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ 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,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+
+uint8_t Servo::attached9 = 0;
+uint8_t Servo::attached10 = 0;
+
+void Servo::seizeTimer1()
+{
+ uint8_t oldSREG = SREG;
+
+ cli();
+ TCCR1A = _BV(WGM11); /* Fast PWM, ICR1 is top */
+ TCCR1B = _BV(WGM13) | _BV(WGM12) /* Fast PWM, ICR1 is top */
+ | _BV(CS11) /* div 8 clock prescaler */
+ ;
+ OCR1A = 3000;
+ OCR1B = 3000;
+ ICR1 = clockCyclesPerMicrosecond()*(20000L/8); // 20000 uS is a bit fast for the refresh, 20ms, but
+ // it keeps us from overflowing ICR1 at 20MHz clocks
+ // That "/8" at the end is the prescaler.
+#if defined(__AVR_ATmega8__)
+ TIMSK &= ~(_BV(TICIE1) | _BV(OCIE1A) | _BV(OCIE1B) | _BV(TOIE1) );
+#else
+ TIMSK1 &= ~(_BV(OCIE1A) | _BV(OCIE1B) | _BV(TOIE1) );
+#endif
+
+ SREG = oldSREG; // undo cli()
+}
+
+void Servo::releaseTimer1() {}
+
+#define NO_ANGLE (0xff)
+
+Servo::Servo() : pin(0), angle(NO_ANGLE) {}
+
+uint8_t Servo::attach(int pinArg)
+{
+ return attach(pinArg, 544, 2400);
+}
+
+uint8_t Servo::attach(int pinArg, int min, int max)
+{
+ if (pinArg != 9 && pinArg != 10) return 0;
+
+ min16 = min / 16;
+ max16 = max / 16;
+
+ pin = pinArg;
+ angle = NO_ANGLE;
+ digitalWrite(pin, LOW);
+ pinMode(pin, OUTPUT);
+
+ if (!attached9 && !attached10) seizeTimer1();
+
+ if (pin == 9) {
+ attached9 = 1;
+ TCCR1A = (TCCR1A & ~_BV(COM1A0)) | _BV(COM1A1);
+ }
+
+ if (pin == 10) {
+ attached10 = 1;
+ TCCR1A = (TCCR1A & ~_BV(COM1B0)) | _BV(COM1B1);
+ }
+ return 1;
+}
+
+void Servo::detach()
+{
+ // muck with timer flags
+ if (pin == 9) {
+ attached9 = 0;
+ TCCR1A = TCCR1A & ~_BV(COM1A0) & ~_BV(COM1A1);
+ pinMode(pin, INPUT);
+ }
+
+ if (pin == 10) {
+ attached10 = 0;
+ TCCR1A = TCCR1A & ~_BV(COM1B0) & ~_BV(COM1B1);
+ pinMode(pin, INPUT);
+ }
+
+ if (!attached9 && !attached10) releaseTimer1();
+}
+
+void Servo::write(int angleArg)
+{
+ uint16_t p;
+
+ if (angleArg < 0) angleArg = 0;
+ if (angleArg > 180) angleArg = 180;
+ angle = angleArg;
+
+ // bleh, have to use longs to prevent overflow, could be tricky if always a 16MHz clock, but not true
+ // That 8L on the end is the TCNT1 prescaler, it will need to change if the clock's prescaler changes,
+ // but then there will likely be an overflow problem, so it will have to be handled by a human.
+ p = (min16*16L*clockCyclesPerMicrosecond() + (max16-min16)*(16L*clockCyclesPerMicrosecond())*angle/180L)/8L;
+ if (pin == 9) OCR1A = p;
+ if (pin == 10) OCR1B = p;
+}
+
+uint8_t Servo::read()
+{
+ return angle;
+}
+
+uint8_t Servo::attached()
+{
+ if (pin == 9 && attached9) return 1;
+ if (pin == 10 && attached10) return 1;
+ return 0;
+}
diff --git a/libraries/Servo/Servo.h b/libraries/Servo/Servo.h
new file mode 100755
index 0000000..0b0e8db
--- /dev/null
+++ b/libraries/Servo/Servo.h
@@ -0,0 +1,52 @@
+#ifndef Servo_h
+#define Servo_h
+
+/*
+ Servo.h - Hardware Servo Timer Library
+ Author: Jim Studt, jim@federated.com
+ Copyright (c) 2007 David A. Mellis. All right reserved.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ 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,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#include <inttypes.h>
+
+class Servo
+{
+ private:
+ uint8_t pin;
+ uint8_t angle; // in degrees
+ uint8_t min16; // minimum pulse, 16uS units (default is 34)
+ uint8_t max16; // maximum pulse, 16uS units, 0-4ms range (default is 150)
+ static void seizeTimer1();
+ static void releaseTimer1();
+ static uint8_t attached9;
+ static uint8_t attached10;
+ public:
+ Servo();
+ uint8_t attach(int);
+ // pulse length for 0 degrees in microseconds, 544uS default
+ // pulse length for 180 degrees in microseconds, 2400uS default
+ uint8_t attach(int, int, int);
+ // attach to a pin, sets pinMode, returns 0 on failure, won't
+ // position the servo until a subsequent write() happens
+ // Only works for 9 and 10.
+ void detach();
+ void write(int); // specify the angle in degrees, 0 to 180
+ uint8_t read();
+ uint8_t attached();
+};
+
+#endif
diff --git a/libraries/Servo/examples/Knob/Knob.pde b/libraries/Servo/examples/Knob/Knob.pde
new file mode 100644
index 0000000..886e107
--- /dev/null
+++ b/libraries/Servo/examples/Knob/Knob.pde
@@ -0,0 +1,22 @@
+// Controlling a servo position using a potentiometer (variable resistor)
+// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
+
+#include <Servo.h>
+
+Servo myservo; // create servo object to control a servo
+
+int potpin = 0; // analog pin used to connect the potentiometer
+int val; // variable to read the value from the analog pin
+
+void setup()
+{
+ myservo.attach(9); // attaches the servo on pin 9 to the servo object
+}
+
+void loop()
+{
+ val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
+ val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
+ myservo.write(val); // sets the servo position according to the scaled value
+ delay(15); // waits for the servo to get there
+}
diff --git a/libraries/Servo/examples/Sweep/Sweep.pde b/libraries/Servo/examples/Sweep/Sweep.pde
new file mode 100644
index 0000000..52e6056
--- /dev/null
+++ b/libraries/Servo/examples/Sweep/Sweep.pde
@@ -0,0 +1,29 @@
+// Sweep
+// by BARRAGAN <http://barraganstudio.com>
+
+#include <Servo.h>
+
+Servo myservo; // create servo object to control a servo
+ // a maximum of eight servo objects can be created
+
+int pos = 0; // variable to store the servo position
+
+void setup()
+{
+ myservo.attach(9); // attaches the servo on pin 9 to the servo object
+}
+
+
+void loop()
+{
+ for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
+ { // in steps of 1 degree
+ myservo.write(pos); // tell servo to go to position in variable 'pos'
+ delay(15); // waits 15ms for the servo to reach the position
+ }
+ for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
+ {
+ myservo.write(pos); // tell servo to go to position in variable 'pos'
+ delay(15); // waits 15ms for the servo to reach the position
+ }
+}
diff --git a/libraries/Servo/keywords.txt b/libraries/Servo/keywords.txt
new file mode 100755
index 0000000..918c46a
--- /dev/null
+++ b/libraries/Servo/keywords.txt
@@ -0,0 +1,22 @@
+#######################################
+# Syntax Coloring Map Servo
+#######################################
+
+#######################################
+# Datatypes (KEYWORD1)
+#######################################
+
+Servo KEYWORD1
+
+#######################################
+# Methods and Functions (KEYWORD2)
+#######################################
+attach KEYWORD2
+detach KEYWORD2
+write KEYWORD2
+read KEYWORD2
+attached KEYWORD2
+
+#######################################
+# Constants (LITERAL1)
+#######################################