Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2105 - 1
#include <linux/joystick.h>
2
#include "main.h"
3
#include "gamepad.h"
4
#include "frame.h"
5
 
6
volatile int *Axis = NULL;
7
volatile char *Button = NULL;
8
volatile float Sensitivity = 1.0;
9
int NumberOfAxesAvailiable;
10
int NumberOfButtonsAvailiable;
11
char GamepadConnected[30];
12
u8 IsActive = 0;
13
u8 ProgramSelect = 0;
14
 
15
int recordingButtonTemp = 125;
16
int padFd;
17
struct js_event js;
18
 
19
struct str_ExternControl externControl;                 //struct for controlling MikroKopter
20
struct str_ExternControl externControlGier;             //struct for controlling Yaw (Gier) only
21
 
22
u8 gamepad_values_convert_for_cam(int tmpvalue){
23
        u8 tmp;
24
        double tmpdouble = tmpvalue;
25
        tmp = ((((tmpdouble/(MaxAxisValue))-1)*125)-131)*-1;
26
        if(tmp == 0){tmp = 255;}
27
        return(tmp);
28
}
29
 
30
//>> Converting gamepad values to values readable by a MikroKopter
31
//------------------------------------------------------------------------------------------------------
32
u8 gamepad_values_convert_for_kopter(int tmpvalue){
33
        u8 tmp;
34
        double tmpdouble = tmpvalue;
35
        tmp = ((((tmpdouble/(MaxAxisValue*Sensitivity))-1)*125)-131)*-1;
36
        /*Increase values exponentially
37
        if(tmpvalue < 0){
38
                tmpdouble = tmpvalue*-1;
39
        }else{tmpdouble = tmpvalue;}
40
        tmp = ((30497*tmpdouble*tmpdouble*tmpdouble)/11187226521286200+(10667778*tmpdouble*tmpdouble)/532725072442200+(2592340312*tmpdouble)/11187226521286);
41
        if(tmpvalue < 0){
42
                tmp = (tmp*-1)+255;
43
        }
44
        */
45
        return(tmp);
46
}
47
 
48
u8 gamepad_values_convert_for_kopter_gas(int tmpvalue){
49
        u8 tmp;
50
        double tmpdouble = tmpvalue;
51
        tmp = ((((tmpdouble/(MaxAxisValue))-1)*125)-131)*-1;
52
        return(tmp);
53
}
54
 
55
int check_if_button_pushed(){
56
        read_gamepad_data();
57
        int buttonpushed = -1;
58
        for (int i = 0; i < NumberOfButtonsAvailiable; i++)
59
        {
60
                if(Button[i] != 0){
61
                        buttonpushed = i;
62
                }
63
        }
64
        return(buttonpushed);
65
}
66
 
67
//>> Check if controls are active
68
//------------------------------------------------------------------------------------------------------
69
void check_if_controls_active(){
70
        if(check_if_button_pushed() == GamepadLayout[17] && IsActive == 0){
71
                IsActive = 1;
72
                while(1){if(!value_sum(0)){break;};}
73
        }else if(check_if_button_pushed() == GamepadLayout[17] && IsActive == 1){
74
                IsActive = 0;
75
                while(1){if(!value_sum(0)){break;};}
76
        }
77
}
78
//>> Check if program has been switched
79
//------------------------------------------------------------------------------------------------------
80
void check_program_switch(){
81
        if(check_if_button_pushed() == GamepadLayout[11]){
82
                frame_print("", 1, 0);
83
                ProgramSelect = 1;
84
        }else if(check_if_button_pushed() == GamepadLayout[12]){
85
                frame_print("", 1, 0);
86
                ProgramSelect = 2;
87
        }
88
}
89
 
90
//>> Check if sensitivity of the controls has been adjusted
91
//------------------------------------------------------------------------------------------------------
92
void get_sensitivity(){
93
        if(check_if_button_pushed() == GamepadLayout[15] && Sensitivity <= 2.0){
94
                if(Sensitivity < 2){buzzer_short(1);}
95
                Sensitivity += 0.2;
96
                while(1){if(!value_sum(0)){break;};}
97
        }else if(check_if_button_pushed() == GamepadLayout[16] && Sensitivity > 1.0){
98
                if(Sensitivity > 1){buzzer_short(1);}
99
                Sensitivity -= 0.2;
100
                while(1){if(!value_sum(0)){break;};}
101
        }
102
}
103
 
