Added more code for side and angled sensor
Dependencies: QEI2 PID Watchdog VL53L1X_Filter BNOWrapper ros_lib_kinetic
wheelchair.cpp@41:1a687bcf4b0a, 2019-07-11 (annotated)
- Committer:
- t1jain
- Date:
- Thu Jul 11 20:58:26 2019 +0000
- Revision:
- 41:1a687bcf4b0a
- Parent:
- 40:11b8567c507b
- Child:
- 42:62c49ad06707
Cleaned up code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JesiMiranda | 35:5a2fed4c2e9f | 1 | /************************************************************************* |
JesiMiranda | 35:5a2fed4c2e9f | 2 | * Importing header into wheelchair.cpp * |
JesiMiranda | 35:5a2fed4c2e9f | 3 | **************************************************************************/ |
ryanlin97 | 0:fc0c4a184482 | 4 | #include "wheelchair.h" |
t1jain | 30:c25b2556e84d | 5 | |
JesiMiranda | 35:5a2fed4c2e9f | 6 | /************************************************************************* |
JesiMiranda | 35:5a2fed4c2e9f | 7 | * Defining global variables * |
JesiMiranda | 35:5a2fed4c2e9f | 8 | **************************************************************************/ |
jvfausto | 21:3489cffad196 | 9 | bool manual_drive = false; // Variable changes between joystick and auto drive |
jvfausto | 21:3489cffad196 | 10 | double encoder_distance; // Keeps distanse due to original position |
JesiMiranda | 35:5a2fed4c2e9f | 11 | |
jvfausto | 21:3489cffad196 | 12 | volatile double Setpoint, Output, Input, Input2; // Variables for PID |
jvfausto | 21:3489cffad196 | 13 | volatile double pid_yaw, Distance, Setpoint2, Output2, encoder_distance2; // Variables for PID |
jvfausto | 21:3489cffad196 | 14 | volatile double vIn, vOut, vDesired; // Variables for PID Velosity |
jvfausto | 21:3489cffad196 | 15 | volatile double vInS, vOutS, vDesiredS; // Variables for PID Slave Wheel |
jvfausto | 21:3489cffad196 | 16 | volatile double yIn, yOut, yDesired; // Variables for PID turn velosity |
t1jain | 30:c25b2556e84d | 17 | // int* ToFDataPointer1; |
t1jain | 30:c25b2556e84d | 18 | // int* ToFDataPointer2; |
t1jain | 30:c25b2556e84d | 19 | |
JesiMiranda | 35:5a2fed4c2e9f | 20 | int ledgeArrayLF[150]; |
JesiMiranda | 35:5a2fed4c2e9f | 21 | int ledgeArrayRF[150]; |
t1jain | 30:c25b2556e84d | 22 | int* ToFDataPointer1 = ledgeArrayLF; |
t1jain | 30:c25b2556e84d | 23 | int* ToFDataPointer2 = ledgeArrayRF; |
JesiMiranda | 35:5a2fed4c2e9f | 24 | Statistics LFTStats(ToFDataPointer1, 149, 1); |
JesiMiranda | 35:5a2fed4c2e9f | 25 | Statistics RFTStats(ToFDataPointer2, 149, 1); |
t1jain | 40:11b8567c507b | 26 | int k = 0; |
t1jain | 38:cb87f4e353b9 | 27 | |
t1jain | 38:cb87f4e353b9 | 28 | int ledgeArrayLB[150]; |
t1jain | 38:cb87f4e353b9 | 29 | int ledgeArrayRB[150]; |
t1jain | 38:cb87f4e353b9 | 30 | int* ToFDataPointer3 = ledgeArrayLB; |
t1jain | 38:cb87f4e353b9 | 31 | int* ToFDataPointer4 = ledgeArrayRB; |
t1jain | 38:cb87f4e353b9 | 32 | Statistics LBTStats(ToFDataPointer3, 149, 1); |
t1jain | 38:cb87f4e353b9 | 33 | Statistics RBTStats(ToFDataPointer4, 149, 1); |
ryanlin97 | 11:d14a1f7f1297 | 34 | |
jvfausto | 21:3489cffad196 | 35 | double dist_old, curr_pos; // Variables for odometry position |
t1jain | 30:c25b2556e84d | 36 | double outlierToF[4]; |
jvfausto | 19:71a6621ee5c3 | 37 | |
JesiMiranda | 35:5a2fed4c2e9f | 38 | /************************************************************************* |
JesiMiranda | 35:5a2fed4c2e9f | 39 | * Creating PID objects * |
JesiMiranda | 35:5a2fed4c2e9f | 40 | **************************************************************************/ |
jvfausto | 21:3489cffad196 | 41 | PID myPID(&pid_yaw, &Output, &Setpoint, 5.5, .00, 0.0036, P_ON_E, DIRECT); // Angle PID object constructor |
jvfausto | 21:3489cffad196 | 42 | PID myPIDDistance(&Input, &Output, &Setpoint, 5.5, .00, 0.002, P_ON_E, DIRECT); // Distance PID object constructor |
jvfausto | 21:3489cffad196 | 43 | PID PIDVelosity(&vIn, &vOut, &vDesired, 5.5, .00, .002, P_ON_E, DIRECT); // Velosity PID Constructor |
jvfausto | 21:3489cffad196 | 44 | PID PIDSlaveV(&vInS, &vOutS, &vDesiredS, 5.5, .00, .002, P_ON_E, DIRECT); // Slave Velosity PID Constructor |
jvfausto | 21:3489cffad196 | 45 | PID PIDAngularV(&yIn, &yOut, &yDesired, 5.5, .00, .002, P_ON_E, DIRECT); // Angular Velosity PID Constructor |
JesiMiranda | 35:5a2fed4c2e9f | 46 | |
jvfausto | 19:71a6621ee5c3 | 47 | |
JesiMiranda | 35:5a2fed4c2e9f | 48 | /************************************************************************* |
JesiMiranda | 35:5a2fed4c2e9f | 49 | * Thread measures current angular position * |
JesiMiranda | 35:5a2fed4c2e9f | 50 | **************************************************************************/ |
JesiMiranda | 35:5a2fed4c2e9f | 51 | void Wheelchair::compass_thread() |
JesiMiranda | 35:5a2fed4c2e9f | 52 | { |
JesiMiranda | 35:5a2fed4c2e9f | 53 | curr_yaw = imu->yaw(); |
JesiMiranda | 35:5a2fed4c2e9f | 54 | z_angular = curr_yaw; |
jvfausto | 21:3489cffad196 | 55 | } |
jvfausto | 17:7f3b69300bb6 | 56 | |
JesiMiranda | 35:5a2fed4c2e9f | 57 | /************************************************************************* |
JesiMiranda | 35:5a2fed4c2e9f | 58 | * Thread measures velocity of wheels and distance traveled * |
JesiMiranda | 35:5a2fed4c2e9f | 59 | **************************************************************************/ |
JesiMiranda | 35:5a2fed4c2e9f | 60 | void Wheelchair::velocity_thread() |
ryanlin97 | 1:c0beadca1617 | 61 | { |
jvfausto | 21:3489cffad196 | 62 | curr_vel = wheel->getVelocity(); |
jvfausto | 21:3489cffad196 | 63 | curr_velS = wheelS->getVelocity(); |
jvfausto | 21:3489cffad196 | 64 | curr_pos = wheel->getDistance(53.975); |
ryanlin97 | 1:c0beadca1617 | 65 | } |
ryanlin97 | 6:0cd57bdd8fbc | 66 | |
t1jain | 34:b89967adc86c | 67 | void Wheelchair::emergencyButton_thread () |
t1jain | 34:b89967adc86c | 68 | { |
t1jain | 34:b89967adc86c | 69 | while(1) { |
t1jain | 34:b89967adc86c | 70 | while(!e_button) { |
t1jain | 34:b89967adc86c | 71 | |
t1jain | 34:b89967adc86c | 72 | //Stop wheelchair |
t1jain | 34:b89967adc86c | 73 | Wheelchair::stop(); |
t1jain | 34:b89967adc86c | 74 | printf("E-button has been pressed\r\n"); |
t1jain | 34:b89967adc86c | 75 | off->write(high); // Turn off PCB |
t1jain | 34:b89967adc86c | 76 | on->write(0); // Make sure PCB not on |
t1jain | 34:b89967adc86c | 77 | //Reset Board |
t1jain | 34:b89967adc86c | 78 | NVIC_SystemReset(); |
t1jain | 34:b89967adc86c | 79 | |
t1jain | 34:b89967adc86c | 80 | } |
t1jain | 34:b89967adc86c | 81 | |
t1jain | 34:b89967adc86c | 82 | } |
t1jain | 34:b89967adc86c | 83 | } |
t1jain | 34:b89967adc86c | 84 | |
JesiMiranda | 35:5a2fed4c2e9f | 85 | /************************************************************************* |
JesiMiranda | 37:e0e6d3fe06a2 | 86 | * Thread checks ToF sensors for safety of wheelchair movement * |
JesiMiranda | 35:5a2fed4c2e9f | 87 | **************************************************************************/ |
t1jain | 30:c25b2556e84d | 88 | void Wheelchair::ToFSafe_thread() |
ryanlin97 | 1:c0beadca1617 | 89 | { |
jvfausto | 21:3489cffad196 | 90 | int ToFV[12]; |
JesiMiranda | 35:5a2fed4c2e9f | 91 | for(int i = 0; i < 6; i++) { // Reads from the ToF Sensors |
jvfausto | 21:3489cffad196 | 92 | ToFV[i] = (*(ToF+i))->readFromOneSensor(); |
t1jain | 32:fb26baa75d44 | 93 | //out->printf("%d ", ToFV[i]); |
JesiMiranda | 35:5a2fed4c2e9f | 94 | } |
JesiMiranda | 35:5a2fed4c2e9f | 95 | |
JesiMiranda | 35:5a2fed4c2e9f | 96 | //out->printf("\r\n"); |
JesiMiranda | 35:5a2fed4c2e9f | 97 | |
t1jain | 40:11b8567c507b | 98 | k++; |
JesiMiranda | 35:5a2fed4c2e9f | 99 | |
t1jain | 40:11b8567c507b | 100 | if (k == 150) { |
t1jain | 40:11b8567c507b | 101 | k = 1; |
t1jain | 30:c25b2556e84d | 102 | } |
JesiMiranda | 35:5a2fed4c2e9f | 103 | |
t1jain | 40:11b8567c507b | 104 | ledgeArrayLF[k] = (*(ToF+1))->readFromOneSensor(); |
t1jain | 40:11b8567c507b | 105 | ledgeArrayRF[k] = (*(ToF+4))->readFromOneSensor(); |
JesiMiranda | 35:5a2fed4c2e9f | 106 | |
t1jain | 41:1a687bcf4b0a | 107 | /* for(int i = 0; i < 100; i++) |
t1jain | 31:06f2362caf12 | 108 | { |
t1jain | 31:06f2362caf12 | 109 | out->printf("%d, ",ledgeArrayRF[i]); |
t1jain | 31:06f2362caf12 | 110 | } |
t1jain | 41:1a687bcf4b0a | 111 | out->printf("\r\n"); */ |
t1jain | 34:b89967adc86c | 112 | |
t1jain | 30:c25b2556e84d | 113 | outlierToF[0] = LFTStats.mean() + 2*LFTStats.stdev(); |
JesiMiranda | 35:5a2fed4c2e9f | 114 | outlierToF[1] = RFTStats.mean() + 2*RFTStats.stdev(); |
JesiMiranda | 35:5a2fed4c2e9f | 115 | |
t1jain | 40:11b8567c507b | 116 | for(int i = 0; i < 2; i++) { // Reads from the ToF Sensors |
jvfausto | 27:da718b990837 | 117 | runningAverage[i] = ((runningAverage[i]*(4) + ToFV[(i*3)+1]) / 5); |
t1jain | 30:c25b2556e84d | 118 | } |
JesiMiranda | 35:5a2fed4c2e9f | 119 | |
jvfausto | 27:da718b990837 | 120 | int sensor1 = ToFV[0]; |
jvfausto | 27:da718b990837 | 121 | int sensor4 = ToFV[3]; |
JesiMiranda | 35:5a2fed4c2e9f | 122 | if(curr_vel < 1 &&((2 * maxDecelerationSlow*sensor1 < curr_vel*curr_vel*1000*1000 || |
JesiMiranda | 35:5a2fed4c2e9f | 123 | 2 * maxDecelerationSlow*sensor4 < curr_vel*curr_vel*1000*1000) && |
JesiMiranda | 35:5a2fed4c2e9f | 124 | (sensor1 < 1500 || sensor4 < 1500)) || |
JesiMiranda | 35:5a2fed4c2e9f | 125 | 550 > sensor1 || 550 > sensor4) { |
JesiMiranda | 35:5a2fed4c2e9f | 126 | if(x->read() > def) { |
jvfausto | 26:662693bd7f31 | 127 | x->write(def); |
t1jain | 30:c25b2556e84d | 128 | forwardSafety = 1; // You cannot move forward |
jvfausto | 26:662693bd7f31 | 129 | } |
jvfausto | 26:662693bd7f31 | 130 | } |
JesiMiranda | 35:5a2fed4c2e9f | 131 | |
JesiMiranda | 35:5a2fed4c2e9f | 132 | else if(curr_vel > 1 &&((2 * maxDecelerationFast*sensor1 < curr_vel*curr_vel*1000*1000 || |
JesiMiranda | 35:5a2fed4c2e9f | 133 | 2 * maxDecelerationFast*sensor4 < curr_vel*curr_vel*1000*1000) && |
JesiMiranda | 35:5a2fed4c2e9f | 134 | (sensor1 < 1500 || sensor4 < 1500)) || |
JesiMiranda | 35:5a2fed4c2e9f | 135 | 550 > sensor1 || 550 > sensor4) { |
JesiMiranda | 35:5a2fed4c2e9f | 136 | if(x->read() > def) { |
jvfausto | 21:3489cffad196 | 137 | x->write(def); |
t1jain | 34:b89967adc86c | 138 | forwardSafety = 1; // You cannot move forward |
jvfausto | 21:3489cffad196 | 139 | } |
jvfausto | 21:3489cffad196 | 140 | } |
JesiMiranda | 35:5a2fed4c2e9f | 141 | |
t1jain | 30:c25b2556e84d | 142 | else if ((runningAverage[0] > outlierToF[0]) || (runningAverage[1] > outlierToF[1])) { |
t1jain | 30:c25b2556e84d | 143 | forwardSafety = 1; |
t1jain | 38:cb87f4e353b9 | 144 | out->printf("I'M STOPPING BECAUSE OF A FRONT LEDGE\r\n"); |
JesiMiranda | 35:5a2fed4c2e9f | 145 | } |
JesiMiranda | 35:5a2fed4c2e9f | 146 | |
jvfausto | 21:3489cffad196 | 147 | else |
jvfausto | 21:3489cffad196 | 148 | forwardSafety = 0; |
t1jain | 38:cb87f4e353b9 | 149 | |
t1jain | 38:cb87f4e353b9 | 150 | //////////////////////////////////////////////////////////////////////////// |
t1jain | 38:cb87f4e353b9 | 151 | |
t1jain | 40:11b8567c507b | 152 | ledgeArrayLB[k] = (*(ToF+1))->readFromOneSensor(); |
t1jain | 40:11b8567c507b | 153 | ledgeArrayRB[k] = (*(ToF+4))->readFromOneSensor(); |
t1jain | 38:cb87f4e353b9 | 154 | |
t1jain | 38:cb87f4e353b9 | 155 | outlierToF[2] = LBTStats.mean() + 2*LBTStats.stdev(); |
t1jain | 38:cb87f4e353b9 | 156 | outlierToF[3] = RBTStats.mean() + 2*RBTStats.stdev(); |
t1jain | 38:cb87f4e353b9 | 157 | |
t1jain | 40:11b8567c507b | 158 | for(int i = 2; i < 4; i++) { // Reads from the ToF Sensors |
t1jain | 38:cb87f4e353b9 | 159 | runningAverage[i] = ((runningAverage[i]*(4) + ToFV[(i*3)+1]) / 5); |
t1jain | 38:cb87f4e353b9 | 160 | } |
t1jain | 38:cb87f4e353b9 | 161 | |
t1jain | 38:cb87f4e353b9 | 162 | int sensor7 = ToFV[6]; |
t1jain | 38:cb87f4e353b9 | 163 | int sensor10 = ToFV[9]; |
t1jain | 38:cb87f4e353b9 | 164 | if(curr_vel < 1 &&((2 * maxDecelerationSlow*sensor7 < curr_vel*curr_vel*1000*1000 || |
t1jain | 38:cb87f4e353b9 | 165 | 2 * maxDecelerationSlow*sensor10 < curr_vel*curr_vel*1000*1000) && |
t1jain | 38:cb87f4e353b9 | 166 | (sensor7 < 1500 || sensor10 < 1500)) || |
t1jain | 38:cb87f4e353b9 | 167 | 550 > sensor7 || 550 > sensor10) { |
t1jain | 38:cb87f4e353b9 | 168 | if(x->read() > def) { |
t1jain | 38:cb87f4e353b9 | 169 | x->write(def); |
t1jain | 39:5438557a8b4e | 170 | backwardSafety = 1; // You cannot move backwards |
t1jain | 38:cb87f4e353b9 | 171 | } |
t1jain | 38:cb87f4e353b9 | 172 | } |
t1jain | 38:cb87f4e353b9 | 173 | |
t1jain | 38:cb87f4e353b9 | 174 | else if(curr_vel > 1 &&((2 * maxDecelerationFast*sensor1 < curr_vel*curr_vel*1000*1000 || |
t1jain | 38:cb87f4e353b9 | 175 | 2 * maxDecelerationFast*sensor4 < curr_vel*curr_vel*1000*1000) && |
t1jain | 38:cb87f4e353b9 | 176 | (sensor7 < 1500 || sensor10 < 1500)) || |
t1jain | 38:cb87f4e353b9 | 177 | 550 > sensor7 || 550 > sensor10) { |
t1jain | 38:cb87f4e353b9 | 178 | if(x->read() > def) { |
t1jain | 38:cb87f4e353b9 | 179 | x->write(def); |
t1jain | 39:5438557a8b4e | 180 | backwardSafety = 1; // You cannot move backwards |
t1jain | 38:cb87f4e353b9 | 181 | } |
t1jain | 38:cb87f4e353b9 | 182 | } |
t1jain | 38:cb87f4e353b9 | 183 | |
t1jain | 38:cb87f4e353b9 | 184 | else if ((runningAverage[2] > outlierToF[2]) || (runningAverage[3] > outlierToF[3])) { |
t1jain | 38:cb87f4e353b9 | 185 | backwardSafety = 1; |
t1jain | 38:cb87f4e353b9 | 186 | out->printf("I'M STOPPING BECAUSE OF A BACK LEDGE\r\n"); |
t1jain | 38:cb87f4e353b9 | 187 | } |
t1jain | 38:cb87f4e353b9 | 188 | |
t1jain | 38:cb87f4e353b9 | 189 | else |
t1jain | 38:cb87f4e353b9 | 190 | backwardSafety = 0; |
t1jain | 41:1a687bcf4b0a | 191 | |
t1jain | 38:cb87f4e353b9 | 192 | //////////////////////////////////////////////////////////////////////////// |
JesiMiranda | 35:5a2fed4c2e9f | 193 | |
JesiMiranda | 37:e0e6d3fe06a2 | 194 | /*Side Tof begin*/ |
t1jain | 34:b89967adc86c | 195 | int sensor3 = ToFV[2]; //front left |
t1jain | 34:b89967adc86c | 196 | int sensor6 = ToFV[5]; //front right |
t1jain | 34:b89967adc86c | 197 | int sensor9 = ToFV[8]; //back |
t1jain | 34:b89967adc86c | 198 | int sensor12 = ToFV[11]; //back |
t1jain | 34:b89967adc86c | 199 | |
JesiMiranda | 37:e0e6d3fe06a2 | 200 | double currAngularVelocity = imu->gyro_x(); //Current angular velocity from IMU |
JesiMiranda | 37:e0e6d3fe06a2 | 201 | double angle = imu->yaw() * 3.14159 / 180; //from IMU, in rads |
JesiMiranda | 37:e0e6d3fe06a2 | 202 | double arcLength = WheelchairRadius * currAngularVelocity * |
JesiMiranda | 37:e0e6d3fe06a2 | 203 | currAngularVelocity / (2 * maxAngularDeceleration); //S = r*Ө, in cm |
JesiMiranda | 37:e0e6d3fe06a2 | 204 | |
JesiMiranda | 37:e0e6d3fe06a2 | 205 | /* Clear the front side first, else continue going straight or can't turn |
JesiMiranda | 37:e0e6d3fe06a2 | 206 | After clearing the front sideand movinf forward, check if can clear the back |
JesiMiranda | 37:e0e6d3fe06a2 | 207 | when turning */ |
JesiMiranda | 37:e0e6d3fe06a2 | 208 | |
JesiMiranda | 37:e0e6d3fe06a2 | 209 | //When either sensors too close to the wall, can't turn |
JesiMiranda | 37:e0e6d3fe06a2 | 210 | if(sensor3 <= minWallLength) { |
JesiMiranda | 37:e0e6d3fe06a2 | 211 | leftSafety = 1; |
JesiMiranda | 37:e0e6d3fe06a2 | 212 | out-> printf("Detecting wall to the left!\n"); |
JesiMiranda | 37:e0e6d3fe06a2 | 213 | } |
JesiMiranda | 37:e0e6d3fe06a2 | 214 | else{ |
JesiMiranda | 37:e0e6d3fe06a2 | 215 | leftSafety = 0; |
JesiMiranda | 37:e0e6d3fe06a2 | 216 | } |
JesiMiranda | 37:e0e6d3fe06a2 | 217 | |
JesiMiranda | 37:e0e6d3fe06a2 | 218 | if(sensor6 <= minWallLength) { |
JesiMiranda | 37:e0e6d3fe06a2 | 219 | rightSafety = 1; |
JesiMiranda | 37:e0e6d3fe06a2 | 220 | out-> printf("Detecting wall to the right!\n"); |
JesiMiranda | 37:e0e6d3fe06a2 | 221 | } |
JesiMiranda | 37:e0e6d3fe06a2 | 222 | else { |
JesiMiranda | 37:e0e6d3fe06a2 | 223 | rightSafety = 0; |
t1jain | 34:b89967adc86c | 224 | } |
t1jain | 34:b89967adc86c | 225 | |
JesiMiranda | 37:e0e6d3fe06a2 | 226 | /*Check whether safe to keep turning |
JesiMiranda | 37:e0e6d3fe06a2 | 227 | Know the exact moment you can stop the chair going at a certain speed before |
JesiMiranda | 37:e0e6d3fe06a2 | 228 | its too late*/ |
JesiMiranda | 37:e0e6d3fe06a2 | 229 | if((currAngularVelocity * currAngularVelocity > 2 * |
JesiMiranda | 37:e0e6d3fe06a2 | 230 | maxAngularDeceleration * angle) && (sensor3/10 <= arcLength + 10)) { |
JesiMiranda | 37:e0e6d3fe06a2 | 231 | leftSafety = 1; //Not safe to turn left |
JesiMiranda | 37:e0e6d3fe06a2 | 232 | out-> printf("Too fast to the left!\n"); |
JesiMiranda | 37:e0e6d3fe06a2 | 233 | } |
JesiMiranda | 37:e0e6d3fe06a2 | 234 | else{ |
JesiMiranda | 37:e0e6d3fe06a2 | 235 | leftSafety = 0; |
JesiMiranda | 37:e0e6d3fe06a2 | 236 | } |
JesiMiranda | 37:e0e6d3fe06a2 | 237 | if((currAngularVelocity * currAngularVelocity > 2 * |
JesiMiranda | 37:e0e6d3fe06a2 | 238 | maxAngularDeceleration * angle) && (sensor6/10 <= arcLength + 10)) { |
JesiMiranda | 37:e0e6d3fe06a2 | 239 | rightSafety = 1; //Not safe to turn right |
JesiMiranda | 37:e0e6d3fe06a2 | 240 | out-> printf("Too fast to the right!\n"); |
t1jain | 34:b89967adc86c | 241 | } |
JesiMiranda | 37:e0e6d3fe06a2 | 242 | else{ |
JesiMiranda | 37:e0e6d3fe06a2 | 243 | rightSafety = 0; |
JesiMiranda | 37:e0e6d3fe06a2 | 244 | } |
JesiMiranda | 37:e0e6d3fe06a2 | 245 | |
JesiMiranda | 37:e0e6d3fe06a2 | 246 | //Safe to continue turning |
JesiMiranda | 37:e0e6d3fe06a2 | 247 | //Check if can turn left and back side sensors |
t1jain | 39:5438557a8b4e | 248 | // Staring t road and frequenctly checking |
JesiMiranda | 37:e0e6d3fe06a2 | 249 | //Check the back sensor |
JesiMiranda | 37:e0e6d3fe06a2 | 250 | /*int sensor7 = ToFV[0]; //back sensor NOTTT SURE |
JesiMiranda | 37:e0e6d3fe06a2 | 251 | int sensor8 = ToFV[3]; //back sensor |
JesiMiranda | 37:e0e6d3fe06a2 | 252 | |
JesiMiranda | 37:e0e6d3fe06a2 | 253 | if(curr_vel < 1 &&((2 * maxDecelerationSlow*sensor7 < curr_vel*curr_vel*1000*1000 || |
JesiMiranda | 37:e0e6d3fe06a2 | 254 | 2 * maxDecelerationSlow*sensor8 < curr_vel*curr_vel*1000*1000) && |
JesiMiranda | 37:e0e6d3fe06a2 | 255 | (sensor7 < 1500 || sensor8 < 1500)) || |
JesiMiranda | 37:e0e6d3fe06a2 | 256 | 550 > sensor7 || 550 > sensor8) |
JesiMiranda | 37:e0e6d3fe06a2 | 257 | { |
JesiMiranda | 37:e0e6d3fe06a2 | 258 | //out->printf("i am in danger\r\n"); |
JesiMiranda | 37:e0e6d3fe06a2 | 259 | if(x->read() > def) |
JesiMiranda | 37:e0e6d3fe06a2 | 260 | { |
JesiMiranda | 37:e0e6d3fe06a2 | 261 | x->write(def); |
JesiMiranda | 37:e0e6d3fe06a2 | 262 | backwardSafety = 1;// You cannot move backward |
JesiMiranda | 37:e0e6d3fe06a2 | 263 | } |
JesiMiranda | 37:e0e6d3fe06a2 | 264 | } |
JesiMiranda | 37:e0e6d3fe06a2 | 265 | //When going to fast to stop from wall |
JesiMiranda | 37:e0e6d3fe06a2 | 266 | else if(curr_vel > 1 &&((2 * maxDecelerationFast*sensor7 < curr_vel*curr_vel*1000*1000 || |
JesiMiranda | 37:e0e6d3fe06a2 | 267 | 2 * maxDecelerationFast*sensor8 < curr_vel*curr_vel*1000*1000) && |
JesiMiranda | 37:e0e6d3fe06a2 | 268 | (sensor7 < 1500 || sensor8 < 1500)) || |
JesiMiranda | 37:e0e6d3fe06a2 | 269 | 550 > sensor7 || 550 > sensor8) |
JesiMiranda | 37:e0e6d3fe06a2 | 270 | { |
JesiMiranda | 37:e0e6d3fe06a2 | 271 | //out->printf("i am in danger\r\n"); |
JesiMiranda | 37:e0e6d3fe06a2 | 272 | if(x->read() > def) |
JesiMiranda | 37:e0e6d3fe06a2 | 273 | { |
JesiMiranda | 37:e0e6d3fe06a2 | 274 | x->write(def); |
JesiMiranda | 37:e0e6d3fe06a2 | 275 | backwardSafety = 1; |
JesiMiranda | 37:e0e6d3fe06a2 | 276 | } |
JesiMiranda | 37:e0e6d3fe06a2 | 277 | }*/ |
JesiMiranda | 37:e0e6d3fe06a2 | 278 | |
JesiMiranda | 37:e0e6d3fe06a2 | 279 | /*Side Tof end*/ |
JesiMiranda | 37:e0e6d3fe06a2 | 280 | |
jvfausto | 21:3489cffad196 | 281 | } |
ryanlin97 | 6:0cd57bdd8fbc | 282 | |
JesiMiranda | 35:5a2fed4c2e9f | 283 | /************************************************************************* |
JesiMiranda | 35:5a2fed4c2e9f | 284 | * Constructor for Wheelchair class * |
JesiMiranda | 35:5a2fed4c2e9f | 285 | **************************************************************************/ |
JesiMiranda | 35:5a2fed4c2e9f | 286 | Wheelchair::Wheelchair(PinName xPin, PinName yPin, Serial* pc, Timer* time, QEI* qei, QEI* qeiS, |
JesiMiranda | 35:5a2fed4c2e9f | 287 | VL53L1X** ToFT) |
jvfausto | 21:3489cffad196 | 288 | { |
jvfausto | 21:3489cffad196 | 289 | x_position = 0; |
jvfausto | 21:3489cffad196 | 290 | y_position = 0; |
jvfausto | 21:3489cffad196 | 291 | forwardSafety = 0; |
t1jain | 39:5438557a8b4e | 292 | backwardSafety = 0; |
JesiMiranda | 35:5a2fed4c2e9f | 293 | |
jvfausto | 21:3489cffad196 | 294 | /* Initializes X and Y variables to Pins */ |
JesiMiranda | 35:5a2fed4c2e9f | 295 | x = new PwmOut(xPin); |
jvfausto | 21:3489cffad196 | 296 | y = new PwmOut(yPin); |
JesiMiranda | 35:5a2fed4c2e9f | 297 | |
jvfausto | 21:3489cffad196 | 298 | /* Initializes IMU Library */ |
jvfausto | 26:662693bd7f31 | 299 | out = pc; // "out" is called for serial monitor |
jvfausto | 26:662693bd7f31 | 300 | out->printf("on\r\n"); |
JesiMiranda | 37:e0e6d3fe06a2 | 301 | imu = new IMUWheelchair(pc, time); |
jvfausto | 21:3489cffad196 | 302 | Wheelchair::stop(); // Wheelchair is initially stationary |
jvfausto | 21:3489cffad196 | 303 | imu->setup(); // turns on the IMU |
jvfausto | 21:3489cffad196 | 304 | wheelS = qeiS; // "wheel" is called for encoder |
JesiMiranda | 35:5a2fed4c2e9f | 305 | wheel = qei; |
jvfausto | 21:3489cffad196 | 306 | ToF = ToFT; // passes pointer with addresses of ToF sensors |
JesiMiranda | 35:5a2fed4c2e9f | 307 | |
JesiMiranda | 35:5a2fed4c2e9f | 308 | for(int i = 0; i < 12; i++) { // initializes the ToF Sensors |
jvfausto | 21:3489cffad196 | 309 | (*(ToF+i))->initReading(0x31+((0x02)*i), 50000); |
jvfausto | 21:3489cffad196 | 310 | } |
JesiMiranda | 35:5a2fed4c2e9f | 311 | |
jvfausto | 21:3489cffad196 | 312 | out->printf("wheelchair setup done \r\n"); // Make sure it initialized; prints in serial monitor |
jvfausto | 21:3489cffad196 | 313 | ti = time; |
JesiMiranda | 35:5a2fed4c2e9f | 314 | for(int i = 0; i < 10; i++) { |
JesiMiranda | 35:5a2fed4c2e9f | 315 | (*(ToF+1))->readFromOneSensor(); |
JesiMiranda | 35:5a2fed4c2e9f | 316 | (*(ToF+1))->readFromOneSensor(); |
t1jain | 31:06f2362caf12 | 317 | } |
JesiMiranda | 35:5a2fed4c2e9f | 318 | for(int i = 0; i < 150; i++) { |
jvfausto | 27:da718b990837 | 319 | ledgeArrayLF[i] = (*(ToF+1))->readFromOneSensor(); |
t1jain | 30:c25b2556e84d | 320 | ledgeArrayRF[i] = (*(ToF+4))->readFromOneSensor(); |
jvfausto | 27:da718b990837 | 321 | } |
JesiMiranda | 35:5a2fed4c2e9f | 322 | |
t1jain | 30:c25b2556e84d | 323 | outlierToF[0] = LFTStats.mean() + 2*LFTStats.stdev(); |
t1jain | 30:c25b2556e84d | 324 | outlierToF[1] = RFTStats.mean() + 2*RFTStats.stdev(); |
t1jain | 34:b89967adc86c | 325 | |
jvfausto | 21:3489cffad196 | 326 | myPID.SetMode(AUTOMATIC); // PID mode: Automatic |
jvfausto | 21:3489cffad196 | 327 | } |
jvfausto | 21:3489cffad196 | 328 | |
JesiMiranda | 35:5a2fed4c2e9f | 329 | /************************************************************************* |
JesiMiranda | 35:5a2fed4c2e9f | 330 | * Move wheelchair with joystick on manual mode * |
JesiMiranda | 35:5a2fed4c2e9f | 331 | **************************************************************************/ |
JesiMiranda | 35:5a2fed4c2e9f | 332 | void Wheelchair::move(float x_coor, float y_coor) |
jvfausto | 21:3489cffad196 | 333 | { |
JesiMiranda | 35:5a2fed4c2e9f | 334 | /* Scales one joystick measurement to the chair's joystick measurement */ |
ryanlin97 | 4:29a27953fe70 | 335 | float scaled_x = ((x_coor * 1.6f) + 1.7f)/3.3f; |
ryanlin97 | 4:29a27953fe70 | 336 | float scaled_y = (3.3f - (y_coor * 1.6f))/3.3f; |
jvfausto | 21:3489cffad196 | 337 | |
JesiMiranda | 35:5a2fed4c2e9f | 338 | /* Sends the scaled joystic values to the chair */ |
JesiMiranda | 35:5a2fed4c2e9f | 339 | x->write(scaled_x); |
ryanlin97 | 4:29a27953fe70 | 340 | y->write(scaled_y); |
ryanlin97 | 5:e0ccaab3959a | 341 | } |
JesiMiranda | 35:5a2fed4c2e9f | 342 | |
JesiMiranda | 35:5a2fed4c2e9f | 343 | |
JesiMiranda | 35:5a2fed4c2e9f | 344 | /************************************************************************* |
JesiMiranda | 35:5a2fed4c2e9f | 345 | * Automatic mode: move forward and update x,y coordinate sent to chair * |
JesiMiranda | 35:5a2fed4c2e9f | 346 | **************************************************************************/ |
JesiMiranda | 35:5a2fed4c2e9f | 347 | void Wheelchair::forward() |
ryanlin97 | 1:c0beadca1617 | 348 | { |
jvfausto | 26:662693bd7f31 | 349 | //printf("current velosity; %f, curr vel S %f\r\n", curr_vel, curr_velS); |
JesiMiranda | 35:5a2fed4c2e9f | 350 | if(forwardSafety == 0) { |
JesiMiranda | 35:5a2fed4c2e9f | 351 | x->write(high); |
JesiMiranda | 35:5a2fed4c2e9f | 352 | y->write(def+offset); |
jvfausto | 21:3489cffad196 | 353 | } |
jvfausto | 26:662693bd7f31 | 354 | out->printf("%f, %f\r\n", curr_pos, wheelS->getDistance(53.975)); |
ryanlin97 | 0:fc0c4a184482 | 355 | } |
JesiMiranda | 35:5a2fed4c2e9f | 356 | |
JesiMiranda | 35:5a2fed4c2e9f | 357 | /************************************************************************* |
JesiMiranda | 35:5a2fed4c2e9f | 358 | * Automatic mode: move in reverse and update x,y coordinate sent to chair* |
JesiMiranda | 35:5a2fed4c2e9f | 359 | **************************************************************************/ |
JesiMiranda | 35:5a2fed4c2e9f | 360 | void Wheelchair::backward() |
ryanlin97 | 1:c0beadca1617 | 361 | { |
t1jain | 39:5438557a8b4e | 362 | if (backwardSafety == 0) { |
t1jain | 39:5438557a8b4e | 363 | x->write(low); |
t1jain | 39:5438557a8b4e | 364 | y->write(def); |
t1jain | 39:5438557a8b4e | 365 | } |
ryanlin97 | 0:fc0c4a184482 | 366 | } |
JesiMiranda | 35:5a2fed4c2e9f | 367 | /************************************************************************* |
JesiMiranda | 35:5a2fed4c2e9f | 368 | * Automatic mode: move right and update x,y coordinate sent to chair * |
JesiMiranda | 35:5a2fed4c2e9f | 369 | **************************************************************************/ |
JesiMiranda | 35:5a2fed4c2e9f | 370 | void Wheelchair::right() |
ryanlin97 | 1:c0beadca1617 | 371 | { |
t1jain | 34:b89967adc86c | 372 | //if safe to move, from ToFSafety |
JesiMiranda | 37:e0e6d3fe06a2 | 373 | if(rightSafety == 0) { |
JesiMiranda | 35:5a2fed4c2e9f | 374 | x->write(def); |
JesiMiranda | 35:5a2fed4c2e9f | 375 | y->write(low); |
t1jain | 34:b89967adc86c | 376 | } |
ryanlin97 | 0:fc0c4a184482 | 377 | } |
JesiMiranda | 35:5a2fed4c2e9f | 378 | /************************************************************************* |
JesiMiranda | 35:5a2fed4c2e9f | 379 | * Automatic mode: move left and update x,y coordinate sent to chair * |
JesiMiranda | 35:5a2fed4c2e9f | 380 | **************************************************************************/ |
JesiMiranda | 35:5a2fed4c2e9f | 381 | void Wheelchair::left() |
ryanlin97 | 1:c0beadca1617 | 382 | { |
t1jain | 34:b89967adc86c | 383 | //if safe to move, from ToFSafety |
JesiMiranda | 37:e0e6d3fe06a2 | 384 | if(leftSafety == 0) { |
t1jain | 34:b89967adc86c | 385 | x->write(def); |
t1jain | 34:b89967adc86c | 386 | y->write(high); |
t1jain | 34:b89967adc86c | 387 | } |
ryanlin97 | 0:fc0c4a184482 | 388 | } |
JesiMiranda | 35:5a2fed4c2e9f | 389 | /************************************************************************* |
JesiMiranda | 35:5a2fed4c2e9f | 390 | * Stop the wheelchair * |
JesiMiranda | 35:5a2fed4c2e9f | 391 | **************************************************************************/ |
JesiMiranda | 35:5a2fed4c2e9f | 392 | void Wheelchair::stop() |
ryanlin97 | 1:c0beadca1617 | 393 | { |
ryanlin97 | 0:fc0c4a184482 | 394 | x->write(def); |
ryanlin97 | 0:fc0c4a184482 | 395 | y->write(def); |
ryanlin97 | 6:0cd57bdd8fbc | 396 | } |
JesiMiranda | 35:5a2fed4c2e9f | 397 | /************************************************************************* |
JesiMiranda | 35:5a2fed4c2e9f | 398 | * Counter-clockwise is ( - ) * |
JesiMiranda | 35:5a2fed4c2e9f | 399 | * Clockwise is ( + ) * |
JesiMiranda | 35:5a2fed4c2e9f | 400 | * Range of deg: 0 to 360 * |
JesiMiranda | 35:5a2fed4c2e9f | 401 | * This method takes in an angle from user and adjusts for turning right * |
JesiMiranda | 35:5a2fed4c2e9f | 402 | **************************************************************************/ |
jvfausto | 21:3489cffad196 | 403 | void Wheelchair::pid_right(int deg) |
ryanlin97 | 12:921488918749 | 404 | { |
t1jain | 33:f3585571f11e | 405 | bool overturn = false; // Boolean if angle over 360˚ |
JesiMiranda | 35:5a2fed4c2e9f | 406 | |
JesiMiranda | 35:5a2fed4c2e9f | 407 | out->printf("pid right\r\r\n"); |
jvfausto | 21:3489cffad196 | 408 | x->write(def); // Update x sent to chair to be stationary |
jvfausto | 21:3489cffad196 | 409 | Setpoint = curr_yaw + deg; // Relative angle we want to turn |
jvfausto | 21:3489cffad196 | 410 | pid_yaw = curr_yaw; // Sets pid_yaw to angle input from user |
JesiMiranda | 35:5a2fed4c2e9f | 411 | |
jvfausto | 21:3489cffad196 | 412 | /* Turns on overturn boolean if setpoint over 360˚ */ |
JesiMiranda | 35:5a2fed4c2e9f | 413 | if(Setpoint > 360) { |
ryanlin97 | 12:921488918749 | 414 | overturn = true; |
ryanlin97 | 12:921488918749 | 415 | } |
JesiMiranda | 35:5a2fed4c2e9f | 416 | |
jvfausto | 21:3489cffad196 | 417 | myPID.SetTunings(5.5,0, 0.0035); // Sets the constants for P and D |
jvfausto | 21:3489cffad196 | 418 | myPID.SetOutputLimits(0, def-low-.15); // Limit is set to the differnce between def and low |
jvfausto | 21:3489cffad196 | 419 | myPID.SetControllerDirection(DIRECT); // PID mode: Direct |
JesiMiranda | 35:5a2fed4c2e9f | 420 | |
JesiMiranda | 35:5a2fed4c2e9f | 421 | /* PID stops when approaching a litte less than desired angle */ |
JesiMiranda | 35:5a2fed4c2e9f | 422 | while(pid_yaw < Setpoint - 3) { |
jvfausto | 21:3489cffad196 | 423 | /* PID is set to correct angle range if angle greater than 360˚*/ |
JesiMiranda | 35:5a2fed4c2e9f | 424 | if(overturn && curr_yaw < Setpoint-deg-1) { |
JesiMiranda | 35:5a2fed4c2e9f | 425 | pid_yaw = curr_yaw + 360; |
JesiMiranda | 35:5a2fed4c2e9f | 426 | } else { |
jvfausto | 17:7f3b69300bb6 | 427 | pid_yaw = curr_yaw; |
ryanlin97 | 12:921488918749 | 428 | } |
JesiMiranda | 35:5a2fed4c2e9f | 429 | |
jvfausto | 21:3489cffad196 | 430 | myPID.Compute(); // Does PID calculations |
jvfausto | 21:3489cffad196 | 431 | double tempor = -Output+def; // Temporary value with the voltage output |
JesiMiranda | 35:5a2fed4c2e9f | 432 | y->write(tempor); // Update y sent to chair |
JesiMiranda | 35:5a2fed4c2e9f | 433 | |
jvfausto | 21:3489cffad196 | 434 | /* Prints to serial monitor the current angle and setpoint */ |
JesiMiranda | 35:5a2fed4c2e9f | 435 | out->printf("curr_yaw %f\r\r\n", curr_yaw); |
jvfausto | 21:3489cffad196 | 436 | out->printf("Setpoint = %f \r\n", Setpoint); |
JesiMiranda | 35:5a2fed4c2e9f | 437 | |
jvfausto | 21:3489cffad196 | 438 | wait(.05); // Small delay (milliseconds) |
ryanlin97 | 12:921488918749 | 439 | } |
JesiMiranda | 35:5a2fed4c2e9f | 440 | |
jvfausto | 21:3489cffad196 | 441 | /* Saftey stop for wheelchair */ |
JesiMiranda | 35:5a2fed4c2e9f | 442 | Wheelchair::stop(); |
jvfausto | 21:3489cffad196 | 443 | out->printf("done \r\n"); |
jvfausto | 21:3489cffad196 | 444 | } |
JesiMiranda | 35:5a2fed4c2e9f | 445 | /************************************************************************* |
JesiMiranda | 35:5a2fed4c2e9f | 446 | * Counter-clockwise is ( - ) * |
JesiMiranda | 35:5a2fed4c2e9f | 447 | * Clockwise is ( + ) * |
JesiMiranda | 35:5a2fed4c2e9f | 448 | * Range of deg: 0 to 360 * |
JesiMiranda | 35:5a2fed4c2e9f | 449 | * This method takes in an angle from user and adjusts for turning left * |
JesiMiranda | 35:5a2fed4c2e9f | 450 | **************************************************************************/ |
JesiMiranda | 35:5a2fed4c2e9f | 451 | void Wheelchair::pid_left(int deg) |
ryanlin97 | 12:921488918749 | 452 | { |
jvfausto | 21:3489cffad196 | 453 | bool overturn = false; //Boolean if angle under 0˚ |
JesiMiranda | 35:5a2fed4c2e9f | 454 | |
JesiMiranda | 35:5a2fed4c2e9f | 455 | out->printf("pid Left\r\r\n"); |
jvfausto | 21:3489cffad196 | 456 | x->write(def); // Update x sent to chair to be stationary |
jvfausto | 21:3489cffad196 | 457 | Setpoint = curr_yaw - deg; // Relative angle we want to turn |
jvfausto | 21:3489cffad196 | 458 | pid_yaw = curr_yaw; // Sets pid_yaw to angle input from user |
JesiMiranda | 35:5a2fed4c2e9f | 459 | |
jvfausto | 21:3489cffad196 | 460 | /* Turns on overturn boolean if setpoint less than 0˚ */ |
JesiMiranda | 35:5a2fed4c2e9f | 461 | if(Setpoint < 0) { |
ryanlin97 | 12:921488918749 | 462 | overturn = true; |
ryanlin97 | 12:921488918749 | 463 | } |
JesiMiranda | 35:5a2fed4c2e9f | 464 | |
jvfausto | 21:3489cffad196 | 465 | myPID.SetTunings(5,0, 0.004); // Sets the constants for P and D |
t1jain | 33:f3585571f11e | 466 | myPID.SetOutputLimits(0,high-def-.12); // Limit is set to the differnce between def and low |
jvfausto | 21:3489cffad196 | 467 | myPID.SetControllerDirection(REVERSE); // PID mode: Reverse |
JesiMiranda | 35:5a2fed4c2e9f | 468 | |
jvfausto | 21:3489cffad196 | 469 | /* PID stops when approaching a litte more than desired angle */ |
JesiMiranda | 35:5a2fed4c2e9f | 470 | while(pid_yaw > Setpoint+3) { |
JesiMiranda | 35:5a2fed4c2e9f | 471 | /* PID is set to correct angle range if angle less than 0˚ */ |
JesiMiranda | 35:5a2fed4c2e9f | 472 | if(overturn && curr_yaw > Setpoint+deg+1) { |
JesiMiranda | 35:5a2fed4c2e9f | 473 | pid_yaw = curr_yaw - 360; |
JesiMiranda | 35:5a2fed4c2e9f | 474 | } else { |
JesiMiranda | 35:5a2fed4c2e9f | 475 | pid_yaw = curr_yaw; |
JesiMiranda | 35:5a2fed4c2e9f | 476 | } |
JesiMiranda | 35:5a2fed4c2e9f | 477 | |
jvfausto | 21:3489cffad196 | 478 | myPID.Compute(); // Does PID calculations |
jvfausto | 21:3489cffad196 | 479 | double tempor = Output+def; // Temporary value with the voltage output |
jvfausto | 21:3489cffad196 | 480 | y->write(tempor); // Update y sent to chair |
JesiMiranda | 35:5a2fed4c2e9f | 481 | |
jvfausto | 21:3489cffad196 | 482 | /* Prints to serial monitor the current angle and setpoint */ |
jvfausto | 17:7f3b69300bb6 | 483 | out->printf("curr_yaw %f\r\n", curr_yaw); |
jvfausto | 21:3489cffad196 | 484 | out->printf("Setpoint = %f \r\n", Setpoint); |
JesiMiranda | 35:5a2fed4c2e9f | 485 | |
jvfausto | 21:3489cffad196 | 486 | wait(.05); // Small delay (milliseconds) |
ryanlin97 | 12:921488918749 | 487 | } |
JesiMiranda | 35:5a2fed4c2e9f | 488 | |
JesiMiranda | 35:5a2fed4c2e9f | 489 | /* Saftey stop for wheelchair */ |
JesiMiranda | 35:5a2fed4c2e9f | 490 | Wheelchair::stop(); |
jvfausto | 21:3489cffad196 | 491 | out->printf("done \r\n"); |
ryanlin97 | 12:921488918749 | 492 | |
jvfausto | 21:3489cffad196 | 493 | } |
JesiMiranda | 35:5a2fed4c2e9f | 494 | |
JesiMiranda | 35:5a2fed4c2e9f | 495 | /************************************************************************* |
JesiMiranda | 35:5a2fed4c2e9f | 496 | * This method determines whether to turn left or right * |
JesiMiranda | 35:5a2fed4c2e9f | 497 | **************************************************************************/ |
JesiMiranda | 35:5a2fed4c2e9f | 498 | void Wheelchair::pid_turn(int deg) |
JesiMiranda | 35:5a2fed4c2e9f | 499 | { |
JesiMiranda | 35:5a2fed4c2e9f | 500 | |
JesiMiranda | 35:5a2fed4c2e9f | 501 | /***************************************************************** |
JesiMiranda | 35:5a2fed4c2e9f | 502 | * Sets angle to coterminal angle for left turn if deg > 180 * |
JesiMiranda | 35:5a2fed4c2e9f | 503 | * Sets angle to coterminal angle for right turn if deg < -180 * |
JesiMiranda | 35:5a2fed4c2e9f | 504 | *****************************************************************/ |
JesiMiranda | 35:5a2fed4c2e9f | 505 | if(deg > 180) { |
ryanlin97 | 12:921488918749 | 506 | deg -= 360; |
JesiMiranda | 35:5a2fed4c2e9f | 507 | } else if(deg < -180) { |
JesiMiranda | 35:5a2fed4c2e9f | 508 | deg +=360; |
ryanlin97 | 12:921488918749 | 509 | } |
JesiMiranda | 35:5a2fed4c2e9f | 510 | |
jvfausto | 21:3489cffad196 | 511 | /* Makes sure angle inputted to function is positive */ |
ryanlin97 | 12:921488918749 | 512 | int turnAmt = abs(deg); |
JesiMiranda | 35:5a2fed4c2e9f | 513 | |
jvfausto | 21:3489cffad196 | 514 | /* Calls PID_right if deg > 0, else calls PID_left if deg < 0 */ |
JesiMiranda | 35:5a2fed4c2e9f | 515 | if(deg >= 0) { |
JesiMiranda | 35:5a2fed4c2e9f | 516 | Wheelchair::pid_right(turnAmt); |
JesiMiranda | 35:5a2fed4c2e9f | 517 | } else { |
JesiMiranda | 35:5a2fed4c2e9f | 518 | Wheelchair::pid_left(turnAmt); |
jvfausto | 21:3489cffad196 | 519 | } |
ryanlin97 | 12:921488918749 | 520 | |
jvfausto | 21:3489cffad196 | 521 | } |
jvfausto | 21:3489cffad196 | 522 | |
JesiMiranda | 35:5a2fed4c2e9f | 523 | /************************************************************************* |
JesiMiranda | 35:5a2fed4c2e9f | 524 | * This method takes in distance to travel and adjust to move forward * |
JesiMiranda | 35:5a2fed4c2e9f | 525 | **************************************************************************/ |
jvfausto | 19:71a6621ee5c3 | 526 | void Wheelchair::pid_forward(double mm) |
jvfausto | 17:7f3b69300bb6 | 527 | { |
jvfausto | 21:3489cffad196 | 528 | mm -= 20; // Makes sure distance does not overshoot |
jvfausto | 21:3489cffad196 | 529 | Input = 0; // Initializes input to zero: Test latter w/o |
jvfausto | 21:3489cffad196 | 530 | wheel->reset(); // Resets encoders so that they start at 0 |
JesiMiranda | 35:5a2fed4c2e9f | 531 | |
jvfausto | 17:7f3b69300bb6 | 532 | out->printf("pid foward\r\n"); |
JesiMiranda | 35:5a2fed4c2e9f | 533 | |
jvfausto | 21:3489cffad196 | 534 | double tempor; // Initializes Temporary variable for x input |
jvfausto | 21:3489cffad196 | 535 | Setpoint = mm; // Initializes the setpoint to desired value |
JesiMiranda | 35:5a2fed4c2e9f | 536 | |
jvfausto | 21:3489cffad196 | 537 | myPIDDistance.SetTunings(5.5,0, 0.0015); // Sets constants for P and D |
JesiMiranda | 35:5a2fed4c2e9f | 538 | myPIDDistance.SetOutputLimits(0,high-def-.15); // Limit set to difference between high and def |
jvfausto | 21:3489cffad196 | 539 | myPIDDistance.SetControllerDirection(DIRECT); // PID mode: Direct |
JesiMiranda | 35:5a2fed4c2e9f | 540 | |
jvfausto | 21:3489cffad196 | 541 | y->write(def+offset); // Update y to make chair stationary |
JesiMiranda | 35:5a2fed4c2e9f | 542 | |
jvfausto | 21:3489cffad196 | 543 | /* Chair stops moving when Setpoint is reached */ |
JesiMiranda | 35:5a2fed4c2e9f | 544 | while(Input < Setpoint) { |
JesiMiranda | 35:5a2fed4c2e9f | 545 | |
JesiMiranda | 35:5a2fed4c2e9f | 546 | if(out->readable()) { // Emergency Break |
jvfausto | 21:3489cffad196 | 547 | break; |
jvfausto | 21:3489cffad196 | 548 | } |
jvfausto | 17:7f3b69300bb6 | 549 | |
jvfausto | 21:3489cffad196 | 550 | Input = wheel->getDistance(53.975); // Gets distance from Encoder into PID |
jvfausto | 21:3489cffad196 | 551 | wait(.05); // Slight Delay: *****Test without |
jvfausto | 21:3489cffad196 | 552 | myPIDDistance.Compute(); // Compute distance traveled by chair |
JesiMiranda | 35:5a2fed4c2e9f | 553 | |
JesiMiranda | 35:5a2fed4c2e9f | 554 | tempor = Output + def; // Temporary output variable |
jvfausto | 21:3489cffad196 | 555 | x->write(tempor); // Update x sent to chair |
jvfausto | 17:7f3b69300bb6 | 556 | |
jvfausto | 21:3489cffad196 | 557 | /* Prints to serial monitor the distance traveled by chair */ |
jvfausto | 19:71a6621ee5c3 | 558 | out->printf("distance %f\r\n", Input); |
JesiMiranda | 35:5a2fed4c2e9f | 559 | } |
JesiMiranda | 35:5a2fed4c2e9f | 560 | |
JesiMiranda | 35:5a2fed4c2e9f | 561 | } |
jvfausto | 21:3489cffad196 | 562 | |
JesiMiranda | 35:5a2fed4c2e9f | 563 | /************************************************************************* |
JesiMiranda | 35:5a2fed4c2e9f | 564 | * This method returns the relative angular position of chair * |
JesiMiranda | 35:5a2fed4c2e9f | 565 | **************************************************************************/ |
jvfausto | 21:3489cffad196 | 566 | double Wheelchair::getTwistZ() |
jvfausto | 17:7f3b69300bb6 | 567 | { |
jvfausto | 21:3489cffad196 | 568 | return imu->gyro_z(); |
JesiMiranda | 35:5a2fed4c2e9f | 569 | } |
jvfausto | 18:663b6d693252 | 570 | |
JesiMiranda | 35:5a2fed4c2e9f | 571 | /************************************************************************* |
JesiMiranda | 35:5a2fed4c2e9f | 572 | * This method computes the relative angle for Twist message in ROS * |
JesiMiranda | 35:5a2fed4c2e9f | 573 | **************************************************************************/ |
jvfausto | 21:3489cffad196 | 574 | void Wheelchair::pid_twistA() |
jvfausto | 21:3489cffad196 | 575 | { |
jvfausto | 21:3489cffad196 | 576 | /* Initialize variables for angle and update x,y sent to chair */ |
jvfausto | 21:3489cffad196 | 577 | char c; |
jvfausto | 21:3489cffad196 | 578 | double temporA = def; |
JesiMiranda | 35:5a2fed4c2e9f | 579 | y->write(def); |
JesiMiranda | 35:5a2fed4c2e9f | 580 | x->write(def); |
JesiMiranda | 35:5a2fed4c2e9f | 581 | |
jvfausto | 21:3489cffad196 | 582 | PIDAngularV.SetTunings(.00015,0, 0.00); // Sets the constants for P and D |
jvfausto | 21:3489cffad196 | 583 | PIDAngularV.SetOutputLimits(-.1, .1); // Limit set to be in range specified |
jvfausto | 21:3489cffad196 | 584 | PIDAngularV.SetControllerDirection(DIRECT); // PID mode: Direct |
JesiMiranda | 35:5a2fed4c2e9f | 585 | |
jvfausto | 21:3489cffad196 | 586 | /* Computes angular position of wheelchair while turning */ |
JesiMiranda | 35:5a2fed4c2e9f | 587 | while(1) { |
jvfausto | 21:3489cffad196 | 588 | yDesired = angularV; |
JesiMiranda | 35:5a2fed4c2e9f | 589 | |
jvfausto | 21:3489cffad196 | 590 | /* Update and set all variable so that the chair is stationary |
jvfausto | 21:3489cffad196 | 591 | * if the desired angle is zero |
jvfausto | 21:3489cffad196 | 592 | */ |
JesiMiranda | 35:5a2fed4c2e9f | 593 | if(yDesired == 0) { |
jvfausto | 21:3489cffad196 | 594 | x->write(def); |
jvfausto | 21:3489cffad196 | 595 | y->write(def); |
jvfausto | 21:3489cffad196 | 596 | yDesired = 0; |
ryanlin97 | 8:381a4ec3fef8 | 597 | return; |
ryanlin97 | 7:5e38d43fbce3 | 598 | } |
JesiMiranda | 35:5a2fed4c2e9f | 599 | |
jvfausto | 21:3489cffad196 | 600 | /* Continuously updates with current angle measured by IMU */ |
JesiMiranda | 35:5a2fed4c2e9f | 601 | yIn = imu->gyro_z(); |
jvfausto | 21:3489cffad196 | 602 | PIDAngularV.Compute(); |
jvfausto | 21:3489cffad196 | 603 | temporA += yOut; // Temporary value with the voltage output |
jvfausto | 21:3489cffad196 | 604 | y->write(temporA); // Update y sent to chair |
JesiMiranda | 35:5a2fed4c2e9f | 605 | |
jvfausto | 21:3489cffad196 | 606 | //out->printf("temporA: %f, yDesired %f, angle: %f\r\n", temporA, yDesired, imu->gyro_z()); |
jvfausto | 21:3489cffad196 | 607 | wait(.05); // Small delay (milliseconds) |
ryanlin97 | 6:0cd57bdd8fbc | 608 | } |
JesiMiranda | 35:5a2fed4c2e9f | 609 | |
JesiMiranda | 35:5a2fed4c2e9f | 610 | } |
ryanlin97 | 6:0cd57bdd8fbc | 611 | |
JesiMiranda | 35:5a2fed4c2e9f | 612 | /************************************************************************* |
JesiMiranda | 35:5a2fed4c2e9f | 613 | * This method computes the relative velocity for Twist message in ROS * |
JesiMiranda | 35:5a2fed4c2e9f | 614 | **************************************************************************/ |
jvfausto | 21:3489cffad196 | 615 | void Wheelchair::pid_twistV() |
ryanlin97 | 6:0cd57bdd8fbc | 616 | { |
jvfausto | 21:3489cffad196 | 617 | /* Initializes variables as default */ |
jvfausto | 21:3489cffad196 | 618 | double temporV = def; |
jvfausto | 26:662693bd7f31 | 619 | double temporS = def+offset; |
jvfausto | 21:3489cffad196 | 620 | vDesiredS = 0; |
jvfausto | 21:3489cffad196 | 621 | x->write(def); |
jvfausto | 21:3489cffad196 | 622 | y->write(def); |
jvfausto | 21:3489cffad196 | 623 | wheel->reset(); // Resets the encoders |
jvfausto | 21:3489cffad196 | 624 | /* Sets the constants for P and D */ |
JesiMiranda | 35:5a2fed4c2e9f | 625 | PIDVelosity.SetTunings(.0005,0, 0.00); |
JesiMiranda | 35:5a2fed4c2e9f | 626 | PIDSlaveV.SetTunings(.005,0.000001, 0.000001); |
JesiMiranda | 35:5a2fed4c2e9f | 627 | |
jvfausto | 21:3489cffad196 | 628 | /* Limits to the range specified */ |
JesiMiranda | 35:5a2fed4c2e9f | 629 | PIDVelosity.SetOutputLimits(-.005, .005); |
JesiMiranda | 35:5a2fed4c2e9f | 630 | PIDSlaveV.SetOutputLimits(-.002, .002); |
JesiMiranda | 35:5a2fed4c2e9f | 631 | |
jvfausto | 21:3489cffad196 | 632 | /* PID mode: Direct */ |
JesiMiranda | 35:5a2fed4c2e9f | 633 | PIDVelosity.SetControllerDirection(DIRECT); |
JesiMiranda | 35:5a2fed4c2e9f | 634 | PIDSlaveV.SetControllerDirection(DIRECT); |
JesiMiranda | 35:5a2fed4c2e9f | 635 | |
JesiMiranda | 35:5a2fed4c2e9f | 636 | while(1) { |
jvfausto | 21:3489cffad196 | 637 | linearV = .7; |
jvfausto | 21:3489cffad196 | 638 | test1 = linearV*100; |
jvfausto | 21:3489cffad196 | 639 | vel = curr_vel; |
jvfausto | 21:3489cffad196 | 640 | vDesired = linearV*100; |
jvfausto | 21:3489cffad196 | 641 | if(out->readable()) |
jvfausto | 21:3489cffad196 | 642 | return; |
JesiMiranda | 35:5a2fed4c2e9f | 643 | /* Update and set all variable so that the chair is stationary |
JesiMiranda | 35:5a2fed4c2e9f | 644 | * if the velocity is zero |
JesiMiranda | 35:5a2fed4c2e9f | 645 | */ |
JesiMiranda | 35:5a2fed4c2e9f | 646 | if(linearV == 0) { |
jvfausto | 21:3489cffad196 | 647 | x->write(def); |
jvfausto | 21:3489cffad196 | 648 | y->write(def); |
ryanlin97 | 8:381a4ec3fef8 | 649 | |
jvfausto | 21:3489cffad196 | 650 | vel = 0; |
jvfausto | 21:3489cffad196 | 651 | vDesired = 0; |
jvfausto | 21:3489cffad196 | 652 | dist_old = 0; |
ryanlin97 | 8:381a4ec3fef8 | 653 | return; |
ryanlin97 | 8:381a4ec3fef8 | 654 | } |
JesiMiranda | 35:5a2fed4c2e9f | 655 | |
JesiMiranda | 35:5a2fed4c2e9f | 656 | if(vDesired >= 0) { |
jvfausto | 21:3489cffad196 | 657 | PIDVelosity.SetTunings(.000004,0, 0.00); // Sets the constants for P and D |
jvfausto | 21:3489cffad196 | 658 | PIDVelosity.SetOutputLimits(-.002, .002); // Limits to the range specified |
JesiMiranda | 35:5a2fed4c2e9f | 659 | } else { |
jvfausto | 21:3489cffad196 | 660 | PIDVelosity.SetTunings(.000015,0, 0.00); // Sets the constants for P and D |
jvfausto | 21:3489cffad196 | 661 | PIDVelosity.SetOutputLimits(-.0005, .0005); // Limits to range specified |
JesiMiranda | 35:5a2fed4c2e9f | 662 | } |
JesiMiranda | 35:5a2fed4c2e9f | 663 | |
jvfausto | 21:3489cffad196 | 664 | /* Sets maximum value of variable to 1 */ |
JesiMiranda | 35:5a2fed4c2e9f | 665 | if(temporV >= 1.5) { |
jvfausto | 21:3489cffad196 | 666 | temporV = 1.5; |
ryanlin97 | 8:381a4ec3fef8 | 667 | } |
jvfausto | 21:3489cffad196 | 668 | /* Scales and makes some adjustments to velocity */ |
jvfausto | 21:3489cffad196 | 669 | vIn = curr_vel*100; |
jvfausto | 21:3489cffad196 | 670 | vInS = curr_vel-curr_velS; |
jvfausto | 21:3489cffad196 | 671 | PIDVelosity.Compute(); |
jvfausto | 21:3489cffad196 | 672 | PIDSlaveV.Compute(); |
JesiMiranda | 35:5a2fed4c2e9f | 673 | if(forwardSafety == 0) { |
JesiMiranda | 35:5a2fed4c2e9f | 674 | temporV += vOut; |
JesiMiranda | 35:5a2fed4c2e9f | 675 | temporS += vOutS; |
JesiMiranda | 35:5a2fed4c2e9f | 676 | |
JesiMiranda | 35:5a2fed4c2e9f | 677 | /* Updates x,y sent to Wheelchair and for Odometry message in ROS */ |
JesiMiranda | 35:5a2fed4c2e9f | 678 | x->write(temporV); |
JesiMiranda | 35:5a2fed4c2e9f | 679 | test2 = temporV; |
JesiMiranda | 35:5a2fed4c2e9f | 680 | y->write(temporS); |
JesiMiranda | 35:5a2fed4c2e9f | 681 | } else { |
jvfausto | 21:3489cffad196 | 682 | x->write(def); |
jvfausto | 21:3489cffad196 | 683 | y->write(def); |
jvfausto | 21:3489cffad196 | 684 | } |
jvfausto | 21:3489cffad196 | 685 | //out->printf("Velosity: %f, Velosity2: %f, temporV %f, temporS %f\r\n", curr_vel, curr_velS, temporV, temporS); |
jvfausto | 21:3489cffad196 | 686 | Wheelchair::odomMsg(); |
jvfausto | 21:3489cffad196 | 687 | wait(.01); // Small delay (milliseconds) |
ryanlin97 | 6:0cd57bdd8fbc | 688 | } |
ryanlin97 | 11:d14a1f7f1297 | 689 | } |
ryanlin97 | 11:d14a1f7f1297 | 690 | |
JesiMiranda | 35:5a2fed4c2e9f | 691 | /************************************************************************* |
JesiMiranda | 35:5a2fed4c2e9f | 692 | * This method calculates the relative position of the chair everytime the* |
JesiMiranda | 35:5a2fed4c2e9f | 693 | * encoders reset by setting its old position as the origin to calculate * |
JesiMiranda | 35:5a2fed4c2e9f | 694 | * the new position * |
JesiMiranda | 35:5a2fed4c2e9f | 695 | **************************************************************************/ |
jvfausto | 21:3489cffad196 | 696 | void Wheelchair::odomMsg() |
ryanlin97 | 11:d14a1f7f1297 | 697 | { |
jvfausto | 21:3489cffad196 | 698 | double dist_new = curr_pos; |
jvfausto | 21:3489cffad196 | 699 | double dist = dist_new-dist_old; |
jvfausto | 21:3489cffad196 | 700 | double temp_x = dist*sin(z_angular*3.14159/180); |
jvfausto | 21:3489cffad196 | 701 | double temp_y = dist*cos(z_angular*3.14159/180); |
JesiMiranda | 35:5a2fed4c2e9f | 702 | |
jvfausto | 21:3489cffad196 | 703 | x_position += temp_x; |
jvfausto | 21:3489cffad196 | 704 | y_position += temp_y; |
JesiMiranda | 35:5a2fed4c2e9f | 705 | |
jvfausto | 21:3489cffad196 | 706 | dist_old = dist_new; |
JesiMiranda | 35:5a2fed4c2e9f | 707 | } |
jvfausto | 21:3489cffad196 | 708 | |
JesiMiranda | 35:5a2fed4c2e9f | 709 | /************************************************************************** |
JesiMiranda | 35:5a2fed4c2e9f | 710 | * This method prints the Odometry message to the serial monitor * |
JesiMiranda | 35:5a2fed4c2e9f | 711 | ***************************************************************************/ |
JesiMiranda | 35:5a2fed4c2e9f | 712 | void Wheelchair::showOdom() |
JesiMiranda | 35:5a2fed4c2e9f | 713 | { |
JesiMiranda | 35:5a2fed4c2e9f | 714 | out->printf("x %f, y %f, angle %f", x_position, y_position, z_angular); |
JesiMiranda | 35:5a2fed4c2e9f | 715 | } |
jvfausto | 21:3489cffad196 | 716 | |
JesiMiranda | 35:5a2fed4c2e9f | 717 | /************************************************************************** |
JesiMiranda | 35:5a2fed4c2e9f | 718 | * This method returns the approximate distance based on the wheel diameter* |
JesiMiranda | 35:5a2fed4c2e9f | 719 | ***************************************************************************/ |
jvfausto | 21:3489cffad196 | 720 | float Wheelchair::getDistance() |
jvfausto | 21:3489cffad196 | 721 | { |
jvfausto | 21:3489cffad196 | 722 | return wheel->getDistance(Diameter); |
ryanlin97 | 6:0cd57bdd8fbc | 723 | } |
ryanlin97 | 8:381a4ec3fef8 | 724 | |
JesiMiranda | 35:5a2fed4c2e9f | 725 | /************************************************************************** |
JesiMiranda | 35:5a2fed4c2e9f | 726 | * This method resets the Encoder's * |
JesiMiranda | 35:5a2fed4c2e9f | 727 | ***************************************************************************/ |
jvfausto | 21:3489cffad196 | 728 | void Wheelchair::resetDistance() |
jvfausto | 21:3489cffad196 | 729 | { |
ryanlin97 | 12:921488918749 | 730 | wheel->reset(); |
jvfausto | 21:3489cffad196 | 731 | } |
jvfausto | 21:3489cffad196 | 732 | |
JesiMiranda | 35:5a2fed4c2e9f | 733 | /*---------------------------------------------------------------------------------------------------*/ |
JesiMiranda | 35:5a2fed4c2e9f | 734 | /*---------------------------------------------------------------------------------------------------*/ |
JesiMiranda | 35:5a2fed4c2e9f | 735 | /*---------------------------------------------------------------------------------------------------*/ |
JesiMiranda | 35:5a2fed4c2e9f | 736 | /*---------------------------------------------------------------------------------------------------*/ |
JesiMiranda | 35:5a2fed4c2e9f | 737 | /*---------------------------------------------------------------------------------------------------*/ |
jvfausto | 21:3489cffad196 | 738 | |
JesiMiranda | 35:5a2fed4c2e9f | 739 | /*Predetermined paths For Demmo*/ |
JesiMiranda | 35:5a2fed4c2e9f | 740 | void Wheelchair::desk() |
jvfausto | 21:3489cffad196 | 741 | { |
jvfausto | 19:71a6621ee5c3 | 742 | Wheelchair::pid_forward(5461); |
jvfausto | 19:71a6621ee5c3 | 743 | Wheelchair::pid_right(87); |
jvfausto | 19:71a6621ee5c3 | 744 | Wheelchair::pid_forward(3658); |
jvfausto | 19:71a6621ee5c3 | 745 | Wheelchair::pid_right(87); |
jvfausto | 19:71a6621ee5c3 | 746 | Wheelchair::pid_forward(3658); |
jvfausto | 21:3489cffad196 | 747 | } |
JesiMiranda | 35:5a2fed4c2e9f | 748 | |
JesiMiranda | 35:5a2fed4c2e9f | 749 | void Wheelchair::kitchen() |
jvfausto | 21:3489cffad196 | 750 | { |
jvfausto | 19:71a6621ee5c3 | 751 | Wheelchair::pid_forward(5461); |
jvfausto | 20:f42db4ae16f0 | 752 | Wheelchair::pid_right(87); |
jvfausto | 19:71a6621ee5c3 | 753 | Wheelchair::pid_forward(3658); |
jvfausto | 19:71a6621ee5c3 | 754 | Wheelchair::pid_left(90); |
jvfausto | 19:71a6621ee5c3 | 755 | Wheelchair::pid_forward(305); |
jvfausto | 21:3489cffad196 | 756 | } |
JesiMiranda | 35:5a2fed4c2e9f | 757 | |
jvfausto | 21:3489cffad196 | 758 | void Wheelchair::desk_to_kitchen() |
jvfausto | 21:3489cffad196 | 759 | { |
jvfausto | 19:71a6621ee5c3 | 760 | Wheelchair::pid_right(180); |
jvfausto | 19:71a6621ee5c3 | 761 | Wheelchair::pid_forward(3700); |
jvfausto | 21:3489cffad196 | 762 | } |