00001 /*! \file servo.h \brief Interrupt-driven RC Servo function library. */ 00002 //***************************************************************************** 00003 // 00004 // File Name : 'servo.h' 00005 // Title : Interrupt-driven RC Servo function library 00006 // Author : Pascal Stang - Copyright (C) 2002 00007 // Created : 7/31/2002 00008 // Revised : 8/02/2002 00009 // Version : 1.0 00010 // Target MCU : Atmel AVR Series 00011 // Editor Tabs : 4 00012 // 00013 // NOTE: you need the latest version (3.2+) of the AVR-GCC compiler to use this 00014 // function library. Download it from http://www.avrfreaks.net/AVRGCC 00015 // 00016 // Description : This code allows you to drive up to 8 RC servos from any 00017 // combination of ports and pins on the AVR processor. Using interrupts, 00018 // this code continuously sends control signals to the servo to maintain 00019 // position even while your code is doing other work. 00020 // 00021 // The servoInit and servoOff effectively turn on and turn off servo 00022 // control. When you run ServoInit, it automatically assigns each 00023 // "channel" of servo control to be output on the SERVO_DEFAULT_PORT. 00024 // One "channel" of servo control can control one servo and must be 00025 // assigned single I/O pin for output. 00026 // 00027 // If you're using all eight channels (SERVO_NUM_CHANNELS = 8), then 00028 // then by default the code will use SERVO_DEFAULT_PORT pins 0-7. 00029 // If you're only using four channels, then pins 0-3 will be used by 00030 // default. 00031 // 00032 // The command servoSetChannelIO(channel, port, pin) allows you to 00033 // reassign the output of any channel to any port and I/O pin you 00034 // choose. For exampe, if you have an RC servo connected to PORTC, pin 6, 00035 // and you wish to use channel 2 to control it, use: 00036 // 00037 // servoSerChannelIO( 2, _SFR_IO_ADDR(PORTC), 6) 00038 // 00039 // (NOTE: you must include the "_SRF_IO_ADDR()" part around your port) 00040 // 00041 // The servoSetPostion and servoGetPosition commands allow you to command 00042 // a given servo to your desired position. The position you request must 00043 // lie between the SERVO_MIN and SERVO_MAX limit you defined. 00044 // 00045 // This code is distributed under the GNU Public License 00046 // which can be found at http://www.gnu.org/licenses/gpl.txt 00047 // 00048 //***************************************************************************** 00049 00050 #ifndef SERVO_H 00051 #define SERVO_H 00052 00053 #include "global.h" 00054 #include "timer.h" 00055 00056 // include configuration 00057 #include "servoconf.h" 00058 00059 // constants/macros/typdefs 00060 00061 // set number of servo channels (1 to 8) 00062 // This is the number of servos you would like to drive 00063 // Each "Channel" can control one servo and by default will 00064 // map directly to the port pin of the same number on the 00065 // SERVO_DEFAULT_PORT. You can change this default port/pin 00066 // assignment for a given channel to any port/pin you like. 00067 // See the "servoSetChannelIO" function below. 00068 #define SERVO_NUM_CHANNELS 4 00069 // set default SERVO output port 00070 // This is the AVR port which you have connected to your servos 00071 // See top of file for how servo "channels" map to port pins 00072 #define SERVO_DEFAULT_PORT PORTB 00073 // set servo characteristics (min and max position) 00074 // You must find these by testing using your brand/type of servos. 00075 // The min/max settings will change proportional to F_CPU, the CPU 00076 // clock frequency. These numbers are good for parallax servos at 00077 // an F_CPU of 8MHz. 00078 #define SERVO_MAX 71 00079 #define SERVO_MIN 17 00080 #define POSITION_MAX 255 00081 00082 typedef struct struct_ServoChannel 00083 { 00084 // hardware I/O port and pin for this channel 00085 u08 port; 00086 u08 pin; 00087 // PWM duty setting which corresponds to servo position 00088 u16 duty; 00089 } ServoChannelType; 00090 00091 // functions 00092 00093 // initializes servo system 00094 // You must run this to begin servo control 00095 void servoInit(void); 00096 00097 // turns off servo system 00098 // This stops controlling the servos and 00099 // returns control of the SERVOPORT to your code 00100 void servoOff(void); 00101 00102 // set the port and I/O pin you wish to use for a given channel 00103 // If you do not assign a port and I/O pin for a channel (ie. you don't 00104 // use this command) then all output will be done through the 00105 // SERVO_DEFAULT_PORT. See above definition of SERVO_DEFAULT_PORT. 00106 void servoSetChannelIO(u08 channel, u08 port, u08 pin); 00107 00108 // set and get servo position on a given channel 00109 // servoSetPosition() commands the servo on <channel> to the position you 00110 // desire. The position input must lie between 0 and POSITION_MAX and 00111 // will be automatically scaled to raw positions between SERVO_MIN and 00112 // SERVO_MAX 00113 // servoGetPosition() returns the most recently set postition of the 00114 // servo on <channel>. The return value will be scaled 0->POSITION_MAX 00115 void servoSetPosition(u08 channel, u08 position); 00116 u08 servoGetPosition(u08 channel); 00117 00118 // set and get raw servo position on a given channel 00119 // Works like non-raw commands but position is not scaled. Position must 00120 // be between SERVO_MIN and SERVO_MAX 00121 void servoSetPositionRaw(u08 channel, u16 position); 00122 u16 servoGetPositionRaw(u08 channel); 00123 00124 // servo interrupt service routine 00125 void servoService(void); 00126 00127 #endif