First Draft, serial print change based on distance

Committer:
liam94
Date:
Thu Dec 16 11:41:38 2021 +0000
Revision:
1:a1795335ef8c
Vnice

Who changed what in which revision?

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