Yiming Zhang
/
botton
mid
Joystick/Joystick.cpp@0:9a525a0d1d3f, 2020-04-27 (annotated)
- Committer:
- sjuzyz
- Date:
- Mon Apr 27 12:01:16 2020 +0000
- Revision:
- 0:9a525a0d1d3f
mid
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sjuzyz | 0:9a525a0d1d3f | 1 | #include "Joystick.h" |
sjuzyz | 0:9a525a0d1d3f | 2 | |
sjuzyz | 0:9a525a0d1d3f | 3 | Joystick::Joystick(PinName vertPin,PinName horizPin,PinName clickPin) |
sjuzyz | 0:9a525a0d1d3f | 4 | { |
sjuzyz | 0:9a525a0d1d3f | 5 | vert = new AnalogIn(vertPin); |
sjuzyz | 0:9a525a0d1d3f | 6 | horiz = new AnalogIn(horizPin); |
sjuzyz | 0:9a525a0d1d3f | 7 | click = new InterruptIn(clickPin); |
sjuzyz | 0:9a525a0d1d3f | 8 | } |
sjuzyz | 0:9a525a0d1d3f | 9 | |
sjuzyz | 0:9a525a0d1d3f | 10 | void Joystick::init() |
sjuzyz | 0:9a525a0d1d3f | 11 | { |
sjuzyz | 0:9a525a0d1d3f | 12 | // read centred values of joystick |
sjuzyz | 0:9a525a0d1d3f | 13 | _x0 = horiz->read(); |
sjuzyz | 0:9a525a0d1d3f | 14 | _y0 = vert->read(); |
sjuzyz | 0:9a525a0d1d3f | 15 | |
sjuzyz | 0:9a525a0d1d3f | 16 | // this assumes that the joystick is centred when the init function is called |
sjuzyz | 0:9a525a0d1d3f | 17 | // if perfectly centred, the pots should read 0.5, but this may |
sjuzyz | 0:9a525a0d1d3f | 18 | // not be the case and x0 and y0 will be used to calibrate readings |
sjuzyz | 0:9a525a0d1d3f | 19 | |
sjuzyz | 0:9a525a0d1d3f | 20 | // turn on pull-down for button -> this assumes the other side of the button |
sjuzyz | 0:9a525a0d1d3f | 21 | // is connected to +3V3 so we read 1 when pressed and 0 when not pressed |
sjuzyz | 0:9a525a0d1d3f | 22 | click->mode(PullDown); |
sjuzyz | 0:9a525a0d1d3f | 23 | // we therefore need to fire the interrupt on a rising edge |
sjuzyz | 0:9a525a0d1d3f | 24 | click->rise(callback(this,&Joystick::click_isr)); |
sjuzyz | 0:9a525a0d1d3f | 25 | // need to use a callback since mbed-os5 - basically tells it to look in this class for the ISR |
sjuzyz | 0:9a525a0d1d3f | 26 | _click_flag = 0; |
sjuzyz | 0:9a525a0d1d3f | 27 | |
sjuzyz | 0:9a525a0d1d3f | 28 | } |
sjuzyz | 0:9a525a0d1d3f | 29 | |
sjuzyz | 0:9a525a0d1d3f | 30 | Direction Joystick::get_direction() |
sjuzyz | 0:9a525a0d1d3f | 31 | { |
sjuzyz | 0:9a525a0d1d3f | 32 | float angle = get_angle(); // 0 to 360, -1 for centred |
sjuzyz | 0:9a525a0d1d3f | 33 | |
sjuzyz | 0:9a525a0d1d3f | 34 | Direction d; |
sjuzyz | 0:9a525a0d1d3f | 35 | // partition 360 into segments and check which segment the angle is in |
sjuzyz | 0:9a525a0d1d3f | 36 | if (angle < 0.0f) { |
sjuzyz | 0:9a525a0d1d3f | 37 | d = CENTRE; // check for -1.0 angle |
sjuzyz | 0:9a525a0d1d3f | 38 | } else if (angle < 22.5f) { // then keep going in 45 degree increments |
sjuzyz | 0:9a525a0d1d3f | 39 | d = N; |
sjuzyz | 0:9a525a0d1d3f | 40 | } else if (angle < 67.5f) { |
sjuzyz | 0:9a525a0d1d3f | 41 | d = NE; |
sjuzyz | 0:9a525a0d1d3f | 42 | } else if (angle < 112.5f) { |
sjuzyz | 0:9a525a0d1d3f | 43 | d = E; |
sjuzyz | 0:9a525a0d1d3f | 44 | } else if (angle < 157.5f) { |
sjuzyz | 0:9a525a0d1d3f | 45 | d = SE; |
sjuzyz | 0:9a525a0d1d3f | 46 | } else if (angle < 202.5f) { |
sjuzyz | 0:9a525a0d1d3f | 47 | d = S; |
sjuzyz | 0:9a525a0d1d3f | 48 | } else if (angle < 247.5f) { |
sjuzyz | 0:9a525a0d1d3f | 49 | d = SW; |
sjuzyz | 0:9a525a0d1d3f | 50 | } else if (angle < 292.5f) { |
sjuzyz | 0:9a525a0d1d3f | 51 | d = W; |
sjuzyz | 0:9a525a0d1d3f | 52 | } else if (angle < 337.5f) { |
sjuzyz | 0:9a525a0d1d3f | 53 | d = NW; |
sjuzyz | 0:9a525a0d1d3f | 54 | } else { |
sjuzyz | 0:9a525a0d1d3f | 55 | d = N; |
sjuzyz | 0:9a525a0d1d3f | 56 | } |
sjuzyz | 0:9a525a0d1d3f | 57 | |
sjuzyz | 0:9a525a0d1d3f | 58 | return d; |
sjuzyz | 0:9a525a0d1d3f | 59 | } |
sjuzyz | 0:9a525a0d1d3f | 60 | |
sjuzyz | 0:9a525a0d1d3f | 61 | // this method gets the magnitude of the joystick movement |
sjuzyz | 0:9a525a0d1d3f | 62 | float Joystick::get_mag() |
sjuzyz | 0:9a525a0d1d3f | 63 | { |
sjuzyz | 0:9a525a0d1d3f | 64 | Polar p = get_polar(); |
sjuzyz | 0:9a525a0d1d3f | 65 | return p.mag; |
sjuzyz | 0:9a525a0d1d3f | 66 | } |
sjuzyz | 0:9a525a0d1d3f | 67 | |
sjuzyz | 0:9a525a0d1d3f | 68 | // this method gets the angle of joystick movement (0 to 360, 0 North) |
sjuzyz | 0:9a525a0d1d3f | 69 | float Joystick::get_angle() |
sjuzyz | 0:9a525a0d1d3f | 70 | { |
sjuzyz | 0:9a525a0d1d3f | 71 | Polar p = get_polar(); |
sjuzyz | 0:9a525a0d1d3f | 72 | return p.angle; |
sjuzyz | 0:9a525a0d1d3f | 73 | } |
sjuzyz | 0:9a525a0d1d3f | 74 | |
sjuzyz | 0:9a525a0d1d3f | 75 | // get raw joystick coordinate in range -1 to 1 |
sjuzyz | 0:9a525a0d1d3f | 76 | // Direction (x,y) |
sjuzyz | 0:9a525a0d1d3f | 77 | // North (0,1) |
sjuzyz | 0:9a525a0d1d3f | 78 | // East (1,0) |
sjuzyz | 0:9a525a0d1d3f | 79 | // South (0,-1) |
sjuzyz | 0:9a525a0d1d3f | 80 | // West (-1,0) |
sjuzyz | 0:9a525a0d1d3f | 81 | Vector2D Joystick::get_coord() |
sjuzyz | 0:9a525a0d1d3f | 82 | { |
sjuzyz | 0:9a525a0d1d3f | 83 | // read() returns value in range 0.0 to 1.0 so is scaled and centre value |
sjuzyz | 0:9a525a0d1d3f | 84 | // substracted to get values in the range -1.0 to 1.0 |
sjuzyz | 0:9a525a0d1d3f | 85 | float x = 2.0f*( horiz->read() - _x0 ); |
sjuzyz | 0:9a525a0d1d3f | 86 | float y = 2.0f*( vert->read() - _y0 ); |
sjuzyz | 0:9a525a0d1d3f | 87 | |
sjuzyz | 0:9a525a0d1d3f | 88 | // Note: the x value here is inverted to ensure the positive x is to the |
sjuzyz | 0:9a525a0d1d3f | 89 | // right. This is simply due to how the potentiometer on the joystick |
sjuzyz | 0:9a525a0d1d3f | 90 | // I was using was connected up. It could have been corrected in hardware |
sjuzyz | 0:9a525a0d1d3f | 91 | // by swapping the power supply pins. Instead it is done in software so may |
sjuzyz | 0:9a525a0d1d3f | 92 | // need to be changed depending on your wiring setup |
sjuzyz | 0:9a525a0d1d3f | 93 | |
sjuzyz | 0:9a525a0d1d3f | 94 | Vector2D coord = {-x,y}; |
sjuzyz | 0:9a525a0d1d3f | 95 | return coord; |
sjuzyz | 0:9a525a0d1d3f | 96 | } |
sjuzyz | 0:9a525a0d1d3f | 97 | |
sjuzyz | 0:9a525a0d1d3f | 98 | // This maps the raw x,y coord onto a circular grid. |
sjuzyz | 0:9a525a0d1d3f | 99 | // See: http://mathproofs.blogspot.co.uk/2005/07/mapping-square-to-circle.html |
sjuzyz | 0:9a525a0d1d3f | 100 | Vector2D Joystick::get_mapped_coord() |
sjuzyz | 0:9a525a0d1d3f | 101 | { |
sjuzyz | 0:9a525a0d1d3f | 102 | Vector2D coord = get_coord(); |
sjuzyz | 0:9a525a0d1d3f | 103 | |
sjuzyz | 0:9a525a0d1d3f | 104 | // do the transformation |
sjuzyz | 0:9a525a0d1d3f | 105 | float x = coord.x*sqrt(1.0f-pow(coord.y,2.0f)/2.0f); |
sjuzyz | 0:9a525a0d1d3f | 106 | float y = coord.y*sqrt(1.0f-pow(coord.x,2.0f)/2.0f); |
sjuzyz | 0:9a525a0d1d3f | 107 | |
sjuzyz | 0:9a525a0d1d3f | 108 | Vector2D mapped_coord = {x,y}; |
sjuzyz | 0:9a525a0d1d3f | 109 | return mapped_coord; |
sjuzyz | 0:9a525a0d1d3f | 110 | } |
sjuzyz | 0:9a525a0d1d3f | 111 | |
sjuzyz | 0:9a525a0d1d3f | 112 | // this function converts the mapped coordinates into polar form |
sjuzyz | 0:9a525a0d1d3f | 113 | Polar Joystick::get_polar() |
sjuzyz | 0:9a525a0d1d3f | 114 | { |
sjuzyz | 0:9a525a0d1d3f | 115 | // get the mapped coordinate |
sjuzyz | 0:9a525a0d1d3f | 116 | Vector2D coord = get_mapped_coord(); |
sjuzyz | 0:9a525a0d1d3f | 117 | |
sjuzyz | 0:9a525a0d1d3f | 118 | // at this point, 0 degrees (i.e. x-axis) will be defined to the East. |
sjuzyz | 0:9a525a0d1d3f | 119 | // We want 0 degrees to correspond to North and increase clockwise to 359 |
sjuzyz | 0:9a525a0d1d3f | 120 | // like a compass heading, so we need to swap the axis and invert y |
sjuzyz | 0:9a525a0d1d3f | 121 | float x = coord.y; |
sjuzyz | 0:9a525a0d1d3f | 122 | float y = coord.x; |
sjuzyz | 0:9a525a0d1d3f | 123 | |
sjuzyz | 0:9a525a0d1d3f | 124 | float mag = sqrt(x*x+y*y); // pythagoras |
sjuzyz | 0:9a525a0d1d3f | 125 | float angle = RAD2DEG*atan2(y,x); |
sjuzyz | 0:9a525a0d1d3f | 126 | // angle will be in range -180 to 180, so add 360 to negative angles to |
sjuzyz | 0:9a525a0d1d3f | 127 | // move to 0 to 360 range |
sjuzyz | 0:9a525a0d1d3f | 128 | if (angle < 0.0f) { |
sjuzyz | 0:9a525a0d1d3f | 129 | angle+=360.0f; |
sjuzyz | 0:9a525a0d1d3f | 130 | } |
sjuzyz | 0:9a525a0d1d3f | 131 | |
sjuzyz | 0:9a525a0d1d3f | 132 | // the noise on the ADC causes the values of x and y to fluctuate slightly |
sjuzyz | 0:9a525a0d1d3f | 133 | // around the centred values. This causes the random angle values to get |
sjuzyz | 0:9a525a0d1d3f | 134 | // calculated when the joystick is centred and untouched. This is also when |
sjuzyz | 0:9a525a0d1d3f | 135 | // the magnitude is very small, so we can check for a small magnitude and then |
sjuzyz | 0:9a525a0d1d3f | 136 | // set the angle to -1. This will inform us when the angle is invalid and the |
sjuzyz | 0:9a525a0d1d3f | 137 | // joystick is centred |
sjuzyz | 0:9a525a0d1d3f | 138 | |
sjuzyz | 0:9a525a0d1d3f | 139 | if (mag < TOL) { |
sjuzyz | 0:9a525a0d1d3f | 140 | mag = 0.0f; |
sjuzyz | 0:9a525a0d1d3f | 141 | angle = -1.0f; |
sjuzyz | 0:9a525a0d1d3f | 142 | } |
sjuzyz | 0:9a525a0d1d3f | 143 | |
sjuzyz | 0:9a525a0d1d3f | 144 | Polar p = {mag,angle}; |
sjuzyz | 0:9a525a0d1d3f | 145 | return p; |
sjuzyz | 0:9a525a0d1d3f | 146 | } |
sjuzyz | 0:9a525a0d1d3f | 147 | |
sjuzyz | 0:9a525a0d1d3f | 148 | bool Joystick::button_pressed() |
sjuzyz | 0:9a525a0d1d3f | 149 | { |
sjuzyz | 0:9a525a0d1d3f | 150 | // ISR must have been triggered |
sjuzyz | 0:9a525a0d1d3f | 151 | if (_click_flag) { |
sjuzyz | 0:9a525a0d1d3f | 152 | _click_flag = 0; // clear flag |
sjuzyz | 0:9a525a0d1d3f | 153 | return true; |
sjuzyz | 0:9a525a0d1d3f | 154 | } else { |
sjuzyz | 0:9a525a0d1d3f | 155 | return false; |
sjuzyz | 0:9a525a0d1d3f | 156 | } |
sjuzyz | 0:9a525a0d1d3f | 157 | } |
sjuzyz | 0:9a525a0d1d3f | 158 | |
sjuzyz | 0:9a525a0d1d3f | 159 | void Joystick::click_isr() |
sjuzyz | 0:9a525a0d1d3f | 160 | { |
sjuzyz | 0:9a525a0d1d3f | 161 | _click_flag = 1; |
sjuzyz | 0:9a525a0d1d3f | 162 | } |