Ahmed Hedait / Mbed 2 deprecated el16ah

Dependencies:   mbed

Committer:
ahmedhedait
Date:
Mon Apr 30 02:09:48 2018 +0000
Revision:
2:a47f45c2fe6f
I updated that the small circle which starts at coordinates (5,5) moves in 4 different direction North,South,East,West without the circle getting back to its original position again.

Who changed what in which revision?

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