Final tidy of code following installation of new sensor, more comments added prior to submission

Dependencies:   mbed

Committer:
legstar85
Date:
Fri Jan 21 14:26:56 2022 +0000
Revision:
13:5ad65a688f3f
Updated due to issues with using Start Temp variable later in program now resolved. Doxygen completed and folders in place correctly

Who changed what in which revision?

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