Subversion Repositories Projects

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1465 - 1
/*********************************************************************/
2
/*                                                                                                                                                                                                                                                                       */
3
/*                                                                       NG-Video 5,8GHz                                                                                                                                 */
4
/*                                                                                                                                                                                                                                                                       */
5
/*                                                              Copyright (C) 2011 - gebad                                                                                               */
6
/*                                                                                                                                                                                                                                                                       */
7
/*      This code is distributed under the GNU Public License                                            */
8
/*      which can be found at http://www.gnu.org/licenses/gpl.txt                                */
9
/*                                                                                                                                                                                                                                                                       */
10
/*      using                                                                                                                                                                                                                                    */
11
/*! \file servo.c \brief Interrupt-driven RC Servo function library. */
12
/*                                                                                                                                                                                                                                                                       */
13
/*File Name             : 'servo.c'                                                                                                                                                                              */
14
/*Title                         : Interrupt-driven RC Servo function library                                     */
15
/*Author                        : Pascal Stang - Copyright (C) 2002                                                                              */
16
/*Created                       : 7/31/2002                                                                                                                                                                              */
17
/*Revised                       : 8/02/2002                                                                                                                                                                              */
18
/*Version                       : 1.0                                                                                                                                                                                                    */
19
/*Target MCU    : Atmel AVR Series                                                                                                                                               */
20
/*Editor Tabs   : 4                                                                                                                                                                                                              */
21
/*                                                                                                                                                                                                                                                                       */
22
/*ingroup driver_sw                                                                                                                                                                                                      */
23
/*defgroup servo Interrupt-driven RC Servo Function Library (servo.c)*/
24
/*code #include "servo.h" \endcode                                                                                                                                       */
25
/*par Overview                                                                                                                                                                                                                   */
26
/*                                                                                                                                                                                                                                                                       */
27
/*This code is distributed under the GNU Public License                                                  */
28
/*which can be found at http://www.gnu.org/licenses/gpl.txt                                      */
29
/*                                                                                                                                                                                                                                                                       */
30
/*********************************************************************/
31
 
32
#ifndef SERVO_H
33
#define SERVO_H
34
 
35
// The numbers below good for parallax servos at an F_CPU of 20.000MHz.
36
// Da einige Servo's auch eien Winkel von 180 grd zulassen, Wertebereich
37
// entgegen den sonst üblichen. Einschränkung mit default Kalibrierung
38
// auf 0,9ms (45) bis 2,1ms(45) gesetzt. Je nach Servo, entspricht einen
39
// Winkel von etwa 180grd
40
// Periode default 20ms
41
 
42
#define SERVO_MAX                               211     // 2,7 ms bei prescaler 256, bei prescaler 64 SERVO_MAX * 4
43
#define SERVO_MIN                                26     // 0,33ms bei prescaler 256, bei prescaler 64 SERVO_MIN * 4
44
#define SERVO_STEPS                     255     // Servo-Schritte von links nach rechts, Anschlagkalibrierung spielt keine Rolle
45
#define SERVO_PRESCALER 256     // bei prescaler 256, bei prescaler 64 SERVO_PRESCALER / 4
46
#define STEPS_255                                       0       // Servo-Schritte von links nach rechts, Anschlagkalibrierung spielt keine Rolle
47
#define STEPS_1023                              1       // Servo-Schritte von links nach rechts, Anschlagkalibrierung spielt keine Rolle
48
 
49
typedef struct //Servo-Konstante je nach Prescaler
50
{
51
        uint16_t max;
52
        uint16_t min;
53
        uint16_t steps;
54
        uint16_t prescaler;
55
}ServoConst_t;
56
 
57
typedef struct //struct_ServoChannel
58
{
59
        uint8_t  pin;                                           // hardware I/O port and pin for this channel
60
        uint16_t duty;                                  // PWM duty setting which corresponds to servo position
61
        uint8_t  rev;                                           // Parameter, wie on/off; reverse; range
62
        uint16_t min;                                           // SERVO_MIN + Parameter min
63
        uint16_t max;                                           // SERVO_MAX - Parameter max
64
        uint16_t mid_scaled;            // skalierte Servomitte
65
        int16_t  mid;                                           // Servomitte = SERVO_STEPS/2 +/- x Schritte; bei Pescaler 256 wird nur uint8_t benötigt aber bei 64
66
}ServoChannelType;
67
 
68
uint8_t          sIdxSteps;                     // 0 für 255 und 1 für 1023 Schritte; Prescaler 256 oder 64
69
 
70
// Define servo limits (depend on hardware)
71
typedef struct {
72
        uint16_t min;
73
        uint16_t max;
74
}servo_limit_t;
75
 
76
extern const servo_limit_t servo_limit[2][3];
77
 
78
// Define servo positions (depend on hardware)
79
typedef struct {
80
        uint16_t min;
81
        uint16_t max;
82
        uint16_t mid;
83
}steps_pw_t;
84
 
85
// Servopositionen für 950µs, 2,05ms und 1,5ms ==> Ergebnis Schritte. Da Zeit in µs ist F_CPU*e-1
86
extern const steps_pw_t steps_pw[2];
87
 
88
#define RIGHT                                   0               // Servopostionen, welche vom Einbau abhängig sind
89
#define LEFT                                    1
90
#define MIDDLE                          2
91
 
92
#define POSIDX_MAX              4
93
extern const uint8_t PosIdx[POSIDX_MAX];                // anzufahrende Servopositionen 0=MIN, 1=MID, 2=MAX
94
 
95
// functions
96
 
97
// initializes servo system
98
//                              You must run this to begin servo control
99
void servoInit(uint8_t servo_period);
100
 
101
// turns off servo system
102
//                              This stops controlling the servos and
103
//                              returns control of the SERVOPORT to your code
104
void servoOff(void);
105
 
106
// set servo position on a given channel 
107
//                              servoSetPosition() commands the servo on <channel> to the position you
108
//                              desire. The position input must lie between 0 and POSITION_MAX and
109
//                              will be automatically scaled to raw positions between SERVO_MIN and
110
//                              SERVO_MAX
111
void servoSetPosition(uint8_t channel, uint16_t position);
112
 
113
// set raw servo position on a given channel
114
//                              Works like non-raw commands but position is not scaled. Position must
115
//                              be between SERVO_MIN and SERVO_MAX
116
void servoSetPositionRaw(uint8_t channel, uint16_t position);
117
 
118
// set servo to a specific angle
119
void servoSetAngle(uint8_t servo_nr, int16_t angle);
120
 
121
// vor servoInit(), oder vor sei() ServoWerte mit servoSet...() initialisieren, einschließlich servoSetPosition(...)!
122
void servoSet_rev(uint8_t channel, uint8_t val);
123
void servoSet_min(uint8_t channel, uint16_t min);
124
void servoSet_max(uint8_t channel, uint16_t max);
125
void servoSet_mid(uint8_t channel, uint16_t mid);
126
uint16_t pw_us(uint16_t Steps); // gibt Zeit in µs bei x Servoschritte
127
uint16_t ServoSteps(void);                      // gibt "Konstante" derzeitiger Servoschritte zürück
128
 
129
#endif /* SERVO_H */