It use the joystick to control the copter, when the copter bumper the wall, it will death.

Dependencies:   mbed N5110

Committer:
Wuuu
Date:
Fri May 03 12:19:10 2019 +0000
Revision:
0:29bec73b2c04
Copter, all program in a main.cpp, it implement basic function.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wuuu 0:29bec73b2c04 1 #ifndef GAMEPAD_H
Wuuu 0:29bec73b2c04 2 #define GAMEPAD_H
Wuuu 0:29bec73b2c04 3
Wuuu 0:29bec73b2c04 4 #include <bitset>
Wuuu 0:29bec73b2c04 5
Wuuu 0:29bec73b2c04 6 // Forward declaration of the classes that we use from the mbed library
Wuuu 0:29bec73b2c04 7 // This avoids the need for us to include the huge mbed.h header inside our
Wuuu 0:29bec73b2c04 8 // own library API
Wuuu 0:29bec73b2c04 9 namespace mbed
Wuuu 0:29bec73b2c04 10 {
Wuuu 0:29bec73b2c04 11 class AnalogIn;
Wuuu 0:29bec73b2c04 12 class InterruptIn;
Wuuu 0:29bec73b2c04 13 class PwmOut;
Wuuu 0:29bec73b2c04 14 class Timeout;
Wuuu 0:29bec73b2c04 15 }
Wuuu 0:29bec73b2c04 16
Wuuu 0:29bec73b2c04 17 #define TOL 0.1f
Wuuu 0:29bec73b2c04 18 #define RAD2DEG 57.2957795131f
Wuuu 0:29bec73b2c04 19
Wuuu 0:29bec73b2c04 20 /** Enum for direction */
Wuuu 0:29bec73b2c04 21 enum Direction {
Wuuu 0:29bec73b2c04 22 CENTRE, /**< joystick centred */
Wuuu 0:29bec73b2c04 23 N, /**< pushed North (0)*/
Wuuu 0:29bec73b2c04 24 NE, /**< pushed North-East (45) */
Wuuu 0:29bec73b2c04 25 E, /**< pushed East (90) */
Wuuu 0:29bec73b2c04 26 SE, /**< pushed South-East (135) */
Wuuu 0:29bec73b2c04 27 S, /**< pushed South (180) */
Wuuu 0:29bec73b2c04 28 SW, /**< pushed South-West (225) */
Wuuu 0:29bec73b2c04 29 W, /**< pushed West (270) */
Wuuu 0:29bec73b2c04 30 NW /**< pushed North-West (315) */
Wuuu 0:29bec73b2c04 31 };
Wuuu 0:29bec73b2c04 32
Wuuu 0:29bec73b2c04 33 /** Vector 2D struct */
Wuuu 0:29bec73b2c04 34 struct Vector2D {
Wuuu 0:29bec73b2c04 35 float x; /**< float for x value */
Wuuu 0:29bec73b2c04 36 float y; /**< float for y value */
Wuuu 0:29bec73b2c04 37 };
Wuuu 0:29bec73b2c04 38
Wuuu 0:29bec73b2c04 39 /** Polar coordinate struct */
Wuuu 0:29bec73b2c04 40 struct Polar {
Wuuu 0:29bec73b2c04 41 float mag; /**< float for magnitude */
Wuuu 0:29bec73b2c04 42 float angle; /**< float for angle (in degrees) */
Wuuu 0:29bec73b2c04 43 };
Wuuu 0:29bec73b2c04 44
Wuuu 0:29bec73b2c04 45 /** Gamepad Class
Wuuu 0:29bec73b2c04 46 @brief Library for interfacing with ELEC2645 Gamepad PCB, University of Leeds
Wuuu 0:29bec73b2c04 47 @author Dr Craig A. Evans
Wuuu 0:29bec73b2c04 48 @author Dr Alex Valanvanis
Wuuu 0:29bec73b2c04 49 @date Febraury 2017
Wuuu 0:29bec73b2c04 50 */
Wuuu 0:29bec73b2c04 51 class Gamepad
Wuuu 0:29bec73b2c04 52 {
Wuuu 0:29bec73b2c04 53 public:
Wuuu 0:29bec73b2c04 54 /** Gamepad events
Wuuu 0:29bec73b2c04 55 * @brief List of events that can be registered on the gamepad
Wuuu 0:29bec73b2c04 56 */
Wuuu 0:29bec73b2c04 57 enum GamepadEvent {
Wuuu 0:29bec73b2c04 58 A_PRESSED, ///< Button A has been pressed
Wuuu 0:29bec73b2c04 59 B_PRESSED, ///< Button B has been pressed
Wuuu 0:29bec73b2c04 60 X_PRESSED, ///< Button X has been pressed
Wuuu 0:29bec73b2c04 61 Y_PRESSED, ///< Button Y has been pressed
Wuuu 0:29bec73b2c04 62 L_PRESSED, ///< Button L has been pressed
Wuuu 0:29bec73b2c04 63 R_PRESSED, ///< Button R has been pressed
Wuuu 0:29bec73b2c04 64 BACK_PRESSED, ///< Button "Back" has been pressed
Wuuu 0:29bec73b2c04 65 START_PRESSED, ///< Button "Start" has been pressed
Wuuu 0:29bec73b2c04 66 JOY_PRESSED, ///< Joystick button has been pressed
Wuuu 0:29bec73b2c04 67 N_EVENTS ///< A dummy flag that marks the end of the list
Wuuu 0:29bec73b2c04 68 };
Wuuu 0:29bec73b2c04 69 private:
Wuuu 0:29bec73b2c04 70 mbed::PwmOut *_led1;
Wuuu 0:29bec73b2c04 71 mbed::PwmOut *_led2;
Wuuu 0:29bec73b2c04 72 mbed::PwmOut *_led3;
Wuuu 0:29bec73b2c04 73 mbed::PwmOut *_led4;
Wuuu 0:29bec73b2c04 74 mbed::PwmOut *_led5;
Wuuu 0:29bec73b2c04 75 mbed::PwmOut *_led6;
Wuuu 0:29bec73b2c04 76
Wuuu 0:29bec73b2c04 77 mbed::InterruptIn *_button_A;
Wuuu 0:29bec73b2c04 78 mbed::InterruptIn *_button_B;
Wuuu 0:29bec73b2c04 79 mbed::InterruptIn *_button_X;
Wuuu 0:29bec73b2c04 80 mbed::InterruptIn *_button_Y;
Wuuu 0:29bec73b2c04 81 mbed::InterruptIn *_button_L;
Wuuu 0:29bec73b2c04 82 mbed::InterruptIn *_button_R;
Wuuu 0:29bec73b2c04 83 mbed::InterruptIn *_button_back;
Wuuu 0:29bec73b2c04 84 mbed::InterruptIn *_button_start;
Wuuu 0:29bec73b2c04 85 mbed::InterruptIn *_button_joystick;
Wuuu 0:29bec73b2c04 86
Wuuu 0:29bec73b2c04 87 mbed::AnalogIn *_vert;
Wuuu 0:29bec73b2c04 88 mbed::AnalogIn *_horiz;
Wuuu 0:29bec73b2c04 89
Wuuu 0:29bec73b2c04 90 mbed::PwmOut *_buzzer;
Wuuu 0:29bec73b2c04 91 mbed::AnalogIn *_pot;
Wuuu 0:29bec73b2c04 92
Wuuu 0:29bec73b2c04 93 mbed::Timeout *_timeout;
Wuuu 0:29bec73b2c04 94
Wuuu 0:29bec73b2c04 95 std::bitset<N_EVENTS> _event_state; ///< A binary list of buttons that has been pressed
Wuuu 0:29bec73b2c04 96
Wuuu 0:29bec73b2c04 97 // centred x,y values
Wuuu 0:29bec73b2c04 98 float _x0;
Wuuu 0:29bec73b2c04 99 float _y0;
Wuuu 0:29bec73b2c04 100
Wuuu 0:29bec73b2c04 101 public:
Wuuu 0:29bec73b2c04 102
Wuuu 0:29bec73b2c04 103 /** Constructor */
Wuuu 0:29bec73b2c04 104 Gamepad();
Wuuu 0:29bec73b2c04 105
Wuuu 0:29bec73b2c04 106 /** Destructor */
Wuuu 0:29bec73b2c04 107 ~Gamepad();
Wuuu 0:29bec73b2c04 108
Wuuu 0:29bec73b2c04 109 /** Initialise all peripherals and configure interrupts */
Wuuu 0:29bec73b2c04 110 void init();
Wuuu 0:29bec73b2c04 111
Wuuu 0:29bec73b2c04 112 /** Turn all LEDs on */
Wuuu 0:29bec73b2c04 113 void leds_on();
Wuuu 0:29bec73b2c04 114
Wuuu 0:29bec73b2c04 115 /** Turn all LEDs off */
Wuuu 0:29bec73b2c04 116 void leds_off();
Wuuu 0:29bec73b2c04 117
Wuuu 0:29bec73b2c04 118 /** Set all LEDs to duty-cycle
Wuuu 0:29bec73b2c04 119 *@param value in range 0.0 to 1.0
Wuuu 0:29bec73b2c04 120 */
Wuuu 0:29bec73b2c04 121 void leds(float val) const;
Wuuu 0:29bec73b2c04 122
Wuuu 0:29bec73b2c04 123 /** Set LED to duty-cycle
Wuuu 0:29bec73b2c04 124 *@param led number (0 to 5)
Wuuu 0:29bec73b2c04 125 *@param value in range 0.0 to 1.0
Wuuu 0:29bec73b2c04 126 */
Wuuu 0:29bec73b2c04 127 void led(int n,float val) const;
Wuuu 0:29bec73b2c04 128
Wuuu 0:29bec73b2c04 129 /** Read potentiometer
Wuuu 0:29bec73b2c04 130 *@returns potentiometer value in range 0.0 to 1.0
Wuuu 0:29bec73b2c04 131 */
Wuuu 0:29bec73b2c04 132 float read_pot() const;
Wuuu 0:29bec73b2c04 133
Wuuu 0:29bec73b2c04 134 /** Play tone on piezo
Wuuu 0:29bec73b2c04 135 * @param frequency in Hz
Wuuu 0:29bec73b2c04 136 * @param duration of tone in seconds
Wuuu 0:29bec73b2c04 137 */
Wuuu 0:29bec73b2c04 138 void tone(float frequency, float duration);
Wuuu 0:29bec73b2c04 139
Wuuu 0:29bec73b2c04 140 /**
Wuuu 0:29bec73b2c04 141 * @brief Check whether an event flag has been set and clear it
Wuuu 0:29bec73b2c04 142 * @param id[in] The ID of the event to test
Wuuu 0:29bec73b2c04 143 * @return true if the event occurred
Wuuu 0:29bec73b2c04 144 */
Wuuu 0:29bec73b2c04 145 bool check_event(GamepadEvent const id);
Wuuu 0:29bec73b2c04 146
Wuuu 0:29bec73b2c04 147 /** Get magnitude of joystick movement
Wuuu 0:29bec73b2c04 148 * @returns value in range 0.0 to 1.0
Wuuu 0:29bec73b2c04 149 */
Wuuu 0:29bec73b2c04 150
Wuuu 0:29bec73b2c04 151 float get_mag();
Wuuu 0:29bec73b2c04 152
Wuuu 0:29bec73b2c04 153 /** Get angle of joystick movement
Wuuu 0:29bec73b2c04 154 * @returns value in range 0.0 to 359.9. 0.0 corresponds to N, 180.0 to S. -1.0 is central
Wuuu 0:29bec73b2c04 155 */
Wuuu 0:29bec73b2c04 156 float get_angle();
Wuuu 0:29bec73b2c04 157
Wuuu 0:29bec73b2c04 158 /** Gets joystick direction
Wuuu 0:29bec73b2c04 159 * @returns an enum: CENTRE, N, NE, E, SE, S, SW, W, NW,
Wuuu 0:29bec73b2c04 160 */
Wuuu 0:29bec73b2c04 161 Direction get_direction(); // N,NE,E,SE etc.
Wuuu 0:29bec73b2c04 162
Wuuu 0:29bec73b2c04 163 /** Gets raw cartesian co-ordinates of joystick
Wuuu 0:29bec73b2c04 164 * @returns a struct with x,y members, each in the range 0.0 to 1.0
Wuuu 0:29bec73b2c04 165 */
Wuuu 0:29bec73b2c04 166 Vector2D get_coord(); // cartesian co-ordinates x,y
Wuuu 0:29bec73b2c04 167
Wuuu 0:29bec73b2c04 168 /** Gets cartesian coordinates mapped to circular grid
Wuuu 0:29bec73b2c04 169 * @returns a struct with x,y members, each in the range 0.0 to 1.0
Wuuu 0:29bec73b2c04 170 */
Wuuu 0:29bec73b2c04 171 Vector2D get_mapped_coord(); // x,y mapped to circle
Wuuu 0:29bec73b2c04 172
Wuuu 0:29bec73b2c04 173 /** Gets polar coordinates of the joystick
Wuuu 0:29bec73b2c04 174 * @returns a struct contains mag and angle
Wuuu 0:29bec73b2c04 175 */
Wuuu 0:29bec73b2c04 176 Polar get_polar(); // mag and angle in struct form
Wuuu 0:29bec73b2c04 177
Wuuu 0:29bec73b2c04 178 private:
Wuuu 0:29bec73b2c04 179 void init_buttons();
Wuuu 0:29bec73b2c04 180 void tone_off();
Wuuu 0:29bec73b2c04 181
Wuuu 0:29bec73b2c04 182 void a_isr();
Wuuu 0:29bec73b2c04 183 void b_isr();
Wuuu 0:29bec73b2c04 184 void x_isr();
Wuuu 0:29bec73b2c04 185 void y_isr();
Wuuu 0:29bec73b2c04 186 void l_isr();
Wuuu 0:29bec73b2c04 187 void r_isr();
Wuuu 0:29bec73b2c04 188 void back_isr();
Wuuu 0:29bec73b2c04 189 void start_isr();
Wuuu 0:29bec73b2c04 190 void joy_isr();
Wuuu 0:29bec73b2c04 191 };
Wuuu 0:29bec73b2c04 192
Wuuu 0:29bec73b2c04 193 #endif