MHVLib
20111011
An efficiency oriented runtime library for AVR microcontrollers
|
00001 /* 00002 * Copyright (c) 2011, Make, Hack, Void Inc 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * * Redistributions of source code must retain the above copyright 00008 * notice, this list of conditions and the following disclaimer. 00009 * * Redistributions in binary form must reproduce the above copyright 00010 * notice, this list of conditions and the following disclaimer in the 00011 * documentation and/or other materials provided with the distribution. 00012 * * Neither the name of the Make, Hack, Void nor the 00013 * names of its contributors may be used to endorse or promote products 00014 * derived from this software without specific prior written permission. 00015 * 00016 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00017 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00018 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00019 * DISCLAIMED. IN NO EVENT SHALL MAKE, HACK, VOID BE LIABLE FOR ANY 00020 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00021 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00022 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00023 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00024 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00025 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 */ 00027 00028 00029 #ifndef MHV_SERVOCONTROL_H_ 00030 #define MHV_SERVOCONTROL_H_ 00031 00032 #include <MHV_Timer16.h> 00033 00034 #ifdef MHV_TIMER16_1 00035 00036 // 1ms in timer ticks 00037 #define MHV_SERVO_MIN (F_CPU / (1000L)) 00038 // 2ms in timer ticks 00039 #define MHV_SERVO_MAX ((F_CPU * 2) / (1000L)) 00040 00041 #define MHV_MAX_SERVO_COUNT 254 00042 00043 #define MHV_SERVOCONTROL_DECLARE_TRIGGER(mhvTimer16,mhvServo) \ 00044 void mhvServo##Trigger(void *data) { \ 00045 mhvServo.refreshServos(data);\ 00046 } 00047 00048 #define MHV_SERVOCONTROL_ASSIGN_TRIGGER(mhvTimer16,mhvServo) \ 00049 mhvTimer16.setTriggers(&(mhvServo##Trigger), 0, 0, 0, 0, 0) 00050 00051 /* The servoOrder member is not really part of the control block, but instead is an easy way 00052 * to have an array of N things we can use to specify the order the servos should be serviced 00053 */ 00054 struct MHV_ServoControlBlock { 00055 volatile uint8_t *port; 00056 uint8_t pin; 00057 uint16_t position; 00058 int16_t clockMinOffset; 00059 int16_t clockMaxOffset; 00060 uint8_t servoOrder; // This isn't really a member of the control structure 00061 }; 00062 typedef struct MHV_ServoControlBlock MHV_SERVOCONTROLBLOCK; 00063 00064 class MHV_ServoControl { 00065 private: 00066 MHV_Timer16 *_timer; 00067 MHV_SERVOCONTROLBLOCK *_controlBlocks; 00068 uint8_t _count; 00069 volatile uint8_t _nextServoIndex; 00070 00071 void sortServos(); 00072 00073 public: 00074 MHV_ServoControl(MHV_Timer16 *timer, MHV_SERVOCONTROLBLOCK *controlBlocks, uint8_t count); 00075 void addServo(uint8_t servo, volatile uint8_t *dir, volatile uint8_t *out, volatile uint8_t *in, uint8_t pin, int8_t pinchangeInterrupt); 00076 void tweakServo(uint8_t servo, int8_t minOffset, int8_t maxOffset); 00077 void positionServo(uint8_t servo, uint16_t position); 00078 bool canPosition(); 00079 void positionServoBusyWait(uint8_t servo, uint16_t position); 00080 void refreshServos(void *data); 00081 void enable(); 00082 void disable(); 00083 }; 00084 00085 #endif 00086 00087 #endif /* MHV_SERVOCONTROL_H_ */