Since our code is based on lab 6 code and we also used the joystick to test movement initially. So joystick still existed tough we haven't use that eventually

Fork of Joystick_skeleton by Carter Sharer

Committer:
britneyd
Date:
Wed Apr 26 21:00:09 2017 +0000
Revision:
5:8b41e2fc69f9
Parent:
4:aaca0f94b646
Controller code;

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)
britneyd 5:8b41e2fc69f9 16 raw_hc = (horiz.read() + horiz.read() + horiz.read() + horiz.read() + horiz.read()) / 5.0;
britneyd 5:8b41e2fc69f9 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;
britneyd 5:8b41e2fc69f9 25 rawMinH = raw_hc - delta;
britneyd 5:8b41e2fc69f9 26 rawMaxH = raw_hc + delta;
britneyd 5:8b41e2fc69f9 27 rawMinV = raw_vc - delta;
britneyd 5:8b41e2fc69f9 28 rawMaxV = raw_vc + 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)
britneyd 5:8b41e2fc69f9 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
britneyd 5:8b41e2fc69f9 37 if (avg > rawMaxH) {
britneyd 5:8b41e2fc69f9 38 rawMaxH = avg;
britneyd 5:8b41e2fc69f9 39 }
britneyd 5:8b41e2fc69f9 40 if (avg < rawMinH) {
britneyd 5:8b41e2fc69f9 41 rawMinH = avg;
britneyd 5:8b41e2fc69f9 42 }
csharer 0:46523bf02e61 43
csharer 2:893fd930d3fb 44 //(5)Here we will calculate the total range (Travel) of the joystick
csharer 0:46523bf02e61 45 //using the rawMax/rawMin values we have seen thus far
csharer 2:893fd930d3fb 46 //Since the joystick is not symetrical we have to calculate two ranges
csharer 0:46523bf02e61 47 //Calculate the range from [center, max] and [center, min]
britneyd 5:8b41e2fc69f9 48 float range_pos = rawMaxH - raw_hc;
britneyd 5:8b41e2fc69f9 49 float range_neg = raw_hc - rawMinH;
csharer 0:46523bf02e61 50
csharer 2:893fd930d3fb 51 //(6)Here we will calculate how much our current reading is in one
csharer 2:893fd930d3fb 52 //of the ranges, this will give us a percentage of our _max value we
csharer 2:893fd930d3fb 53 //set in setScale. Then we can apply the scale by multiplying it by our
csharer 2:893fd930d3fb 54 //scale (_max/_min).
csharer 0:46523bf02e61 55 float val;
britneyd 5:8b41e2fc69f9 56 if(avg >= raw_hc) { //Positive Range
britneyd 5:8b41e2fc69f9 57 val = ((avg - raw_hc) / range_pos) * _max;
britneyd 5:8b41e2fc69f9 58 }
britneyd 5:8b41e2fc69f9 59 else{ //Negative Range
britneyd 5:8b41e2fc69f9 60 val = (raw_hc - avg) / range_neg * _min;
britneyd 5:8b41e2fc69f9 61 }
csharer 0:46523bf02e61 62
csharer 2:893fd930d3fb 63 //(7)Here we will apply a dead zone. If the |value| is <= our deadzone then
csharer 2:893fd930d3fb 64 //set it to 0. Otherwise we need to shift the value closer to 0 by dead zone
britneyd 5:8b41e2fc69f9 65 if (abs(val) <= DEAD_ZONE) {
britneyd 5:8b41e2fc69f9 66 val = 0;
britneyd 5:8b41e2fc69f9 67 }
britneyd 5:8b41e2fc69f9 68 else if (val > DEAD_ZONE) {
britneyd 5:8b41e2fc69f9 69 val -= DEAD_ZONE;
britneyd 5:8b41e2fc69f9 70 }
britneyd 5:8b41e2fc69f9 71 else if (val < -DEAD_ZONE) {
britneyd 5:8b41e2fc69f9 72 val += DEAD_ZONE;
britneyd 5:8b41e2fc69f9 73 }
csharer 3:6f854e0cae36 74
britneyd 5:8b41e2fc69f9 75 return val;
csharer 0:46523bf02e61 76 }
csharer 0:46523bf02e61 77
csharer 2:893fd930d3fb 78 //(8) Impliment vertial the same as you did for horizontal
csharer 0:46523bf02e61 79 //Returns the scaled horizontal value of joystick
csharer 0:46523bf02e61 80 float Joystick::vertical(void) {
csharer 3:6f854e0cae36 81 //Get average value (5 samples)
britneyd 5:8b41e2fc69f9 82 float avg = (vert.read() + vert.read() + vert.read() + vert.read() + vert.read()) / 5.0;
csharer 3:6f854e0cae36 83
csharer 3:6f854e0cae36 84 //(4)Watch for Max and Min Values, if we see a new max/min update Raw Max/Min
britneyd 5:8b41e2fc69f9 85 if (avg > rawMaxV) {
britneyd 5:8b41e2fc69f9 86 rawMaxV = avg;
britneyd 5:8b41e2fc69f9 87 }
britneyd 5:8b41e2fc69f9 88 if (avg < rawMinV) {
britneyd 5:8b41e2fc69f9 89 rawMinV = avg;
britneyd 5:8b41e2fc69f9 90 }
csharer 0:46523bf02e61 91
csharer 3:6f854e0cae36 92 //(5)Here we will calculate the total range (Travel) of the joystick
csharer 3:6f854e0cae36 93 //using the rawMax/rawMin values we have seen thus far
csharer 3:6f854e0cae36 94 //Since the joystick is not symetrical we have to calculate two ranges
csharer 3:6f854e0cae36 95 //Calculate the range from [center, max] and [center, min]
britneyd 5:8b41e2fc69f9 96 float range_pos = rawMaxV - raw_vc;
britneyd 5:8b41e2fc69f9 97 float range_neg = raw_vc - rawMinV;
csharer 0:46523bf02e61 98
csharer 3:6f854e0cae36 99 //(6)Here we will calculate how much our current reading is in one
csharer 3:6f854e0cae36 100 //of the ranges, this will give us a percentage of our _max value we
csharer 3:6f854e0cae36 101 //set in setScale. Then we can apply the scale by multiplying it by our
csharer 3:6f854e0cae36 102 //scale (_max/_min).
csharer 0:46523bf02e61 103 float val;
britneyd 5:8b41e2fc69f9 104 if(avg >= raw_vc) { //Positive Range
britneyd 5:8b41e2fc69f9 105 val = ((avg - raw_vc) / range_pos) * _max;
britneyd 5:8b41e2fc69f9 106 }
britneyd 5:8b41e2fc69f9 107 else{ //Negative Range
britneyd 5:8b41e2fc69f9 108 val = (raw_vc - avg) / range_neg * _min;
britneyd 5:8b41e2fc69f9 109 }
csharer 0:46523bf02e61 110
csharer 3:6f854e0cae36 111 //(7)Here we will apply a dead zone. If the |value| is <= our deadzone then
csharer 3:6f854e0cae36 112 //set it to 0. Otherwise we need to shift the value closer to 0 by dead zone
britneyd 5:8b41e2fc69f9 113 if (abs(val) <= DEAD_ZONE) {
britneyd 5:8b41e2fc69f9 114 val = 0;
britneyd 5:8b41e2fc69f9 115 }
britneyd 5:8b41e2fc69f9 116 else if (val > DEAD_ZONE) {
britneyd 5:8b41e2fc69f9 117 val -= DEAD_ZONE;
britneyd 5:8b41e2fc69f9 118 }
britneyd 5:8b41e2fc69f9 119 else if (val < -DEAD_ZONE) {
britneyd 5:8b41e2fc69f9 120 val += DEAD_ZONE;
britneyd 5:8b41e2fc69f9 121 }
britneyd 5:8b41e2fc69f9 122
britneyd 5:8b41e2fc69f9 123 return val;
csharer 0:46523bf02e61 124 }
csharer 0:46523bf02e61 125
csharer 0:46523bf02e61 126 //Set the Min and Max Values of joystick ex: -100, +100
csharer 0:46523bf02e61 127 void Joystick::setScale(float min, float max) {
csharer 1:c42a77267f7b 128 _min = min-1; //Add 1 for round off error
csharer 1:c42a77267f7b 129 _max = max+1; //Add 1 for round off error
csharer 0:46523bf02e61 130 }