Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
nps_radio_control_joystick.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 Antoine Drouin <poinix@gmail.com>
3  * Copyright (C) 2012 The Paparazzi Team
4  *
5  * This file is part of paparazzi.
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, write to
19  * the Free Software Foundation, 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 
69 #include "nps_radio_control.h"
71 
72 // for NPS_JS_AXIS_MODE
73 #include "generated/airframe.h"
74 
75 #include <stdio.h>
76 #include <string.h>
77 #include <stdlib.h>
78 #include <SDL/SDL.h>
79 
80 // axes indice
81 #ifndef NPS_JS_AXIS_ROLL
82 #define NPS_JS_AXIS_ROLL 0
83 #endif
84 #ifndef NPS_JS_AXIS_PITCH
85 #define NPS_JS_AXIS_PITCH 1
86 #endif
87 #ifndef NPS_JS_AXIS_YAW
88 #define NPS_JS_AXIS_YAW 2
89 #endif
90 #ifndef NPS_JS_AXIS_THROTTLE
91 #define NPS_JS_AXIS_THROTTLE 3
92 #endif
93 
94 #ifndef NPS_JS_AXIS_MODE
95 #define JS_NB_AXIS 4
96 #else
97 #define JS_NB_AXIS 5
98 #endif
99 
100 // buttons to switch modes
101 #ifndef NPS_JS_BUTTON_MODE_MANUAL
102 #define NPS_JS_BUTTON_MODE_MANUAL 1
103 #endif
104 #ifndef NPS_JS_BUTTON_MODE_AUTO1
105 #define NPS_JS_BUTTON_MODE_AUTO1 2
106 #endif
107 #ifndef NPS_JS_BUTTON_MODE_AUTO2
108 #define NPS_JS_BUTTON_MODE_AUTO2 3
109 #endif
110 
111 #ifndef NPS_JS_AXIS_MODE
112 #define JS_NB_BUTTONS 3
113 #else
114 #define JS_NB_BUTTONS 0
115 #endif
116 
118 SDL_Joystick *sdl_joystick;
119 SDL_Event sdl_event;
120 
130 int nps_radio_control_joystick_init(const char *device)
131 {
132 
133  nps_joystick.throttle = 0.5;
134  nps_joystick.roll = 0.;
135  nps_joystick.pitch = 0.;
136  nps_joystick.yaw = 0.;
138 
139  //Convert device index string to integer
140  int device_index = atoi(device);
141 
142  //Initialize SDL with joystick support and event support (through video)
143  if (SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO) < 0) {
144  printf("Could not initialize SDL: %s.\n", SDL_GetError());
145  exit(-1);
146  }
147 
148  //Quit SDL at exit
149  atexit(SDL_Quit);
150 
151  //Start the event handler, disable all but joystick button events and quit handler
152  SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
153  SDL_EventState(SDL_KEYDOWN, SDL_IGNORE);
154  SDL_EventState(SDL_KEYUP, SDL_IGNORE);
155  SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
156  SDL_EventState(SDL_MOUSEBUTTONDOWN, SDL_IGNORE);
157  SDL_EventState(SDL_MOUSEBUTTONUP, SDL_IGNORE);
158  SDL_EventState(SDL_JOYAXISMOTION, SDL_IGNORE);
159  SDL_EventState(SDL_JOYBALLMOTION, SDL_IGNORE);
160  SDL_EventState(SDL_JOYHATMOTION, SDL_IGNORE);
161  //SDL_EventState(SDL_JOYBUTTONDOWN,SDL_IGNORE);
162  SDL_EventState(SDL_JOYBUTTONUP, SDL_IGNORE);
163  SDL_EventState(SDL_VIDEORESIZE, SDL_IGNORE);
164  SDL_EventState(SDL_VIDEOEXPOSE, SDL_IGNORE);
165  //SDL_EventState(SDL_QUIT,SDL_IGNORE);
166  SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
167  SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
168 
169  //Check there are actually joysticks attached
170  if (!SDL_NumJoysticks()) {
171  printf("No joysticks attached! Quitting.\n");
172  exit(-1);
173  }
174 
175  //Open the desired joystick and make sure it will work
176  sdl_joystick = SDL_JoystickOpen(device_index);
177  if (!sdl_joystick) {
178  printf("Joystick corresponding to SDL Index %d failed to open! Exiting.\n", device_index);
179  exit(-1);
180  } else if (SDL_JoystickNumAxes(sdl_joystick) < JS_NB_AXIS) {
181  printf("Selected joystick does not support enough axes!\n");
182  printf("Number of axes required: %i\n", JS_NB_AXIS);
183  printf("Number of axes available: %i\n", SDL_JoystickNumAxes(sdl_joystick));
184  SDL_JoystickClose(sdl_joystick);
185  exit(-1);
186  } else if (SDL_JoystickNumButtons(sdl_joystick) < JS_NB_BUTTONS) {
187  printf("Selected joystick does not support enough buttons!\n");
188  printf("Buttons supported: %d needed: %d\n", SDL_JoystickNumButtons(sdl_joystick), JS_NB_BUTTONS);
189  SDL_JoystickClose(sdl_joystick);
190  exit(-1);
191  } else {
192  printf("Using joystick named: %s\n", SDL_JoystickName(device_index));
193  }
194 
195  return 0;
196 }
197 
202 {
203 
204 #if NPS_JS_AXIS_THROTTLE_REVERSED
205  nps_joystick.throttle = ((float)(-1 * SDL_JoystickGetAxis(sdl_joystick, NPS_JS_AXIS_THROTTLE)) - 32767.) / -65534.;
206 #else
207  nps_joystick.throttle = ((float)(SDL_JoystickGetAxis(sdl_joystick, NPS_JS_AXIS_THROTTLE)) - 32767.) / -65534.;
208 #endif
209 #if NPS_JS_AXIS_ROLL_REVERSED
210  nps_joystick.roll = (float)(-1 * SDL_JoystickGetAxis(sdl_joystick, NPS_JS_AXIS_ROLL)) / 32767.;
211 #else
212  nps_joystick.roll = (float)(SDL_JoystickGetAxis(sdl_joystick, NPS_JS_AXIS_ROLL)) / 32767.;
213 #endif
214 #if NPS_JS_AXIS_PITCH_REVERSED
215  nps_joystick.pitch = (float)(-1 * SDL_JoystickGetAxis(sdl_joystick, NPS_JS_AXIS_PITCH)) / 32767.;
216 #else
217  nps_joystick.pitch = (float)(SDL_JoystickGetAxis(sdl_joystick, NPS_JS_AXIS_PITCH)) / 32767.;
218 #endif
219 #if NPS_JS_AXIS_YAW_REVERSED
220  nps_joystick.yaw = (float)(-1 * SDL_JoystickGetAxis(sdl_joystick, NPS_JS_AXIS_YAW)) / 32767.;
221 #else
222  nps_joystick.yaw = (float)(SDL_JoystickGetAxis(sdl_joystick, NPS_JS_AXIS_YAW)) / 32767.;
223 #endif
224  // if an axis is asigned to the mode, use it instead of the buttons
225 #ifdef NPS_JS_AXIS_MODE
226 #if NPS_JS_AXIS_MODE_REVERSED
227  nps_joystick.mode = (float)(-1 * SDL_JoystickGetAxis(sdl_joystick, NPS_JS_AXIS_MODE)) / 32767.;
228 #else
229  nps_joystick.mode = (float)(SDL_JoystickGetAxis(sdl_joystick, NPS_JS_AXIS_MODE)) / 32767.;
230 #endif
231 #endif
232 
233  while (SDL_PollEvent(&sdl_event)) {
234  switch (sdl_event.type) {
235  case SDL_JOYBUTTONDOWN: {
236  switch (sdl_event.jbutton.button) {
237 #ifndef NPS_JS_AXIS_MODE
240  break;
241 
244  break;
245 
248  break;
249 #endif
250  default:
251  //ignore
252  break;
253  }
254  }
255  break;
256 
257  case SDL_QUIT:
258  printf("Quitting...\n");
259  exit(-1);
260  break;
261 
262  default:
263  //do nothing
264  printf("unknown SDL event!!!\n");
265  break;
266  }
267  }
268 }
NPS_JS_BUTTON_MODE_AUTO2
#define NPS_JS_BUTTON_MODE_AUTO2
Definition: nps_radio_control_joystick.c:108
NpsJoystick::roll
double roll
Definition: nps_radio_control_joystick.h:31
nps_joystick
struct NpsJoystick nps_joystick
Definition: nps_radio_control_joystick.c:117
NPS_JS_AXIS_THROTTLE
#define NPS_JS_AXIS_THROTTLE
Definition: nps_radio_control_joystick.c:91
JS_NB_AXIS
#define JS_NB_AXIS
Definition: nps_radio_control_joystick.c:95
MODE_SWITCH_MANUAL
#define MODE_SWITCH_MANUAL
Definition: nps_radio_control.h:27
NpsJoystick::pitch
double pitch
Definition: nps_radio_control_joystick.h:32
sdl_joystick
SDL_Joystick * sdl_joystick
Definition: nps_radio_control_joystick.c:118
nps_radio_control.h
NPS_JS_AXIS_PITCH
#define NPS_JS_AXIS_PITCH
Definition: nps_radio_control_joystick.c:85
NPS_JS_BUTTON_MODE_MANUAL
#define NPS_JS_BUTTON_MODE_MANUAL
Definition: nps_radio_control_joystick.c:102
NpsJoystick
Definition: nps_radio_control_joystick.h:29
MODE_SWITCH_AUTO2
#define MODE_SWITCH_AUTO2
Definition: nps_radio_control.h:29
NpsJoystick::mode
double mode
Definition: nps_radio_control_joystick.h:34
NpsJoystick::throttle
double throttle
Definition: nps_radio_control_joystick.h:30
NpsJoystick::yaw
double yaw
Definition: nps_radio_control_joystick.h:33
JS_NB_BUTTONS
#define JS_NB_BUTTONS
Definition: nps_radio_control_joystick.c:112
MODE_SWITCH_AUTO1
#define MODE_SWITCH_AUTO1
Definition: nps_radio_control.h:28
NPS_JS_AXIS_YAW
#define NPS_JS_AXIS_YAW
Definition: nps_radio_control_joystick.c:88
NPS_JS_AXIS_ROLL
#define NPS_JS_AXIS_ROLL
Definition: nps_radio_control_joystick.c:82
nps_radio_control_joystick_init
int nps_radio_control_joystick_init(const char *device)
Initializes SDL and the joystick.
Definition: nps_radio_control_joystick.c:130
nps_radio_control_joystick_update
void nps_radio_control_joystick_update(void)
Updates joystick buttons from events, directly reads current axis positions.
Definition: nps_radio_control_joystick.c:201
NPS_JS_BUTTON_MODE_AUTO1
#define NPS_JS_BUTTON_MODE_AUTO1
Definition: nps_radio_control_joystick.c:105
nps_radio_control_joystick.h
sdl_event
SDL_Event sdl_event
Definition: nps_radio_control_joystick.c:119