FinalCode

Committer:
ciarankane123
Date:
Fri Feb 04 23:40:21 2022 +0000
Revision:
0:3262800b671c
Project

Who changed what in which revision?

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