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:
csharer
Date:
Thu Oct 20 22:16:14 2016 +0000
Revision:
1:c42a77267f7b
Parent:
0:46523bf02e61
Child:
2:893fd930d3fb
added comments ;

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