Subversion Repositories FlightCtrl

Rev

Rev 1161 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1157 krheinwald 1
/* pitch_inc.c
2
 *
3
 * Copyright 2009 Klaus Rheinwald
4
 *
5
 */
6
 
7
#include "main.h"
8
#include "parameter.h"
9
#include "fc.h"
10
#include "pitch.h"
11
 
12
#define STICK_FACTOR    ((int16_t) 256)
1162 krheinwald 13
#define MAX_STICK       125
14
#define MIN_STICK       -MAX_STICK
1157 krheinwald 15
 
1161 krheinwald 16
static int16_t          stickValue;                             // Aktueller Stick-Wert
1157 krheinwald 17
 
1161 krheinwald 18
static unsigned char expTable[] = {                             // Tabelle zum Mappen auf exponentielle Werten
19
    1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,5,5,5,5,6,
20
    6,6,6,7,7,7,8,8,8,9,9,10,10,10,11,11,12,12,13,14,14,15,15,16,17,18,18,19,20,21,22,
21
    23,24,25,26,27,29,30,31,33,34,36,37,39,41,43,45,47,49,51,53,56,58,61,64,66,69,73,
22
    76,79,83,87,91,95,99,103,108,113,118,123,129,135,141,147,154,161,168,176,184,192,
23
    200,210,219,229,239,250
24
};
25
 
1157 krheinwald 26
int pitch_inc_value( void ) {
27
 
1161 krheinwald 28
        int8_t  rawStickDiff  = PPM_diff[EE_Parameter.Kanalbelegung[K_GAS]];      //-125..+125
29
//      int32_t rawStickValue = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]];        //-125..+125
30
        int16_t rawStickValue = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]];        //-125..+125
1157 krheinwald 31
 
32
        DebugOut.Analog[25] = rawStickValue;
33
 
1160 krheinwald 34
//      rawStickValue = ((rawStickValue * rawStickValue * rawStickValue) / (PARAM_INC_GAS_SCALE+1))  / (PARAM_INC_GAS_SCALE+1);
1161 krheinwald 35
//      rawStickValue = (rawStickValue * rawStickValue * rawStickValue) / (PARAM_INC_GAS_SCALE+1);
1160 krheinwald 36
//      rawStickValue = (rawStickValue * rawStickValue) / ((rawStickValue > 0 ? PARAM_INC_GAS_SCALE : -PARAM_INC_GAS_SCALE) +1);
1157 krheinwald 37
 
1161 krheinwald 38
        if (rawStickValue > 3) {
1162 krheinwald 39
            if (rawStickValue > MAX_STICK)
40
                rawStickValue = MAX_STICK;
1161 krheinwald 41
            rawStickValue = expTable[rawStickValue] * PARAM_INC_GAS_P_SCALE;
42
        } else if (rawStickValue < -3) {
1162 krheinwald 43
            if (rawStickValue < MIN_STICK)
44
                rawStickValue = MIN_STICK;
1161 krheinwald 45
            rawStickValue = -expTable[-rawStickValue] * PARAM_INC_GAS_P_SCALE;
46
        }
47
 
48
        DebugOut.Analog[23] = rawStickValue;
49
 
1157 krheinwald 50
        // Neuer Stick-Wert
1162 krheinwald 51
        if (rawStickValue < 0 && stickValue < MIN_STICK*STICK_FACTOR - rawStickValue) // ToDo
52
            stickValue = MIN_STICK*STICK_FACTOR;
53
        else if (rawStickValue > 0 && stickValue > MAX_STICK*STICK_FACTOR - rawStickValue )
54
            stickValue = MAX_STICK*STICK_FACTOR;
1157 krheinwald 55
        else if (rawStickValue != 0)
56
            stickValue += rawStickValue;
1161 krheinwald 57
 
58
        if (rawStickValue * rawStickDiff > 0) {     // Add only when moving awy from center
1162 krheinwald 59
            if (abs(stickValue) < MAX_STICK*STICK_FACTOR - abs(rawStickDiff * PARAM_INC_GAS_D_SCALE)) {  // Do not wrap around
1161 krheinwald 60
                stickValue += rawStickDiff * PARAM_INC_GAS_D_SCALE;
61
                DebugOut.Analog[24] = rawStickDiff * PARAM_INC_GAS_D_SCALE;
62
            }
63
        }
1157 krheinwald 64
 
65
        DebugOut.Analog[26] = stickValue;
1162 krheinwald 66
        DebugOut.Analog[31] = (stickValue / STICK_FACTOR) + MAX_STICK;
1157 krheinwald 67
 
1162 krheinwald 68
        return (stickValue / STICK_FACTOR) + MAX_STICK;        //0..+250
1157 krheinwald 69
}
70
 
71
void pitch_inc_init( void ) {
72
 
73
    printf("\r\npitch_inc_init()");
1161 krheinwald 74
    printf("\r\n sizeof(expTable) = %d\r\n", sizeof(expTable));
1157 krheinwald 75
 
1161 krheinwald 76
 
1162 krheinwald 77
    stickValue = MIN_STICK*STICK_FACTOR;
1157 krheinwald 78
 
79
    pitchValueFP = pitch_inc_value;
80
}
81