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
Joystick.cpp@3:6f854e0cae36, 2016-10-20 (annotated)
- Committer:
- csharer
- Date:
- Thu Oct 20 23:44:34 2016 +0000
- Revision:
- 3:6f854e0cae36
- Parent:
- 2:893fd930d3fb
- Child:
- 4:aaca0f94b646
updated;
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:6f854e0cae36 | 16 | raw_hc = 0; |
csharer | 3:6f854e0cae36 | 17 | raw_vc = 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 | 3:6f854e0cae36 | 25 | rawMinH = 0; |
csharer | 3:6f854e0cae36 | 26 | rawMaxH = 0; |
csharer | 3:6f854e0cae36 | 27 | rawMinV = 0; |
csharer | 3:6f854e0cae36 | 28 | rawMaxV = 0; |
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 | 3:6f854e0cae36 | 34 | float avg = horiz.read(); |
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 | 3:6f854e0cae36 | 37 | rawMaxH = avg; |
csharer | 3:6f854e0cae36 | 38 | rawMinH = avg; |
csharer | 0:46523bf02e61 | 39 | |
csharer | 2:893fd930d3fb | 40 | //(5)Here we will calculate the total range (Travel) of the joystick |
csharer | 0:46523bf02e61 | 41 | //using the rawMax/rawMin values we have seen thus far |
csharer | 2:893fd930d3fb | 42 | //Since the joystick is not symetrical we have to calculate two ranges |
csharer | 0:46523bf02e61 | 43 | //Calculate the range from [center, max] and [center, min] |
csharer | 3:6f854e0cae36 | 44 | float range_pos = 0; |
csharer | 3:6f854e0cae36 | 45 | float range_neg = 0; |
csharer | 0:46523bf02e61 | 46 | |
csharer | 2:893fd930d3fb | 47 | //(6)Here we will calculate how much our current reading is in one |
csharer | 2:893fd930d3fb | 48 | //of the ranges, this will give us a percentage of our _max value we |
csharer | 2:893fd930d3fb | 49 | //set in setScale. Then we can apply the scale by multiplying it by our |
csharer | 2:893fd930d3fb | 50 | //scale (_max/_min). |
csharer | 0:46523bf02e61 | 51 | float val; |
csharer | 2:893fd930d3fb | 52 | if(avg >= raw_hc) //Positive Range |
csharer | 3:6f854e0cae36 | 53 | val = 0; |
csharer | 2:893fd930d3fb | 54 | else //Negative Range |
csharer | 3:6f854e0cae36 | 55 | val = 0; |
csharer | 0:46523bf02e61 | 56 | |
csharer | 2:893fd930d3fb | 57 | //(7)Here we will apply a dead zone. If the |value| is <= our deadzone then |
csharer | 2:893fd930d3fb | 58 | //set it to 0. Otherwise we need to shift the value closer to 0 by dead zone |
csharer | 3:6f854e0cae36 | 59 | |
csharer | 3:6f854e0cae36 | 60 | |
csharer | 3:6f854e0cae36 | 61 | return horiz.read(); |
csharer | 0:46523bf02e61 | 62 | } |
csharer | 0:46523bf02e61 | 63 | |
csharer | 2:893fd930d3fb | 64 | //(8) Impliment vertial the same as you did for horizontal |
csharer | 0:46523bf02e61 | 65 | //Returns the scaled horizontal value of joystick |
csharer | 0:46523bf02e61 | 66 | float Joystick::vertical(void) { |
csharer | 3:6f854e0cae36 | 67 | //Get average value (5 samples) |
csharer | 3:6f854e0cae36 | 68 | float avg = vert.read(); |
csharer | 3:6f854e0cae36 | 69 | |
csharer | 3:6f854e0cae36 | 70 | //(4)Watch for Max and Min Values, if we see a new max/min update Raw Max/Min |
csharer | 3:6f854e0cae36 | 71 | rawMaxV = avg; |
csharer | 3:6f854e0cae36 | 72 | rawMinV = avg; |
csharer | 0:46523bf02e61 | 73 | |
csharer | 3:6f854e0cae36 | 74 | //(5)Here we will calculate the total range (Travel) of the joystick |
csharer | 3:6f854e0cae36 | 75 | //using the rawMax/rawMin values we have seen thus far |
csharer | 3:6f854e0cae36 | 76 | //Since the joystick is not symetrical we have to calculate two ranges |
csharer | 3:6f854e0cae36 | 77 | //Calculate the range from [center, max] and [center, min] |
csharer | 3:6f854e0cae36 | 78 | float range_pos = 0; |
csharer | 3:6f854e0cae36 | 79 | float range_neg = 0; |
csharer | 0:46523bf02e61 | 80 | |
csharer | 3:6f854e0cae36 | 81 | //(6)Here we will calculate how much our current reading is in one |
csharer | 3:6f854e0cae36 | 82 | //of the ranges, this will give us a percentage of our _max value we |
csharer | 3:6f854e0cae36 | 83 | //set in setScale. Then we can apply the scale by multiplying it by our |
csharer | 3:6f854e0cae36 | 84 | //scale (_max/_min). |
csharer | 0:46523bf02e61 | 85 | float val; |
csharer | 0:46523bf02e61 | 86 | if(avg >= raw_vc) //find scaled pot value |
csharer | 3:6f854e0cae36 | 87 | val = 0; |
csharer | 0:46523bf02e61 | 88 | else |
csharer | 3:6f854e0cae36 | 89 | val = 0; |
csharer | 0:46523bf02e61 | 90 | |
csharer | 3:6f854e0cae36 | 91 | //(7)Here we will apply a dead zone. If the |value| is <= our deadzone then |
csharer | 3:6f854e0cae36 | 92 | //set it to 0. Otherwise we need to shift the value closer to 0 by dead zone |
csharer | 3:6f854e0cae36 | 93 | |
csharer | 3:6f854e0cae36 | 94 | return vert.read(); |
csharer | 0:46523bf02e61 | 95 | } |
csharer | 0:46523bf02e61 | 96 | |
csharer | 0:46523bf02e61 | 97 | //Set the Min and Max Values of joystick ex: -100, +100 |
csharer | 0:46523bf02e61 | 98 | void Joystick::setScale(float min, float max) { |
csharer | 1:c42a77267f7b | 99 | _min = min-1; //Add 1 for round off error |
csharer | 1:c42a77267f7b | 100 | _max = max+1; //Add 1 for round off error |
csharer | 0:46523bf02e61 | 101 | } |