104
//>> Preparing Data for remotely controlling a Camera-Gimbal
105
//------------------------------------------------------------------------------------------------------
106
void create_data_camera(serial_data_struct* pdata_package_camera_control, serial_data_struct* pdata_package_camera_control_gier){
107
        pdata_package_camera_control->data[0] = gamepad_values_convert_for_cam(Axis[GamepadLayout[0]]);                                                                                         //Nick
108
        pdata_package_camera_control->data[1] = gamepad_values_convert_for_cam(Axis[GamepadLayout[1]]);                                                                                         //Roll
109
        pdata_package_camera_control->data[2] = gamepad_values_convert_for_cam(Axis[GamepadLayout[3]]);                                                                                         //
110
        if(Button[GamepadLayout[8]] > 0){recordingButtonTemp = 131;}else if(Button[GamepadLayout[9]] > 0){recordingButtonTemp = 125;}
111
        pdata_package_camera_control->data[3] = recordingButtonTemp;
112
        if(Button[GamepadLayout[10]]){pdata_package_camera_control->data[4] = 131;}else{pdata_package_camera_control->data[4] = 125;}
113
        int i = 5;
114
        while(i < 12){
115
                pdata_package_camera_control->data[i] = 0;
116
                i++;
117
        }
118
        //Add Yaw (Gier)-funcionality
119
        externControlGier.Gier = gamepad_values_convert_for_kopter(Axis[GamepadLayout[2]]);
120
        externControlGier.Config = EC_VALID | EC_GAS_ADD;
121
        externControlGier.Frame = 'z';                                                                          //character for echo
122
        u8 *tmpData = (unsigned char *) &externControlGier;
123
        for (int i = 0; i < pdata_package_camera_control_gier->cmdLength; ++i)
124
        {
125
                pdata_package_camera_control_gier->data[i] = tmpData[i];
126
        }
127
 
128
        //Print out values on screen
129
        char tmpbuffer[255];
130
        sprintf(tmpbuffer, "Nick: %d    \n     Roll: %d     \n     Gier: %d     \n     Zoom: %d     \n     Aufnahme: %d    \n     Auslösung: %d", pdata_package_camera_control->data[0], pdata_package_camera_control->data[1], pdata_package_camera_control_gier->data[2], pdata_package_camera_control->data[2], pdata_package_camera_control->data[3], pdata_package_camera_control->data[4]);
131
        frame_values("VALUES", 5, 2);
132
        frame_values(tmpbuffer, 5, 4);
133
}
134
 
135
//>> Preparing Data for remotely controlling a MikroKopter
136
//------------------------------------------------------------------------------------------------------
137
void create_data_kopter(serial_data_struct* pdata_package_kopter_control){
138
        if(check_if_button_pushed() == GamepadLayout[13]){                              //Motor start
139
                externControl.Nick = 131;
140
                externControl.Roll = 131;
141
                externControl.Gier = 131;
142
                externControl.Gas = 131;       
143
        }else if(check_if_button_pushed() == GamepadLayout[14]){                //Motor stop
144
                externControl.Nick = 131;
145
                externControl.Roll = 125;
146
                externControl.Gier = 125;
147
                externControl.Gas = 131;
148
        }else{
149
                externControl.Nick = gamepad_values_convert_for_kopter(Axis[GamepadLayout[4]]);
150
                externControl.Roll = gamepad_values_convert_for_kopter(Axis[GamepadLayout[5]]);
151
                externControl.Gier = gamepad_values_convert_for_kopter(Axis[GamepadLayout[6]]);
152
                externControl.Gas = gamepad_values_convert_for_kopter_gas(Axis[GamepadLayout[7]]);
153
        }
154
        externControl.Config = EC_VALID | EC_GAS_ADD | EC_IGNORE_RC;
155
        externControl.Frame = 'z';                                                                              //character for echo
156
        u8 *tmpData = (unsigned char *) &externControl;
157
        for (int i = 0; i < pdata_package_kopter_control->cmdLength; ++i)
158
        {
159
                pdata_package_kopter_control->data[i] = tmpData[i];
160
        }
161
        //Print out values on screen
162
        char tmpbuffer[255];
163
        sprintf(tmpbuffer, "Nick: %d    \n     Roll: %d     \n     Gier: %d     \n     Gas : %d   \n", pdata_package_kopter_control->data[0], pdata_package_kopter_control->data[1], pdata_package_kopter_control->data[2], pdata_package_kopter_control->data[3]);
164
        frame_values("VALUES", 5, 2);
165
        frame_values(tmpbuffer, 5, 4);
166
}
167
 
168
void read_gamepad_data(){
169
        read(padFd, &js, sizeof(struct js_event));
170
        switch (js.type & ~JS_EVENT_INIT)
171
        {
172
                case JS_EVENT_AXIS:
173
                        Axis   [ js.number ] = js.value;
174
                        break;
175
                case JS_EVENT_BUTTON:
176
                        Button [ js.number ] = js.value;
177
                        break;
178
        }
179
}
180
 
181
//>> Initialize Gamepad
182
//------------------------------------------------------------------------------------------------------
183
void gamepad_init(){
184
        if((padFd = open(GAME_PAD, O_RDONLY)) == -1)
185
        {
186
                printf( "Couldn't open gamepad\n" );
187
        }
188
        ioctl( padFd, JSIOCGAXES, &NumberOfAxesAvailiable );
189
        ioctl( padFd, JSIOCGBUTTONS, &NumberOfButtonsAvailiable );
190
        ioctl( padFd, JSIOCGNAME(80), &GamepadConnected );
191
        Axis = (int *) calloc( NumberOfAxesAvailiable, sizeof( int ) );
192
        Button = (char *) calloc( NumberOfButtonsAvailiable, sizeof( char ) );
193
        fcntl( padFd, F_SETFL, O_NONBLOCK );    /* use non-blocking mode */
194
}