Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: ESE519_Lab6_part1_skeleton
Fork of Joystick_skelleton by
Joystick.cpp
00001 //Joystick (INCOMPLETE IMPLIMENTATION) 00002 //Author: Carter Sharer 00003 //Date: 10/18/16 00004 00005 #include "Joystick.h" 00006 #include "mbed.h" 00007 00008 //Member function definitions including constructor 00009 Joystick::Joystick(PinName pinA, PinName pinB) : horiz(pinA), vert(pinB) { 00010 //Wait 1 second for voltage to settle 00011 wait(1); 00012 00013 //(1)Set Raw Center Values, this is where the joystick sits naturaly 00014 //set raw horizontal center to current value (average with 5 readings) 00015 //set raw vertial center to current value (average with 5 readings) 00016 raw_hc = 0; 00017 raw_vc = 0; 00018 00019 //(2)Initalize the Rax Max to some value less then the real max value. 00020 //We dont know what the max value will be until we read it, thats ok. 00021 //But we can assume it will be greater then 0.8 (center + 0.3) so lets 00022 //set it to that for now. We will update this later if we see a larger value 00023 //Now do the same for the the Raw Min 00024 float delta = 0.3; 00025 rawMinH = 0; 00026 rawMaxH = 0; 00027 rawMinV = 0; 00028 rawMaxV = 0; 00029 } 00030 00031 //Returns the scaled vertial value of joystick 00032 float Joystick::horizontal(void) { 00033 //(3)Get average val (5 samples) 00034 float avg = horiz.read(); 00035 00036 //(4)Watch for Max and Min Values, if we see a new max/min update Raw Max/Min 00037 rawMaxH = avg; 00038 rawMinH = avg; 00039 00040 //(5)Here we will calculate the total range (Travel) of the joystick 00041 //using the rawMax/rawMin values we have seen thus far 00042 //Since the joystick is not symetrical we have to calculate two ranges 00043 //Calculate the range from [center, max] and [center, min] 00044 float range_pos = 0; 00045 float range_neg = 0; 00046 00047 //(6)Here we will calculate how much our current reading is in one 00048 //of the ranges, this will give us a percentage of our _max value we 00049 //set in setScale. Then we can apply the scale by multiplying it by our 00050 //scale (_max/_min). 00051 float val; 00052 if(avg >= raw_hc) //Positive Range 00053 val = 0; 00054 else //Negative Range 00055 val = 0; 00056 00057 //(7)Here we will apply a dead zone. If the |value| is <= our deadzone then 00058 //set it to 0. Otherwise we need to shift the value closer to 0 by dead zone 00059 00060 00061 return horiz.read(); 00062 } 00063 00064 //(8) Impliment vertial the same as you did for horizontal 00065 //Returns the scaled horizontal value of joystick 00066 float Joystick::vertical(void) { 00067 //Get average value (5 samples) 00068 float avg = vert.read(); 00069 00070 //(4)Watch for Max and Min Values, if we see a new max/min update Raw Max/Min 00071 rawMaxV = avg; 00072 rawMinV = avg; 00073 00074 //(5)Here we will calculate the total range (Travel) of the joystick 00075 //using the rawMax/rawMin values we have seen thus far 00076 //Since the joystick is not symetrical we have to calculate two ranges 00077 //Calculate the range from [center, max] and [center, min] 00078 float range_pos = 0; 00079 float range_neg = 0; 00080 00081 //(6)Here we will calculate how much our current reading is in one 00082 //of the ranges, this will give us a percentage of our _max value we 00083 //set in setScale. Then we can apply the scale by multiplying it by our 00084 //scale (_max/_min). 00085 float val; 00086 if(avg >= raw_vc) //find scaled pot value 00087 val = 0; 00088 else 00089 val = 0; 00090 00091 //(7)Here we will apply a dead zone. If the |value| is <= our deadzone then 00092 //set it to 0. Otherwise we need to shift the value closer to 0 by dead zone 00093 00094 return vert.read(); 00095 } 00096 00097 //Set the Min and Max Values of joystick ex: -100, +100 00098 void Joystick::setScale(float min, float max) { 00099 _min = min-1; //Add 1 for round off error 00100 _max = max+1; //Add 1 for round off error 00101 }
Generated on Wed Jul 13 2022 08:18:41 by
 1.7.2
 1.7.2 
    