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 #include "Gamepad.h"
Wuuu 0:29bec73b2c04 2
Wuuu 0:29bec73b2c04 3 #include "mbed.h"
Wuuu 0:29bec73b2c04 4
Wuuu 0:29bec73b2c04 5 //////////// constructor/destructor ////////////
Wuuu 0:29bec73b2c04 6 Gamepad::Gamepad()
Wuuu 0:29bec73b2c04 7 :
Wuuu 0:29bec73b2c04 8 _led1(new PwmOut(PTA1)),
Wuuu 0:29bec73b2c04 9 _led2(new PwmOut(PTA2)),
Wuuu 0:29bec73b2c04 10 _led3(new PwmOut(PTC2)),
Wuuu 0:29bec73b2c04 11 _led4(new PwmOut(PTC3)),
Wuuu 0:29bec73b2c04 12 _led5(new PwmOut(PTC4)),
Wuuu 0:29bec73b2c04 13 _led6(new PwmOut(PTD3)),
Wuuu 0:29bec73b2c04 14
Wuuu 0:29bec73b2c04 15 _button_A(new InterruptIn(PTB9)),
Wuuu 0:29bec73b2c04 16 _button_B(new InterruptIn(PTD0)),
Wuuu 0:29bec73b2c04 17 _button_X(new InterruptIn(PTC17)),
Wuuu 0:29bec73b2c04 18 _button_Y(new InterruptIn(PTC12)),
Wuuu 0:29bec73b2c04 19 _button_L(new InterruptIn(PTB18)),
Wuuu 0:29bec73b2c04 20 _button_R(new InterruptIn(PTB3)),
Wuuu 0:29bec73b2c04 21 _button_back(new InterruptIn(PTB19)),
Wuuu 0:29bec73b2c04 22 _button_start(new InterruptIn(PTC5)),
Wuuu 0:29bec73b2c04 23 _button_joystick(new InterruptIn(PTC16)),
Wuuu 0:29bec73b2c04 24
Wuuu 0:29bec73b2c04 25 _vert(new AnalogIn(PTB10)),
Wuuu 0:29bec73b2c04 26 _horiz(new AnalogIn(PTB11)),
Wuuu 0:29bec73b2c04 27
Wuuu 0:29bec73b2c04 28 _buzzer(new PwmOut(PTC10)),
Wuuu 0:29bec73b2c04 29 _pot(new AnalogIn(PTB2)),
Wuuu 0:29bec73b2c04 30
Wuuu 0:29bec73b2c04 31 _timeout(new Timeout()),
Wuuu 0:29bec73b2c04 32
Wuuu 0:29bec73b2c04 33 _event_state(0),
Wuuu 0:29bec73b2c04 34
Wuuu 0:29bec73b2c04 35 _x0(0),
Wuuu 0:29bec73b2c04 36 _y0(0)
Wuuu 0:29bec73b2c04 37 {}
Wuuu 0:29bec73b2c04 38
Wuuu 0:29bec73b2c04 39 Gamepad::~Gamepad()
Wuuu 0:29bec73b2c04 40 {
Wuuu 0:29bec73b2c04 41 delete _led1,_led2,_led3,_led4,_led5,_led6;
Wuuu 0:29bec73b2c04 42 delete _button_A,_button_B,_button_joystick,_vert,_horiz;
Wuuu 0:29bec73b2c04 43 delete _button_X,_button_Y,_button_back,_button_start;
Wuuu 0:29bec73b2c04 44 delete _button_L,_button_R, _buzzer, _pot, _timeout;
Wuuu 0:29bec73b2c04 45 }
Wuuu 0:29bec73b2c04 46
Wuuu 0:29bec73b2c04 47 ///////////////// public methods /////////////////
Wuuu 0:29bec73b2c04 48
Wuuu 0:29bec73b2c04 49 void Gamepad::init()
Wuuu 0:29bec73b2c04 50 {
Wuuu 0:29bec73b2c04 51 leds_off();
Wuuu 0:29bec73b2c04 52 init_buttons();
Wuuu 0:29bec73b2c04 53
Wuuu 0:29bec73b2c04 54 // read centred values of joystick
Wuuu 0:29bec73b2c04 55 _x0 = _horiz->read();
Wuuu 0:29bec73b2c04 56 _y0 = _vert->read();
Wuuu 0:29bec73b2c04 57
Wuuu 0:29bec73b2c04 58 // clear all flags
Wuuu 0:29bec73b2c04 59 _event_state = 0;
Wuuu 0:29bec73b2c04 60 }
Wuuu 0:29bec73b2c04 61
Wuuu 0:29bec73b2c04 62 void Gamepad::leds_off()
Wuuu 0:29bec73b2c04 63 {
Wuuu 0:29bec73b2c04 64 leds(0.0);
Wuuu 0:29bec73b2c04 65 }
Wuuu 0:29bec73b2c04 66
Wuuu 0:29bec73b2c04 67 void Gamepad::leds_on()
Wuuu 0:29bec73b2c04 68 {
Wuuu 0:29bec73b2c04 69 leds(1.0);
Wuuu 0:29bec73b2c04 70 }
Wuuu 0:29bec73b2c04 71
Wuuu 0:29bec73b2c04 72 void Gamepad::leds(float val) const
Wuuu 0:29bec73b2c04 73 {
Wuuu 0:29bec73b2c04 74 if (val < 0.0f) {
Wuuu 0:29bec73b2c04 75 val = 0.0f;
Wuuu 0:29bec73b2c04 76 }
Wuuu 0:29bec73b2c04 77 if (val > 1.0f) {
Wuuu 0:29bec73b2c04 78 val = 1.0f;
Wuuu 0:29bec73b2c04 79 }
Wuuu 0:29bec73b2c04 80
Wuuu 0:29bec73b2c04 81 // leds are active-low, so subtract from 1.0
Wuuu 0:29bec73b2c04 82 // 0.0 corresponds to fully-off, 1.0 to fully-on
Wuuu 0:29bec73b2c04 83 val = 1.0f - val;
Wuuu 0:29bec73b2c04 84
Wuuu 0:29bec73b2c04 85 _led1->write(val);
Wuuu 0:29bec73b2c04 86 _led2->write(val);
Wuuu 0:29bec73b2c04 87 _led3->write(val);
Wuuu 0:29bec73b2c04 88 _led4->write(val);
Wuuu 0:29bec73b2c04 89 _led5->write(val);
Wuuu 0:29bec73b2c04 90 _led6->write(val);
Wuuu 0:29bec73b2c04 91 }
Wuuu 0:29bec73b2c04 92
Wuuu 0:29bec73b2c04 93 void Gamepad::led(int n,float val) const
Wuuu 0:29bec73b2c04 94 {
Wuuu 0:29bec73b2c04 95 // ensure they are within vlaid range
Wuuu 0:29bec73b2c04 96 if (val < 0.0f) {
Wuuu 0:29bec73b2c04 97 val = 0.0f;
Wuuu 0:29bec73b2c04 98 }
Wuuu 0:29bec73b2c04 99 if (val > 1.0f) {
Wuuu 0:29bec73b2c04 100 val = 1.0f;
Wuuu 0:29bec73b2c04 101 }
Wuuu 0:29bec73b2c04 102
Wuuu 0:29bec73b2c04 103 switch (n) {
Wuuu 0:29bec73b2c04 104
Wuuu 0:29bec73b2c04 105 // check for valid LED number and set value
Wuuu 0:29bec73b2c04 106
Wuuu 0:29bec73b2c04 107 case 1:
Wuuu 0:29bec73b2c04 108 _led1->write(1.0f-val); // active-low so subtract from 1
Wuuu 0:29bec73b2c04 109 break;
Wuuu 0:29bec73b2c04 110 case 2:
Wuuu 0:29bec73b2c04 111 _led2->write(1.0f-val); // active-low so subtract from 1
Wuuu 0:29bec73b2c04 112 break;
Wuuu 0:29bec73b2c04 113 case 3:
Wuuu 0:29bec73b2c04 114 _led3->write(1.0f-val); // active-low so subtract from 1
Wuuu 0:29bec73b2c04 115 break;
Wuuu 0:29bec73b2c04 116 case 4:
Wuuu 0:29bec73b2c04 117 _led4->write(1.0f-val); // active-low so subtract from 1
Wuuu 0:29bec73b2c04 118 break;
Wuuu 0:29bec73b2c04 119 case 5:
Wuuu 0:29bec73b2c04 120 _led5->write(1.0f-val); // active-low so subtract from 1
Wuuu 0:29bec73b2c04 121 break;
Wuuu 0:29bec73b2c04 122 case 6:
Wuuu 0:29bec73b2c04 123 _led6->write(1.0f-val); // active-low so subtract from 1
Wuuu 0:29bec73b2c04 124 break;
Wuuu 0:29bec73b2c04 125
Wuuu 0:29bec73b2c04 126 }
Wuuu 0:29bec73b2c04 127 }
Wuuu 0:29bec73b2c04 128
Wuuu 0:29bec73b2c04 129 float Gamepad::read_pot() const
Wuuu 0:29bec73b2c04 130 {
Wuuu 0:29bec73b2c04 131 return _pot->read();
Wuuu 0:29bec73b2c04 132 }
Wuuu 0:29bec73b2c04 133
Wuuu 0:29bec73b2c04 134 void Gamepad::tone(float frequency, float duration)
Wuuu 0:29bec73b2c04 135 {
Wuuu 0:29bec73b2c04 136 _buzzer->period(1.0f/frequency);
Wuuu 0:29bec73b2c04 137 _buzzer->write(0.5); // 50% duty cycle - square wave
Wuuu 0:29bec73b2c04 138 _timeout->attach(callback(this, &Gamepad::tone_off), duration );
Wuuu 0:29bec73b2c04 139 }
Wuuu 0:29bec73b2c04 140
Wuuu 0:29bec73b2c04 141 bool Gamepad::check_event(GamepadEvent const id)
Wuuu 0:29bec73b2c04 142 {
Wuuu 0:29bec73b2c04 143 // Check whether event flag is set
Wuuu 0:29bec73b2c04 144 if (_event_state[id]) {
Wuuu 0:29bec73b2c04 145 _event_state.reset(id); // clear flag
Wuuu 0:29bec73b2c04 146 return true;
Wuuu 0:29bec73b2c04 147 } else {
Wuuu 0:29bec73b2c04 148 return false;
Wuuu 0:29bec73b2c04 149 }
Wuuu 0:29bec73b2c04 150 }
Wuuu 0:29bec73b2c04 151
Wuuu 0:29bec73b2c04 152 // this method gets the magnitude of the joystick movement
Wuuu 0:29bec73b2c04 153 float Gamepad::get_mag()
Wuuu 0:29bec73b2c04 154 {
Wuuu 0:29bec73b2c04 155 Polar p = get_polar();
Wuuu 0:29bec73b2c04 156 return p.mag;
Wuuu 0:29bec73b2c04 157 }
Wuuu 0:29bec73b2c04 158
Wuuu 0:29bec73b2c04 159 // this method gets the angle of joystick movement (0 to 360, 0 North)
Wuuu 0:29bec73b2c04 160 float Gamepad::get_angle()
Wuuu 0:29bec73b2c04 161 {
Wuuu 0:29bec73b2c04 162 Polar p = get_polar();
Wuuu 0:29bec73b2c04 163 return p.angle;
Wuuu 0:29bec73b2c04 164 }
Wuuu 0:29bec73b2c04 165
Wuuu 0:29bec73b2c04 166 Direction Gamepad::get_direction()
Wuuu 0:29bec73b2c04 167 {
Wuuu 0:29bec73b2c04 168 float angle = get_angle(); // 0 to 360, -1 for centred
Wuuu 0:29bec73b2c04 169
Wuuu 0:29bec73b2c04 170 Direction d;
Wuuu 0:29bec73b2c04 171 // partition 360 into segments and check which segment the angle is in
Wuuu 0:29bec73b2c04 172 if (angle < 0.0f) {
Wuuu 0:29bec73b2c04 173 d = CENTRE; // check for -1.0 angle
Wuuu 0:29bec73b2c04 174 } else if (angle < 22.5f) { // then keep going in 45 degree increments
Wuuu 0:29bec73b2c04 175 d = N;
Wuuu 0:29bec73b2c04 176 } else if (angle < 67.5f) {
Wuuu 0:29bec73b2c04 177 d = NE;
Wuuu 0:29bec73b2c04 178 } else if (angle < 112.5f) {
Wuuu 0:29bec73b2c04 179 d = E;
Wuuu 0:29bec73b2c04 180 } else if (angle < 157.5f) {
Wuuu 0:29bec73b2c04 181 d = SE;
Wuuu 0:29bec73b2c04 182 } else if (angle < 202.5f) {
Wuuu 0:29bec73b2c04 183 d = S;
Wuuu 0:29bec73b2c04 184 } else if (angle < 247.5f) {
Wuuu 0:29bec73b2c04 185 d = SW;
Wuuu 0:29bec73b2c04 186 } else if (angle < 292.5f) {
Wuuu 0:29bec73b2c04 187 d = W;
Wuuu 0:29bec73b2c04 188 } else if (angle < 337.5f) {
Wuuu 0:29bec73b2c04 189 d = NW;
Wuuu 0:29bec73b2c04 190 } else {
Wuuu 0:29bec73b2c04 191 d = N;
Wuuu 0:29bec73b2c04 192 }
Wuuu 0:29bec73b2c04 193
Wuuu 0:29bec73b2c04 194 return d;
Wuuu 0:29bec73b2c04 195 }
Wuuu 0:29bec73b2c04 196
Wuuu 0:29bec73b2c04 197 ///////////////////// private methods ////////////////////////
Wuuu 0:29bec73b2c04 198
Wuuu 0:29bec73b2c04 199 void Gamepad::tone_off()
Wuuu 0:29bec73b2c04 200 {
Wuuu 0:29bec73b2c04 201 // called after timeout
Wuuu 0:29bec73b2c04 202 _buzzer->write(0.0);
Wuuu 0:29bec73b2c04 203 }
Wuuu 0:29bec73b2c04 204
Wuuu 0:29bec73b2c04 205 void Gamepad::init_buttons()
Wuuu 0:29bec73b2c04 206 {
Wuuu 0:29bec73b2c04 207 // turn on pull-downs as other side of button is connected to 3V3
Wuuu 0:29bec73b2c04 208 // button is 0 when not pressed and 1 when pressed
Wuuu 0:29bec73b2c04 209 _button_A->mode(PullDown);
Wuuu 0:29bec73b2c04 210 _button_B->mode(PullDown);
Wuuu 0:29bec73b2c04 211 _button_X->mode(PullDown);
Wuuu 0:29bec73b2c04 212 _button_Y->mode(PullDown);
Wuuu 0:29bec73b2c04 213 _button_back->mode(PullDown);
Wuuu 0:29bec73b2c04 214 _button_start->mode(PullDown);
Wuuu 0:29bec73b2c04 215 _button_L->mode(PullDown);
Wuuu 0:29bec73b2c04 216 _button_R->mode(PullDown);
Wuuu 0:29bec73b2c04 217 _button_joystick->mode(PullDown);
Wuuu 0:29bec73b2c04 218 // therefore setup rising edge interrupts
Wuuu 0:29bec73b2c04 219 _button_A->rise(callback(this,&Gamepad::a_isr));
Wuuu 0:29bec73b2c04 220 _button_B->rise(callback(this,&Gamepad::b_isr));
Wuuu 0:29bec73b2c04 221 _button_X->rise(callback(this,&Gamepad::x_isr));
Wuuu 0:29bec73b2c04 222 _button_Y->rise(callback(this,&Gamepad::y_isr));
Wuuu 0:29bec73b2c04 223 _button_L->rise(callback(this,&Gamepad::l_isr));
Wuuu 0:29bec73b2c04 224 _button_R->rise(callback(this,&Gamepad::r_isr));
Wuuu 0:29bec73b2c04 225 _button_start->rise(callback(this,&Gamepad::start_isr));
Wuuu 0:29bec73b2c04 226 _button_back->rise(callback(this,&Gamepad::back_isr));
Wuuu 0:29bec73b2c04 227 _button_joystick->rise(callback(this,&Gamepad::joy_isr));
Wuuu 0:29bec73b2c04 228 }
Wuuu 0:29bec73b2c04 229
Wuuu 0:29bec73b2c04 230 // button interrupts ISRs
Wuuu 0:29bec73b2c04 231 // Each of these simply sets the appropriate event bit in the _event_state
Wuuu 0:29bec73b2c04 232 // variable
Wuuu 0:29bec73b2c04 233 void Gamepad::a_isr()
Wuuu 0:29bec73b2c04 234 {
Wuuu 0:29bec73b2c04 235 _event_state.set(A_PRESSED);
Wuuu 0:29bec73b2c04 236 }
Wuuu 0:29bec73b2c04 237 void Gamepad::b_isr()
Wuuu 0:29bec73b2c04 238 {
Wuuu 0:29bec73b2c04 239 _event_state.set(B_PRESSED);
Wuuu 0:29bec73b2c04 240 }
Wuuu 0:29bec73b2c04 241 void Gamepad::x_isr()
Wuuu 0:29bec73b2c04 242 {
Wuuu 0:29bec73b2c04 243 _event_state.set(X_PRESSED);
Wuuu 0:29bec73b2c04 244 }
Wuuu 0:29bec73b2c04 245 void Gamepad::y_isr()
Wuuu 0:29bec73b2c04 246 {
Wuuu 0:29bec73b2c04 247 _event_state.set(Y_PRESSED);
Wuuu 0:29bec73b2c04 248 }
Wuuu 0:29bec73b2c04 249 void Gamepad::l_isr()
Wuuu 0:29bec73b2c04 250 {
Wuuu 0:29bec73b2c04 251 _event_state.set(L_PRESSED);
Wuuu 0:29bec73b2c04 252 }
Wuuu 0:29bec73b2c04 253 void Gamepad::r_isr()
Wuuu 0:29bec73b2c04 254 {
Wuuu 0:29bec73b2c04 255 _event_state.set(R_PRESSED);
Wuuu 0:29bec73b2c04 256 }
Wuuu 0:29bec73b2c04 257 void Gamepad::back_isr()
Wuuu 0:29bec73b2c04 258 {
Wuuu 0:29bec73b2c04 259 _event_state.set(BACK_PRESSED);
Wuuu 0:29bec73b2c04 260 }
Wuuu 0:29bec73b2c04 261 void Gamepad::start_isr()
Wuuu 0:29bec73b2c04 262 {
Wuuu 0:29bec73b2c04 263 _event_state.set(START_PRESSED);
Wuuu 0:29bec73b2c04 264 }
Wuuu 0:29bec73b2c04 265 void Gamepad::joy_isr()
Wuuu 0:29bec73b2c04 266 {
Wuuu 0:29bec73b2c04 267 _event_state.set(JOY_PRESSED);
Wuuu 0:29bec73b2c04 268 }
Wuuu 0:29bec73b2c04 269
Wuuu 0:29bec73b2c04 270 // get raw joystick coordinate in range -1 to 1
Wuuu 0:29bec73b2c04 271 // Direction (x,y)
Wuuu 0:29bec73b2c04 272 // North (0,1)
Wuuu 0:29bec73b2c04 273 // East (1,0)
Wuuu 0:29bec73b2c04 274 // South (0,-1)
Wuuu 0:29bec73b2c04 275 // West (-1,0)
Wuuu 0:29bec73b2c04 276 Vector2D Gamepad::get_coord()
Wuuu 0:29bec73b2c04 277 {
Wuuu 0:29bec73b2c04 278 // read() returns value in range 0.0 to 1.0 so is scaled and centre value
Wuuu 0:29bec73b2c04 279 // substracted to get values in the range -1.0 to 1.0
Wuuu 0:29bec73b2c04 280 float x = 2.0f*( _horiz->read() - _x0 );
Wuuu 0:29bec73b2c04 281 float y = 2.0f*( _vert->read() - _y0 );
Wuuu 0:29bec73b2c04 282
Wuuu 0:29bec73b2c04 283 // Note: the x value here is inverted to ensure the positive x is to the
Wuuu 0:29bec73b2c04 284 // right. This is simply due to how the potentiometer on the joystick
Wuuu 0:29bec73b2c04 285 // I was using was connected up. It could have been corrected in hardware
Wuuu 0:29bec73b2c04 286 // by swapping the power supply pins. Instead it is done in software so may
Wuuu 0:29bec73b2c04 287 // need to be changed depending on your wiring setup
Wuuu 0:29bec73b2c04 288
Wuuu 0:29bec73b2c04 289 Vector2D coord = {-x,y};
Wuuu 0:29bec73b2c04 290 return coord;
Wuuu 0:29bec73b2c04 291 }
Wuuu 0:29bec73b2c04 292
Wuuu 0:29bec73b2c04 293 // This maps the raw x,y coord onto a circular grid.
Wuuu 0:29bec73b2c04 294 // See: http://mathproofs.blogspot.co.uk/2005/07/mapping-square-to-circle.html
Wuuu 0:29bec73b2c04 295 Vector2D Gamepad::get_mapped_coord()
Wuuu 0:29bec73b2c04 296 {
Wuuu 0:29bec73b2c04 297 Vector2D coord = get_coord();
Wuuu 0:29bec73b2c04 298
Wuuu 0:29bec73b2c04 299 // do the transformation
Wuuu 0:29bec73b2c04 300 float x = coord.x*sqrt(1.0f-pow(coord.y,2.0f)/2.0f);
Wuuu 0:29bec73b2c04 301 float y = coord.y*sqrt(1.0f-pow(coord.x,2.0f)/2.0f);
Wuuu 0:29bec73b2c04 302
Wuuu 0:29bec73b2c04 303 Vector2D mapped_coord = {x,y};
Wuuu 0:29bec73b2c04 304 return mapped_coord;
Wuuu 0:29bec73b2c04 305 }
Wuuu 0:29bec73b2c04 306
Wuuu 0:29bec73b2c04 307 // this function converts the mapped coordinates into polar form
Wuuu 0:29bec73b2c04 308 Polar Gamepad::get_polar()
Wuuu 0:29bec73b2c04 309 {
Wuuu 0:29bec73b2c04 310 // get the mapped coordinate
Wuuu 0:29bec73b2c04 311 Vector2D coord = get_mapped_coord();
Wuuu 0:29bec73b2c04 312
Wuuu 0:29bec73b2c04 313 // at this point, 0 degrees (i.e. x-axis) will be defined to the East.
Wuuu 0:29bec73b2c04 314 // We want 0 degrees to correspond to North and increase clockwise to 359
Wuuu 0:29bec73b2c04 315 // like a compass heading, so we need to swap the axis and invert y
Wuuu 0:29bec73b2c04 316 float x = coord.y;
Wuuu 0:29bec73b2c04 317 float y = coord.x;
Wuuu 0:29bec73b2c04 318
Wuuu 0:29bec73b2c04 319 float mag = sqrt(x*x+y*y); // pythagoras
Wuuu 0:29bec73b2c04 320 float angle = RAD2DEG*atan2(y,x);
Wuuu 0:29bec73b2c04 321 // angle will be in range -180 to 180, so add 360 to negative angles to
Wuuu 0:29bec73b2c04 322 // move to 0 to 360 range
Wuuu 0:29bec73b2c04 323 if (angle < 0.0f) {
Wuuu 0:29bec73b2c04 324 angle+=360.0f;
Wuuu 0:29bec73b2c04 325 }
Wuuu 0:29bec73b2c04 326
Wuuu 0:29bec73b2c04 327 // the noise on the ADC causes the values of x and y to fluctuate slightly
Wuuu 0:29bec73b2c04 328 // around the centred values. This causes the random angle values to get
Wuuu 0:29bec73b2c04 329 // calculated when the joystick is centred and untouched. This is also when
Wuuu 0:29bec73b2c04 330 // the magnitude is very small, so we can check for a small magnitude and then
Wuuu 0:29bec73b2c04 331 // set the angle to -1. This will inform us when the angle is invalid and the
Wuuu 0:29bec73b2c04 332 // joystick is centred
Wuuu 0:29bec73b2c04 333
Wuuu 0:29bec73b2c04 334 if (mag < TOL) {
Wuuu 0:29bec73b2c04 335 mag = 0.0f;
Wuuu 0:29bec73b2c04 336 angle = -1.0f;
Wuuu 0:29bec73b2c04 337 }
Wuuu 0:29bec73b2c04 338
Wuuu 0:29bec73b2c04 339 Polar p = {mag,angle};
Wuuu 0:29bec73b2c04 340 return p;
Wuuu 0:29bec73b2c04 341 }