Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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
120
130int nps_radio_control_joystick_init(const char *device)
131{
132
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)
144 printf("Could not initialize SDL: %s.\n", SDL_GetError());
145 exit(-1);
146 }
147
148 //Quit SDL at exit
150
151 //Start the event handler, disable all but joystick button events and quit handler
161 //SDL_EventState(SDL_JOYBUTTONDOWN,SDL_IGNORE);
165 //SDL_EventState(SDL_QUIT,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
177 if (!sdl_joystick) {
178 printf("Joystick corresponding to SDL Index %d failed to open! Exiting.\n", device_index);
179 exit(-1);
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));
185 exit(-1);
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);
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
206#else
208#endif
209#if NPS_JS_AXIS_ROLL_REVERSED
211#else
213#endif
214#if NPS_JS_AXIS_PITCH_REVERSED
216#else
218#endif
219#if NPS_JS_AXIS_YAW_REVERSED
221#else
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
228#else
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}
uint16_t foo
Definition main_demo5.c:58
#define MODE_SWITCH_MANUAL
#define MODE_SWITCH_AUTO1
#define MODE_SWITCH_AUTO2
void nps_radio_control_joystick_update(void)
Updates joystick buttons from events, directly reads current axis positions.
SDL_Event sdl_event
#define NPS_JS_BUTTON_MODE_MANUAL
#define NPS_JS_BUTTON_MODE_AUTO2
struct NpsJoystick nps_joystick
#define NPS_JS_AXIS_ROLL
#define JS_NB_AXIS
#define NPS_JS_BUTTON_MODE_AUTO1
int nps_radio_control_joystick_init(const char *device)
Initializes SDL and the joystick.
#define NPS_JS_AXIS_THROTTLE
#define JS_NB_BUTTONS
#define NPS_JS_AXIS_PITCH
#define NPS_JS_AXIS_YAW
SDL_Joystick * sdl_joystick