a

Fork of ESE519_Lab6_part1_skeleton by Carter Sharer

Committer:
hydroguy45
Date:
Wed Oct 24 21:02:19 2018 +0000
Revision:
8:cc7eb86a4b2f
asdfghj

Who changed what in which revision?

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