ESE519 blank Joystick class

Dependents:   ESE519_Lab6_part1_skeleton

Fork of Joystick_skelleton by Carter Sharer

Committer:
csharer
Date:
Thu Oct 20 23:12:18 2016 +0000
Revision:
2:893fd930d3fb
Parent:
1:c42a77267f7b
Child:
3:6f854e0cae36
added comments; changed DEAD_ZONE/2 to just DEAD_ZONE;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
csharer 1:c42a77267f7b 1 //Joystick (FULL 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 0:46523bf02e61 16 raw_hc = (horiz.read() + horiz.read() + horiz.read() + horiz.read() + horiz.read()) / 5.0;
csharer 0:46523bf02e61 17 raw_vc = (vert.read() + vert.read() + vert.read() + vert.read() + vert.read()) / 5.0;
csharer 0:46523bf02e61 18
csharer 2:893fd930d3fb 19 //(2)Initalize the Rax Max to some value less then the real max value.
csharer 2:893fd930d3fb 20 //We dont know what the max value will be until we read it, thats ok.
csharer 2:893fd930d3fb 21 //But we can assume it will be greater then 0.8 (center + 0.3) so lets
csharer 2:893fd930d3fb 22 //set it to that for now. We will update this later if we see a larger value
csharer 0:46523bf02e61 23 //Now do the same for the the Raw Min
csharer 0:46523bf02e61 24 float delta = 0.3;
csharer 2:893fd930d3fb 25 rawMinH = raw_hc - delta;
csharer 2:893fd930d3fb 26 rawMaxH = raw_hc + delta;
csharer 2:893fd930d3fb 27 rawMinV = raw_hc - delta;
csharer 2:893fd930d3fb 28 rawMaxV = raw_hc + delta;
csharer 0:46523bf02e61 29 }
csharer 0:46523bf02e61 30
csharer 0:46523bf02e61 31 //Returns the scaled vertial value of joystick
csharer 0:46523bf02e61 32 float Joystick::horizontal(void) {
csharer 2:893fd930d3fb 33 //(3)Get average val (5 samples)
csharer 0:46523bf02e61 34 float avg = (horiz.read() + horiz.read() + horiz.read() + horiz.read() + horiz.read()) / 5.0;
csharer 0:46523bf02e61 35
csharer 2:893fd930d3fb 36 //(4)Watch for Max and Min Values, if we see a new max/min update Raw Max/Min
csharer 0:46523bf02e61 37 if(avg > rawMaxH)
csharer 0:46523bf02e61 38 rawMaxH = avg;
csharer 0:46523bf02e61 39 if(avg < rawMinH)
csharer 0:46523bf02e61 40 rawMinH = avg;
csharer 0:46523bf02e61 41
csharer 2:893fd930d3fb 42 //(5)Here we will calculate the total range (Travel) of the joystick
csharer 0:46523bf02e61 43 //using the rawMax/rawMin values we have seen thus far
csharer 2:893fd930d3fb 44 //Since the joystick is not symetrical we have to calculate two ranges
csharer 0:46523bf02e61 45 //Calculate the range from [center, max] and [center, min]
csharer 0:46523bf02e61 46 float range_pos = rawMaxH - raw_hc;
csharer 0:46523bf02e61 47 float range_neg = raw_hc - rawMinH;
csharer 0:46523bf02e61 48
csharer 2:893fd930d3fb 49 //(6)Here we will calculate how much our current reading is in one
csharer 2:893fd930d3fb 50 //of the ranges, this will give us a percentage of our _max value we
csharer 2:893fd930d3fb 51 //set in setScale. Then we can apply the scale by multiplying it by our
csharer 2:893fd930d3fb 52 //scale (_max/_min).
csharer 0:46523bf02e61 53 float val;
csharer 2:893fd930d3fb 54 if(avg >= raw_hc) //Positive Range
csharer 2:893fd930d3fb 55 val = (((avg - raw_hc) / range_pos) * _max) + DEAD_ZONE; // percent to max * max
csharer 2:893fd930d3fb 56 else //Negative Range
csharer 2:893fd930d3fb 57 val = (((raw_hc - avg) / range_neg) * _min) - DEAD_ZONE; //percent to min * min
csharer 0:46523bf02e61 58
csharer 2:893fd930d3fb 59 //(7)Here we will apply a dead zone. If the |value| is <= our deadzone then
csharer 2:893fd930d3fb 60 //set it to 0. Otherwise we need to shift the value closer to 0 by dead zone
csharer 2:893fd930d3fb 61 if((val <= DEAD_ZONE) && (val >= -DEAD_ZONE)) {
csharer 0:46523bf02e61 62 return 0;
csharer 0:46523bf02e61 63 }
csharer 2:893fd930d3fb 64 else if(val > DEAD_ZONE) {//posotive vals
csharer 2:893fd930d3fb 65 return val - DEAD_ZONE;
csharer 0:46523bf02e61 66 }
csharer 0:46523bf02e61 67 else { //negative vals
csharer 2:893fd930d3fb 68 return val + DEAD_ZONE;
csharer 0:46523bf02e61 69 }
csharer 0:46523bf02e61 70 }
csharer 0:46523bf02e61 71
csharer 2:893fd930d3fb 72 //(8) Impliment vertial the same as you did for horizontal
csharer 0:46523bf02e61 73 //Returns the scaled horizontal value of joystick
csharer 0:46523bf02e61 74 float Joystick::vertical(void) {
csharer 0:46523bf02e61 75 //Get average value
csharer 0:46523bf02e61 76 float avg = (vert.read() + vert.read() + vert.read() + vert.read() + vert.read()) / 5.0;
csharer 0:46523bf02e61 77
csharer 0:46523bf02e61 78 //Watch for Max and Min Values
csharer 0:46523bf02e61 79 if(avg > rawMaxV)
csharer 0:46523bf02e61 80 rawMaxV = avg;
csharer 0:46523bf02e61 81 if(avg < rawMinV)
csharer 0:46523bf02e61 82 rawMinV = avg;
csharer 0:46523bf02e61 83
csharer 0:46523bf02e61 84 //Calculate Range (Total Travel of joystick)
csharer 0:46523bf02e61 85 float range_pos = rawMaxV - raw_vc;
csharer 0:46523bf02e61 86 float range_neg = raw_vc - rawMinV;
csharer 0:46523bf02e61 87
csharer 0:46523bf02e61 88 float val;
csharer 0:46523bf02e61 89 if(avg >= raw_vc) //find scaled pot value
csharer 2:893fd930d3fb 90 val = (((avg - raw_vc) / range_pos) * _max) + DEAD_ZONE;
csharer 0:46523bf02e61 91 else
csharer 2:893fd930d3fb 92 val = (((raw_vc - avg) / range_neg) * _min) - DEAD_ZONE;
csharer 0:46523bf02e61 93
csharer 0:46523bf02e61 94 //If val is in dead zone range return 0
csharer 2:893fd930d3fb 95 if((val <= DEAD_ZONE) && (val >= -DEAD_ZONE)) {
csharer 0:46523bf02e61 96 return 0;
csharer 0:46523bf02e61 97 }
csharer 0:46523bf02e61 98 //Else return val minus dead zone / 2
csharer 2:893fd930d3fb 99 else if(val > DEAD_ZONE) {//posotive vals
csharer 2:893fd930d3fb 100 return val - DEAD_ZONE;
csharer 0:46523bf02e61 101 }
csharer 0:46523bf02e61 102 else { //negative vals
csharer 2:893fd930d3fb 103 return val + DEAD_ZONE;
csharer 0:46523bf02e61 104 }
csharer 0:46523bf02e61 105 }
csharer 0:46523bf02e61 106
csharer 0:46523bf02e61 107 //Set the Min and Max Values of joystick ex: -100, +100
csharer 0:46523bf02e61 108 void Joystick::setScale(float min, float max) {
csharer 1:c42a77267f7b 109 _min = min-1; //Add 1 for round off error
csharer 1:c42a77267f7b 110 _max = max+1; //Add 1 for round off error
csharer 0:46523bf02e61 111 }