Added variable Test SideToF sensore

Dependencies:   QEI2 chair_BNO055 PID Watchdog VL53L1X_Filter ros_lib_kinetic

Committer:
t1jain
Date:
Mon Jul 01 23:20:48 2019 +0000
Revision:
31:06f2362caf12
Parent:
30:c25b2556e84d
Child:
32:fb26baa75d44
aa

Who changed what in which revision?

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