519_La

Fork of Joystick_skeleton by Carter Sharer

Committer:
chirags
Date:
Fri Nov 04 21:27:03 2016 +0000
Revision:
5:4e531d160998
Parent:
4:aaca0f94b646
Child:
6:7a0edb75e7fd
to share;

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 5:4e531d160998 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 5:4e531d160998 52 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
csharer 3:6f854e0cae36 69 val = 0;
csharer 2:893fd930d3fb 70 else //Negative Range
csharer 3:6f854e0cae36 71 val = 0;
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
csharer 3:6f854e0cae36 77 return horiz.read();
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)
csharer 3:6f854e0cae36 84 float avg = vert.read();
csharer 3:6f854e0cae36 85
csharer 3:6f854e0cae36 86 //(4)Watch for Max and Min Values, if we see a new max/min update Raw Max/Min
csharer 3:6f854e0cae36 87 rawMaxV = avg;
csharer 3:6f854e0cae36 88 rawMinV = avg;
csharer 0:46523bf02e61 89
csharer 3:6f854e0cae36 90 //(5)Here we will calculate the total range (Travel) of the joystick
csharer 3:6f854e0cae36 91 //using the rawMax/rawMin values we have seen thus far
csharer 3:6f854e0cae36 92 //Since the joystick is not symetrical we have to calculate two ranges
csharer 3:6f854e0cae36 93 //Calculate the range from [center, max] and [center, min]
csharer 3:6f854e0cae36 94 float range_pos = 0;
csharer 3:6f854e0cae36 95 float range_neg = 0;
csharer 0:46523bf02e61 96
csharer 3:6f854e0cae36 97 //(6)Here we will calculate how much our current reading is in one
csharer 3:6f854e0cae36 98 //of the ranges, this will give us a percentage of our _max value we
csharer 3:6f854e0cae36 99 //set in setScale. Then we can apply the scale by multiplying it by our
csharer 3:6f854e0cae36 100 //scale (_max/_min).
csharer 0:46523bf02e61 101 float val;
csharer 0:46523bf02e61 102 if(avg >= raw_vc) //find scaled pot value
csharer 3:6f854e0cae36 103 val = 0;
csharer 0:46523bf02e61 104 else
csharer 3:6f854e0cae36 105 val = 0;
csharer 0:46523bf02e61 106
csharer 3:6f854e0cae36 107 //(7)Here we will apply a dead zone. If the |value| is <= our deadzone then
csharer 3:6f854e0cae36 108 //set it to 0. Otherwise we need to shift the value closer to 0 by dead zone
csharer 3:6f854e0cae36 109
csharer 3:6f854e0cae36 110 return vert.read();
csharer 0:46523bf02e61 111 }
csharer 0:46523bf02e61 112
csharer 0:46523bf02e61 113 //Set the Min and Max Values of joystick ex: -100, +100
csharer 0:46523bf02e61 114 void Joystick::setScale(float min, float max) {
csharer 1:c42a77267f7b 115 _min = min-1; //Add 1 for round off error
csharer 1:c42a77267f7b 116 _max = max+1; //Add 1 for round off error
csharer 0:46523bf02e61 117 }