519_La

Fork of Joystick_skeleton by Carter Sharer

Committer:
chirags
Date:
Sun Nov 13 20:39:37 2016 +0000
Revision:
6:7a0edb75e7fd
Parent:
5:4e531d160998
Commit the part 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
csharer 4:aaca0f94b646 1 //Joystick (INCOMPLETE IMPLIMENTATION)
csharer 1:c42a77267f7b 2 //Author: Carter Sharer
csharer 1:c42a77267f7b 3 //Date: 10/18/16
csharer 1:c42a77267f7b 4
csharer 0:46523bf02e61 5 #include "Joystick.h"
csharer 0:46523bf02e61 6 #include "mbed.h"
csharer 0:46523bf02e61 7
csharer 0:46523bf02e61 8 //Member function definitions including constructor
csharer 0:46523bf02e61 9 Joystick::Joystick(PinName pinA, PinName pinB) : horiz(pinA), vert(pinB) {
csharer 0:46523bf02e61 10 //Wait 1 second for voltage to settle
csharer 0:46523bf02e61 11 wait(1);
csharer 0:46523bf02e61 12
csharer 2:893fd930d3fb 13 //(1)Set Raw Center Values, this is where the joystick sits naturaly
csharer 2:893fd930d3fb 14 //set raw horizontal center to current value (average with 5 readings)
csharer 2:893fd930d3fb 15 //set raw vertial center to current value (average with 5 readings)
csharer 3:6f854e0cae36 16 raw_hc = 0;
csharer 3:6f854e0cae36 17 raw_vc = 0;
csharer 0:46523bf02e61 18
chirags 5:4e531d160998 19 for (int i = 0; i<5; i++) {
chirags 5:4e531d160998 20 raw_hc += horiz.read();
chirags 5:4e531d160998 21 raw_vc += vert.read();
chirags 5:4e531d160998 22 }
chirags 5:4e531d160998 23 raw_hc /= 5;
chirags 5:4e531d160998 24 raw_vc /= 5;
chirags 5:4e531d160998 25
chirags 5:4e531d160998 26
csharer 2:893fd930d3fb 27 //(2)Initalize the Rax Max to some value less then the real max value.
csharer 2:893fd930d3fb 28 //We dont know what the max value will be until we read it, thats ok.
csharer 2:893fd930d3fb 29 //But we can assume it will be greater then 0.8 (center + 0.3) so lets
csharer 2:893fd930d3fb 30 //set it to that for now. We will update this later if we see a larger value
csharer 0:46523bf02e61 31 //Now do the same for the the Raw Min
csharer 0:46523bf02e61 32 float delta = 0.3;
chirags 5:4e531d160998 33 rawMinH = raw_hc - delta;
chirags 5:4e531d160998 34 rawMaxH = raw_hc + delta;
chirags 5:4e531d160998 35 rawMinV = raw_vc - delta;
chirags 5:4e531d160998 36 rawMaxV = raw_vc + delta;
csharer 0:46523bf02e61 37 }
csharer 0:46523bf02e61 38
csharer 0:46523bf02e61 39 //Returns the scaled vertial value of joystick
csharer 0:46523bf02e61 40 float Joystick::horizontal(void) {
csharer 2:893fd930d3fb 41 //(3)Get average val (5 samples)
chirags 5:4e531d160998 42 float avg = 0;
csharer 0:46523bf02e61 43
chirags 5:4e531d160998 44 for (int i = 0; i < 5; i++) {
chirags 6:7a0edb75e7fd 45 avg += horiz.read();
chirags 5:4e531d160998 46 }
chirags 5:4e531d160998 47 avg /= 5;
csharer 2:893fd930d3fb 48 //(4)Watch for Max and Min Values, if we see a new max/min update Raw Max/Min
chirags 5:4e531d160998 49 if(avg > rawMaxH) {
chirags 5:4e531d160998 50 rawMaxH = avg;
chirags 5:4e531d160998 51 }
chirags 6:7a0edb75e7fd 52 else if(avg < rawMinH) {
chirags 5:4e531d160998 53 rawMinH = avg;
chirags 5:4e531d160998 54 }
csharer 0:46523bf02e61 55
csharer 2:893fd930d3fb 56 //(5)Here we will calculate the total range (Travel) of the joystick
csharer 0:46523bf02e61 57 //using the rawMax/rawMin values we have seen thus far
csharer 2:893fd930d3fb 58 //Since the joystick is not symetrical we have to calculate two ranges
csharer 0:46523bf02e61 59 //Calculate the range from [center, max] and [center, min]
chirags 5:4e531d160998 60 float range_pos = rawMaxH - raw_hc;
chirags 5:4e531d160998 61 float range_neg = raw_hc - rawMinH;
csharer 0:46523bf02e61 62
csharer 2:893fd930d3fb 63 //(6)Here we will calculate how much our current reading is in one
csharer 2:893fd930d3fb 64 //of the ranges, this will give us a percentage of our _max value we
csharer 2:893fd930d3fb 65 //set in setScale. Then we can apply the scale by multiplying it by our
csharer 2:893fd930d3fb 66 //scale (_max/_min).
csharer 0:46523bf02e61 67 float val;
csharer 2:893fd930d3fb 68 if(avg >= raw_hc) //Positive Range
chirags 6:7a0edb75e7fd 69 val = ((avg - raw_hc)/range_pos)*(_max - 1);
csharer 2:893fd930d3fb 70 else //Negative Range
chirags 6:7a0edb75e7fd 71 val = ((raw_hc - avg)/range_neg)*(_min + 1);
csharer 0:46523bf02e61 72
csharer 2:893fd930d3fb 73 //(7)Here we will apply a dead zone. If the |value| is <= our deadzone then
csharer 2:893fd930d3fb 74 //set it to 0. Otherwise we need to shift the value closer to 0 by dead zone
csharer 3:6f854e0cae36 75
csharer 3:6f854e0cae36 76
chirags 6:7a0edb75e7fd 77 return val;
csharer 0:46523bf02e61 78 }
csharer 0:46523bf02e61 79
csharer 2:893fd930d3fb 80 //(8) Impliment vertial the same as you did for horizontal
csharer 0:46523bf02e61 81 //Returns the scaled horizontal value of joystick
csharer 0:46523bf02e61 82 float Joystick::vertical(void) {
csharer 3:6f854e0cae36 83 //Get average value (5 samples)
chirags 6:7a0edb75e7fd 84 float avg = 0;
csharer 3:6f854e0cae36 85
chirags 6:7a0edb75e7fd 86 for(int i = 0; i < 5; i++) {
chirags 6:7a0edb75e7fd 87 avg += vert.read();
chirags 6:7a0edb75e7fd 88 }
chirags 6:7a0edb75e7fd 89 avg /= 5;
csharer 3:6f854e0cae36 90 //(4)Watch for Max and Min Values, if we see a new max/min update Raw Max/Min
chirags 6:7a0edb75e7fd 91 if(avg > rawMaxV) {
chirags 6:7a0edb75e7fd 92 rawMaxV = avg;
chirags 6:7a0edb75e7fd 93 }
chirags 6:7a0edb75e7fd 94 if(avg < rawMinV) {
chirags 6:7a0edb75e7fd 95 rawMinV = avg;
chirags 6:7a0edb75e7fd 96 }
csharer 0:46523bf02e61 97
csharer 3:6f854e0cae36 98 //(5)Here we will calculate the total range (Travel) of the joystick
csharer 3:6f854e0cae36 99 //using the rawMax/rawMin values we have seen thus far
csharer 3:6f854e0cae36 100 //Since the joystick is not symetrical we have to calculate two ranges
csharer 3:6f854e0cae36 101 //Calculate the range from [center, max] and [center, min]
chirags 6:7a0edb75e7fd 102 float range_pos = rawMaxV - raw_vc;
chirags 6:7a0edb75e7fd 103 float range_neg = raw_vc - rawMinV;
csharer 0:46523bf02e61 104
csharer 3:6f854e0cae36 105 //(6)Here we will calculate how much our current reading is in one
csharer 3:6f854e0cae36 106 //of the ranges, this will give us a percentage of our _max value we
csharer 3:6f854e0cae36 107 //set in setScale. Then we can apply the scale by multiplying it by our
csharer 3:6f854e0cae36 108 //scale (_max/_min).
chirags 6:7a0edb75e7fd 109 float val = 0;
chirags 6:7a0edb75e7fd 110 if(avg >= raw_vc) //Positive Range
chirags 6:7a0edb75e7fd 111 val = ((avg - raw_vc)/range_pos)*(_max - 1);
chirags 6:7a0edb75e7fd 112 else //Negative Range
chirags 6:7a0edb75e7fd 113 val = ((raw_vc - avg)/range_neg)*(_min + 1);
csharer 0:46523bf02e61 114
csharer 3:6f854e0cae36 115 //(7)Here we will apply a dead zone. If the |value| is <= our deadzone then
csharer 3:6f854e0cae36 116 //set it to 0. Otherwise we need to shift the value closer to 0 by dead zone
chirags 6:7a0edb75e7fd 117 return val;
csharer 0:46523bf02e61 118 }
csharer 0:46523bf02e61 119
csharer 0:46523bf02e61 120 //Set the Min and Max Values of joystick ex: -100, +100
csharer 0:46523bf02e61 121 void Joystick::setScale(float min, float max) {
csharer 1:c42a77267f7b 122 _min = min-1; //Add 1 for round off error
csharer 1:c42a77267f7b 123 _max = max+1; //Add 1 for round off error
csharer 0:46523bf02e61 124 }