Updated with emergency button and watchdog code
Dependencies: QEI2 chair_BNO055 PID VL53L1X_Filter
Dependents: wheelchairControlSumer2019
wheelchair.cpp@30:a6659c5a8109, 2019-06-28 (annotated)
- Committer:
- t1jain
- Date:
- Fri Jun 28 17:00:27 2019 +0000
- Revision:
- 30:a6659c5a8109
- Parent:
- 29:052247c0f0e0
Cleaned up code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jvfausto | 21:3489cffad196 | 1 | |
ryanlin97 | 0:fc0c4a184482 | 2 | #include "wheelchair.h" |
jvfausto | 21:3489cffad196 | 3 | |
jvfausto | 21:3489cffad196 | 4 | bool manual_drive = false; // Variable changes between joystick and auto drive |
jvfausto | 21:3489cffad196 | 5 | double encoder_distance; // Keeps distanse due to original position |
jvfausto | 21:3489cffad196 | 6 | |
jvfausto | 21:3489cffad196 | 7 | volatile double Setpoint, Output, Input, Input2; // Variables for PID |
jvfausto | 21:3489cffad196 | 8 | volatile double pid_yaw, Distance, Setpoint2, Output2, encoder_distance2; // Variables for PID |
jvfausto | 21:3489cffad196 | 9 | volatile double vIn, vOut, vDesired; // Variables for PID Velosity |
jvfausto | 21:3489cffad196 | 10 | volatile double vInS, vOutS, vDesiredS; // Variables for PID Slave Wheel |
jvfausto | 21:3489cffad196 | 11 | volatile double yIn, yOut, yDesired; // Variables for PID turn velosity |
ryanlin97 | 11:d14a1f7f1297 | 12 | |
jvfausto | 21:3489cffad196 | 13 | double dist_old, curr_pos; // Variables for odometry position |
jvfausto | 19:71a6621ee5c3 | 14 | |
t1jain | 29:052247c0f0e0 | 15 | //DigitalOut signal(D5); // Output to send "STOP" signal to wheelchair |
t1jain | 28:6b2610acca97 | 16 | DigitalIn button(PD_0, PullUp); // Emergency Stop button |
jvfausto | 21:3489cffad196 | 17 | |
jvfausto | 21:3489cffad196 | 18 | PID myPID(&pid_yaw, &Output, &Setpoint, 5.5, .00, 0.0036, P_ON_E, DIRECT); // Angle PID object constructor |
jvfausto | 21:3489cffad196 | 19 | PID myPIDDistance(&Input, &Output, &Setpoint, 5.5, .00, 0.002, P_ON_E, DIRECT); // Distance PID object constructor |
jvfausto | 21:3489cffad196 | 20 | PID PIDVelosity(&vIn, &vOut, &vDesired, 5.5, .00, .002, P_ON_E, DIRECT); // Velosity PID Constructor |
jvfausto | 21:3489cffad196 | 21 | PID PIDSlaveV(&vInS, &vOutS, &vDesiredS, 5.5, .00, .002, P_ON_E, DIRECT); // Slave Velosity PID Constructor |
jvfausto | 21:3489cffad196 | 22 | PID PIDAngularV(&yIn, &yOut, &yDesired, 5.5, .00, .002, P_ON_E, DIRECT); // Angular Velosity PID Constructor |
jvfausto | 21:3489cffad196 | 23 | |
jvfausto | 19:71a6621ee5c3 | 24 | |
jvfausto | 21:3489cffad196 | 25 | /* Thread measures current angular position */ |
jvfausto | 21:3489cffad196 | 26 | void Wheelchair::compass_thread() |
jvfausto | 21:3489cffad196 | 27 | { |
ryanlin97 | 11:d14a1f7f1297 | 28 | curr_yaw = imu->yaw(); |
jvfausto | 21:3489cffad196 | 29 | z_angular = curr_yaw; |
jvfausto | 21:3489cffad196 | 30 | } |
jvfausto | 17:7f3b69300bb6 | 31 | |
jvfausto | 21:3489cffad196 | 32 | /* Thread measures velocity of wheels and distance traveled */ |
jvfausto | 21:3489cffad196 | 33 | void Wheelchair::velocity_thread() |
ryanlin97 | 1:c0beadca1617 | 34 | { |
jvfausto | 21:3489cffad196 | 35 | curr_vel = wheel->getVelocity(); |
jvfausto | 21:3489cffad196 | 36 | curr_velS = wheelS->getVelocity(); |
jvfausto | 21:3489cffad196 | 37 | curr_pos = wheel->getDistance(53.975); |
ryanlin97 | 1:c0beadca1617 | 38 | } |
ryanlin97 | 6:0cd57bdd8fbc | 39 | |
t1jain | 27:0b1a837f123c | 40 | void Wheelchair::emergencyButton_thread () { |
t1jain | 27:0b1a837f123c | 41 | while(1) { |
t1jain | 28:6b2610acca97 | 42 | //signal = 0; |
t1jain | 27:0b1a837f123c | 43 | while(!button) { |
t1jain | 27:0b1a837f123c | 44 | |
t1jain | 27:0b1a837f123c | 45 | //Send something to stop wheelchair |
t1jain | 28:6b2610acca97 | 46 | //signal = 1; |
t1jain | 27:0b1a837f123c | 47 | printf("I'm dead\n\n\n"); |
t1jain | 27:0b1a837f123c | 48 | |
t1jain | 27:0b1a837f123c | 49 | //Reset Board |
t1jain | 27:0b1a837f123c | 50 | NVIC_SystemReset(); |
t1jain | 27:0b1a837f123c | 51 | |
t1jain | 27:0b1a837f123c | 52 | } |
t1jain | 27:0b1a837f123c | 53 | |
t1jain | 27:0b1a837f123c | 54 | } |
t1jain | 27:0b1a837f123c | 55 | } |
t1jain | 27:0b1a837f123c | 56 | |
jvfausto | 21:3489cffad196 | 57 | void Wheelchair::assistSafe_thread() |
ryanlin97 | 1:c0beadca1617 | 58 | { |
jvfausto | 21:3489cffad196 | 59 | int ToFV[12]; |
t1jain | 30:a6659c5a8109 | 60 | for(int i = 0; i < 6; i++) // Reads from the ToF Sensors |
jvfausto | 21:3489cffad196 | 61 | { |
jvfausto | 21:3489cffad196 | 62 | ToFV[i] = (*(ToF+i))->readFromOneSensor(); |
jvfausto | 21:3489cffad196 | 63 | //out->printf("%d ", ToFV[i]); |
jvfausto | 21:3489cffad196 | 64 | } |
jvfausto | 26:662693bd7f31 | 65 | //out->printf("\r\n"); |
jvfausto | 26:662693bd7f31 | 66 | int sensor1 = ToFV[1]; |
jvfausto | 21:3489cffad196 | 67 | int sensor4 = ToFV[5]; |
t1jain | 30:a6659c5a8109 | 68 | |
jvfausto | 26:662693bd7f31 | 69 | //out->printf("%d, %d\r\n", sensor1, sensor4); |
jvfausto | 26:662693bd7f31 | 70 | /*if(curr_vel < 1 &&((2 * maxDecelerationSlow*sensor1 < curr_vel*curr_vel*1000*1000 || |
jvfausto | 26:662693bd7f31 | 71 | 2 * maxDecelerationSlow*sensor4 < curr_vel*curr_vel*1000*1000) && |
jvfausto | 26:662693bd7f31 | 72 | (sensor1 < 1500 || sensor4 < 1500)) || |
jvfausto | 26:662693bd7f31 | 73 | 550 > sensor1 || 550 > sensor4) |
jvfausto | 26:662693bd7f31 | 74 | { |
jvfausto | 26:662693bd7f31 | 75 | //out->printf("i am in danger\r\n"); |
jvfausto | 26:662693bd7f31 | 76 | if(x->read() > def) |
jvfausto | 26:662693bd7f31 | 77 | { |
jvfausto | 26:662693bd7f31 | 78 | x->write(def); |
jvfausto | 26:662693bd7f31 | 79 | forwardSafety = 1; |
jvfausto | 26:662693bd7f31 | 80 | } |
jvfausto | 26:662693bd7f31 | 81 | } |
jvfausto | 26:662693bd7f31 | 82 | else if(curr_vel > 1 &&((2 * maxDecelerationFast*sensor1 < curr_vel*curr_vel*1000*1000 || |
jvfausto | 26:662693bd7f31 | 83 | 2 * maxDecelerationFast*sensor4 < curr_vel*curr_vel*1000*1000) && |
jvfausto | 21:3489cffad196 | 84 | (sensor1 < 1500 || sensor4 < 1500)) || |
jvfausto | 21:3489cffad196 | 85 | 550 > sensor1 || 550 > sensor4) |
jvfausto | 21:3489cffad196 | 86 | { |
jvfausto | 21:3489cffad196 | 87 | //out->printf("i am in danger\r\n"); |
jvfausto | 21:3489cffad196 | 88 | if(x->read() > def) |
jvfausto | 21:3489cffad196 | 89 | { |
jvfausto | 21:3489cffad196 | 90 | x->write(def); |
jvfausto | 21:3489cffad196 | 91 | forwardSafety = 1; |
jvfausto | 21:3489cffad196 | 92 | } |
jvfausto | 21:3489cffad196 | 93 | } |
jvfausto | 21:3489cffad196 | 94 | else |
jvfausto | 21:3489cffad196 | 95 | forwardSafety = 0; |
jvfausto | 26:662693bd7f31 | 96 | } |
jvfausto | 26:662693bd7f31 | 97 | */ |
jvfausto | 21:3489cffad196 | 98 | } |
ryanlin97 | 6:0cd57bdd8fbc | 99 | |
jvfausto | 21:3489cffad196 | 100 | /* Constructor for Wheelchair class */ |
jvfausto | 21:3489cffad196 | 101 | Wheelchair::Wheelchair(PinName xPin, PinName yPin, Serial* pc, Timer* time, QEI* qei, QEI* qeiS, |
jvfausto | 21:3489cffad196 | 102 | VL53L1X** ToFT) |
jvfausto | 21:3489cffad196 | 103 | { |
jvfausto | 21:3489cffad196 | 104 | x_position = 0; |
jvfausto | 21:3489cffad196 | 105 | y_position = 0; |
jvfausto | 21:3489cffad196 | 106 | forwardSafety = 0; |
jvfausto | 21:3489cffad196 | 107 | /* Initializes X and Y variables to Pins */ |
jvfausto | 21:3489cffad196 | 108 | x = new PwmOut(xPin); |
jvfausto | 21:3489cffad196 | 109 | y = new PwmOut(yPin); |
jvfausto | 21:3489cffad196 | 110 | /* Initializes IMU Library */ |
jvfausto | 26:662693bd7f31 | 111 | out = pc; // "out" is called for serial monitor |
jvfausto | 26:662693bd7f31 | 112 | out->printf("on\r\n"); |
jvfausto | 21:3489cffad196 | 113 | imu = new chair_BNO055(pc, time); |
jvfausto | 21:3489cffad196 | 114 | Wheelchair::stop(); // Wheelchair is initially stationary |
t1jain | 30:a6659c5a8109 | 115 | imu->setup(); // Turns on the IMU |
jvfausto | 21:3489cffad196 | 116 | wheelS = qeiS; // "wheel" is called for encoder |
jvfausto | 21:3489cffad196 | 117 | wheel = qei; |
t1jain | 30:a6659c5a8109 | 118 | ToF = ToFT; // Passes pointer with addresses of ToF sensors |
jvfausto | 21:3489cffad196 | 119 | |
t1jain | 30:a6659c5a8109 | 120 | for(int i = 0; i < 12; i++) // Initializes the ToF Sensors |
jvfausto | 21:3489cffad196 | 121 | { |
jvfausto | 21:3489cffad196 | 122 | (*(ToF+i))->initReading(0x31+((0x02)*i), 50000); |
jvfausto | 21:3489cffad196 | 123 | } |
jvfausto | 21:3489cffad196 | 124 | |
t1jain | 30:a6659c5a8109 | 125 | out->printf("wheelchair setup done \r\n"); // Make sure its initialized; prints in serial monitor |
jvfausto | 21:3489cffad196 | 126 | ti = time; |
jvfausto | 21:3489cffad196 | 127 | |
jvfausto | 21:3489cffad196 | 128 | |
jvfausto | 21:3489cffad196 | 129 | myPID.SetMode(AUTOMATIC); // PID mode: Automatic |
jvfausto | 21:3489cffad196 | 130 | } |
jvfausto | 21:3489cffad196 | 131 | |
jvfausto | 21:3489cffad196 | 132 | /* Move wheelchair with joystick on manual mode */ |
jvfausto | 21:3489cffad196 | 133 | void Wheelchair::move(float x_coor, float y_coor) |
jvfausto | 21:3489cffad196 | 134 | { |
jvfausto | 21:3489cffad196 | 135 | /* Scales one joystick measurement to the chair's joystick measurement */ |
ryanlin97 | 4:29a27953fe70 | 136 | float scaled_x = ((x_coor * 1.6f) + 1.7f)/3.3f; |
ryanlin97 | 4:29a27953fe70 | 137 | float scaled_y = (3.3f - (y_coor * 1.6f))/3.3f; |
jvfausto | 21:3489cffad196 | 138 | |
jvfausto | 21:3489cffad196 | 139 | /* Sends the scaled joystic values to the chair */ |
jvfausto | 21:3489cffad196 | 140 | x->write(scaled_x); |
ryanlin97 | 4:29a27953fe70 | 141 | y->write(scaled_y); |
ryanlin97 | 5:e0ccaab3959a | 142 | } |
jvfausto | 21:3489cffad196 | 143 | |
jvfausto | 21:3489cffad196 | 144 | /* Automatic mode: move forward and update x,y coordinate sent to chair */ |
jvfausto | 21:3489cffad196 | 145 | void Wheelchair::forward() |
ryanlin97 | 1:c0beadca1617 | 146 | { |
jvfausto | 26:662693bd7f31 | 147 | //printf("current velosity; %f, curr vel S %f\r\n", curr_vel, curr_velS); |
jvfausto | 21:3489cffad196 | 148 | if(forwardSafety == 0) |
jvfausto | 21:3489cffad196 | 149 | { |
ryanlin97 | 0:fc0c4a184482 | 150 | x->write(high); |
ryanlin97 | 3:a5e71bfdb492 | 151 | y->write(def+offset); |
jvfausto | 21:3489cffad196 | 152 | } |
jvfausto | 26:662693bd7f31 | 153 | out->printf("%f, %f\r\n", curr_pos, wheelS->getDistance(53.975)); |
ryanlin97 | 0:fc0c4a184482 | 154 | } |
jvfausto | 21:3489cffad196 | 155 | |
jvfausto | 21:3489cffad196 | 156 | /* Automatic mode: move in reverse and update x,y coordinate sent to chair */ |
jvfausto | 21:3489cffad196 | 157 | void Wheelchair::backward() |
ryanlin97 | 1:c0beadca1617 | 158 | { |
ryanlin97 | 0:fc0c4a184482 | 159 | x->write(low); |
ryanlin97 | 0:fc0c4a184482 | 160 | y->write(def); |
ryanlin97 | 0:fc0c4a184482 | 161 | } |
jvfausto | 21:3489cffad196 | 162 | |
jvfausto | 21:3489cffad196 | 163 | /* Automatic mode: move right and update x,y coordinate sent to chair */ |
jvfausto | 21:3489cffad196 | 164 | void Wheelchair::right() |
ryanlin97 | 1:c0beadca1617 | 165 | { |
ryanlin97 | 0:fc0c4a184482 | 166 | x->write(def); |
ryanlin97 | 11:d14a1f7f1297 | 167 | y->write(low); |
ryanlin97 | 0:fc0c4a184482 | 168 | } |
ryanlin97 | 0:fc0c4a184482 | 169 | |
jvfausto | 21:3489cffad196 | 170 | /* Automatic mode: move left and update x,y coordinate sent to chair */ |
jvfausto | 21:3489cffad196 | 171 | void Wheelchair::left() |
ryanlin97 | 1:c0beadca1617 | 172 | { |
ryanlin97 | 0:fc0c4a184482 | 173 | x->write(def); |
ryanlin97 | 11:d14a1f7f1297 | 174 | y->write(high); |
ryanlin97 | 0:fc0c4a184482 | 175 | } |
jvfausto | 21:3489cffad196 | 176 | |
jvfausto | 21:3489cffad196 | 177 | /* Stop the wheelchair */ |
jvfausto | 21:3489cffad196 | 178 | void Wheelchair::stop() |
ryanlin97 | 1:c0beadca1617 | 179 | { |
ryanlin97 | 0:fc0c4a184482 | 180 | x->write(def); |
ryanlin97 | 0:fc0c4a184482 | 181 | y->write(def); |
ryanlin97 | 6:0cd57bdd8fbc | 182 | } |
jvfausto | 21:3489cffad196 | 183 | |
jvfausto | 21:3489cffad196 | 184 | /* Counter-clockwise is - |
jvfausto | 21:3489cffad196 | 185 | * Clockwise is + |
jvfausto | 21:3489cffad196 | 186 | * Range of deg: 0 to 360 |
jvfausto | 21:3489cffad196 | 187 | * This constructor takes in an angle from user and adjusts for turning right |
jvfausto | 21:3489cffad196 | 188 | */ |
jvfausto | 21:3489cffad196 | 189 | void Wheelchair::pid_right(int deg) |
ryanlin97 | 12:921488918749 | 190 | { |
jvfausto | 21:3489cffad196 | 191 | bool overturn = false; //Boolean if angle over 360˚ |
ryanlin97 | 12:921488918749 | 192 | |
jvfausto | 21:3489cffad196 | 193 | out->printf("pid right\r\r\n"); |
jvfausto | 21:3489cffad196 | 194 | x->write(def); // Update x sent to chair to be stationary |
jvfausto | 21:3489cffad196 | 195 | Setpoint = curr_yaw + deg; // Relative angle we want to turn |
jvfausto | 21:3489cffad196 | 196 | pid_yaw = curr_yaw; // Sets pid_yaw to angle input from user |
jvfausto | 21:3489cffad196 | 197 | |
jvfausto | 21:3489cffad196 | 198 | /* Turns on overturn boolean if setpoint over 360˚ */ |
jvfausto | 21:3489cffad196 | 199 | if(Setpoint > 360) |
jvfausto | 21:3489cffad196 | 200 | { |
ryanlin97 | 12:921488918749 | 201 | overturn = true; |
ryanlin97 | 12:921488918749 | 202 | } |
jvfausto | 21:3489cffad196 | 203 | |
jvfausto | 21:3489cffad196 | 204 | myPID.SetTunings(5.5,0, 0.0035); // Sets the constants for P and D |
jvfausto | 21:3489cffad196 | 205 | myPID.SetOutputLimits(0, def-low-.15); // Limit is set to the differnce between def and low |
jvfausto | 21:3489cffad196 | 206 | myPID.SetControllerDirection(DIRECT); // PID mode: Direct |
jvfausto | 21:3489cffad196 | 207 | |
jvfausto | 21:3489cffad196 | 208 | /* PID stops when approaching a litte less than desired angle */ |
jvfausto | 21:3489cffad196 | 209 | while(pid_yaw < Setpoint - 3) |
jvfausto | 21:3489cffad196 | 210 | { |
jvfausto | 21:3489cffad196 | 211 | /* PID is set to correct angle range if angle greater than 360˚*/ |
jvfausto | 17:7f3b69300bb6 | 212 | if(overturn && curr_yaw < Setpoint-deg-1) |
jvfausto | 17:7f3b69300bb6 | 213 | { |
jvfausto | 21:3489cffad196 | 214 | pid_yaw = curr_yaw + 360; |
jvfausto | 21:3489cffad196 | 215 | } |
jvfausto | 21:3489cffad196 | 216 | else |
jvfausto | 21:3489cffad196 | 217 | { |
jvfausto | 17:7f3b69300bb6 | 218 | pid_yaw = curr_yaw; |
ryanlin97 | 12:921488918749 | 219 | } |
jvfausto | 21:3489cffad196 | 220 | |
jvfausto | 21:3489cffad196 | 221 | myPID.Compute(); // Does PID calculations |
jvfausto | 21:3489cffad196 | 222 | double tempor = -Output+def; // Temporary value with the voltage output |
jvfausto | 21:3489cffad196 | 223 | y->write(tempor); // Update y sent to chair |
jvfausto | 21:3489cffad196 | 224 | |
jvfausto | 21:3489cffad196 | 225 | /* Prints to serial monitor the current angle and setpoint */ |
jvfausto | 21:3489cffad196 | 226 | out->printf("curr_yaw %f\r\r\n", curr_yaw); |
jvfausto | 21:3489cffad196 | 227 | out->printf("Setpoint = %f \r\n", Setpoint); |
jvfausto | 21:3489cffad196 | 228 | |
jvfausto | 21:3489cffad196 | 229 | wait(.05); // Small delay (milliseconds) |
ryanlin97 | 12:921488918749 | 230 | } |
jvfausto | 21:3489cffad196 | 231 | |
jvfausto | 21:3489cffad196 | 232 | /* Saftey stop for wheelchair */ |
jvfausto | 21:3489cffad196 | 233 | Wheelchair::stop(); |
jvfausto | 21:3489cffad196 | 234 | out->printf("done \r\n"); |
jvfausto | 21:3489cffad196 | 235 | } |
jvfausto | 21:3489cffad196 | 236 | |
jvfausto | 21:3489cffad196 | 237 | /* Counter-clockwise is - |
jvfausto | 21:3489cffad196 | 238 | * Clockwise is + |
jvfausto | 21:3489cffad196 | 239 | * Range of deg: 0 to 360 |
jvfausto | 21:3489cffad196 | 240 | * This constructor takes in an angle from user and adjusts for turning left |
jvfausto | 21:3489cffad196 | 241 | */ |
jvfausto | 21:3489cffad196 | 242 | void Wheelchair::pid_left(int deg) |
ryanlin97 | 12:921488918749 | 243 | { |
jvfausto | 21:3489cffad196 | 244 | bool overturn = false; //Boolean if angle under 0˚ |
ryanlin97 | 12:921488918749 | 245 | |
jvfausto | 21:3489cffad196 | 246 | out->printf("pid Left\r\r\n"); |
jvfausto | 21:3489cffad196 | 247 | x->write(def); // Update x sent to chair to be stationary |
jvfausto | 21:3489cffad196 | 248 | Setpoint = curr_yaw - deg; // Relative angle we want to turn |
jvfausto | 21:3489cffad196 | 249 | pid_yaw = curr_yaw; // Sets pid_yaw to angle input from user |
jvfausto | 21:3489cffad196 | 250 | |
jvfausto | 21:3489cffad196 | 251 | /* Turns on overturn boolean if setpoint less than 0˚ */ |
jvfausto | 21:3489cffad196 | 252 | if(Setpoint < 0) |
jvfausto | 21:3489cffad196 | 253 | { |
ryanlin97 | 12:921488918749 | 254 | overturn = true; |
ryanlin97 | 12:921488918749 | 255 | } |
jvfausto | 21:3489cffad196 | 256 | |
jvfausto | 21:3489cffad196 | 257 | myPID.SetTunings(5,0, 0.004); // Sets the constants for P and D |
t1jain | 30:a6659c5a8109 | 258 | myPID.SetOutputLimits(0,high-def-.12); // Limit is set to the differnce between def and low |
jvfausto | 21:3489cffad196 | 259 | myPID.SetControllerDirection(REVERSE); // PID mode: Reverse |
jvfausto | 21:3489cffad196 | 260 | |
jvfausto | 21:3489cffad196 | 261 | /* PID stops when approaching a litte more than desired angle */ |
jvfausto | 21:3489cffad196 | 262 | while(pid_yaw > Setpoint+3) |
jvfausto | 21:3489cffad196 | 263 | { |
jvfausto | 21:3489cffad196 | 264 | /* PID is set to correct angle range if angle less than 0˚ */ |
jvfausto | 21:3489cffad196 | 265 | if(overturn && curr_yaw > Setpoint+deg+1) |
jvfausto | 17:7f3b69300bb6 | 266 | { |
jvfausto | 17:7f3b69300bb6 | 267 | pid_yaw = curr_yaw - 360; |
jvfausto | 21:3489cffad196 | 268 | } |
jvfausto | 21:3489cffad196 | 269 | else |
jvfausto | 21:3489cffad196 | 270 | { |
jvfausto | 21:3489cffad196 | 271 | pid_yaw = curr_yaw; |
jvfausto | 21:3489cffad196 | 272 | } |
jvfausto | 21:3489cffad196 | 273 | |
jvfausto | 21:3489cffad196 | 274 | myPID.Compute(); // Does PID calculations |
jvfausto | 21:3489cffad196 | 275 | double tempor = Output+def; // Temporary value with the voltage output |
jvfausto | 21:3489cffad196 | 276 | y->write(tempor); // Update y sent to chair |
jvfausto | 21:3489cffad196 | 277 | |
jvfausto | 21:3489cffad196 | 278 | /* Prints to serial monitor the current angle and setpoint */ |
jvfausto | 17:7f3b69300bb6 | 279 | out->printf("curr_yaw %f\r\n", curr_yaw); |
jvfausto | 21:3489cffad196 | 280 | out->printf("Setpoint = %f \r\n", Setpoint); |
jvfausto | 21:3489cffad196 | 281 | |
jvfausto | 21:3489cffad196 | 282 | wait(.05); // Small delay (milliseconds) |
ryanlin97 | 12:921488918749 | 283 | } |
jvfausto | 21:3489cffad196 | 284 | |
jvfausto | 21:3489cffad196 | 285 | /* Saftey stop for wheelchair */ |
jvfausto | 21:3489cffad196 | 286 | Wheelchair::stop(); |
jvfausto | 21:3489cffad196 | 287 | out->printf("done \r\n"); |
ryanlin97 | 12:921488918749 | 288 | |
jvfausto | 21:3489cffad196 | 289 | } |
jvfausto | 21:3489cffad196 | 290 | |
jvfausto | 21:3489cffad196 | 291 | /* This constructor determines whether to turn left or right */ |
jvfausto | 21:3489cffad196 | 292 | void Wheelchair::pid_turn(int deg) |
jvfausto | 21:3489cffad196 | 293 | { |
jvfausto | 21:3489cffad196 | 294 | |
jvfausto | 21:3489cffad196 | 295 | /* Sets angle to coterminal angle for left turn if deg > 180 |
jvfausto | 21:3489cffad196 | 296 | * Sets angle to coterminal angle for right turn if deg < -180 |
jvfausto | 21:3489cffad196 | 297 | */ |
jvfausto | 21:3489cffad196 | 298 | if(deg > 180) |
jvfausto | 21:3489cffad196 | 299 | { |
ryanlin97 | 12:921488918749 | 300 | deg -= 360; |
ryanlin97 | 12:921488918749 | 301 | } |
jvfausto | 21:3489cffad196 | 302 | else if(deg < -180) |
jvfausto | 21:3489cffad196 | 303 | { |
jvfausto | 21:3489cffad196 | 304 | deg +=360; |
ryanlin97 | 12:921488918749 | 305 | } |
ryanlin97 | 12:921488918749 | 306 | |
jvfausto | 21:3489cffad196 | 307 | /* Makes sure angle inputted to function is positive */ |
ryanlin97 | 12:921488918749 | 308 | int turnAmt = abs(deg); |
jvfausto | 21:3489cffad196 | 309 | |
jvfausto | 21:3489cffad196 | 310 | /* Calls PID_right if deg > 0, else calls PID_left if deg < 0 */ |
jvfausto | 21:3489cffad196 | 311 | if(deg >= 0) |
jvfausto | 21:3489cffad196 | 312 | { |
jvfausto | 21:3489cffad196 | 313 | Wheelchair::pid_right(turnAmt); |
jvfausto | 21:3489cffad196 | 314 | } |
jvfausto | 21:3489cffad196 | 315 | else |
jvfausto | 21:3489cffad196 | 316 | { |
jvfausto | 21:3489cffad196 | 317 | Wheelchair::pid_left(turnAmt); |
jvfausto | 21:3489cffad196 | 318 | } |
ryanlin97 | 12:921488918749 | 319 | |
jvfausto | 21:3489cffad196 | 320 | } |
jvfausto | 21:3489cffad196 | 321 | |
jvfausto | 21:3489cffad196 | 322 | /* This constructor takes in distance to travel and adjust to move forward */ |
jvfausto | 19:71a6621ee5c3 | 323 | void Wheelchair::pid_forward(double mm) |
jvfausto | 17:7f3b69300bb6 | 324 | { |
jvfausto | 21:3489cffad196 | 325 | mm -= 20; // Makes sure distance does not overshoot |
jvfausto | 21:3489cffad196 | 326 | Input = 0; // Initializes input to zero: Test latter w/o |
jvfausto | 21:3489cffad196 | 327 | wheel->reset(); // Resets encoders so that they start at 0 |
jvfausto | 21:3489cffad196 | 328 | |
jvfausto | 17:7f3b69300bb6 | 329 | out->printf("pid foward\r\n"); |
jvfausto | 21:3489cffad196 | 330 | |
jvfausto | 21:3489cffad196 | 331 | double tempor; // Initializes Temporary variable for x input |
jvfausto | 21:3489cffad196 | 332 | Setpoint = mm; // Initializes the setpoint to desired value |
jvfausto | 21:3489cffad196 | 333 | |
jvfausto | 21:3489cffad196 | 334 | myPIDDistance.SetTunings(5.5,0, 0.0015); // Sets constants for P and D |
jvfausto | 21:3489cffad196 | 335 | myPIDDistance.SetOutputLimits(0,high-def-.15); // Limit set to difference between high and def |
jvfausto | 21:3489cffad196 | 336 | myPIDDistance.SetControllerDirection(DIRECT); // PID mode: Direct |
jvfausto | 21:3489cffad196 | 337 | |
jvfausto | 21:3489cffad196 | 338 | y->write(def+offset); // Update y to make chair stationary |
jvfausto | 21:3489cffad196 | 339 | |
jvfausto | 21:3489cffad196 | 340 | /* Chair stops moving when Setpoint is reached */ |
jvfausto | 21:3489cffad196 | 341 | while(Input < Setpoint){ |
jvfausto | 21:3489cffad196 | 342 | |
jvfausto | 21:3489cffad196 | 343 | if(out->readable()) // Emergency Break |
jvfausto | 21:3489cffad196 | 344 | { |
jvfausto | 21:3489cffad196 | 345 | break; |
jvfausto | 21:3489cffad196 | 346 | } |
jvfausto | 17:7f3b69300bb6 | 347 | |
jvfausto | 21:3489cffad196 | 348 | Input = wheel->getDistance(53.975); // Gets distance from Encoder into PID |
jvfausto | 21:3489cffad196 | 349 | wait(.05); // Slight Delay: *****Test without |
jvfausto | 21:3489cffad196 | 350 | myPIDDistance.Compute(); // Compute distance traveled by chair |
jvfausto | 21:3489cffad196 | 351 | |
jvfausto | 21:3489cffad196 | 352 | tempor = Output + def; // Temporary output variable |
jvfausto | 21:3489cffad196 | 353 | x->write(tempor); // Update x sent to chair |
jvfausto | 17:7f3b69300bb6 | 354 | |
jvfausto | 21:3489cffad196 | 355 | /* Prints to serial monitor the distance traveled by chair */ |
jvfausto | 19:71a6621ee5c3 | 356 | out->printf("distance %f\r\n", Input); |
jvfausto | 17:7f3b69300bb6 | 357 | } |
ryanlin97 | 12:921488918749 | 358 | |
jvfausto | 17:7f3b69300bb6 | 359 | } |
jvfausto | 21:3489cffad196 | 360 | |
jvfausto | 21:3489cffad196 | 361 | /* This constructor returns the relative angular position of chair */ |
jvfausto | 21:3489cffad196 | 362 | double Wheelchair::getTwistZ() |
jvfausto | 17:7f3b69300bb6 | 363 | { |
jvfausto | 21:3489cffad196 | 364 | return imu->gyro_z(); |
jvfausto | 21:3489cffad196 | 365 | } |
jvfausto | 18:663b6d693252 | 366 | |
jvfausto | 21:3489cffad196 | 367 | /* This constructor computes the relative angle for Twist message in ROS */ |
jvfausto | 21:3489cffad196 | 368 | void Wheelchair::pid_twistA() |
jvfausto | 21:3489cffad196 | 369 | { |
jvfausto | 21:3489cffad196 | 370 | /* Initialize variables for angle and update x,y sent to chair */ |
jvfausto | 21:3489cffad196 | 371 | char c; |
jvfausto | 21:3489cffad196 | 372 | double temporA = def; |
jvfausto | 21:3489cffad196 | 373 | y->write(def); |
jvfausto | 21:3489cffad196 | 374 | x->write(def); |
jvfausto | 21:3489cffad196 | 375 | |
jvfausto | 21:3489cffad196 | 376 | PIDAngularV.SetTunings(.00015,0, 0.00); // Sets the constants for P and D |
jvfausto | 21:3489cffad196 | 377 | PIDAngularV.SetOutputLimits(-.1, .1); // Limit set to be in range specified |
jvfausto | 21:3489cffad196 | 378 | PIDAngularV.SetControllerDirection(DIRECT); // PID mode: Direct |
jvfausto | 21:3489cffad196 | 379 | |
jvfausto | 21:3489cffad196 | 380 | /* Computes angular position of wheelchair while turning */ |
jvfausto | 21:3489cffad196 | 381 | while(1) |
jvfausto | 21:3489cffad196 | 382 | { |
jvfausto | 21:3489cffad196 | 383 | yDesired = angularV; |
jvfausto | 21:3489cffad196 | 384 | |
jvfausto | 21:3489cffad196 | 385 | /* Update and set all variable so that the chair is stationary |
jvfausto | 21:3489cffad196 | 386 | * if the desired angle is zero |
jvfausto | 21:3489cffad196 | 387 | */ |
jvfausto | 21:3489cffad196 | 388 | if(yDesired == 0) |
jvfausto | 18:663b6d693252 | 389 | { |
jvfausto | 21:3489cffad196 | 390 | x->write(def); |
jvfausto | 21:3489cffad196 | 391 | y->write(def); |
jvfausto | 21:3489cffad196 | 392 | yDesired = 0; |
ryanlin97 | 8:381a4ec3fef8 | 393 | return; |
ryanlin97 | 7:5e38d43fbce3 | 394 | } |
jvfausto | 21:3489cffad196 | 395 | |
jvfausto | 21:3489cffad196 | 396 | /* Continuously updates with current angle measured by IMU */ |
jvfausto | 21:3489cffad196 | 397 | yIn = imu->gyro_z(); |
jvfausto | 21:3489cffad196 | 398 | PIDAngularV.Compute(); |
jvfausto | 21:3489cffad196 | 399 | temporA += yOut; // Temporary value with the voltage output |
jvfausto | 21:3489cffad196 | 400 | y->write(temporA); // Update y sent to chair |
jvfausto | 21:3489cffad196 | 401 | |
jvfausto | 21:3489cffad196 | 402 | //out->printf("temporA: %f, yDesired %f, angle: %f\r\n", temporA, yDesired, imu->gyro_z()); |
jvfausto | 21:3489cffad196 | 403 | wait(.05); // Small delay (milliseconds) |
ryanlin97 | 6:0cd57bdd8fbc | 404 | } |
jvfausto | 21:3489cffad196 | 405 | |
jvfausto | 21:3489cffad196 | 406 | } |
ryanlin97 | 6:0cd57bdd8fbc | 407 | |
jvfausto | 21:3489cffad196 | 408 | /* This constructor computes the relative velocity for Twist message in ROS */ |
jvfausto | 21:3489cffad196 | 409 | void Wheelchair::pid_twistV() |
ryanlin97 | 6:0cd57bdd8fbc | 410 | { |
jvfausto | 21:3489cffad196 | 411 | /* Initializes variables as default */ |
jvfausto | 21:3489cffad196 | 412 | double temporV = def; |
jvfausto | 26:662693bd7f31 | 413 | double temporS = def+offset; |
jvfausto | 21:3489cffad196 | 414 | vDesiredS = 0; |
jvfausto | 21:3489cffad196 | 415 | x->write(def); |
jvfausto | 21:3489cffad196 | 416 | y->write(def); |
jvfausto | 21:3489cffad196 | 417 | wheel->reset(); // Resets the encoders |
jvfausto | 21:3489cffad196 | 418 | /* Sets the constants for P and D */ |
jvfausto | 21:3489cffad196 | 419 | PIDVelosity.SetTunings(.0005,0, 0.00); |
jvfausto | 26:662693bd7f31 | 420 | PIDSlaveV.SetTunings(.005,0.000001, 0.000001); |
jvfausto | 21:3489cffad196 | 421 | |
jvfausto | 21:3489cffad196 | 422 | /* Limits to the range specified */ |
jvfausto | 21:3489cffad196 | 423 | PIDVelosity.SetOutputLimits(-.005, .005); |
jvfausto | 21:3489cffad196 | 424 | PIDSlaveV.SetOutputLimits(-.002, .002); |
jvfausto | 21:3489cffad196 | 425 | |
jvfausto | 21:3489cffad196 | 426 | /* PID mode: Direct */ |
jvfausto | 21:3489cffad196 | 427 | PIDVelosity.SetControllerDirection(DIRECT); |
jvfausto | 21:3489cffad196 | 428 | PIDSlaveV.SetControllerDirection(DIRECT); |
jvfausto | 21:3489cffad196 | 429 | |
jvfausto | 21:3489cffad196 | 430 | while(1) |
jvfausto | 21:3489cffad196 | 431 | { |
jvfausto | 21:3489cffad196 | 432 | linearV = .7; |
jvfausto | 21:3489cffad196 | 433 | test1 = linearV*100; |
jvfausto | 21:3489cffad196 | 434 | vel = curr_vel; |
jvfausto | 21:3489cffad196 | 435 | vDesired = linearV*100; |
jvfausto | 21:3489cffad196 | 436 | if(out->readable()) |
jvfausto | 21:3489cffad196 | 437 | return; |
jvfausto | 21:3489cffad196 | 438 | /* Update and set all variable so that the chair is stationary |
jvfausto | 21:3489cffad196 | 439 | * if the velocity is zero |
jvfausto | 21:3489cffad196 | 440 | */ |
jvfausto | 21:3489cffad196 | 441 | if(linearV == 0) |
jvfausto | 21:3489cffad196 | 442 | { |
jvfausto | 21:3489cffad196 | 443 | x->write(def); |
jvfausto | 21:3489cffad196 | 444 | y->write(def); |
ryanlin97 | 8:381a4ec3fef8 | 445 | |
jvfausto | 21:3489cffad196 | 446 | vel = 0; |
jvfausto | 21:3489cffad196 | 447 | vDesired = 0; |
jvfausto | 21:3489cffad196 | 448 | dist_old = 0; |
ryanlin97 | 8:381a4ec3fef8 | 449 | return; |
ryanlin97 | 8:381a4ec3fef8 | 450 | } |
jvfausto | 21:3489cffad196 | 451 | |
jvfausto | 21:3489cffad196 | 452 | if(vDesired >= 0) |
jvfausto | 21:3489cffad196 | 453 | { |
jvfausto | 21:3489cffad196 | 454 | PIDVelosity.SetTunings(.000004,0, 0.00); // Sets the constants for P and D |
jvfausto | 21:3489cffad196 | 455 | PIDVelosity.SetOutputLimits(-.002, .002); // Limits to the range specified |
jvfausto | 21:3489cffad196 | 456 | } |
jvfausto | 21:3489cffad196 | 457 | else |
jvfausto | 21:3489cffad196 | 458 | { |
jvfausto | 21:3489cffad196 | 459 | PIDVelosity.SetTunings(.000015,0, 0.00); // Sets the constants for P and D |
jvfausto | 21:3489cffad196 | 460 | PIDVelosity.SetOutputLimits(-.0005, .0005); // Limits to range specified |
jvfausto | 21:3489cffad196 | 461 | } |
jvfausto | 21:3489cffad196 | 462 | |
jvfausto | 21:3489cffad196 | 463 | /* Sets maximum value of variable to 1 */ |
jvfausto | 21:3489cffad196 | 464 | if(temporV >= 1.5) |
jvfausto | 21:3489cffad196 | 465 | { |
jvfausto | 21:3489cffad196 | 466 | temporV = 1.5; |
ryanlin97 | 8:381a4ec3fef8 | 467 | } |
jvfausto | 21:3489cffad196 | 468 | /* Scales and makes some adjustments to velocity */ |
jvfausto | 21:3489cffad196 | 469 | vIn = curr_vel*100; |
jvfausto | 21:3489cffad196 | 470 | vInS = curr_vel-curr_velS; |
jvfausto | 21:3489cffad196 | 471 | PIDVelosity.Compute(); |
jvfausto | 21:3489cffad196 | 472 | PIDSlaveV.Compute(); |
jvfausto | 21:3489cffad196 | 473 | if(forwardSafety == 0) |
jvfausto | 21:3489cffad196 | 474 | { |
jvfausto | 21:3489cffad196 | 475 | temporV += vOut; |
jvfausto | 21:3489cffad196 | 476 | temporS += vOutS; |
jvfausto | 21:3489cffad196 | 477 | |
jvfausto | 21:3489cffad196 | 478 | /* Updates x,y sent to Wheelchair and for Odometry message in ROS */ |
jvfausto | 21:3489cffad196 | 479 | x->write(temporV); |
jvfausto | 21:3489cffad196 | 480 | test2 = temporV; |
jvfausto | 21:3489cffad196 | 481 | y->write(temporS); |
jvfausto | 21:3489cffad196 | 482 | } |
jvfausto | 21:3489cffad196 | 483 | else |
jvfausto | 21:3489cffad196 | 484 | { |
jvfausto | 21:3489cffad196 | 485 | x->write(def); |
jvfausto | 21:3489cffad196 | 486 | y->write(def); |
jvfausto | 21:3489cffad196 | 487 | } |
jvfausto | 21:3489cffad196 | 488 | //out->printf("Velosity: %f, Velosity2: %f, temporV %f, temporS %f\r\n", curr_vel, curr_velS, temporV, temporS); |
jvfausto | 21:3489cffad196 | 489 | Wheelchair::odomMsg(); |
jvfausto | 21:3489cffad196 | 490 | wait(.01); // Small delay (milliseconds) |
ryanlin97 | 6:0cd57bdd8fbc | 491 | } |
ryanlin97 | 11:d14a1f7f1297 | 492 | } |
ryanlin97 | 11:d14a1f7f1297 | 493 | |
jvfausto | 21:3489cffad196 | 494 | /* This constructor calculates the relative position of the chair everytime the encoders reset |
jvfausto | 21:3489cffad196 | 495 | * by setting its old position as the origin to calculate the new position |
jvfausto | 21:3489cffad196 | 496 | */ |
jvfausto | 21:3489cffad196 | 497 | void Wheelchair::odomMsg() |
ryanlin97 | 11:d14a1f7f1297 | 498 | { |
jvfausto | 21:3489cffad196 | 499 | double dist_new = curr_pos; |
jvfausto | 21:3489cffad196 | 500 | double dist = dist_new-dist_old; |
jvfausto | 21:3489cffad196 | 501 | double temp_x = dist*sin(z_angular*3.14159/180); |
jvfausto | 21:3489cffad196 | 502 | double temp_y = dist*cos(z_angular*3.14159/180); |
jvfausto | 21:3489cffad196 | 503 | |
jvfausto | 21:3489cffad196 | 504 | x_position += temp_x; |
jvfausto | 21:3489cffad196 | 505 | y_position += temp_y; |
ryanlin97 | 11:d14a1f7f1297 | 506 | |
jvfausto | 21:3489cffad196 | 507 | dist_old = dist_new; |
jvfausto | 21:3489cffad196 | 508 | } |
jvfausto | 21:3489cffad196 | 509 | |
jvfausto | 21:3489cffad196 | 510 | /* This constructor prints the Odometry message to the serial monitor */ |
jvfausto | 21:3489cffad196 | 511 | void Wheelchair::showOdom() |
jvfausto | 21:3489cffad196 | 512 | { |
jvfausto | 21:3489cffad196 | 513 | out->printf("x %f, y %f, angle %f", x_position, y_position, z_angular); |
jvfausto | 21:3489cffad196 | 514 | } |
jvfausto | 21:3489cffad196 | 515 | |
jvfausto | 21:3489cffad196 | 516 | /* This constructor returns the approximate distance based on the wheel diameter */ |
jvfausto | 21:3489cffad196 | 517 | float Wheelchair::getDistance() |
jvfausto | 21:3489cffad196 | 518 | { |
jvfausto | 21:3489cffad196 | 519 | return wheel->getDistance(Diameter); |
ryanlin97 | 6:0cd57bdd8fbc | 520 | } |
ryanlin97 | 8:381a4ec3fef8 | 521 | |
jvfausto | 21:3489cffad196 | 522 | /* This constructor resets the wheel encoder's */ |
jvfausto | 21:3489cffad196 | 523 | void Wheelchair::resetDistance() |
jvfausto | 21:3489cffad196 | 524 | { |
ryanlin97 | 12:921488918749 | 525 | wheel->reset(); |
jvfausto | 21:3489cffad196 | 526 | } |
jvfausto | 21:3489cffad196 | 527 | |
jvfausto | 21:3489cffad196 | 528 | |
jvfausto | 21:3489cffad196 | 529 | /*Predetermined paths For Demmo*/ |
jvfausto | 21:3489cffad196 | 530 | void Wheelchair::desk() |
jvfausto | 21:3489cffad196 | 531 | { |
jvfausto | 19:71a6621ee5c3 | 532 | Wheelchair::pid_forward(5461); |
jvfausto | 19:71a6621ee5c3 | 533 | Wheelchair::pid_right(87); |
jvfausto | 19:71a6621ee5c3 | 534 | Wheelchair::pid_forward(3658); |
jvfausto | 19:71a6621ee5c3 | 535 | Wheelchair::pid_right(87); |
jvfausto | 19:71a6621ee5c3 | 536 | Wheelchair::pid_forward(3658); |
jvfausto | 21:3489cffad196 | 537 | } |
jvfausto | 21:3489cffad196 | 538 | |
jvfausto | 21:3489cffad196 | 539 | void Wheelchair::kitchen() |
jvfausto | 21:3489cffad196 | 540 | { |
jvfausto | 19:71a6621ee5c3 | 541 | Wheelchair::pid_forward(5461); |
jvfausto | 20:f42db4ae16f0 | 542 | Wheelchair::pid_right(87); |
jvfausto | 19:71a6621ee5c3 | 543 | Wheelchair::pid_forward(3658); |
jvfausto | 19:71a6621ee5c3 | 544 | Wheelchair::pid_left(90); |
jvfausto | 19:71a6621ee5c3 | 545 | Wheelchair::pid_forward(305); |
jvfausto | 21:3489cffad196 | 546 | } |
jvfausto | 21:3489cffad196 | 547 | |
jvfausto | 21:3489cffad196 | 548 | void Wheelchair::desk_to_kitchen() |
jvfausto | 21:3489cffad196 | 549 | { |
jvfausto | 19:71a6621ee5c3 | 550 | Wheelchair::pid_right(180); |
jvfausto | 19:71a6621ee5c3 | 551 | Wheelchair::pid_forward(3700); |
jvfausto | 21:3489cffad196 | 552 | } |