Coursework

Committer:
sesa514652
Date:
Fri Feb 04 16:44:14 2022 +0000
Revision:
45:f1db729741f7
Parent:
0:1f799c7cce2b
Submission

Who changed what in which revision?

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